Show More
@@ -1,617 +1,622 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 |
|
34 | |||
35 | from rhodecode.model.user import UserModel |
|
35 | from rhodecode.model.user import UserModel | |
36 | from rhodecode.lib.utils import ask_ok |
|
36 | from rhodecode.lib.utils import ask_ok | |
37 | from rhodecode.model import init_model |
|
37 | from rhodecode.model import init_model | |
38 | from rhodecode.model.db import User, Permission, RhodeCodeUi, \ |
|
38 | from rhodecode.model.db import User, Permission, RhodeCodeUi, \ | |
39 | RhodeCodeSetting, UserToPerm, DbMigrateVersion, RepoGroup, \ |
|
39 | RhodeCodeSetting, UserToPerm, DbMigrateVersion, RepoGroup, \ | |
40 | UserRepoGroupToPerm |
|
40 | UserRepoGroupToPerm | |
41 |
|
41 | |||
42 | from sqlalchemy.engine import create_engine |
|
42 | from sqlalchemy.engine import create_engine | |
43 | from rhodecode.model.repos_group import ReposGroupModel |
|
43 | from rhodecode.model.repos_group import ReposGroupModel | |
44 | #from rhodecode.model import meta |
|
44 | #from rhodecode.model import meta | |
45 | from rhodecode.model.meta import Session, Base |
|
45 | from rhodecode.model.meta import Session, Base | |
46 |
|
46 | |||
47 |
|
47 | |||
48 | log = logging.getLogger(__name__) |
|
48 | log = logging.getLogger(__name__) | |
49 |
|
49 | |||
50 |
|
50 | |||
51 | def notify(msg): |
|
51 | def notify(msg): | |
52 | """ |
|
52 | """ | |
53 | Notification for migrations messages |
|
53 | Notification for migrations messages | |
54 | """ |
|
54 | """ | |
55 | ml = len(msg) + (4 * 2) |
|
55 | ml = len(msg) + (4 * 2) | |
56 | print >> sys.stdout, ('*** %s ***\n%s' % (msg, '*' * ml)).upper() |
|
56 | print >> sys.stdout, ('*** %s ***\n%s' % (msg, '*' * ml)).upper() | |
57 |
|
57 | |||
58 |
|
58 | |||
59 | class DbManage(object): |
|
59 | class DbManage(object): | |
60 | def __init__(self, log_sql, dbconf, root, tests=False): |
|
60 | def __init__(self, log_sql, dbconf, root, tests=False): | |
61 | self.dbname = dbconf.split('/')[-1] |
|
61 | self.dbname = dbconf.split('/')[-1] | |
62 | self.tests = tests |
|
62 | self.tests = tests | |
63 | self.root = root |
|
63 | self.root = root | |
64 | self.dburi = dbconf |
|
64 | self.dburi = dbconf | |
65 | self.log_sql = log_sql |
|
65 | self.log_sql = log_sql | |
66 | self.db_exists = False |
|
66 | self.db_exists = False | |
67 | self.init_db() |
|
67 | self.init_db() | |
68 |
|
68 | |||
69 | def init_db(self): |
|
69 | def init_db(self): | |
70 | engine = create_engine(self.dburi, echo=self.log_sql) |
|
70 | engine = create_engine(self.dburi, echo=self.log_sql) | |
71 | init_model(engine) |
|
71 | init_model(engine) | |
72 | self.sa = Session() |
|
72 | self.sa = Session() | |
73 |
|
73 | |||
74 | def create_tables(self, override=False, defaults={}): |
|
74 | def create_tables(self, override=False, defaults={}): | |
75 | """ |
|
75 | """ | |
76 | Create a auth database |
|
76 | Create a auth database | |
77 | """ |
|
77 | """ | |
78 | quiet = defaults.get('quiet') |
|
78 | quiet = defaults.get('quiet') | |
79 | log.info("Any existing database is going to be destroyed") |
|
79 | log.info("Any existing database is going to be destroyed") | |
80 | if self.tests or quiet: |
|
80 | if self.tests or quiet: | |
81 | destroy = True |
|
81 | destroy = True | |
82 | else: |
|
82 | else: | |
83 | destroy = ask_ok('Are you sure to destroy old database ? [y/n]') |
|
83 | destroy = ask_ok('Are you sure to destroy old database ? [y/n]') | |
84 | if not destroy: |
|
84 | if not destroy: | |
85 | sys.exit() |
|
85 | sys.exit() | |
86 | if destroy: |
|
86 | if destroy: | |
87 | Base.metadata.drop_all() |
|
87 | Base.metadata.drop_all() | |
88 |
|
88 | |||
89 | checkfirst = not override |
|
89 | checkfirst = not override | |
90 | Base.metadata.create_all(checkfirst=checkfirst) |
|
90 | Base.metadata.create_all(checkfirst=checkfirst) | |
91 | log.info('Created tables for %s' % self.dbname) |
|
91 | log.info('Created tables for %s' % self.dbname) | |
92 |
|
92 | |||
93 | def set_db_version(self): |
|
93 | def set_db_version(self): | |
94 | ver = DbMigrateVersion() |
|
94 | ver = DbMigrateVersion() | |
95 | ver.version = __dbversion__ |
|
95 | ver.version = __dbversion__ | |
96 | ver.repository_id = 'rhodecode_db_migrations' |
|
96 | ver.repository_id = 'rhodecode_db_migrations' | |
97 | ver.repository_path = 'versions' |
|
97 | ver.repository_path = 'versions' | |
98 | self.sa.add(ver) |
|
98 | self.sa.add(ver) | |
99 | log.info('db version set to: %s' % __dbversion__) |
|
99 | log.info('db version set to: %s' % __dbversion__) | |
100 |
|
100 | |||
101 | def upgrade(self): |
|
101 | def upgrade(self): | |
102 | """ |
|
102 | """ | |
103 | Upgrades given database schema to given revision following |
|
103 | Upgrades given database schema to given revision following | |
104 | all needed steps, to perform the upgrade |
|
104 | all needed steps, to perform the upgrade | |
105 |
|
105 | |||
106 | """ |
|
106 | """ | |
107 |
|
107 | |||
108 | from rhodecode.lib.dbmigrate.migrate.versioning import api |
|
108 | from rhodecode.lib.dbmigrate.migrate.versioning import api | |
109 | from rhodecode.lib.dbmigrate.migrate.exceptions import \ |
|
109 | from rhodecode.lib.dbmigrate.migrate.exceptions import \ | |
110 | DatabaseNotControlledError |
|
110 | DatabaseNotControlledError | |
111 |
|
111 | |||
112 | if 'sqlite' in self.dburi: |
|
112 | if 'sqlite' in self.dburi: | |
113 | print ( |
|
113 | print ( | |
114 | '********************** WARNING **********************\n' |
|
114 | '********************** WARNING **********************\n' | |
115 | 'Make sure your version of sqlite is at least 3.7.X. \n' |
|
115 | 'Make sure your version of sqlite is at least 3.7.X. \n' | |
116 | 'Earlier versions are known to fail on some migrations\n' |
|
116 | 'Earlier versions are known to fail on some migrations\n' | |
117 | '*****************************************************\n' |
|
117 | '*****************************************************\n' | |
118 | ) |
|
118 | ) | |
119 | upgrade = ask_ok('You are about to perform database upgrade, make ' |
|
119 | upgrade = ask_ok('You are about to perform database upgrade, make ' | |
120 | 'sure You backed up your database before. ' |
|
120 | 'sure You backed up your database before. ' | |
121 | 'Continue ? [y/n]') |
|
121 | 'Continue ? [y/n]') | |
122 | if not upgrade: |
|
122 | if not upgrade: | |
123 | sys.exit('Nothing done') |
|
123 | sys.exit('Nothing done') | |
124 |
|
124 | |||
125 | repository_path = jn(dn(dn(dn(os.path.realpath(__file__)))), |
|
125 | repository_path = jn(dn(dn(dn(os.path.realpath(__file__)))), | |
126 | 'rhodecode/lib/dbmigrate') |
|
126 | 'rhodecode/lib/dbmigrate') | |
127 | db_uri = self.dburi |
|
127 | db_uri = self.dburi | |
128 |
|
128 | |||
129 | try: |
|
129 | try: | |
130 | curr_version = api.db_version(db_uri, repository_path) |
|
130 | curr_version = api.db_version(db_uri, repository_path) | |
131 | msg = ('Found current database under version' |
|
131 | msg = ('Found current database under version' | |
132 | ' control with version %s' % curr_version) |
|
132 | ' control with version %s' % curr_version) | |
133 |
|
133 | |||
134 | except (RuntimeError, DatabaseNotControlledError): |
|
134 | except (RuntimeError, DatabaseNotControlledError): | |
135 | curr_version = 1 |
|
135 | curr_version = 1 | |
136 | msg = ('Current database is not under version control. Setting' |
|
136 | msg = ('Current database is not under version control. Setting' | |
137 | ' as version %s' % curr_version) |
|
137 | ' as version %s' % curr_version) | |
138 | api.version_control(db_uri, repository_path, curr_version) |
|
138 | api.version_control(db_uri, repository_path, curr_version) | |
139 |
|
139 | |||
140 | notify(msg) |
|
140 | notify(msg) | |
141 |
|
141 | |||
142 | if curr_version == __dbversion__: |
|
142 | if curr_version == __dbversion__: | |
143 | sys.exit('This database is already at the newest version') |
|
143 | sys.exit('This database is already at the newest version') | |
144 |
|
144 | |||
145 | #====================================================================== |
|
145 | #====================================================================== | |
146 | # UPGRADE STEPS |
|
146 | # UPGRADE STEPS | |
147 | #====================================================================== |
|
147 | #====================================================================== | |
148 |
|
148 | |||
149 | class UpgradeSteps(object): |
|
149 | class UpgradeSteps(object): | |
150 | """ |
|
150 | """ | |
151 | Those steps follow schema versions so for example schema |
|
151 | Those steps follow schema versions so for example schema | |
152 | for example schema with seq 002 == step_2 and so on. |
|
152 | for example schema with seq 002 == step_2 and so on. | |
153 | """ |
|
153 | """ | |
154 |
|
154 | |||
155 | def __init__(self, klass): |
|
155 | def __init__(self, klass): | |
156 | self.klass = klass |
|
156 | self.klass = klass | |
157 |
|
157 | |||
158 | def step_0(self): |
|
158 | def step_0(self): | |
159 | # step 0 is the schema upgrade, and than follow proper upgrades |
|
159 | # step 0 is the schema upgrade, and than follow proper upgrades | |
160 | notify('attempting to do database upgrade to version %s' \ |
|
160 | notify('attempting to do database upgrade to version %s' \ | |
161 | % __dbversion__) |
|
161 | % __dbversion__) | |
162 | api.upgrade(db_uri, repository_path, __dbversion__) |
|
162 | api.upgrade(db_uri, repository_path, __dbversion__) | |
163 | notify('Schema upgrade completed') |
|
163 | notify('Schema upgrade completed') | |
164 |
|
164 | |||
165 | def step_1(self): |
|
165 | def step_1(self): | |
166 | pass |
|
166 | pass | |
167 |
|
167 | |||
168 | def step_2(self): |
|
168 | def step_2(self): | |
169 | notify('Patching repo paths for newer version of RhodeCode') |
|
169 | notify('Patching repo paths for newer version of RhodeCode') | |
170 | self.klass.fix_repo_paths() |
|
170 | self.klass.fix_repo_paths() | |
171 |
|
171 | |||
172 | notify('Patching default user of RhodeCode') |
|
172 | notify('Patching default user of RhodeCode') | |
173 | self.klass.fix_default_user() |
|
173 | self.klass.fix_default_user() | |
174 |
|
174 | |||
175 | log.info('Changing ui settings') |
|
175 | log.info('Changing ui settings') | |
176 | self.klass.create_ui_settings() |
|
176 | self.klass.create_ui_settings() | |
177 |
|
177 | |||
178 | def step_3(self): |
|
178 | def step_3(self): | |
179 | notify('Adding additional settings into RhodeCode db') |
|
179 | notify('Adding additional settings into RhodeCode db') | |
180 | self.klass.fix_settings() |
|
180 | self.klass.fix_settings() | |
181 | notify('Adding ldap defaults') |
|
181 | notify('Adding ldap defaults') | |
182 | self.klass.create_ldap_options(skip_existing=True) |
|
182 | self.klass.create_ldap_options(skip_existing=True) | |
183 |
|
183 | |||
184 | def step_4(self): |
|
184 | def step_4(self): | |
185 | notify('create permissions and fix groups') |
|
185 | notify('create permissions and fix groups') | |
186 | self.klass.create_permissions() |
|
186 | self.klass.create_permissions() | |
187 | self.klass.fixup_groups() |
|
187 | self.klass.fixup_groups() | |
188 |
|
188 | |||
189 | def step_5(self): |
|
189 | def step_5(self): | |
190 | pass |
|
190 | pass | |
191 |
|
191 | |||
192 | def step_6(self): |
|
192 | def step_6(self): | |
193 |
|
193 | |||
194 | notify('re-checking permissions') |
|
194 | notify('re-checking permissions') | |
195 | self.klass.create_permissions() |
|
195 | self.klass.create_permissions() | |
196 |
|
196 | |||
197 | notify('installing new UI options') |
|
197 | notify('installing new UI options') | |
198 | sett4 = RhodeCodeSetting('show_public_icon', True) |
|
198 | sett4 = RhodeCodeSetting('show_public_icon', True) | |
199 | Session().add(sett4) |
|
199 | Session().add(sett4) | |
200 | sett5 = RhodeCodeSetting('show_private_icon', True) |
|
200 | sett5 = RhodeCodeSetting('show_private_icon', True) | |
201 | Session().add(sett5) |
|
201 | Session().add(sett5) | |
202 | sett6 = RhodeCodeSetting('stylify_metatags', False) |
|
202 | sett6 = RhodeCodeSetting('stylify_metatags', False) | |
203 | Session().add(sett6) |
|
203 | Session().add(sett6) | |
204 |
|
204 | |||
205 | notify('fixing old PULL hook') |
|
205 | notify('fixing old PULL hook') | |
206 | _pull = RhodeCodeUi.get_by_key('preoutgoing.pull_logger') |
|
206 | _pull = RhodeCodeUi.get_by_key('preoutgoing.pull_logger') | |
207 | if _pull: |
|
207 | if _pull: | |
208 | _pull.ui_key = RhodeCodeUi.HOOK_PULL |
|
208 | _pull.ui_key = RhodeCodeUi.HOOK_PULL | |
209 | Session().add(_pull) |
|
209 | Session().add(_pull) | |
210 |
|
210 | |||
211 | notify('fixing old PUSH hook') |
|
211 | notify('fixing old PUSH hook') | |
212 | _push = RhodeCodeUi.get_by_key('pretxnchangegroup.push_logger') |
|
212 | _push = RhodeCodeUi.get_by_key('pretxnchangegroup.push_logger') | |
213 | if _push: |
|
213 | if _push: | |
214 | _push.ui_key = RhodeCodeUi.HOOK_PUSH |
|
214 | _push.ui_key = RhodeCodeUi.HOOK_PUSH | |
215 | Session().add(_push) |
|
215 | Session().add(_push) | |
216 |
|
216 | |||
217 | notify('installing new pre-push hook') |
|
217 | notify('installing new pre-push hook') | |
218 | hooks4 = RhodeCodeUi() |
|
218 | hooks4 = RhodeCodeUi() | |
219 | hooks4.ui_section = 'hooks' |
|
219 | hooks4.ui_section = 'hooks' | |
220 | hooks4.ui_key = RhodeCodeUi.HOOK_PRE_PUSH |
|
220 | hooks4.ui_key = RhodeCodeUi.HOOK_PRE_PUSH | |
221 | hooks4.ui_value = 'python:rhodecode.lib.hooks.pre_push' |
|
221 | hooks4.ui_value = 'python:rhodecode.lib.hooks.pre_push' | |
222 | Session().add(hooks4) |
|
222 | Session().add(hooks4) | |
223 |
|
223 | |||
224 | notify('installing new pre-pull hook') |
|
224 | notify('installing new pre-pull hook') | |
225 | hooks6 = RhodeCodeUi() |
|
225 | hooks6 = RhodeCodeUi() | |
226 | hooks6.ui_section = 'hooks' |
|
226 | hooks6.ui_section = 'hooks' | |
227 | hooks6.ui_key = RhodeCodeUi.HOOK_PRE_PULL |
|
227 | hooks6.ui_key = RhodeCodeUi.HOOK_PRE_PULL | |
228 | hooks6.ui_value = 'python:rhodecode.lib.hooks.pre_pull' |
|
228 | hooks6.ui_value = 'python:rhodecode.lib.hooks.pre_pull' | |
229 | Session().add(hooks6) |
|
229 | Session().add(hooks6) | |
230 |
|
230 | |||
231 | notify('installing hgsubversion option') |
|
231 | notify('installing hgsubversion option') | |
232 | # enable hgsubversion disabled by default |
|
232 | # enable hgsubversion disabled by default | |
233 | hgsubversion = RhodeCodeUi() |
|
233 | hgsubversion = RhodeCodeUi() | |
234 | hgsubversion.ui_section = 'extensions' |
|
234 | hgsubversion.ui_section = 'extensions' | |
235 | hgsubversion.ui_key = 'hgsubversion' |
|
235 | hgsubversion.ui_key = 'hgsubversion' | |
236 | hgsubversion.ui_value = '' |
|
236 | hgsubversion.ui_value = '' | |
237 | hgsubversion.ui_active = False |
|
237 | hgsubversion.ui_active = False | |
238 | Session().add(hgsubversion) |
|
238 | Session().add(hgsubversion) | |
239 |
|
239 | |||
240 | notify('installing hg git option') |
|
240 | notify('installing hg git option') | |
241 | # enable hggit disabled by default |
|
241 | # enable hggit disabled by default | |
242 | hggit = RhodeCodeUi() |
|
242 | hggit = RhodeCodeUi() | |
243 | hggit.ui_section = 'extensions' |
|
243 | hggit.ui_section = 'extensions' | |
244 | hggit.ui_key = 'hggit' |
|
244 | hggit.ui_key = 'hggit' | |
245 | hggit.ui_value = '' |
|
245 | hggit.ui_value = '' | |
246 | hggit.ui_active = False |
|
246 | hggit.ui_active = False | |
247 | Session().add(hggit) |
|
247 | Session().add(hggit) | |
248 |
|
248 | |||
249 | notify('re-check default permissions') |
|
249 | notify('re-check default permissions') | |
250 | self.klass.populate_default_permissions() |
|
250 | default_user = User.get_by_username(User.DEFAULT_USER) | |
|
251 | perm = Permission.get_by_key('hg.fork.repository') | |||
|
252 | reg_perm = UserToPerm() | |||
|
253 | reg_perm.user = default_user | |||
|
254 | reg_perm.permission = perm | |||
|
255 | Session().add(reg_perm) | |||
251 |
|
256 | |||
252 | upgrade_steps = [0] + range(curr_version + 1, __dbversion__ + 1) |
|
257 | upgrade_steps = [0] + range(curr_version + 1, __dbversion__ + 1) | |
253 |
|
258 | |||
254 | # CALL THE PROPER ORDER OF STEPS TO PERFORM FULL UPGRADE |
|
259 | # CALL THE PROPER ORDER OF STEPS TO PERFORM FULL UPGRADE | |
255 | _step = None |
|
260 | _step = None | |
256 | for step in upgrade_steps: |
|
261 | for step in upgrade_steps: | |
257 | notify('performing upgrade step %s' % step) |
|
262 | notify('performing upgrade step %s' % step) | |
258 | getattr(UpgradeSteps(self), 'step_%s' % step)() |
|
263 | getattr(UpgradeSteps(self), 'step_%s' % step)() | |
259 | self.sa.commit() |
|
264 | self.sa.commit() | |
260 | _step = step |
|
265 | _step = step | |
261 |
|
266 | |||
262 | notify('upgrade to version %s successful' % _step) |
|
267 | notify('upgrade to version %s successful' % _step) | |
263 |
|
268 | |||
264 | def fix_repo_paths(self): |
|
269 | def fix_repo_paths(self): | |
265 | """ |
|
270 | """ | |
266 | Fixes a old rhodecode version path into new one without a '*' |
|
271 | Fixes a old rhodecode version path into new one without a '*' | |
267 | """ |
|
272 | """ | |
268 |
|
273 | |||
269 | paths = self.sa.query(RhodeCodeUi)\ |
|
274 | paths = self.sa.query(RhodeCodeUi)\ | |
270 | .filter(RhodeCodeUi.ui_key == '/')\ |
|
275 | .filter(RhodeCodeUi.ui_key == '/')\ | |
271 | .scalar() |
|
276 | .scalar() | |
272 |
|
277 | |||
273 | paths.ui_value = paths.ui_value.replace('*', '') |
|
278 | paths.ui_value = paths.ui_value.replace('*', '') | |
274 |
|
279 | |||
275 | try: |
|
280 | try: | |
276 | self.sa.add(paths) |
|
281 | self.sa.add(paths) | |
277 | self.sa.commit() |
|
282 | self.sa.commit() | |
278 | except: |
|
283 | except: | |
279 | self.sa.rollback() |
|
284 | self.sa.rollback() | |
280 | raise |
|
285 | raise | |
281 |
|
286 | |||
282 | def fix_default_user(self): |
|
287 | def fix_default_user(self): | |
283 | """ |
|
288 | """ | |
284 | Fixes a old default user with some 'nicer' default values, |
|
289 | Fixes a old default user with some 'nicer' default values, | |
285 | used mostly for anonymous access |
|
290 | used mostly for anonymous access | |
286 | """ |
|
291 | """ | |
287 | def_user = self.sa.query(User)\ |
|
292 | def_user = self.sa.query(User)\ | |
288 | .filter(User.username == 'default')\ |
|
293 | .filter(User.username == 'default')\ | |
289 | .one() |
|
294 | .one() | |
290 |
|
295 | |||
291 | def_user.name = 'Anonymous' |
|
296 | def_user.name = 'Anonymous' | |
292 | def_user.lastname = 'User' |
|
297 | def_user.lastname = 'User' | |
293 | def_user.email = 'anonymous@rhodecode.org' |
|
298 | def_user.email = 'anonymous@rhodecode.org' | |
294 |
|
299 | |||
295 | try: |
|
300 | try: | |
296 | self.sa.add(def_user) |
|
301 | self.sa.add(def_user) | |
297 | self.sa.commit() |
|
302 | self.sa.commit() | |
298 | except: |
|
303 | except: | |
299 | self.sa.rollback() |
|
304 | self.sa.rollback() | |
300 | raise |
|
305 | raise | |
301 |
|
306 | |||
302 | def fix_settings(self): |
|
307 | def fix_settings(self): | |
303 | """ |
|
308 | """ | |
304 | Fixes rhodecode settings adds ga_code key for google analytics |
|
309 | Fixes rhodecode settings adds ga_code key for google analytics | |
305 | """ |
|
310 | """ | |
306 |
|
311 | |||
307 | hgsettings3 = RhodeCodeSetting('ga_code', '') |
|
312 | hgsettings3 = RhodeCodeSetting('ga_code', '') | |
308 |
|
313 | |||
309 | try: |
|
314 | try: | |
310 | self.sa.add(hgsettings3) |
|
315 | self.sa.add(hgsettings3) | |
311 | self.sa.commit() |
|
316 | self.sa.commit() | |
312 | except: |
|
317 | except: | |
313 | self.sa.rollback() |
|
318 | self.sa.rollback() | |
314 | raise |
|
319 | raise | |
315 |
|
320 | |||
316 | def admin_prompt(self, second=False, defaults={}): |
|
321 | def admin_prompt(self, second=False, defaults={}): | |
317 | if not self.tests: |
|
322 | if not self.tests: | |
318 | import getpass |
|
323 | import getpass | |
319 |
|
324 | |||
320 | # defaults |
|
325 | # defaults | |
321 | username = defaults.get('username') |
|
326 | username = defaults.get('username') | |
322 | password = defaults.get('password') |
|
327 | password = defaults.get('password') | |
323 | email = defaults.get('email') |
|
328 | email = defaults.get('email') | |
324 |
|
329 | |||
325 | def get_password(): |
|
330 | def get_password(): | |
326 | password = getpass.getpass('Specify admin password ' |
|
331 | password = getpass.getpass('Specify admin password ' | |
327 | '(min 6 chars):') |
|
332 | '(min 6 chars):') | |
328 | confirm = getpass.getpass('Confirm password:') |
|
333 | confirm = getpass.getpass('Confirm password:') | |
329 |
|
334 | |||
330 | if password != confirm: |
|
335 | if password != confirm: | |
331 | log.error('passwords mismatch') |
|
336 | log.error('passwords mismatch') | |
332 | return False |
|
337 | return False | |
333 | if len(password) < 6: |
|
338 | if len(password) < 6: | |
334 | log.error('password is to short use at least 6 characters') |
|
339 | log.error('password is to short use at least 6 characters') | |
335 | return False |
|
340 | return False | |
336 |
|
341 | |||
337 | return password |
|
342 | return password | |
338 | if username is None: |
|
343 | if username is None: | |
339 | username = raw_input('Specify admin username:') |
|
344 | username = raw_input('Specify admin username:') | |
340 | if password is None: |
|
345 | if password is None: | |
341 | password = get_password() |
|
346 | password = get_password() | |
342 | if not password: |
|
347 | if not password: | |
343 | #second try |
|
348 | #second try | |
344 | password = get_password() |
|
349 | password = get_password() | |
345 | if not password: |
|
350 | if not password: | |
346 | sys.exit() |
|
351 | sys.exit() | |
347 | if email is None: |
|
352 | if email is None: | |
348 | email = raw_input('Specify admin email:') |
|
353 | email = raw_input('Specify admin email:') | |
349 | self.create_user(username, password, email, True) |
|
354 | self.create_user(username, password, email, True) | |
350 | else: |
|
355 | else: | |
351 | log.info('creating admin and regular test users') |
|
356 | log.info('creating admin and regular test users') | |
352 | from rhodecode.tests import TEST_USER_ADMIN_LOGIN, \ |
|
357 | from rhodecode.tests import TEST_USER_ADMIN_LOGIN, \ | |
353 | TEST_USER_ADMIN_PASS, TEST_USER_ADMIN_EMAIL, \ |
|
358 | TEST_USER_ADMIN_PASS, TEST_USER_ADMIN_EMAIL, \ | |
354 | TEST_USER_REGULAR_LOGIN, TEST_USER_REGULAR_PASS, \ |
|
359 | TEST_USER_REGULAR_LOGIN, TEST_USER_REGULAR_PASS, \ | |
355 | TEST_USER_REGULAR_EMAIL, TEST_USER_REGULAR2_LOGIN, \ |
|
360 | TEST_USER_REGULAR_EMAIL, TEST_USER_REGULAR2_LOGIN, \ | |
356 | TEST_USER_REGULAR2_PASS, TEST_USER_REGULAR2_EMAIL |
|
361 | TEST_USER_REGULAR2_PASS, TEST_USER_REGULAR2_EMAIL | |
357 |
|
362 | |||
358 | self.create_user(TEST_USER_ADMIN_LOGIN, TEST_USER_ADMIN_PASS, |
|
363 | self.create_user(TEST_USER_ADMIN_LOGIN, TEST_USER_ADMIN_PASS, | |
359 | TEST_USER_ADMIN_EMAIL, True) |
|
364 | TEST_USER_ADMIN_EMAIL, True) | |
360 |
|
365 | |||
361 | self.create_user(TEST_USER_REGULAR_LOGIN, TEST_USER_REGULAR_PASS, |
|
366 | self.create_user(TEST_USER_REGULAR_LOGIN, TEST_USER_REGULAR_PASS, | |
362 | TEST_USER_REGULAR_EMAIL, False) |
|
367 | TEST_USER_REGULAR_EMAIL, False) | |
363 |
|
368 | |||
364 | self.create_user(TEST_USER_REGULAR2_LOGIN, TEST_USER_REGULAR2_PASS, |
|
369 | self.create_user(TEST_USER_REGULAR2_LOGIN, TEST_USER_REGULAR2_PASS, | |
365 | TEST_USER_REGULAR2_EMAIL, False) |
|
370 | TEST_USER_REGULAR2_EMAIL, False) | |
366 |
|
371 | |||
367 | def create_ui_settings(self): |
|
372 | def create_ui_settings(self): | |
368 | """ |
|
373 | """ | |
369 | Creates ui settings, fills out hooks |
|
374 | Creates ui settings, fills out hooks | |
370 | and disables dotencode |
|
375 | and disables dotencode | |
371 | """ |
|
376 | """ | |
372 |
|
377 | |||
373 | #HOOKS |
|
378 | #HOOKS | |
374 | hooks1_key = RhodeCodeUi.HOOK_UPDATE |
|
379 | hooks1_key = RhodeCodeUi.HOOK_UPDATE | |
375 | hooks1_ = self.sa.query(RhodeCodeUi)\ |
|
380 | hooks1_ = self.sa.query(RhodeCodeUi)\ | |
376 | .filter(RhodeCodeUi.ui_key == hooks1_key).scalar() |
|
381 | .filter(RhodeCodeUi.ui_key == hooks1_key).scalar() | |
377 |
|
382 | |||
378 | hooks1 = RhodeCodeUi() if hooks1_ is None else hooks1_ |
|
383 | hooks1 = RhodeCodeUi() if hooks1_ is None else hooks1_ | |
379 | hooks1.ui_section = 'hooks' |
|
384 | hooks1.ui_section = 'hooks' | |
380 | hooks1.ui_key = hooks1_key |
|
385 | hooks1.ui_key = hooks1_key | |
381 | hooks1.ui_value = 'hg update >&2' |
|
386 | hooks1.ui_value = 'hg update >&2' | |
382 | hooks1.ui_active = False |
|
387 | hooks1.ui_active = False | |
383 | self.sa.add(hooks1) |
|
388 | self.sa.add(hooks1) | |
384 |
|
389 | |||
385 | hooks2_key = RhodeCodeUi.HOOK_REPO_SIZE |
|
390 | hooks2_key = RhodeCodeUi.HOOK_REPO_SIZE | |
386 | hooks2_ = self.sa.query(RhodeCodeUi)\ |
|
391 | hooks2_ = self.sa.query(RhodeCodeUi)\ | |
387 | .filter(RhodeCodeUi.ui_key == hooks2_key).scalar() |
|
392 | .filter(RhodeCodeUi.ui_key == hooks2_key).scalar() | |
388 | hooks2 = RhodeCodeUi() if hooks2_ is None else hooks2_ |
|
393 | hooks2 = RhodeCodeUi() if hooks2_ is None else hooks2_ | |
389 | hooks2.ui_section = 'hooks' |
|
394 | hooks2.ui_section = 'hooks' | |
390 | hooks2.ui_key = hooks2_key |
|
395 | hooks2.ui_key = hooks2_key | |
391 | hooks2.ui_value = 'python:rhodecode.lib.hooks.repo_size' |
|
396 | hooks2.ui_value = 'python:rhodecode.lib.hooks.repo_size' | |
392 | self.sa.add(hooks2) |
|
397 | self.sa.add(hooks2) | |
393 |
|
398 | |||
394 | hooks3 = RhodeCodeUi() |
|
399 | hooks3 = RhodeCodeUi() | |
395 | hooks3.ui_section = 'hooks' |
|
400 | hooks3.ui_section = 'hooks' | |
396 | hooks3.ui_key = RhodeCodeUi.HOOK_PUSH |
|
401 | hooks3.ui_key = RhodeCodeUi.HOOK_PUSH | |
397 | hooks3.ui_value = 'python:rhodecode.lib.hooks.log_push_action' |
|
402 | hooks3.ui_value = 'python:rhodecode.lib.hooks.log_push_action' | |
398 | self.sa.add(hooks3) |
|
403 | self.sa.add(hooks3) | |
399 |
|
404 | |||
400 | hooks4 = RhodeCodeUi() |
|
405 | hooks4 = RhodeCodeUi() | |
401 | hooks4.ui_section = 'hooks' |
|
406 | hooks4.ui_section = 'hooks' | |
402 | hooks4.ui_key = RhodeCodeUi.HOOK_PRE_PUSH |
|
407 | hooks4.ui_key = RhodeCodeUi.HOOK_PRE_PUSH | |
403 | hooks4.ui_value = 'python:rhodecode.lib.hooks.pre_push' |
|
408 | hooks4.ui_value = 'python:rhodecode.lib.hooks.pre_push' | |
404 | self.sa.add(hooks4) |
|
409 | self.sa.add(hooks4) | |
405 |
|
410 | |||
406 | hooks5 = RhodeCodeUi() |
|
411 | hooks5 = RhodeCodeUi() | |
407 | hooks5.ui_section = 'hooks' |
|
412 | hooks5.ui_section = 'hooks' | |
408 | hooks5.ui_key = RhodeCodeUi.HOOK_PULL |
|
413 | hooks5.ui_key = RhodeCodeUi.HOOK_PULL | |
409 | hooks5.ui_value = 'python:rhodecode.lib.hooks.log_pull_action' |
|
414 | hooks5.ui_value = 'python:rhodecode.lib.hooks.log_pull_action' | |
410 | self.sa.add(hooks5) |
|
415 | self.sa.add(hooks5) | |
411 |
|
416 | |||
412 | hooks6 = RhodeCodeUi() |
|
417 | hooks6 = RhodeCodeUi() | |
413 | hooks6.ui_section = 'hooks' |
|
418 | hooks6.ui_section = 'hooks' | |
414 | hooks6.ui_key = RhodeCodeUi.HOOK_PRE_PULL |
|
419 | hooks6.ui_key = RhodeCodeUi.HOOK_PRE_PULL | |
415 | hooks6.ui_value = 'python:rhodecode.lib.hooks.pre_pull' |
|
420 | hooks6.ui_value = 'python:rhodecode.lib.hooks.pre_pull' | |
416 | self.sa.add(hooks6) |
|
421 | self.sa.add(hooks6) | |
417 |
|
422 | |||
418 | # enable largefiles |
|
423 | # enable largefiles | |
419 | largefiles = RhodeCodeUi() |
|
424 | largefiles = RhodeCodeUi() | |
420 | largefiles.ui_section = 'extensions' |
|
425 | largefiles.ui_section = 'extensions' | |
421 | largefiles.ui_key = 'largefiles' |
|
426 | largefiles.ui_key = 'largefiles' | |
422 | largefiles.ui_value = '' |
|
427 | largefiles.ui_value = '' | |
423 | self.sa.add(largefiles) |
|
428 | self.sa.add(largefiles) | |
424 |
|
429 | |||
425 | # enable hgsubversion disabled by default |
|
430 | # enable hgsubversion disabled by default | |
426 | hgsubversion = RhodeCodeUi() |
|
431 | hgsubversion = RhodeCodeUi() | |
427 | hgsubversion.ui_section = 'extensions' |
|
432 | hgsubversion.ui_section = 'extensions' | |
428 | hgsubversion.ui_key = 'hgsubversion' |
|
433 | hgsubversion.ui_key = 'hgsubversion' | |
429 | hgsubversion.ui_value = '' |
|
434 | hgsubversion.ui_value = '' | |
430 | hgsubversion.ui_active = False |
|
435 | hgsubversion.ui_active = False | |
431 | self.sa.add(hgsubversion) |
|
436 | self.sa.add(hgsubversion) | |
432 |
|
437 | |||
433 | # enable hggit disabled by default |
|
438 | # enable hggit disabled by default | |
434 | hggit = RhodeCodeUi() |
|
439 | hggit = RhodeCodeUi() | |
435 | hggit.ui_section = 'extensions' |
|
440 | hggit.ui_section = 'extensions' | |
436 | hggit.ui_key = 'hggit' |
|
441 | hggit.ui_key = 'hggit' | |
437 | hggit.ui_value = '' |
|
442 | hggit.ui_value = '' | |
438 | hggit.ui_active = False |
|
443 | hggit.ui_active = False | |
439 | self.sa.add(hggit) |
|
444 | self.sa.add(hggit) | |
440 |
|
445 | |||
441 | def create_ldap_options(self, skip_existing=False): |
|
446 | def create_ldap_options(self, skip_existing=False): | |
442 | """Creates ldap settings""" |
|
447 | """Creates ldap settings""" | |
443 |
|
448 | |||
444 | for k, v in [('ldap_active', 'false'), ('ldap_host', ''), |
|
449 | for k, v in [('ldap_active', 'false'), ('ldap_host', ''), | |
445 | ('ldap_port', '389'), ('ldap_tls_kind', 'PLAIN'), |
|
450 | ('ldap_port', '389'), ('ldap_tls_kind', 'PLAIN'), | |
446 | ('ldap_tls_reqcert', ''), ('ldap_dn_user', ''), |
|
451 | ('ldap_tls_reqcert', ''), ('ldap_dn_user', ''), | |
447 | ('ldap_dn_pass', ''), ('ldap_base_dn', ''), |
|
452 | ('ldap_dn_pass', ''), ('ldap_base_dn', ''), | |
448 | ('ldap_filter', ''), ('ldap_search_scope', ''), |
|
453 | ('ldap_filter', ''), ('ldap_search_scope', ''), | |
449 | ('ldap_attr_login', ''), ('ldap_attr_firstname', ''), |
|
454 | ('ldap_attr_login', ''), ('ldap_attr_firstname', ''), | |
450 | ('ldap_attr_lastname', ''), ('ldap_attr_email', '')]: |
|
455 | ('ldap_attr_lastname', ''), ('ldap_attr_email', '')]: | |
451 |
|
456 | |||
452 | if skip_existing and RhodeCodeSetting.get_by_name(k) != None: |
|
457 | if skip_existing and RhodeCodeSetting.get_by_name(k) != None: | |
453 | log.debug('Skipping option %s' % k) |
|
458 | log.debug('Skipping option %s' % k) | |
454 | continue |
|
459 | continue | |
455 | setting = RhodeCodeSetting(k, v) |
|
460 | setting = RhodeCodeSetting(k, v) | |
456 | self.sa.add(setting) |
|
461 | self.sa.add(setting) | |
457 |
|
462 | |||
458 | def fixup_groups(self): |
|
463 | def fixup_groups(self): | |
459 | def_usr = User.get_by_username('default') |
|
464 | def_usr = User.get_by_username('default') | |
460 | for g in RepoGroup.query().all(): |
|
465 | for g in RepoGroup.query().all(): | |
461 | g.group_name = g.get_new_name(g.name) |
|
466 | g.group_name = g.get_new_name(g.name) | |
462 | self.sa.add(g) |
|
467 | self.sa.add(g) | |
463 | # get default perm |
|
468 | # get default perm | |
464 | default = UserRepoGroupToPerm.query()\ |
|
469 | default = UserRepoGroupToPerm.query()\ | |
465 | .filter(UserRepoGroupToPerm.group == g)\ |
|
470 | .filter(UserRepoGroupToPerm.group == g)\ | |
466 | .filter(UserRepoGroupToPerm.user == def_usr)\ |
|
471 | .filter(UserRepoGroupToPerm.user == def_usr)\ | |
467 | .scalar() |
|
472 | .scalar() | |
468 |
|
473 | |||
469 | if default is None: |
|
474 | if default is None: | |
470 | log.debug('missing default permission for group %s adding' % g) |
|
475 | log.debug('missing default permission for group %s adding' % g) | |
471 | ReposGroupModel()._create_default_perms(g) |
|
476 | ReposGroupModel()._create_default_perms(g) | |
472 |
|
477 | |||
473 | def config_prompt(self, test_repo_path='', retries=3, defaults={}): |
|
478 | def config_prompt(self, test_repo_path='', retries=3, defaults={}): | |
474 | _path = defaults.get('repos_location') |
|
479 | _path = defaults.get('repos_location') | |
475 | if retries == 3: |
|
480 | if retries == 3: | |
476 | log.info('Setting up repositories config') |
|
481 | log.info('Setting up repositories config') | |
477 |
|
482 | |||
478 | if _path is not None: |
|
483 | if _path is not None: | |
479 | path = _path |
|
484 | path = _path | |
480 | elif not self.tests and not test_repo_path: |
|
485 | elif not self.tests and not test_repo_path: | |
481 | path = raw_input( |
|
486 | path = raw_input( | |
482 | 'Enter a valid absolute path to store repositories. ' |
|
487 | 'Enter a valid absolute path to store repositories. ' | |
483 | 'All repositories in that path will be added automatically:' |
|
488 | 'All repositories in that path will be added automatically:' | |
484 | ) |
|
489 | ) | |
485 | else: |
|
490 | else: | |
486 | path = test_repo_path |
|
491 | path = test_repo_path | |
487 | path_ok = True |
|
492 | path_ok = True | |
488 |
|
493 | |||
489 | # check proper dir |
|
494 | # check proper dir | |
490 | if not os.path.isdir(path): |
|
495 | if not os.path.isdir(path): | |
491 | path_ok = False |
|
496 | path_ok = False | |
492 | log.error('Given path %s is not a valid directory' % path) |
|
497 | log.error('Given path %s is not a valid directory' % path) | |
493 |
|
498 | |||
494 | elif not os.path.isabs(path): |
|
499 | elif not os.path.isabs(path): | |
495 | path_ok = False |
|
500 | path_ok = False | |
496 | log.error('Given path %s is not an absolute path' % path) |
|
501 | log.error('Given path %s is not an absolute path' % path) | |
497 |
|
502 | |||
498 | # check write access |
|
503 | # check write access | |
499 | elif not os.access(path, os.W_OK) and path_ok: |
|
504 | elif not os.access(path, os.W_OK) and path_ok: | |
500 | path_ok = False |
|
505 | path_ok = False | |
501 | log.error('No write permission to given path %s' % path) |
|
506 | log.error('No write permission to given path %s' % path) | |
502 |
|
507 | |||
503 | if retries == 0: |
|
508 | if retries == 0: | |
504 | sys.exit('max retries reached') |
|
509 | sys.exit('max retries reached') | |
505 | if path_ok is False: |
|
510 | if path_ok is False: | |
506 | retries -= 1 |
|
511 | retries -= 1 | |
507 | return self.config_prompt(test_repo_path, retries) |
|
512 | return self.config_prompt(test_repo_path, retries) | |
508 |
|
513 | |||
509 | return path |
|
514 | return path | |
510 |
|
515 | |||
511 | def create_settings(self, path): |
|
516 | def create_settings(self, path): | |
512 |
|
517 | |||
513 | self.create_ui_settings() |
|
518 | self.create_ui_settings() | |
514 |
|
519 | |||
515 | #HG UI OPTIONS |
|
520 | #HG UI OPTIONS | |
516 | web1 = RhodeCodeUi() |
|
521 | web1 = RhodeCodeUi() | |
517 | web1.ui_section = 'web' |
|
522 | web1.ui_section = 'web' | |
518 | web1.ui_key = 'push_ssl' |
|
523 | web1.ui_key = 'push_ssl' | |
519 | web1.ui_value = 'false' |
|
524 | web1.ui_value = 'false' | |
520 |
|
525 | |||
521 | web2 = RhodeCodeUi() |
|
526 | web2 = RhodeCodeUi() | |
522 | web2.ui_section = 'web' |
|
527 | web2.ui_section = 'web' | |
523 | web2.ui_key = 'allow_archive' |
|
528 | web2.ui_key = 'allow_archive' | |
524 | web2.ui_value = 'gz zip bz2' |
|
529 | web2.ui_value = 'gz zip bz2' | |
525 |
|
530 | |||
526 | web3 = RhodeCodeUi() |
|
531 | web3 = RhodeCodeUi() | |
527 | web3.ui_section = 'web' |
|
532 | web3.ui_section = 'web' | |
528 | web3.ui_key = 'allow_push' |
|
533 | web3.ui_key = 'allow_push' | |
529 | web3.ui_value = '*' |
|
534 | web3.ui_value = '*' | |
530 |
|
535 | |||
531 | web4 = RhodeCodeUi() |
|
536 | web4 = RhodeCodeUi() | |
532 | web4.ui_section = 'web' |
|
537 | web4.ui_section = 'web' | |
533 | web4.ui_key = 'baseurl' |
|
538 | web4.ui_key = 'baseurl' | |
534 | web4.ui_value = '/' |
|
539 | web4.ui_value = '/' | |
535 |
|
540 | |||
536 | paths = RhodeCodeUi() |
|
541 | paths = RhodeCodeUi() | |
537 | paths.ui_section = 'paths' |
|
542 | paths.ui_section = 'paths' | |
538 | paths.ui_key = '/' |
|
543 | paths.ui_key = '/' | |
539 | paths.ui_value = path |
|
544 | paths.ui_value = path | |
540 |
|
545 | |||
541 | phases = RhodeCodeUi() |
|
546 | phases = RhodeCodeUi() | |
542 | phases.ui_section = 'phases' |
|
547 | phases.ui_section = 'phases' | |
543 | phases.ui_key = 'publish' |
|
548 | phases.ui_key = 'publish' | |
544 | phases.ui_value = False |
|
549 | phases.ui_value = False | |
545 |
|
550 | |||
546 | sett1 = RhodeCodeSetting('realm', 'RhodeCode authentication') |
|
551 | sett1 = RhodeCodeSetting('realm', 'RhodeCode authentication') | |
547 | sett2 = RhodeCodeSetting('title', 'RhodeCode') |
|
552 | sett2 = RhodeCodeSetting('title', 'RhodeCode') | |
548 | sett3 = RhodeCodeSetting('ga_code', '') |
|
553 | sett3 = RhodeCodeSetting('ga_code', '') | |
549 |
|
554 | |||
550 | sett4 = RhodeCodeSetting('show_public_icon', True) |
|
555 | sett4 = RhodeCodeSetting('show_public_icon', True) | |
551 | sett5 = RhodeCodeSetting('show_private_icon', True) |
|
556 | sett5 = RhodeCodeSetting('show_private_icon', True) | |
552 | sett6 = RhodeCodeSetting('stylify_metatags', False) |
|
557 | sett6 = RhodeCodeSetting('stylify_metatags', False) | |
553 |
|
558 | |||
554 | self.sa.add(web1) |
|
559 | self.sa.add(web1) | |
555 | self.sa.add(web2) |
|
560 | self.sa.add(web2) | |
556 | self.sa.add(web3) |
|
561 | self.sa.add(web3) | |
557 | self.sa.add(web4) |
|
562 | self.sa.add(web4) | |
558 | self.sa.add(paths) |
|
563 | self.sa.add(paths) | |
559 | self.sa.add(sett1) |
|
564 | self.sa.add(sett1) | |
560 | self.sa.add(sett2) |
|
565 | self.sa.add(sett2) | |
561 | self.sa.add(sett3) |
|
566 | self.sa.add(sett3) | |
562 | self.sa.add(sett4) |
|
567 | self.sa.add(sett4) | |
563 | self.sa.add(sett5) |
|
568 | self.sa.add(sett5) | |
564 | self.sa.add(sett6) |
|
569 | self.sa.add(sett6) | |
565 |
|
570 | |||
566 | self.create_ldap_options() |
|
571 | self.create_ldap_options() | |
567 |
|
572 | |||
568 | log.info('created ui config') |
|
573 | log.info('created ui config') | |
569 |
|
574 | |||
570 | def create_user(self, username, password, email='', admin=False): |
|
575 | def create_user(self, username, password, email='', admin=False): | |
571 | log.info('creating user %s' % username) |
|
576 | log.info('creating user %s' % username) | |
572 | UserModel().create_or_update(username, password, email, |
|
577 | UserModel().create_or_update(username, password, email, | |
573 | firstname='RhodeCode', lastname='Admin', |
|
578 | firstname='RhodeCode', lastname='Admin', | |
574 | active=True, admin=admin) |
|
579 | active=True, admin=admin) | |
575 |
|
580 | |||
576 | def create_default_user(self): |
|
581 | def create_default_user(self): | |
577 | log.info('creating default user') |
|
582 | log.info('creating default user') | |
578 | # create default user for handling default permissions. |
|
583 | # create default user for handling default permissions. | |
579 | UserModel().create_or_update(username='default', |
|
584 | UserModel().create_or_update(username='default', | |
580 | password=str(uuid.uuid1())[:8], |
|
585 | password=str(uuid.uuid1())[:8], | |
581 | email='anonymous@rhodecode.org', |
|
586 | email='anonymous@rhodecode.org', | |
582 | firstname='Anonymous', lastname='User') |
|
587 | firstname='Anonymous', lastname='User') | |
583 |
|
588 | |||
584 | def create_permissions(self): |
|
589 | def create_permissions(self): | |
585 | # module.(access|create|change|delete)_[name] |
|
590 | # module.(access|create|change|delete)_[name] | |
586 | # module.(none|read|write|admin) |
|
591 | # module.(none|read|write|admin) | |
587 |
|
592 | |||
588 | for p in Permission.PERMS: |
|
593 | for p in Permission.PERMS: | |
589 | if not Permission.get_by_key(p[0]): |
|
594 | if not Permission.get_by_key(p[0]): | |
590 | new_perm = Permission() |
|
595 | new_perm = Permission() | |
591 | new_perm.permission_name = p[0] |
|
596 | new_perm.permission_name = p[0] | |
592 | new_perm.permission_longname = p[0] |
|
597 | new_perm.permission_longname = p[0] | |
593 | self.sa.add(new_perm) |
|
598 | self.sa.add(new_perm) | |
594 |
|
599 | |||
595 | def populate_default_permissions(self): |
|
600 | def populate_default_permissions(self): | |
596 | log.info('creating default user permissions') |
|
601 | log.info('creating default user permissions') | |
597 |
|
602 | |||
598 | default_user = User.get_by_username('default') |
|
603 | default_user = User.get_by_username('default') | |
599 |
|
604 | |||
600 | for def_perm in ['hg.register.manual_activate', 'hg.create.repository', |
|
605 | for def_perm in ['hg.register.manual_activate', 'hg.create.repository', | |
601 | 'hg.fork.repository', 'repository.read']: |
|
606 | 'hg.fork.repository', 'repository.read']: | |
602 |
|
607 | |||
603 | perm = self.sa.query(Permission)\ |
|
608 | perm = self.sa.query(Permission)\ | |
604 | .filter(Permission.permission_name == def_perm)\ |
|
609 | .filter(Permission.permission_name == def_perm)\ | |
605 | .scalar() |
|
610 | .scalar() | |
606 | if not perm: |
|
611 | if not perm: | |
607 | raise Exception( |
|
612 | raise Exception( | |
608 | 'CRITICAL: permission %s not found inside database !!' |
|
613 | 'CRITICAL: permission %s not found inside database !!' | |
609 | % def_perm |
|
614 | % def_perm | |
610 | ) |
|
615 | ) | |
611 | if not UserToPerm.query()\ |
|
616 | if not UserToPerm.query()\ | |
612 | .filter(UserToPerm.permission == perm)\ |
|
617 | .filter(UserToPerm.permission == perm)\ | |
613 | .filter(UserToPerm.user == default_user).scalar(): |
|
618 | .filter(UserToPerm.user == default_user).scalar(): | |
614 | reg_perm = UserToPerm() |
|
619 | reg_perm = UserToPerm() | |
615 | reg_perm.user = default_user |
|
620 | reg_perm.user = default_user | |
616 | reg_perm.permission = perm |
|
621 | reg_perm.permission = perm | |
617 | self.sa.add(reg_perm) |
|
622 | self.sa.add(reg_perm) |
General Comments 0
You need to be logged in to leave comments.
Login now