Show More
@@ -1,382 +1,402 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 |
|
6 | Database creation, and setup module for RhodeCode. Used for creation | |
|
7 | of database as well as for migration operations | |||
7 |
|
8 | |||
8 | :created_on: Apr 10, 2010 |
|
9 | :created_on: Apr 10, 2010 | |
9 | :author: marcink |
|
10 | :author: marcink | |
10 | :copyright: (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com> |
|
11 | :copyright: (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com> | |
11 | :license: GPLv3, see COPYING for more details. |
|
12 | :license: GPLv3, see COPYING for more details. | |
12 | """ |
|
13 | """ | |
13 | # This program is free software; you can redistribute it and/or |
|
14 | # This program is free software; you can redistribute it and/or | |
14 | # modify it under the terms of the GNU General Public License |
|
15 | # modify it under the terms of the GNU General Public License | |
15 | # as published by the Free Software Foundation; version 2 |
|
16 | # as published by the Free Software Foundation; version 2 | |
16 | # of the License or (at your opinion) any later version of the license. |
|
17 | # of the License or (at your opinion) any later version of the license. | |
17 | # |
|
18 | # | |
18 | # 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, | |
19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
20 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
21 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | # GNU General Public License for more details. |
|
22 | # GNU General Public License for more details. | |
22 | # |
|
23 | # | |
23 | # 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 | |
24 | # along with this program; if not, write to the Free Software |
|
25 | # along with this program; if not, write to the Free Software | |
25 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
26 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | |
26 | # MA 02110-1301, USA. |
|
27 | # MA 02110-1301, USA. | |
27 |
|
28 | |||
28 | import os |
|
29 | import os | |
29 | import sys |
|
30 | import sys | |
30 | import uuid |
|
31 | import uuid | |
31 | import logging |
|
32 | import logging | |
32 | from os.path import dirname as dn, join as jn |
|
33 | from os.path import dirname as dn, join as jn | |
33 |
|
34 | |||
34 | from rhodecode import __dbversion__ |
|
35 | from rhodecode import __dbversion__ | |
35 | from rhodecode.model import meta |
|
36 | from rhodecode.model import meta | |
36 |
|
37 | |||
37 | from rhodecode.lib.auth import get_crypt_password |
|
38 | from rhodecode.lib.auth import get_crypt_password | |
38 | from rhodecode.lib.utils import ask_ok |
|
39 | from rhodecode.lib.utils import ask_ok | |
39 | from rhodecode.model import init_model |
|
40 | from rhodecode.model import init_model | |
40 | from rhodecode.model.db import User, Permission, RhodeCodeUi, RhodeCodeSettings, \ |
|
41 | from rhodecode.model.db import User, Permission, RhodeCodeUi, RhodeCodeSettings, \ | |
41 | UserToPerm, DbMigrateVersion |
|
42 | UserToPerm, DbMigrateVersion | |
42 |
|
43 | |||
43 | from sqlalchemy.engine import create_engine |
|
44 | from sqlalchemy.engine import create_engine | |
44 |
|
45 | |||
45 |
|
46 | |||
46 | log = logging.getLogger(__name__) |
|
47 | log = logging.getLogger(__name__) | |
47 |
|
48 | |||
48 | class DbManage(object): |
|
49 | class DbManage(object): | |
49 | def __init__(self, log_sql, dbconf, root, tests=False): |
|
50 | def __init__(self, log_sql, dbconf, root, tests=False): | |
50 | self.dbname = dbconf.split('/')[-1] |
|
51 | self.dbname = dbconf.split('/')[-1] | |
51 | self.tests = tests |
|
52 | self.tests = tests | |
52 | self.root = root |
|
53 | self.root = root | |
53 | self.dburi = dbconf |
|
54 | self.dburi = dbconf | |
54 | engine = create_engine(self.dburi, echo=log_sql) |
|
55 | engine = create_engine(self.dburi, echo=log_sql) | |
55 | init_model(engine) |
|
56 | init_model(engine) | |
56 | self.sa = meta.Session() |
|
57 | self.sa = meta.Session() | |
57 | self.db_exists = False |
|
58 | self.db_exists = False | |
58 |
|
59 | |||
59 | def check_for_db(self, override): |
|
60 | def check_for_db(self, override): | |
60 | db_path = jn(self.root, self.dbname) |
|
61 | db_path = jn(self.root, self.dbname) | |
61 | if self.dburi.startswith('sqlite'): |
|
62 | if self.dburi.startswith('sqlite'): | |
62 | log.info('checking for existing db in %s', db_path) |
|
63 | log.info('checking for existing db in %s', db_path) | |
63 | if os.path.isfile(db_path): |
|
64 | if os.path.isfile(db_path): | |
64 |
|
65 | |||
65 | self.db_exists = True |
|
66 | self.db_exists = True | |
66 | if not override: |
|
67 | if not override: | |
67 | raise Exception('database already exists') |
|
68 | raise Exception('database already exists') | |
68 |
|
69 | |||
69 | def create_tables(self, override=False): |
|
70 | def create_tables(self, override=False): | |
70 | """Create a auth database |
|
71 | """Create a auth database | |
71 | """ |
|
72 | """ | |
72 |
|
73 | |||
73 | self.check_for_db(override) |
|
74 | self.check_for_db(override) | |
74 | if self.db_exists: |
|
75 | if self.db_exists: | |
75 | log.info("database exist and it's going to be destroyed") |
|
76 | log.info("database exist and it's going to be destroyed") | |
76 | if self.tests: |
|
77 | if self.tests: | |
77 | destroy = True |
|
78 | destroy = True | |
78 | else: |
|
79 | else: | |
79 | destroy = ask_ok('Are you sure to destroy old database ? [y/n]') |
|
80 | destroy = ask_ok('Are you sure to destroy old database ? [y/n]') | |
80 | if not destroy: |
|
81 | if not destroy: | |
81 | sys.exit() |
|
82 | sys.exit() | |
82 | if self.db_exists and destroy: |
|
83 | if self.db_exists and destroy: | |
83 | os.remove(jn(self.root, self.dbname)) |
|
84 | os.remove(jn(self.root, self.dbname)) | |
84 | checkfirst = not override |
|
85 | checkfirst = not override | |
85 | meta.Base.metadata.create_all(checkfirst=checkfirst) |
|
86 | meta.Base.metadata.create_all(checkfirst=checkfirst) | |
86 | log.info('Created tables for %s', self.dbname) |
|
87 | log.info('Created tables for %s', self.dbname) | |
87 |
|
88 | |||
88 |
|
89 | |||
89 |
|
90 | |||
90 | def set_db_version(self): |
|
91 | def set_db_version(self): | |
91 | try: |
|
92 | try: | |
92 | ver = DbMigrateVersion() |
|
93 | ver = DbMigrateVersion() | |
93 | ver.version = __dbversion__ |
|
94 | ver.version = __dbversion__ | |
94 | ver.repository_id = 'rhodecode_db_migrations' |
|
95 | ver.repository_id = 'rhodecode_db_migrations' | |
95 | ver.repository_path = 'versions' |
|
96 | ver.repository_path = 'versions' | |
96 | self.sa.add(ver) |
|
97 | self.sa.add(ver) | |
97 | self.sa.commit() |
|
98 | self.sa.commit() | |
98 | except: |
|
99 | except: | |
99 | self.sa.rollback() |
|
100 | self.sa.rollback() | |
100 | raise |
|
101 | raise | |
101 | log.info('db version set to: %s', __dbversion__) |
|
102 | log.info('db version set to: %s', __dbversion__) | |
102 |
|
103 | |||
103 | def fix_repo_paths(self): |
|
104 | def fix_repo_paths(self): | |
104 | """Fixes a old rhodecode version path into new one without a '*' |
|
105 | """Fixes a old rhodecode version path into new one without a '*' | |
105 | """ |
|
106 | """ | |
106 |
|
107 | |||
107 | paths = self.sa.query(RhodeCodeUi)\ |
|
108 | paths = self.sa.query(RhodeCodeUi)\ | |
108 | .filter(RhodeCodeUi.ui_key == '/')\ |
|
109 | .filter(RhodeCodeUi.ui_key == '/')\ | |
109 | .scalar() |
|
110 | .scalar() | |
110 |
|
111 | |||
111 | paths.ui_value = paths.ui_value.replace('*', '') |
|
112 | paths.ui_value = paths.ui_value.replace('*', '') | |
112 |
|
113 | |||
113 | try: |
|
114 | try: | |
114 | self.sa.add(paths) |
|
115 | self.sa.add(paths) | |
115 | self.sa.commit() |
|
116 | self.sa.commit() | |
116 | except: |
|
117 | except: | |
117 | self.sa.rollback() |
|
118 | self.sa.rollback() | |
118 | raise |
|
119 | raise | |
119 |
|
120 | |||
|
121 | def fix_default_user(self): | |||
|
122 | """Fixes a old default user with some 'nicer' default values, | |||
|
123 | used mostly for anonymous access | |||
|
124 | """ | |||
|
125 | def_user = self.sa.query(User)\ | |||
|
126 | .filter(User.username == 'default')\ | |||
|
127 | .one() | |||
|
128 | ||||
|
129 | def_user.name = 'Anonymous' | |||
|
130 | def_user.lastname = 'User' | |||
|
131 | def_user.email = 'anonymous@rhodecode.org' | |||
|
132 | ||||
|
133 | try: | |||
|
134 | self.sa.add(def_user) | |||
|
135 | self.sa.commit() | |||
|
136 | except: | |||
|
137 | self.sa.rollback() | |||
|
138 | raise | |||
|
139 | ||||
120 |
|
140 | |||
121 |
|
141 | |||
122 | def admin_prompt(self, second=False): |
|
142 | def admin_prompt(self, second=False): | |
123 | if not self.tests: |
|
143 | if not self.tests: | |
124 | import getpass |
|
144 | import getpass | |
125 |
|
145 | |||
126 |
|
146 | |||
127 | def get_password(): |
|
147 | def get_password(): | |
128 | password = getpass.getpass('Specify admin password (min 6 chars):') |
|
148 | password = getpass.getpass('Specify admin password (min 6 chars):') | |
129 | confirm = getpass.getpass('Confirm password:') |
|
149 | confirm = getpass.getpass('Confirm password:') | |
130 |
|
150 | |||
131 | if password != confirm: |
|
151 | if password != confirm: | |
132 | log.error('passwords mismatch') |
|
152 | log.error('passwords mismatch') | |
133 | return False |
|
153 | return False | |
134 | if len(password) < 6: |
|
154 | if len(password) < 6: | |
135 | log.error('password is to short use at least 6 characters') |
|
155 | log.error('password is to short use at least 6 characters') | |
136 | return False |
|
156 | return False | |
137 |
|
157 | |||
138 | return password |
|
158 | return password | |
139 |
|
159 | |||
140 | username = raw_input('Specify admin username:') |
|
160 | username = raw_input('Specify admin username:') | |
141 |
|
161 | |||
142 | password = get_password() |
|
162 | password = get_password() | |
143 | if not password: |
|
163 | if not password: | |
144 | #second try |
|
164 | #second try | |
145 | password = get_password() |
|
165 | password = get_password() | |
146 | if not password: |
|
166 | if not password: | |
147 | sys.exit() |
|
167 | sys.exit() | |
148 |
|
168 | |||
149 | email = raw_input('Specify admin email:') |
|
169 | email = raw_input('Specify admin email:') | |
150 | self.create_user(username, password, email, True) |
|
170 | self.create_user(username, password, email, True) | |
151 | else: |
|
171 | else: | |
152 | log.info('creating admin and regular test users') |
|
172 | log.info('creating admin and regular test users') | |
153 | self.create_user('test_admin', 'test12', 'test_admin@mail.com', True) |
|
173 | self.create_user('test_admin', 'test12', 'test_admin@mail.com', True) | |
154 | self.create_user('test_regular', 'test12', 'test_regular@mail.com', False) |
|
174 | self.create_user('test_regular', 'test12', 'test_regular@mail.com', False) | |
155 | self.create_user('test_regular2', 'test12', 'test_regular2@mail.com', False) |
|
175 | self.create_user('test_regular2', 'test12', 'test_regular2@mail.com', False) | |
156 |
|
176 | |||
157 | def create_ui_settings(self): |
|
177 | def create_ui_settings(self): | |
158 | """Creates ui settings, fills out hooks |
|
178 | """Creates ui settings, fills out hooks | |
159 | and disables dotencode |
|
179 | and disables dotencode | |
160 |
|
180 | |||
161 | """ |
|
181 | """ | |
162 | #HOOKS |
|
182 | #HOOKS | |
163 | hooks1_key = 'changegroup.update' |
|
183 | hooks1_key = 'changegroup.update' | |
164 | hooks1_ = self.sa.query(RhodeCodeUi)\ |
|
184 | hooks1_ = self.sa.query(RhodeCodeUi)\ | |
165 | .filter(RhodeCodeUi.ui_key == hooks1_key).scalar() |
|
185 | .filter(RhodeCodeUi.ui_key == hooks1_key).scalar() | |
166 |
|
186 | |||
167 | hooks1 = RhodeCodeUi() if hooks1_ is None else hooks1_ |
|
187 | hooks1 = RhodeCodeUi() if hooks1_ is None else hooks1_ | |
168 | hooks1.ui_section = 'hooks' |
|
188 | hooks1.ui_section = 'hooks' | |
169 | hooks1.ui_key = hooks1_key |
|
189 | hooks1.ui_key = hooks1_key | |
170 | hooks1.ui_value = 'hg update >&2' |
|
190 | hooks1.ui_value = 'hg update >&2' | |
171 | hooks1.ui_active = False |
|
191 | hooks1.ui_active = False | |
172 |
|
192 | |||
173 | hooks2_key = 'changegroup.repo_size' |
|
193 | hooks2_key = 'changegroup.repo_size' | |
174 | hooks2_ = self.sa.query(RhodeCodeUi)\ |
|
194 | hooks2_ = self.sa.query(RhodeCodeUi)\ | |
175 | .filter(RhodeCodeUi.ui_key == hooks2_key).scalar() |
|
195 | .filter(RhodeCodeUi.ui_key == hooks2_key).scalar() | |
176 |
|
196 | |||
177 | hooks2 = RhodeCodeUi() if hooks2_ is None else hooks2_ |
|
197 | hooks2 = RhodeCodeUi() if hooks2_ is None else hooks2_ | |
178 | hooks2.ui_section = 'hooks' |
|
198 | hooks2.ui_section = 'hooks' | |
179 | hooks2.ui_key = hooks2_key |
|
199 | hooks2.ui_key = hooks2_key | |
180 | hooks2.ui_value = 'python:rhodecode.lib.hooks.repo_size' |
|
200 | hooks2.ui_value = 'python:rhodecode.lib.hooks.repo_size' | |
181 |
|
201 | |||
182 | hooks3 = RhodeCodeUi() |
|
202 | hooks3 = RhodeCodeUi() | |
183 | hooks3.ui_section = 'hooks' |
|
203 | hooks3.ui_section = 'hooks' | |
184 | hooks3.ui_key = 'pretxnchangegroup.push_logger' |
|
204 | hooks3.ui_key = 'pretxnchangegroup.push_logger' | |
185 | hooks3.ui_value = 'python:rhodecode.lib.hooks.log_push_action' |
|
205 | hooks3.ui_value = 'python:rhodecode.lib.hooks.log_push_action' | |
186 |
|
206 | |||
187 | hooks4 = RhodeCodeUi() |
|
207 | hooks4 = RhodeCodeUi() | |
188 | hooks4.ui_section = 'hooks' |
|
208 | hooks4.ui_section = 'hooks' | |
189 | hooks4.ui_key = 'preoutgoing.pull_logger' |
|
209 | hooks4.ui_key = 'preoutgoing.pull_logger' | |
190 | hooks4.ui_value = 'python:rhodecode.lib.hooks.log_pull_action' |
|
210 | hooks4.ui_value = 'python:rhodecode.lib.hooks.log_pull_action' | |
191 |
|
211 | |||
192 | #For mercurial 1.7 set backward comapatibility with format |
|
212 | #For mercurial 1.7 set backward comapatibility with format | |
193 | dotencode_disable = RhodeCodeUi() |
|
213 | dotencode_disable = RhodeCodeUi() | |
194 | dotencode_disable.ui_section = 'format' |
|
214 | dotencode_disable.ui_section = 'format' | |
195 | dotencode_disable.ui_key = 'dotencode' |
|
215 | dotencode_disable.ui_key = 'dotencode' | |
196 | dotencode_disable.ui_value = 'false' |
|
216 | dotencode_disable.ui_value = 'false' | |
197 |
|
217 | |||
198 | try: |
|
218 | try: | |
199 | self.sa.add(hooks1) |
|
219 | self.sa.add(hooks1) | |
200 | self.sa.add(hooks2) |
|
220 | self.sa.add(hooks2) | |
201 | self.sa.add(hooks3) |
|
221 | self.sa.add(hooks3) | |
202 | self.sa.add(hooks4) |
|
222 | self.sa.add(hooks4) | |
203 | self.sa.add(dotencode_disable) |
|
223 | self.sa.add(dotencode_disable) | |
204 | self.sa.commit() |
|
224 | self.sa.commit() | |
205 | except: |
|
225 | except: | |
206 | self.sa.rollback() |
|
226 | self.sa.rollback() | |
207 | raise |
|
227 | raise | |
208 |
|
228 | |||
209 |
|
229 | |||
210 | def create_ldap_options(self): |
|
230 | def create_ldap_options(self): | |
211 | """Creates ldap settings""" |
|
231 | """Creates ldap settings""" | |
212 |
|
232 | |||
213 | try: |
|
233 | try: | |
214 | for k in ['ldap_active', 'ldap_host', 'ldap_port', 'ldap_ldaps', |
|
234 | for k in ['ldap_active', 'ldap_host', 'ldap_port', 'ldap_ldaps', | |
215 | 'ldap_dn_user', 'ldap_dn_pass', 'ldap_base_dn']: |
|
235 | 'ldap_dn_user', 'ldap_dn_pass', 'ldap_base_dn']: | |
216 |
|
236 | |||
217 | setting = RhodeCodeSettings(k, '') |
|
237 | setting = RhodeCodeSettings(k, '') | |
218 | self.sa.add(setting) |
|
238 | self.sa.add(setting) | |
219 | self.sa.commit() |
|
239 | self.sa.commit() | |
220 | except: |
|
240 | except: | |
221 | self.sa.rollback() |
|
241 | self.sa.rollback() | |
222 | raise |
|
242 | raise | |
223 |
|
243 | |||
224 | def config_prompt(self, test_repo_path=''): |
|
244 | def config_prompt(self, test_repo_path=''): | |
225 | log.info('Setting up repositories config') |
|
245 | log.info('Setting up repositories config') | |
226 |
|
246 | |||
227 | if not self.tests and not test_repo_path: |
|
247 | if not self.tests and not test_repo_path: | |
228 | path = raw_input('Specify valid full path to your repositories' |
|
248 | path = raw_input('Specify valid full path to your repositories' | |
229 | ' you can change this later in application settings:') |
|
249 | ' you can change this later in application settings:') | |
230 | else: |
|
250 | else: | |
231 | path = test_repo_path |
|
251 | path = test_repo_path | |
232 |
|
252 | |||
233 | if not os.path.isdir(path): |
|
253 | if not os.path.isdir(path): | |
234 | log.error('You entered wrong path: %s', path) |
|
254 | log.error('You entered wrong path: %s', path) | |
235 | sys.exit() |
|
255 | sys.exit() | |
236 |
|
256 | |||
237 | self.create_ui_settings() |
|
257 | self.create_ui_settings() | |
238 |
|
258 | |||
239 | #HG UI OPTIONS |
|
259 | #HG UI OPTIONS | |
240 | web1 = RhodeCodeUi() |
|
260 | web1 = RhodeCodeUi() | |
241 | web1.ui_section = 'web' |
|
261 | web1.ui_section = 'web' | |
242 | web1.ui_key = 'push_ssl' |
|
262 | web1.ui_key = 'push_ssl' | |
243 | web1.ui_value = 'false' |
|
263 | web1.ui_value = 'false' | |
244 |
|
264 | |||
245 | web2 = RhodeCodeUi() |
|
265 | web2 = RhodeCodeUi() | |
246 | web2.ui_section = 'web' |
|
266 | web2.ui_section = 'web' | |
247 | web2.ui_key = 'allow_archive' |
|
267 | web2.ui_key = 'allow_archive' | |
248 | web2.ui_value = 'gz zip bz2' |
|
268 | web2.ui_value = 'gz zip bz2' | |
249 |
|
269 | |||
250 | web3 = RhodeCodeUi() |
|
270 | web3 = RhodeCodeUi() | |
251 | web3.ui_section = 'web' |
|
271 | web3.ui_section = 'web' | |
252 | web3.ui_key = 'allow_push' |
|
272 | web3.ui_key = 'allow_push' | |
253 | web3.ui_value = '*' |
|
273 | web3.ui_value = '*' | |
254 |
|
274 | |||
255 | web4 = RhodeCodeUi() |
|
275 | web4 = RhodeCodeUi() | |
256 | web4.ui_section = 'web' |
|
276 | web4.ui_section = 'web' | |
257 | web4.ui_key = 'baseurl' |
|
277 | web4.ui_key = 'baseurl' | |
258 | web4.ui_value = '/' |
|
278 | web4.ui_value = '/' | |
259 |
|
279 | |||
260 | paths = RhodeCodeUi() |
|
280 | paths = RhodeCodeUi() | |
261 | paths.ui_section = 'paths' |
|
281 | paths.ui_section = 'paths' | |
262 | paths.ui_key = '/' |
|
282 | paths.ui_key = '/' | |
263 | paths.ui_value = path |
|
283 | paths.ui_value = path | |
264 |
|
284 | |||
265 |
|
285 | |||
266 | hgsettings1 = RhodeCodeSettings('realm', 'RhodeCode authentication') |
|
286 | hgsettings1 = RhodeCodeSettings('realm', 'RhodeCode authentication') | |
267 | hgsettings2 = RhodeCodeSettings('title', 'RhodeCode') |
|
287 | hgsettings2 = RhodeCodeSettings('title', 'RhodeCode') | |
268 |
|
288 | |||
269 |
|
289 | |||
270 | try: |
|
290 | try: | |
271 | self.sa.add(web1) |
|
291 | self.sa.add(web1) | |
272 | self.sa.add(web2) |
|
292 | self.sa.add(web2) | |
273 | self.sa.add(web3) |
|
293 | self.sa.add(web3) | |
274 | self.sa.add(web4) |
|
294 | self.sa.add(web4) | |
275 | self.sa.add(paths) |
|
295 | self.sa.add(paths) | |
276 | self.sa.add(hgsettings1) |
|
296 | self.sa.add(hgsettings1) | |
277 | self.sa.add(hgsettings2) |
|
297 | self.sa.add(hgsettings2) | |
278 |
|
298 | |||
279 | self.sa.commit() |
|
299 | self.sa.commit() | |
280 | except: |
|
300 | except: | |
281 | self.sa.rollback() |
|
301 | self.sa.rollback() | |
282 | raise |
|
302 | raise | |
283 |
|
303 | |||
284 | self.create_ldap_options() |
|
304 | self.create_ldap_options() | |
285 |
|
305 | |||
286 | log.info('created ui config') |
|
306 | log.info('created ui config') | |
287 |
|
307 | |||
288 | def create_user(self, username, password, email='', admin=False): |
|
308 | def create_user(self, username, password, email='', admin=False): | |
289 | log.info('creating administrator user %s', username) |
|
309 | log.info('creating administrator user %s', username) | |
290 | new_user = User() |
|
310 | new_user = User() | |
291 | new_user.username = username |
|
311 | new_user.username = username | |
292 | new_user.password = get_crypt_password(password) |
|
312 | new_user.password = get_crypt_password(password) | |
293 | new_user.name = 'RhodeCode' |
|
313 | new_user.name = 'RhodeCode' | |
294 | new_user.lastname = 'Admin' |
|
314 | new_user.lastname = 'Admin' | |
295 | new_user.email = email |
|
315 | new_user.email = email | |
296 | new_user.admin = admin |
|
316 | new_user.admin = admin | |
297 | new_user.active = True |
|
317 | new_user.active = True | |
298 |
|
318 | |||
299 | try: |
|
319 | try: | |
300 | self.sa.add(new_user) |
|
320 | self.sa.add(new_user) | |
301 | self.sa.commit() |
|
321 | self.sa.commit() | |
302 | except: |
|
322 | except: | |
303 | self.sa.rollback() |
|
323 | self.sa.rollback() | |
304 | raise |
|
324 | raise | |
305 |
|
325 | |||
306 | def create_default_user(self): |
|
326 | def create_default_user(self): | |
307 | log.info('creating default user') |
|
327 | log.info('creating default user') | |
308 | #create default user for handling default permissions. |
|
328 | #create default user for handling default permissions. | |
309 | def_user = User() |
|
329 | def_user = User() | |
310 | def_user.username = 'default' |
|
330 | def_user.username = 'default' | |
311 | def_user.password = get_crypt_password(str(uuid.uuid1())[:8]) |
|
331 | def_user.password = get_crypt_password(str(uuid.uuid1())[:8]) | |
312 | def_user.name = 'Anonymous' |
|
332 | def_user.name = 'Anonymous' | |
313 | def_user.lastname = 'User' |
|
333 | def_user.lastname = 'User' | |
314 | def_user.email = 'anonymous@rhodecode.org' |
|
334 | def_user.email = 'anonymous@rhodecode.org' | |
315 | def_user.admin = False |
|
335 | def_user.admin = False | |
316 | def_user.active = False |
|
336 | def_user.active = False | |
317 | try: |
|
337 | try: | |
318 | self.sa.add(def_user) |
|
338 | self.sa.add(def_user) | |
319 | self.sa.commit() |
|
339 | self.sa.commit() | |
320 | except: |
|
340 | except: | |
321 | self.sa.rollback() |
|
341 | self.sa.rollback() | |
322 | raise |
|
342 | raise | |
323 |
|
343 | |||
324 | def create_permissions(self): |
|
344 | def create_permissions(self): | |
325 | #module.(access|create|change|delete)_[name] |
|
345 | #module.(access|create|change|delete)_[name] | |
326 | #module.(read|write|owner) |
|
346 | #module.(read|write|owner) | |
327 | perms = [('repository.none', 'Repository no access'), |
|
347 | perms = [('repository.none', 'Repository no access'), | |
328 | ('repository.read', 'Repository read access'), |
|
348 | ('repository.read', 'Repository read access'), | |
329 | ('repository.write', 'Repository write access'), |
|
349 | ('repository.write', 'Repository write access'), | |
330 | ('repository.admin', 'Repository admin access'), |
|
350 | ('repository.admin', 'Repository admin access'), | |
331 | ('hg.admin', 'Hg Administrator'), |
|
351 | ('hg.admin', 'Hg Administrator'), | |
332 | ('hg.create.repository', 'Repository create'), |
|
352 | ('hg.create.repository', 'Repository create'), | |
333 | ('hg.create.none', 'Repository creation disabled'), |
|
353 | ('hg.create.none', 'Repository creation disabled'), | |
334 | ('hg.register.none', 'Register disabled'), |
|
354 | ('hg.register.none', 'Register disabled'), | |
335 | ('hg.register.manual_activate', 'Register new user with rhodecode without manual activation'), |
|
355 | ('hg.register.manual_activate', 'Register new user with rhodecode without manual activation'), | |
336 | ('hg.register.auto_activate', 'Register new user with rhodecode without auto activation'), |
|
356 | ('hg.register.auto_activate', 'Register new user with rhodecode without auto activation'), | |
337 | ] |
|
357 | ] | |
338 |
|
358 | |||
339 | for p in perms: |
|
359 | for p in perms: | |
340 | new_perm = Permission() |
|
360 | new_perm = Permission() | |
341 | new_perm.permission_name = p[0] |
|
361 | new_perm.permission_name = p[0] | |
342 | new_perm.permission_longname = p[1] |
|
362 | new_perm.permission_longname = p[1] | |
343 | try: |
|
363 | try: | |
344 | self.sa.add(new_perm) |
|
364 | self.sa.add(new_perm) | |
345 | self.sa.commit() |
|
365 | self.sa.commit() | |
346 | except: |
|
366 | except: | |
347 | self.sa.rollback() |
|
367 | self.sa.rollback() | |
348 | raise |
|
368 | raise | |
349 |
|
369 | |||
350 | def populate_default_permissions(self): |
|
370 | def populate_default_permissions(self): | |
351 | log.info('creating default user permissions') |
|
371 | log.info('creating default user permissions') | |
352 |
|
372 | |||
353 | default_user = self.sa.query(User)\ |
|
373 | default_user = self.sa.query(User)\ | |
354 | .filter(User.username == 'default').scalar() |
|
374 | .filter(User.username == 'default').scalar() | |
355 |
|
375 | |||
356 | reg_perm = UserToPerm() |
|
376 | reg_perm = UserToPerm() | |
357 | reg_perm.user = default_user |
|
377 | reg_perm.user = default_user | |
358 | reg_perm.permission = self.sa.query(Permission)\ |
|
378 | reg_perm.permission = self.sa.query(Permission)\ | |
359 | .filter(Permission.permission_name == 'hg.register.manual_activate')\ |
|
379 | .filter(Permission.permission_name == 'hg.register.manual_activate')\ | |
360 | .scalar() |
|
380 | .scalar() | |
361 |
|
381 | |||
362 | create_repo_perm = UserToPerm() |
|
382 | create_repo_perm = UserToPerm() | |
363 | create_repo_perm.user = default_user |
|
383 | create_repo_perm.user = default_user | |
364 | create_repo_perm.permission = self.sa.query(Permission)\ |
|
384 | create_repo_perm.permission = self.sa.query(Permission)\ | |
365 | .filter(Permission.permission_name == 'hg.create.repository')\ |
|
385 | .filter(Permission.permission_name == 'hg.create.repository')\ | |
366 | .scalar() |
|
386 | .scalar() | |
367 |
|
387 | |||
368 | default_repo_perm = UserToPerm() |
|
388 | default_repo_perm = UserToPerm() | |
369 | default_repo_perm.user = default_user |
|
389 | default_repo_perm.user = default_user | |
370 | default_repo_perm.permission = self.sa.query(Permission)\ |
|
390 | default_repo_perm.permission = self.sa.query(Permission)\ | |
371 | .filter(Permission.permission_name == 'repository.read')\ |
|
391 | .filter(Permission.permission_name == 'repository.read')\ | |
372 | .scalar() |
|
392 | .scalar() | |
373 |
|
393 | |||
374 | try: |
|
394 | try: | |
375 | self.sa.add(reg_perm) |
|
395 | self.sa.add(reg_perm) | |
376 | self.sa.add(create_repo_perm) |
|
396 | self.sa.add(create_repo_perm) | |
377 | self.sa.add(default_repo_perm) |
|
397 | self.sa.add(default_repo_perm) | |
378 | self.sa.commit() |
|
398 | self.sa.commit() | |
379 | except: |
|
399 | except: | |
380 | self.sa.rollback() |
|
400 | self.sa.rollback() | |
381 | raise |
|
401 | raise | |
382 |
|
402 |
@@ -1,101 +1,104 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ |
|
2 | """ | |
3 | rhodecode.lib.dbmigrate.__init__ |
|
3 | rhodecode.lib.dbmigrate.__init__ | |
4 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
4 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
5 |
|
5 | |||
6 | Database migration modules |
|
6 | Database migration modules | |
7 |
|
7 | |||
8 | :created_on: Dec 11, 2010 |
|
8 | :created_on: Dec 11, 2010 | |
9 | :author: marcink |
|
9 | :author: marcink | |
10 | :copyright: (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com> |
|
10 | :copyright: (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com> | |
11 | :license: GPLv3, see COPYING for more details. |
|
11 | :license: GPLv3, see COPYING for more details. | |
12 | """ |
|
12 | """ | |
13 | # This program is free software; you can redistribute it and/or |
|
13 | # This program is free software; you can redistribute it and/or | |
14 | # modify it under the terms of the GNU General Public License |
|
14 | # modify it under the terms of the GNU General Public License | |
15 | # as published by the Free Software Foundation; version 2 |
|
15 | # as published by the Free Software Foundation; version 2 | |
16 | # of the License or (at your opinion) any later version of the license. |
|
16 | # of the License or (at your opinion) any later version of the license. | |
17 | # |
|
17 | # | |
18 | # This program is distributed in the hope that it will be useful, |
|
18 | # This program is distributed in the hope that it will be useful, | |
19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | # GNU General Public License for more details. |
|
21 | # GNU General Public License for more details. | |
22 | # |
|
22 | # | |
23 | # You should have received a copy of the GNU General Public License |
|
23 | # You should have received a copy of the GNU General Public License | |
24 | # along with this program; if not, write to the Free Software |
|
24 | # along with this program; if not, write to the Free Software | |
25 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
25 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | |
26 | # MA 02110-1301, USA. |
|
26 | # MA 02110-1301, USA. | |
27 |
|
27 | |||
28 | import logging |
|
28 | import logging | |
29 | from sqlalchemy import engine_from_config |
|
29 | from sqlalchemy import engine_from_config | |
30 |
|
30 | |||
31 | from rhodecode import __dbversion__ |
|
31 | from rhodecode import __dbversion__ | |
32 | from rhodecode.lib.dbmigrate.migrate.versioning import api |
|
32 | from rhodecode.lib.dbmigrate.migrate.versioning import api | |
33 | from rhodecode.lib.dbmigrate.migrate.exceptions import \ |
|
33 | from rhodecode.lib.dbmigrate.migrate.exceptions import \ | |
34 | DatabaseNotControlledError |
|
34 | DatabaseNotControlledError | |
35 | from rhodecode.lib.utils import BasePasterCommand, Command, add_cache |
|
35 | from rhodecode.lib.utils import BasePasterCommand, Command, add_cache | |
36 | from rhodecode.lib.db_manage import DbManage |
|
36 | from rhodecode.lib.db_manage import DbManage | |
37 |
|
37 | |||
38 | log = logging.getLogger(__name__) |
|
38 | log = logging.getLogger(__name__) | |
39 |
|
39 | |||
40 | class UpgradeDb(BasePasterCommand): |
|
40 | class UpgradeDb(BasePasterCommand): | |
41 | """Command used for paster to upgrade our database to newer version |
|
41 | """Command used for paster to upgrade our database to newer version | |
42 | """ |
|
42 | """ | |
43 |
|
43 | |||
44 | max_args = 1 |
|
44 | max_args = 1 | |
45 | min_args = 1 |
|
45 | min_args = 1 | |
46 |
|
46 | |||
47 | usage = "CONFIG_FILE" |
|
47 | usage = "CONFIG_FILE" | |
48 | summary = "Upgrades current db to newer version given configuration file" |
|
48 | summary = "Upgrades current db to newer version given configuration file" | |
49 | group_name = "RhodeCode" |
|
49 | group_name = "RhodeCode" | |
50 |
|
50 | |||
51 | parser = Command.standard_parser(verbose=True) |
|
51 | parser = Command.standard_parser(verbose=True) | |
52 |
|
52 | |||
53 | def command(self): |
|
53 | def command(self): | |
54 | from pylons import config |
|
54 | from pylons import config | |
55 |
|
55 | |||
56 | add_cache(config) |
|
56 | add_cache(config) | |
57 |
|
57 | |||
58 |
|
58 | |||
59 |
|
59 | |||
60 | repository_path = 'rhodecode/lib/dbmigrate' |
|
60 | repository_path = 'rhodecode/lib/dbmigrate' | |
61 | db_uri = config['sqlalchemy.db1.url'] |
|
61 | db_uri = config['sqlalchemy.db1.url'] | |
62 |
|
62 | |||
63 | try: |
|
63 | try: | |
64 | curr_version = api.db_version(db_uri, repository_path) |
|
64 | curr_version = api.db_version(db_uri, repository_path) | |
65 | msg = ('Found current database under version' |
|
65 | msg = ('Found current database under version' | |
66 | ' control with version %s' % curr_version) |
|
66 | ' control with version %s' % curr_version) | |
67 |
|
67 | |||
68 | except (RuntimeError, DatabaseNotControlledError), e: |
|
68 | except (RuntimeError, DatabaseNotControlledError), e: | |
69 | curr_version = 1 |
|
69 | curr_version = 1 | |
70 | msg = ('Current database is not under version control setting' |
|
70 | msg = ('Current database is not under version control setting' | |
71 | ' as version %s' % curr_version) |
|
71 | ' as version %s' % curr_version) | |
72 | api.version_control(db_uri, repository_path, curr_version) |
|
72 | api.version_control(db_uri, repository_path, curr_version) | |
73 |
|
73 | |||
74 | self.notify_msg(msg) |
|
74 | self.notify_msg(msg) | |
75 |
|
75 | |||
76 | #now we have our dbversion we can do upgrade |
|
76 | #now we have our dbversion we can do upgrade | |
77 | self.notify_msg('attempting to do database upgrade to version %s' \ |
|
77 | self.notify_msg('attempting to do database upgrade to version %s' \ | |
78 | % __dbversion__) |
|
78 | % __dbversion__) | |
79 |
|
79 | |||
80 | api.upgrade(db_uri, repository_path, __dbversion__) |
|
80 | api.upgrade(db_uri, repository_path, __dbversion__) | |
81 | self.notify_msg('Schema upgrade completed') |
|
81 | self.notify_msg('Schema upgrade completed') | |
82 |
|
82 | |||
83 | #we need to make now some extra operations into database |
|
83 | #we need to make now some extra operations into database | |
84 |
self.notify_msg('Pro |
|
84 | self.notify_msg('Propagating database updates') | |
85 |
|
85 | |||
86 | dbmanage = DbManage(log_sql=True, dbconf=db_uri, |
|
86 | dbmanage = DbManage(log_sql=True, dbconf=db_uri, | |
87 | root=config['here'], tests=False) |
|
87 | root=config['here'], tests=False) | |
88 |
|
88 | |||
89 |
self.notify_msg('Patching repo paths for newer version of |
|
89 | self.notify_msg('Patching repo paths for newer version of RhodeCode') | |
90 | dbmanage.fix_repo_paths() |
|
90 | dbmanage.fix_repo_paths() | |
91 |
|
91 | |||
|
92 | self.notify_msg('Patching default user of RhodeCode') | |||
|
93 | dbmanage.fix_default_user() | |||
|
94 | ||||
92 | self.notify_msg('Changing ui settings') |
|
95 | self.notify_msg('Changing ui settings') | |
93 | dbmanage.create_ui_settings() |
|
96 | dbmanage.create_ui_settings() | |
94 |
|
97 | |||
95 |
|
98 | |||
96 | def update_parser(self): |
|
99 | def update_parser(self): | |
97 | self.parser.add_option('--sql', |
|
100 | self.parser.add_option('--sql', | |
98 | action='store_true', |
|
101 | action='store_true', | |
99 | dest='just_sql', |
|
102 | dest='just_sql', | |
100 | help="Prints upgrade sql for further investigation", |
|
103 | help="Prints upgrade sql for further investigation", | |
101 | default=False) |
|
104 | default=False) |
General Comments 0
You need to be logged in to leave comments.
Login now