Show More
@@ -1,476 +1,482 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ |
|
2 | """ | |
3 | rhodecode.lib.db_manage |
|
3 | rhodecode.lib.db_manage | |
4 | ~~~~~~~~~~~~~~~~~~~~~~~ |
|
4 | ~~~~~~~~~~~~~~~~~~~~~~~ | |
5 |
|
5 | |||
6 | Database creation, and setup module for RhodeCode. Used for creation |
|
6 | Database creation, and setup module for RhodeCode. Used for creation | |
7 | of database as well as for migration operations |
|
7 | of database as well as for migration operations | |
8 |
|
8 | |||
9 | :created_on: Apr 10, 2010 |
|
9 | :created_on: Apr 10, 2010 | |
10 | :author: marcink |
|
10 | :author: marcink | |
11 | :copyright: (C) 2009-2011 Marcin Kuzminski <marcin@python-works.com> |
|
11 | :copyright: (C) 2009-2011 Marcin Kuzminski <marcin@python-works.com> | |
12 | :license: GPLv3, see COPYING for more details. |
|
12 | :license: GPLv3, see COPYING for more details. | |
13 | """ |
|
13 | """ | |
14 | # This program is free software: you can redistribute it and/or modify |
|
14 | # This program is free software: you can redistribute it and/or modify | |
15 | # it under the terms of the GNU General Public License as published by |
|
15 | # it under the terms of the GNU General Public License as published by | |
16 | # the Free Software Foundation, either version 3 of the License, or |
|
16 | # the Free Software Foundation, either version 3 of the License, or | |
17 | # (at your option) any later version. |
|
17 | # (at your option) any later version. | |
18 | # |
|
18 | # | |
19 | # This program is distributed in the hope that it will be useful, |
|
19 | # This program is distributed in the hope that it will be useful, | |
20 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
20 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
21 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 | # GNU General Public License for more details. |
|
22 | # GNU General Public License for more details. | |
23 | # |
|
23 | # | |
24 | # You should have received a copy of the GNU General Public License |
|
24 | # You should have received a copy of the GNU General Public License | |
25 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
25 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
26 |
|
26 | |||
27 | import os |
|
27 | import os | |
28 | import sys |
|
28 | import sys | |
29 | import uuid |
|
29 | import uuid | |
30 | import logging |
|
30 | import logging | |
31 | from os.path import dirname as dn, join as jn |
|
31 | from os.path import dirname as dn, join as jn | |
32 |
|
32 | |||
33 | from rhodecode import __dbversion__ |
|
33 | from rhodecode import __dbversion__ | |
34 | from rhodecode.model import meta |
|
34 | from rhodecode.model import meta | |
35 |
|
35 | |||
36 | from rhodecode.model.user import UserModel |
|
36 | from rhodecode.model.user import UserModel | |
37 | from rhodecode.lib.utils import ask_ok |
|
37 | from rhodecode.lib.utils import ask_ok | |
38 | from rhodecode.model import init_model |
|
38 | from rhodecode.model import init_model | |
39 | from rhodecode.model.db import User, Permission, RhodeCodeUi, \ |
|
39 | from rhodecode.model.db import User, Permission, RhodeCodeUi, \ | |
40 | RhodeCodeSetting, UserToPerm, DbMigrateVersion |
|
40 | RhodeCodeSetting, UserToPerm, DbMigrateVersion | |
41 |
|
41 | |||
42 | from sqlalchemy.engine import create_engine |
|
42 | from sqlalchemy.engine import create_engine | |
43 |
|
43 | |||
44 | log = logging.getLogger(__name__) |
|
44 | log = logging.getLogger(__name__) | |
45 |
|
45 | |||
46 |
|
46 | |||
47 | class DbManage(object): |
|
47 | class DbManage(object): | |
48 | def __init__(self, log_sql, dbconf, root, tests=False): |
|
48 | def __init__(self, log_sql, dbconf, root, tests=False): | |
49 | self.dbname = dbconf.split('/')[-1] |
|
49 | self.dbname = dbconf.split('/')[-1] | |
50 | self.tests = tests |
|
50 | self.tests = tests | |
51 | self.root = root |
|
51 | self.root = root | |
52 | self.dburi = dbconf |
|
52 | self.dburi = dbconf | |
53 | self.log_sql = log_sql |
|
53 | self.log_sql = log_sql | |
54 | self.db_exists = False |
|
54 | self.db_exists = False | |
55 | self.init_db() |
|
55 | self.init_db() | |
56 |
|
56 | |||
57 | def init_db(self): |
|
57 | def init_db(self): | |
58 | engine = create_engine(self.dburi, echo=self.log_sql) |
|
58 | engine = create_engine(self.dburi, echo=self.log_sql) | |
59 | init_model(engine) |
|
59 | init_model(engine) | |
60 | self.sa = meta.Session |
|
60 | self.sa = meta.Session | |
61 |
|
61 | |||
62 | def create_tables(self, override=False): |
|
62 | def create_tables(self, override=False): | |
63 | """Create a auth database |
|
63 | """ | |
|
64 | Create a auth database | |||
64 | """ |
|
65 | """ | |
65 |
|
66 | |||
66 | log.info("Any existing database is going to be destroyed") |
|
67 | log.info("Any existing database is going to be destroyed") | |
67 | if self.tests: |
|
68 | if self.tests: | |
68 | destroy = True |
|
69 | destroy = True | |
69 | else: |
|
70 | else: | |
70 | destroy = ask_ok('Are you sure to destroy old database ? [y/n]') |
|
71 | destroy = ask_ok('Are you sure to destroy old database ? [y/n]') | |
71 | if not destroy: |
|
72 | if not destroy: | |
72 | sys.exit() |
|
73 | sys.exit() | |
73 | if destroy: |
|
74 | if destroy: | |
74 | meta.Base.metadata.drop_all() |
|
75 | meta.Base.metadata.drop_all() | |
75 |
|
76 | |||
76 | checkfirst = not override |
|
77 | checkfirst = not override | |
77 | meta.Base.metadata.create_all(checkfirst=checkfirst) |
|
78 | meta.Base.metadata.create_all(checkfirst=checkfirst) | |
78 | log.info('Created tables for %s', self.dbname) |
|
79 | log.info('Created tables for %s', self.dbname) | |
79 |
|
80 | |||
80 | def set_db_version(self): |
|
81 | def set_db_version(self): | |
81 | ver = DbMigrateVersion() |
|
82 | ver = DbMigrateVersion() | |
82 | ver.version = __dbversion__ |
|
83 | ver.version = __dbversion__ | |
83 | ver.repository_id = 'rhodecode_db_migrations' |
|
84 | ver.repository_id = 'rhodecode_db_migrations' | |
84 | ver.repository_path = 'versions' |
|
85 | ver.repository_path = 'versions' | |
85 | self.sa.add(ver) |
|
86 | self.sa.add(ver) | |
86 | log.info('db version set to: %s', __dbversion__) |
|
87 | log.info('db version set to: %s', __dbversion__) | |
87 |
|
88 | |||
88 | def upgrade(self): |
|
89 | def upgrade(self): | |
89 | """Upgrades given database schema to given revision following |
|
90 | """ | |
|
91 | Upgrades given database schema to given revision following | |||
90 | all needed steps, to perform the upgrade |
|
92 | all needed steps, to perform the upgrade | |
91 |
|
93 | |||
92 | """ |
|
94 | """ | |
93 |
|
95 | |||
94 | from rhodecode.lib.dbmigrate.migrate.versioning import api |
|
96 | from rhodecode.lib.dbmigrate.migrate.versioning import api | |
95 | from rhodecode.lib.dbmigrate.migrate.exceptions import \ |
|
97 | from rhodecode.lib.dbmigrate.migrate.exceptions import \ | |
96 | DatabaseNotControlledError |
|
98 | DatabaseNotControlledError | |
97 |
|
99 | |||
98 | upgrade = ask_ok('You are about to perform database upgrade, make ' |
|
100 | upgrade = ask_ok('You are about to perform database upgrade, make ' | |
99 | 'sure You backed up your database before. ' |
|
101 | 'sure You backed up your database before. ' | |
100 | 'Continue ? [y/n]') |
|
102 | 'Continue ? [y/n]') | |
101 | if not upgrade: |
|
103 | if not upgrade: | |
102 | sys.exit('Nothing done') |
|
104 | sys.exit('Nothing done') | |
103 |
|
105 | |||
104 | repository_path = jn(dn(dn(dn(os.path.realpath(__file__)))), |
|
106 | repository_path = jn(dn(dn(dn(os.path.realpath(__file__)))), | |
105 | 'rhodecode/lib/dbmigrate') |
|
107 | 'rhodecode/lib/dbmigrate') | |
106 | db_uri = self.dburi |
|
108 | db_uri = self.dburi | |
107 |
|
109 | |||
108 | try: |
|
110 | try: | |
109 | curr_version = api.db_version(db_uri, repository_path) |
|
111 | curr_version = api.db_version(db_uri, repository_path) | |
110 | msg = ('Found current database under version' |
|
112 | msg = ('Found current database under version' | |
111 | ' control with version %s' % curr_version) |
|
113 | ' control with version %s' % curr_version) | |
112 |
|
114 | |||
113 | except (RuntimeError, DatabaseNotControlledError): |
|
115 | except (RuntimeError, DatabaseNotControlledError): | |
114 | curr_version = 1 |
|
116 | curr_version = 1 | |
115 | msg = ('Current database is not under version control. Setting' |
|
117 | msg = ('Current database is not under version control. Setting' | |
116 | ' as version %s' % curr_version) |
|
118 | ' as version %s' % curr_version) | |
117 | api.version_control(db_uri, repository_path, curr_version) |
|
119 | api.version_control(db_uri, repository_path, curr_version) | |
118 |
|
120 | |||
119 | print (msg) |
|
121 | print (msg) | |
120 |
|
122 | |||
121 | if curr_version == __dbversion__: |
|
123 | if curr_version == __dbversion__: | |
122 | sys.exit('This database is already at the newest version') |
|
124 | sys.exit('This database is already at the newest version') | |
123 |
|
125 | |||
124 | #====================================================================== |
|
126 | #====================================================================== | |
125 | # UPGRADE STEPS |
|
127 | # UPGRADE STEPS | |
126 | #====================================================================== |
|
128 | #====================================================================== | |
127 | class UpgradeSteps(object): |
|
129 | class UpgradeSteps(object): | |
128 | """Those steps follow schema versions so for example schema |
|
130 | """ | |
|
131 | Those steps follow schema versions so for example schema | |||
129 | for example schema with seq 002 == step_2 and so on. |
|
132 | for example schema with seq 002 == step_2 and so on. | |
130 | """ |
|
133 | """ | |
131 |
|
134 | |||
132 | def __init__(self, klass): |
|
135 | def __init__(self, klass): | |
133 | self.klass = klass |
|
136 | self.klass = klass | |
134 |
|
137 | |||
135 | def step_0(self): |
|
138 | def step_0(self): | |
136 | #step 0 is the schema upgrade, and than follow proper upgrades |
|
139 | # step 0 is the schema upgrade, and than follow proper upgrades | |
137 | print ('attempting to do database upgrade to version %s' \ |
|
140 | print ('attempting to do database upgrade to version %s' \ | |
138 | % __dbversion__) |
|
141 | % __dbversion__) | |
139 | api.upgrade(db_uri, repository_path, __dbversion__) |
|
142 | api.upgrade(db_uri, repository_path, __dbversion__) | |
140 | print ('Schema upgrade completed') |
|
143 | print ('Schema upgrade completed') | |
141 |
|
144 | |||
142 | def step_1(self): |
|
145 | def step_1(self): | |
143 | pass |
|
146 | pass | |
144 |
|
147 | |||
145 | def step_2(self): |
|
148 | def step_2(self): | |
146 | print ('Patching repo paths for newer version of RhodeCode') |
|
149 | print ('Patching repo paths for newer version of RhodeCode') | |
147 | self.klass.fix_repo_paths() |
|
150 | self.klass.fix_repo_paths() | |
148 |
|
151 | |||
149 | print ('Patching default user of RhodeCode') |
|
152 | print ('Patching default user of RhodeCode') | |
150 | self.klass.fix_default_user() |
|
153 | self.klass.fix_default_user() | |
151 |
|
154 | |||
152 | log.info('Changing ui settings') |
|
155 | log.info('Changing ui settings') | |
153 | self.klass.create_ui_settings() |
|
156 | self.klass.create_ui_settings() | |
154 |
|
157 | |||
155 | def step_3(self): |
|
158 | def step_3(self): | |
156 | print ('Adding additional settings into RhodeCode db') |
|
159 | print ('Adding additional settings into RhodeCode db') | |
157 | self.klass.fix_settings() |
|
160 | self.klass.fix_settings() | |
158 | print ('Adding ldap defaults') |
|
161 | print ('Adding ldap defaults') | |
159 | self.klass.create_ldap_options(skip_existing=True) |
|
162 | self.klass.create_ldap_options(skip_existing=True) | |
160 |
|
163 | |||
161 | upgrade_steps = [0] + range(curr_version + 1, __dbversion__ + 1) |
|
164 | upgrade_steps = [0] + range(curr_version + 1, __dbversion__ + 1) | |
162 |
|
165 | |||
163 | #CALL THE PROPER ORDER OF STEPS TO PERFORM FULL UPGRADE |
|
166 | # CALL THE PROPER ORDER OF STEPS TO PERFORM FULL UPGRADE | |
164 | for step in upgrade_steps: |
|
167 | for step in upgrade_steps: | |
165 | print ('performing upgrade step %s' % step) |
|
168 | print ('performing upgrade step %s' % step) | |
166 | getattr(UpgradeSteps(self), 'step_%s' % step)() |
|
169 | getattr(UpgradeSteps(self), 'step_%s' % step)() | |
167 |
|
170 | |||
168 | def fix_repo_paths(self): |
|
171 | def fix_repo_paths(self): | |
169 | """Fixes a old rhodecode version path into new one without a '*' |
|
172 | """ | |
|
173 | Fixes a old rhodecode version path into new one without a '*' | |||
170 | """ |
|
174 | """ | |
171 |
|
175 | |||
172 | paths = self.sa.query(RhodeCodeUi)\ |
|
176 | paths = self.sa.query(RhodeCodeUi)\ | |
173 | .filter(RhodeCodeUi.ui_key == '/')\ |
|
177 | .filter(RhodeCodeUi.ui_key == '/')\ | |
174 | .scalar() |
|
178 | .scalar() | |
175 |
|
179 | |||
176 | paths.ui_value = paths.ui_value.replace('*', '') |
|
180 | paths.ui_value = paths.ui_value.replace('*', '') | |
177 |
|
181 | |||
178 | try: |
|
182 | try: | |
179 | self.sa.add(paths) |
|
183 | self.sa.add(paths) | |
180 | self.sa.commit() |
|
184 | self.sa.commit() | |
181 | except: |
|
185 | except: | |
182 | self.sa.rollback() |
|
186 | self.sa.rollback() | |
183 | raise |
|
187 | raise | |
184 |
|
188 | |||
185 | def fix_default_user(self): |
|
189 | def fix_default_user(self): | |
186 | """Fixes a old default user with some 'nicer' default values, |
|
190 | """ | |
|
191 | Fixes a old default user with some 'nicer' default values, | |||
187 | used mostly for anonymous access |
|
192 | used mostly for anonymous access | |
188 | """ |
|
193 | """ | |
189 | def_user = self.sa.query(User)\ |
|
194 | def_user = self.sa.query(User)\ | |
190 | .filter(User.username == 'default')\ |
|
195 | .filter(User.username == 'default')\ | |
191 | .one() |
|
196 | .one() | |
192 |
|
197 | |||
193 | def_user.name = 'Anonymous' |
|
198 | def_user.name = 'Anonymous' | |
194 | def_user.lastname = 'User' |
|
199 | def_user.lastname = 'User' | |
195 | def_user.email = 'anonymous@rhodecode.org' |
|
200 | def_user.email = 'anonymous@rhodecode.org' | |
196 |
|
201 | |||
197 | try: |
|
202 | try: | |
198 | self.sa.add(def_user) |
|
203 | self.sa.add(def_user) | |
199 | self.sa.commit() |
|
204 | self.sa.commit() | |
200 | except: |
|
205 | except: | |
201 | self.sa.rollback() |
|
206 | self.sa.rollback() | |
202 | raise |
|
207 | raise | |
203 |
|
208 | |||
204 | def fix_settings(self): |
|
209 | def fix_settings(self): | |
205 | """Fixes rhodecode settings adds ga_code key for google analytics |
|
210 | """ | |
|
211 | Fixes rhodecode settings adds ga_code key for google analytics | |||
206 | """ |
|
212 | """ | |
207 |
|
213 | |||
208 | hgsettings3 = RhodeCodeSetting('ga_code', '') |
|
214 | hgsettings3 = RhodeCodeSetting('ga_code', '') | |
209 |
|
215 | |||
210 | try: |
|
216 | try: | |
211 | self.sa.add(hgsettings3) |
|
217 | self.sa.add(hgsettings3) | |
212 | self.sa.commit() |
|
218 | self.sa.commit() | |
213 | except: |
|
219 | except: | |
214 | self.sa.rollback() |
|
220 | self.sa.rollback() | |
215 | raise |
|
221 | raise | |
216 |
|
222 | |||
217 | def admin_prompt(self, second=False): |
|
223 | def admin_prompt(self, second=False): | |
218 | if not self.tests: |
|
224 | if not self.tests: | |
219 | import getpass |
|
225 | import getpass | |
220 |
|
226 | |||
221 | def get_password(): |
|
227 | def get_password(): | |
222 | password = getpass.getpass('Specify admin password ' |
|
228 | password = getpass.getpass('Specify admin password ' | |
223 | '(min 6 chars):') |
|
229 | '(min 6 chars):') | |
224 | confirm = getpass.getpass('Confirm password:') |
|
230 | confirm = getpass.getpass('Confirm password:') | |
225 |
|
231 | |||
226 | if password != confirm: |
|
232 | if password != confirm: | |
227 | log.error('passwords mismatch') |
|
233 | log.error('passwords mismatch') | |
228 | return False |
|
234 | return False | |
229 | if len(password) < 6: |
|
235 | if len(password) < 6: | |
230 | log.error('password is to short use at least 6 characters') |
|
236 | log.error('password is to short use at least 6 characters') | |
231 | return False |
|
237 | return False | |
232 |
|
238 | |||
233 | return password |
|
239 | return password | |
234 |
|
240 | |||
235 | username = raw_input('Specify admin username:') |
|
241 | username = raw_input('Specify admin username:') | |
236 |
|
242 | |||
237 | password = get_password() |
|
243 | password = get_password() | |
238 | if not password: |
|
244 | if not password: | |
239 | #second try |
|
245 | #second try | |
240 | password = get_password() |
|
246 | password = get_password() | |
241 | if not password: |
|
247 | if not password: | |
242 | sys.exit() |
|
248 | sys.exit() | |
243 |
|
249 | |||
244 | email = raw_input('Specify admin email:') |
|
250 | email = raw_input('Specify admin email:') | |
245 | self.create_user(username, password, email, True) |
|
251 | self.create_user(username, password, email, True) | |
246 | else: |
|
252 | else: | |
247 | log.info('creating admin and regular test users') |
|
253 | log.info('creating admin and regular test users') | |
248 | from rhodecode.tests import TEST_USER_ADMIN_LOGIN,\ |
|
254 | from rhodecode.tests import TEST_USER_ADMIN_LOGIN,\ | |
249 |
TEST_USER_ADMIN_PASS |
|
255 | TEST_USER_ADMIN_PASS, TEST_USER_ADMIN_EMAIL,\ | |
250 |
TEST_USER_REGULAR_PASS, |
|
256 | TEST_USER_REGULAR_LOGIN, TEST_USER_REGULAR_PASS,\ | |
251 |
TEST_USER_REGULAR2_LOGIN, |
|
257 | TEST_USER_REGULAR_EMAIL, TEST_USER_REGULAR2_LOGIN, \ | |
252 | TEST_USER_REGULAR2_EMAIL |
|
258 | TEST_USER_REGULAR2_PASS, TEST_USER_REGULAR2_EMAIL | |
253 |
|
259 | |||
254 | self.create_user(TEST_USER_ADMIN_LOGIN, TEST_USER_ADMIN_PASS, |
|
260 | self.create_user(TEST_USER_ADMIN_LOGIN, TEST_USER_ADMIN_PASS, | |
255 | TEST_USER_ADMIN_EMAIL, True) |
|
261 | TEST_USER_ADMIN_EMAIL, True) | |
256 |
|
262 | |||
257 | self.create_user(TEST_USER_REGULAR_LOGIN, TEST_USER_REGULAR_PASS, |
|
263 | self.create_user(TEST_USER_REGULAR_LOGIN, TEST_USER_REGULAR_PASS, | |
258 | TEST_USER_REGULAR_EMAIL, False) |
|
264 | TEST_USER_REGULAR_EMAIL, False) | |
259 |
|
265 | |||
260 | self.create_user(TEST_USER_REGULAR2_LOGIN, TEST_USER_REGULAR2_PASS, |
|
266 | self.create_user(TEST_USER_REGULAR2_LOGIN, TEST_USER_REGULAR2_PASS, | |
261 | TEST_USER_REGULAR2_EMAIL, False) |
|
267 | TEST_USER_REGULAR2_EMAIL, False) | |
262 |
|
268 | |||
263 | def create_ui_settings(self): |
|
269 | def create_ui_settings(self): | |
264 | """Creates ui settings, fills out hooks |
|
270 | """ | |
|
271 | Creates ui settings, fills out hooks | |||
265 | and disables dotencode |
|
272 | and disables dotencode | |
|
273 | """ | |||
266 |
|
274 | |||
267 | """ |
|
|||
268 | #HOOKS |
|
275 | #HOOKS | |
269 | hooks1_key = RhodeCodeUi.HOOK_UPDATE |
|
276 | hooks1_key = RhodeCodeUi.HOOK_UPDATE | |
270 | hooks1_ = self.sa.query(RhodeCodeUi)\ |
|
277 | hooks1_ = self.sa.query(RhodeCodeUi)\ | |
271 | .filter(RhodeCodeUi.ui_key == hooks1_key).scalar() |
|
278 | .filter(RhodeCodeUi.ui_key == hooks1_key).scalar() | |
272 |
|
279 | |||
273 | hooks1 = RhodeCodeUi() if hooks1_ is None else hooks1_ |
|
280 | hooks1 = RhodeCodeUi() if hooks1_ is None else hooks1_ | |
274 | hooks1.ui_section = 'hooks' |
|
281 | hooks1.ui_section = 'hooks' | |
275 | hooks1.ui_key = hooks1_key |
|
282 | hooks1.ui_key = hooks1_key | |
276 | hooks1.ui_value = 'hg update >&2' |
|
283 | hooks1.ui_value = 'hg update >&2' | |
277 | hooks1.ui_active = False |
|
284 | hooks1.ui_active = False | |
278 |
|
285 | |||
279 | hooks2_key = RhodeCodeUi.HOOK_REPO_SIZE |
|
286 | hooks2_key = RhodeCodeUi.HOOK_REPO_SIZE | |
280 | hooks2_ = self.sa.query(RhodeCodeUi)\ |
|
287 | hooks2_ = self.sa.query(RhodeCodeUi)\ | |
281 | .filter(RhodeCodeUi.ui_key == hooks2_key).scalar() |
|
288 | .filter(RhodeCodeUi.ui_key == hooks2_key).scalar() | |
282 |
|
289 | |||
283 | hooks2 = RhodeCodeUi() if hooks2_ is None else hooks2_ |
|
290 | hooks2 = RhodeCodeUi() if hooks2_ is None else hooks2_ | |
284 | hooks2.ui_section = 'hooks' |
|
291 | hooks2.ui_section = 'hooks' | |
285 | hooks2.ui_key = hooks2_key |
|
292 | hooks2.ui_key = hooks2_key | |
286 | hooks2.ui_value = 'python:rhodecode.lib.hooks.repo_size' |
|
293 | hooks2.ui_value = 'python:rhodecode.lib.hooks.repo_size' | |
287 |
|
294 | |||
288 | hooks3 = RhodeCodeUi() |
|
295 | hooks3 = RhodeCodeUi() | |
289 | hooks3.ui_section = 'hooks' |
|
296 | hooks3.ui_section = 'hooks' | |
290 | hooks3.ui_key = RhodeCodeUi.HOOK_PUSH |
|
297 | hooks3.ui_key = RhodeCodeUi.HOOK_PUSH | |
291 | hooks3.ui_value = 'python:rhodecode.lib.hooks.log_push_action' |
|
298 | hooks3.ui_value = 'python:rhodecode.lib.hooks.log_push_action' | |
292 |
|
299 | |||
293 | hooks4 = RhodeCodeUi() |
|
300 | hooks4 = RhodeCodeUi() | |
294 | hooks4.ui_section = 'hooks' |
|
301 | hooks4.ui_section = 'hooks' | |
295 | hooks4.ui_key = RhodeCodeUi.HOOK_PULL |
|
302 | hooks4.ui_key = RhodeCodeUi.HOOK_PULL | |
296 | hooks4.ui_value = 'python:rhodecode.lib.hooks.log_pull_action' |
|
303 | hooks4.ui_value = 'python:rhodecode.lib.hooks.log_pull_action' | |
297 |
|
304 | |||
298 | # For mercurial 1.7 set backward comapatibility with format |
|
305 | # For mercurial 1.7 set backward comapatibility with format | |
299 | dotencode_disable = RhodeCodeUi() |
|
306 | dotencode_disable = RhodeCodeUi() | |
300 | dotencode_disable.ui_section = 'format' |
|
307 | dotencode_disable.ui_section = 'format' | |
301 | dotencode_disable.ui_key = 'dotencode' |
|
308 | dotencode_disable.ui_key = 'dotencode' | |
302 | dotencode_disable.ui_value = 'false' |
|
309 | dotencode_disable.ui_value = 'false' | |
303 |
|
310 | |||
304 | # enable largefiles |
|
311 | # enable largefiles | |
305 | largefiles = RhodeCodeUi() |
|
312 | largefiles = RhodeCodeUi() | |
306 | largefiles.ui_section = 'extensions' |
|
313 | largefiles.ui_section = 'extensions' | |
307 | largefiles.ui_key = 'largefiles' |
|
314 | largefiles.ui_key = 'largefiles' | |
308 |
largefiles.ui_value = ' |
|
315 | largefiles.ui_value = '' | |
309 |
|
316 | |||
310 | self.sa.add(hooks1) |
|
317 | self.sa.add(hooks1) | |
311 | self.sa.add(hooks2) |
|
318 | self.sa.add(hooks2) | |
312 | self.sa.add(hooks3) |
|
319 | self.sa.add(hooks3) | |
313 | self.sa.add(hooks4) |
|
320 | self.sa.add(hooks4) | |
314 | self.sa.add(largefiles) |
|
321 | self.sa.add(largefiles) | |
315 |
|
322 | |||
316 | def create_ldap_options(self, skip_existing=False): |
|
323 | def create_ldap_options(self, skip_existing=False): | |
317 | """Creates ldap settings""" |
|
324 | """Creates ldap settings""" | |
318 |
|
325 | |||
319 | for k, v in [('ldap_active', 'false'), ('ldap_host', ''), |
|
326 | for k, v in [('ldap_active', 'false'), ('ldap_host', ''), | |
320 | ('ldap_port', '389'), ('ldap_tls_kind', 'PLAIN'), |
|
327 | ('ldap_port', '389'), ('ldap_tls_kind', 'PLAIN'), | |
321 | ('ldap_tls_reqcert', ''), ('ldap_dn_user', ''), |
|
328 | ('ldap_tls_reqcert', ''), ('ldap_dn_user', ''), | |
322 | ('ldap_dn_pass', ''), ('ldap_base_dn', ''), |
|
329 | ('ldap_dn_pass', ''), ('ldap_base_dn', ''), | |
323 | ('ldap_filter', ''), ('ldap_search_scope', ''), |
|
330 | ('ldap_filter', ''), ('ldap_search_scope', ''), | |
324 | ('ldap_attr_login', ''), ('ldap_attr_firstname', ''), |
|
331 | ('ldap_attr_login', ''), ('ldap_attr_firstname', ''), | |
325 | ('ldap_attr_lastname', ''), ('ldap_attr_email', '')]: |
|
332 | ('ldap_attr_lastname', ''), ('ldap_attr_email', '')]: | |
326 |
|
333 | |||
327 | if skip_existing and RhodeCodeSetting.get_by_name(k) != None: |
|
334 | if skip_existing and RhodeCodeSetting.get_by_name(k) != None: | |
328 | log.debug('Skipping option %s' % k) |
|
335 | log.debug('Skipping option %s' % k) | |
329 | continue |
|
336 | continue | |
330 | setting = RhodeCodeSetting(k, v) |
|
337 | setting = RhodeCodeSetting(k, v) | |
331 | self.sa.add(setting) |
|
338 | self.sa.add(setting) | |
332 |
|
339 | |||
333 | def config_prompt(self, test_repo_path='', retries=3): |
|
340 | def config_prompt(self, test_repo_path='', retries=3): | |
334 | if retries == 3: |
|
341 | if retries == 3: | |
335 | log.info('Setting up repositories config') |
|
342 | log.info('Setting up repositories config') | |
336 |
|
343 | |||
337 | if not self.tests and not test_repo_path: |
|
344 | if not self.tests and not test_repo_path: | |
338 | path = raw_input('Specify valid full path to your repositories' |
|
345 | path = raw_input('Specify valid full path to your repositories' | |
339 | ' you can change this later in application settings:') |
|
346 | ' you can change this later in application settings:') | |
340 | else: |
|
347 | else: | |
341 | path = test_repo_path |
|
348 | path = test_repo_path | |
342 | path_ok = True |
|
349 | path_ok = True | |
343 |
|
350 | |||
344 | #check proper dir |
|
351 | # check proper dir | |
345 | if not os.path.isdir(path): |
|
352 | if not os.path.isdir(path): | |
346 | path_ok = False |
|
353 | path_ok = False | |
347 | log.error('Given path %s is not a valid directory', path) |
|
354 | log.error('Given path %s is not a valid directory', path) | |
348 |
|
355 | |||
349 | #check write access |
|
356 | # check write access | |
350 | if not os.access(path, os.W_OK) and path_ok: |
|
357 | if not os.access(path, os.W_OK) and path_ok: | |
351 | path_ok = False |
|
358 | path_ok = False | |
352 | log.error('No write permission to given path %s', path) |
|
359 | log.error('No write permission to given path %s', path) | |
353 |
|
360 | |||
354 |
|
||||
355 | if retries == 0: |
|
361 | if retries == 0: | |
356 | sys.exit('max retries reached') |
|
362 | sys.exit('max retries reached') | |
357 | if path_ok is False: |
|
363 | if path_ok is False: | |
358 | retries -= 1 |
|
364 | retries -= 1 | |
359 | return self.config_prompt(test_repo_path, retries) |
|
365 | return self.config_prompt(test_repo_path, retries) | |
360 |
|
366 | |||
361 | return path |
|
367 | return path | |
362 |
|
368 | |||
363 | def create_settings(self, path): |
|
369 | def create_settings(self, path): | |
364 |
|
370 | |||
365 | self.create_ui_settings() |
|
371 | self.create_ui_settings() | |
366 |
|
372 | |||
367 | #HG UI OPTIONS |
|
373 | #HG UI OPTIONS | |
368 | web1 = RhodeCodeUi() |
|
374 | web1 = RhodeCodeUi() | |
369 | web1.ui_section = 'web' |
|
375 | web1.ui_section = 'web' | |
370 | web1.ui_key = 'push_ssl' |
|
376 | web1.ui_key = 'push_ssl' | |
371 | web1.ui_value = 'false' |
|
377 | web1.ui_value = 'false' | |
372 |
|
378 | |||
373 | web2 = RhodeCodeUi() |
|
379 | web2 = RhodeCodeUi() | |
374 | web2.ui_section = 'web' |
|
380 | web2.ui_section = 'web' | |
375 | web2.ui_key = 'allow_archive' |
|
381 | web2.ui_key = 'allow_archive' | |
376 | web2.ui_value = 'gz zip bz2' |
|
382 | web2.ui_value = 'gz zip bz2' | |
377 |
|
383 | |||
378 | web3 = RhodeCodeUi() |
|
384 | web3 = RhodeCodeUi() | |
379 | web3.ui_section = 'web' |
|
385 | web3.ui_section = 'web' | |
380 | web3.ui_key = 'allow_push' |
|
386 | web3.ui_key = 'allow_push' | |
381 | web3.ui_value = '*' |
|
387 | web3.ui_value = '*' | |
382 |
|
388 | |||
383 | web4 = RhodeCodeUi() |
|
389 | web4 = RhodeCodeUi() | |
384 | web4.ui_section = 'web' |
|
390 | web4.ui_section = 'web' | |
385 | web4.ui_key = 'baseurl' |
|
391 | web4.ui_key = 'baseurl' | |
386 | web4.ui_value = '/' |
|
392 | web4.ui_value = '/' | |
387 |
|
393 | |||
388 | paths = RhodeCodeUi() |
|
394 | paths = RhodeCodeUi() | |
389 | paths.ui_section = 'paths' |
|
395 | paths.ui_section = 'paths' | |
390 | paths.ui_key = '/' |
|
396 | paths.ui_key = '/' | |
391 | paths.ui_value = path |
|
397 | paths.ui_value = path | |
392 |
|
398 | |||
393 | hgsettings1 = RhodeCodeSetting('realm', 'RhodeCode authentication') |
|
399 | hgsettings1 = RhodeCodeSetting('realm', 'RhodeCode authentication') | |
394 | hgsettings2 = RhodeCodeSetting('title', 'RhodeCode') |
|
400 | hgsettings2 = RhodeCodeSetting('title', 'RhodeCode') | |
395 | hgsettings3 = RhodeCodeSetting('ga_code', '') |
|
401 | hgsettings3 = RhodeCodeSetting('ga_code', '') | |
396 |
|
402 | |||
397 | self.sa.add(web1) |
|
403 | self.sa.add(web1) | |
398 | self.sa.add(web2) |
|
404 | self.sa.add(web2) | |
399 | self.sa.add(web3) |
|
405 | self.sa.add(web3) | |
400 | self.sa.add(web4) |
|
406 | self.sa.add(web4) | |
401 | self.sa.add(paths) |
|
407 | self.sa.add(paths) | |
402 | self.sa.add(hgsettings1) |
|
408 | self.sa.add(hgsettings1) | |
403 | self.sa.add(hgsettings2) |
|
409 | self.sa.add(hgsettings2) | |
404 | self.sa.add(hgsettings3) |
|
410 | self.sa.add(hgsettings3) | |
405 |
|
411 | |||
406 | self.create_ldap_options() |
|
412 | self.create_ldap_options() | |
407 |
|
413 | |||
408 | log.info('created ui config') |
|
414 | log.info('created ui config') | |
409 |
|
415 | |||
410 | def create_user(self, username, password, email='', admin=False): |
|
416 | def create_user(self, username, password, email='', admin=False): | |
411 | log.info('creating user %s', username) |
|
417 | log.info('creating user %s', username) | |
412 | UserModel().create_or_update(username, password, email, |
|
418 | UserModel().create_or_update(username, password, email, | |
413 | name='RhodeCode', lastname='Admin', |
|
419 | name='RhodeCode', lastname='Admin', | |
414 | active=True, admin=admin) |
|
420 | active=True, admin=admin) | |
415 |
|
421 | |||
416 | def create_default_user(self): |
|
422 | def create_default_user(self): | |
417 | log.info('creating default user') |
|
423 | log.info('creating default user') | |
418 | # create default user for handling default permissions. |
|
424 | # create default user for handling default permissions. | |
419 | UserModel().create_or_update(username='default', |
|
425 | UserModel().create_or_update(username='default', | |
420 | password=str(uuid.uuid1())[:8], |
|
426 | password=str(uuid.uuid1())[:8], | |
421 | email='anonymous@rhodecode.org', |
|
427 | email='anonymous@rhodecode.org', | |
422 | name='Anonymous', lastname='User') |
|
428 | name='Anonymous', lastname='User') | |
423 |
|
429 | |||
424 | def create_permissions(self): |
|
430 | def create_permissions(self): | |
425 | #module.(access|create|change|delete)_[name] |
|
431 | # module.(access|create|change|delete)_[name] | |
426 | #module.(read|write|owner) |
|
432 | # module.(read|write|owner) | |
427 | perms = [('repository.none', 'Repository no access'), |
|
433 | perms = [('repository.none', 'Repository no access'), | |
428 | ('repository.read', 'Repository read access'), |
|
434 | ('repository.read', 'Repository read access'), | |
429 | ('repository.write', 'Repository write access'), |
|
435 | ('repository.write', 'Repository write access'), | |
430 | ('repository.admin', 'Repository admin access'), |
|
436 | ('repository.admin', 'Repository admin access'), | |
431 | ('hg.admin', 'Hg Administrator'), |
|
437 | ('hg.admin', 'Hg Administrator'), | |
432 | ('hg.create.repository', 'Repository create'), |
|
438 | ('hg.create.repository', 'Repository create'), | |
433 | ('hg.create.none', 'Repository creation disabled'), |
|
439 | ('hg.create.none', 'Repository creation disabled'), | |
434 | ('hg.register.none', 'Register disabled'), |
|
440 | ('hg.register.none', 'Register disabled'), | |
435 | ('hg.register.manual_activate', 'Register new user with ' |
|
441 | ('hg.register.manual_activate', 'Register new user with ' | |
436 | 'RhodeCode without manual' |
|
442 | 'RhodeCode without manual' | |
437 | 'activation'), |
|
443 | 'activation'), | |
438 |
|
444 | |||
439 | ('hg.register.auto_activate', 'Register new user with ' |
|
445 | ('hg.register.auto_activate', 'Register new user with ' | |
440 | 'RhodeCode without auto ' |
|
446 | 'RhodeCode without auto ' | |
441 | 'activation'), |
|
447 | 'activation'), | |
442 | ] |
|
448 | ] | |
443 |
|
449 | |||
444 | for p in perms: |
|
450 | for p in perms: | |
445 | new_perm = Permission() |
|
451 | new_perm = Permission() | |
446 | new_perm.permission_name = p[0] |
|
452 | new_perm.permission_name = p[0] | |
447 | new_perm.permission_longname = p[1] |
|
453 | new_perm.permission_longname = p[1] | |
448 | self.sa.add(new_perm) |
|
454 | self.sa.add(new_perm) | |
449 |
|
455 | |||
450 | def populate_default_permissions(self): |
|
456 | def populate_default_permissions(self): | |
451 | log.info('creating default user permissions') |
|
457 | log.info('creating default user permissions') | |
452 |
|
458 | |||
453 | default_user = self.sa.query(User)\ |
|
459 | default_user = self.sa.query(User)\ | |
454 | .filter(User.username == 'default').scalar() |
|
460 | .filter(User.username == 'default').scalar() | |
455 |
|
461 | |||
456 | reg_perm = UserToPerm() |
|
462 | reg_perm = UserToPerm() | |
457 | reg_perm.user = default_user |
|
463 | reg_perm.user = default_user | |
458 | reg_perm.permission = self.sa.query(Permission)\ |
|
464 | reg_perm.permission = self.sa.query(Permission)\ | |
459 | .filter(Permission.permission_name == 'hg.register.manual_activate')\ |
|
465 | .filter(Permission.permission_name == 'hg.register.manual_activate')\ | |
460 | .scalar() |
|
466 | .scalar() | |
461 |
|
467 | |||
462 | create_repo_perm = UserToPerm() |
|
468 | create_repo_perm = UserToPerm() | |
463 | create_repo_perm.user = default_user |
|
469 | create_repo_perm.user = default_user | |
464 | create_repo_perm.permission = self.sa.query(Permission)\ |
|
470 | create_repo_perm.permission = self.sa.query(Permission)\ | |
465 | .filter(Permission.permission_name == 'hg.create.repository')\ |
|
471 | .filter(Permission.permission_name == 'hg.create.repository')\ | |
466 | .scalar() |
|
472 | .scalar() | |
467 |
|
473 | |||
468 | default_repo_perm = UserToPerm() |
|
474 | default_repo_perm = UserToPerm() | |
469 | default_repo_perm.user = default_user |
|
475 | default_repo_perm.user = default_user | |
470 | default_repo_perm.permission = self.sa.query(Permission)\ |
|
476 | default_repo_perm.permission = self.sa.query(Permission)\ | |
471 | .filter(Permission.permission_name == 'repository.read')\ |
|
477 | .filter(Permission.permission_name == 'repository.read')\ | |
472 | .scalar() |
|
478 | .scalar() | |
473 |
|
479 | |||
474 | self.sa.add(reg_perm) |
|
480 | self.sa.add(reg_perm) | |
475 | self.sa.add(create_repo_perm) |
|
481 | self.sa.add(create_repo_perm) | |
476 | self.sa.add(default_repo_perm) |
|
482 | self.sa.add(default_repo_perm) |
General Comments 0
You need to be logged in to leave comments.
Login now