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