Show More
@@ -1,309 +1,311 b'' | |||
|
1 | 1 | #!/usr/bin/env python |
|
2 | 2 | # encoding: utf-8 |
|
3 | 3 | # database management for RhodeCode |
|
4 | 4 | # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com> |
|
5 | 5 | # |
|
6 | 6 | # This program is free software; you can redistribute it and/or |
|
7 | 7 | # modify it under the terms of the GNU General Public License |
|
8 | 8 | # as published by the Free Software Foundation; version 2 |
|
9 | 9 | # of the License or (at your opinion) any later version of the license. |
|
10 | 10 | # |
|
11 | 11 | # This program is distributed in the hope that it will be useful, |
|
12 | 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 | 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 | 14 | # GNU General Public License for more details. |
|
15 | 15 | # |
|
16 | 16 | # You should have received a copy of the GNU General Public License |
|
17 | 17 | # along with this program; if not, write to the Free Software |
|
18 | 18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 | 19 | # MA 02110-1301, USA. |
|
20 | 20 | |
|
21 | 21 | """ |
|
22 | 22 | Created on April 10, 2010 |
|
23 | 23 | database management and creation for RhodeCode |
|
24 | 24 | @author: marcink |
|
25 | 25 | """ |
|
26 | 26 | |
|
27 | 27 | from os.path import dirname as dn, join as jn |
|
28 | 28 | import os |
|
29 | 29 | import sys |
|
30 | 30 | import uuid |
|
31 | 31 | |
|
32 | 32 | from rhodecode.lib.auth import get_crypt_password |
|
33 | 33 | from rhodecode.lib.utils import ask_ok |
|
34 | 34 | from rhodecode.model import init_model |
|
35 | 35 | from rhodecode.model.db import User, Permission, RhodeCodeUi, RhodeCodeSettings, \ |
|
36 | 36 | UserToPerm |
|
37 | 37 | from rhodecode.model import meta |
|
38 | 38 | from sqlalchemy.engine import create_engine |
|
39 | 39 | import logging |
|
40 | 40 | |
|
41 | 41 | log = logging.getLogger(__name__) |
|
42 | 42 | |
|
43 | 43 | class DbManage(object): |
|
44 |
def __init__(self, log_sql, db |
|
|
45 |
self.dbname = db |
|
|
44 | def __init__(self, log_sql, dbconf, root, tests=False): | |
|
45 | self.dbname = dbconf.split('/')[-1] | |
|
46 | 46 | self.tests = tests |
|
47 | 47 | self.root = root |
|
48 | dburi = 'sqlite:////%s' % jn(self.root, self.dbname) | |
|
49 | engine = create_engine(dburi, echo=log_sql) | |
|
48 | self.dburi = dbconf | |
|
49 | engine = create_engine(self.dburi, echo=log_sql) | |
|
50 | 50 | init_model(engine) |
|
51 | 51 | self.sa = meta.Session() |
|
52 | 52 | self.db_exists = False |
|
53 | 53 | |
|
54 | 54 | def check_for_db(self, override): |
|
55 | 55 | db_path = jn(self.root, self.dbname) |
|
56 | if self.dburi.startswith('sqlite'): | |
|
56 | 57 | log.info('checking for existing db in %s', db_path) |
|
57 | 58 | if os.path.isfile(db_path): |
|
59 | ||
|
58 | 60 | self.db_exists = True |
|
59 | 61 | if not override: |
|
60 | 62 | raise Exception('database already exists') |
|
61 | 63 | |
|
62 | 64 | def create_tables(self, override=False): |
|
63 | 65 | """ |
|
64 | 66 | Create a auth database |
|
65 | 67 | """ |
|
66 | 68 | self.check_for_db(override) |
|
67 | 69 | if self.db_exists: |
|
68 | 70 | log.info("database exist and it's going to be destroyed") |
|
69 | 71 | if self.tests: |
|
70 | 72 | destroy = True |
|
71 | 73 | else: |
|
72 | 74 | destroy = ask_ok('Are you sure to destroy old database ? [y/n]') |
|
73 | 75 | if not destroy: |
|
74 | 76 | sys.exit() |
|
75 | 77 | if self.db_exists and destroy: |
|
76 | 78 | os.remove(jn(self.root, self.dbname)) |
|
77 | 79 | checkfirst = not override |
|
78 | 80 | meta.Base.metadata.create_all(checkfirst=checkfirst) |
|
79 | 81 | log.info('Created tables for %s', self.dbname) |
|
80 | 82 | |
|
81 | 83 | def admin_prompt(self, second=False): |
|
82 | 84 | if not self.tests: |
|
83 | 85 | import getpass |
|
84 | 86 | |
|
85 | 87 | |
|
86 | 88 | def get_password(): |
|
87 | 89 | password = getpass.getpass('Specify admin password (min 6 chars):') |
|
88 | 90 | confirm = getpass.getpass('Confirm password:') |
|
89 | 91 | |
|
90 | 92 | if password != confirm: |
|
91 | 93 | log.error('passwords mismatch') |
|
92 | 94 | return False |
|
93 | 95 | if len(password) < 6: |
|
94 | 96 | log.error('password is to short use at least 6 characters') |
|
95 | 97 | return False |
|
96 | 98 | |
|
97 | 99 | return password |
|
98 | 100 | |
|
99 | 101 | username = raw_input('Specify admin username:') |
|
100 | 102 | |
|
101 | 103 | password = get_password() |
|
102 | 104 | if not password: |
|
103 | 105 | #second try |
|
104 | 106 | password = get_password() |
|
105 | 107 | if not password: |
|
106 | 108 | sys.exit() |
|
107 | 109 | |
|
108 | 110 | email = raw_input('Specify admin email:') |
|
109 | 111 | self.create_user(username, password, email, True) |
|
110 | 112 | else: |
|
111 | 113 | log.info('creating admin and regular test users') |
|
112 | 114 | self.create_user('test_admin', 'test12', 'test_admin@mail.com', True) |
|
113 | 115 | self.create_user('test_regular', 'test12', 'test_regular@mail.com', False) |
|
114 | 116 | self.create_user('test_regular2', 'test12', 'test_regular2@mail.com', False) |
|
115 | 117 | |
|
116 | 118 | |
|
117 | 119 | |
|
118 | 120 | def config_prompt(self, test_repo_path=''): |
|
119 | 121 | log.info('Setting up repositories config') |
|
120 | 122 | |
|
121 | 123 | if not self.tests and not test_repo_path: |
|
122 | 124 | path = raw_input('Specify valid full path to your repositories' |
|
123 | 125 | ' you can change this later in application settings:') |
|
124 | 126 | else: |
|
125 | 127 | path = test_repo_path |
|
126 | 128 | |
|
127 | 129 | if not os.path.isdir(path): |
|
128 | 130 | log.error('You entered wrong path: %s', path) |
|
129 | 131 | sys.exit() |
|
130 | 132 | |
|
131 | 133 | hooks1 = RhodeCodeUi() |
|
132 | 134 | hooks1.ui_section = 'hooks' |
|
133 | 135 | hooks1.ui_key = 'changegroup.update' |
|
134 | 136 | hooks1.ui_value = 'hg update >&2' |
|
135 | 137 | hooks1.ui_active = False |
|
136 | 138 | |
|
137 | 139 | hooks2 = RhodeCodeUi() |
|
138 | 140 | hooks2.ui_section = 'hooks' |
|
139 | 141 | hooks2.ui_key = 'changegroup.repo_size' |
|
140 | 142 | hooks2.ui_value = 'python:rhodecode.lib.hooks.repo_size' |
|
141 | 143 | |
|
142 | 144 | hooks3 = RhodeCodeUi() |
|
143 | 145 | hooks3.ui_section = 'hooks' |
|
144 | 146 | hooks3.ui_key = 'pretxnchangegroup.push_logger' |
|
145 | 147 | hooks3.ui_value = 'python:rhodecode.lib.hooks.log_push_action' |
|
146 | 148 | |
|
147 | 149 | hooks4 = RhodeCodeUi() |
|
148 | 150 | hooks4.ui_section = 'hooks' |
|
149 | 151 | hooks4.ui_key = 'preoutgoing.pull_logger' |
|
150 | 152 | hooks4.ui_value = 'python:rhodecode.lib.hooks.log_pull_action' |
|
151 | 153 | |
|
152 | 154 | #for mercurial 1.7 set backward comapatibility with format |
|
153 | 155 | |
|
154 | 156 | dotencode_disable = RhodeCodeUi() |
|
155 | 157 | dotencode_disable.ui_section = 'format' |
|
156 | 158 | dotencode_disable.ui_key = 'dotencode' |
|
157 | 159 | dotencode_disable.ui_section = 'false' |
|
158 | 160 | |
|
159 | 161 | |
|
160 | 162 | web1 = RhodeCodeUi() |
|
161 | 163 | web1.ui_section = 'web' |
|
162 | 164 | web1.ui_key = 'push_ssl' |
|
163 | 165 | web1.ui_value = 'false' |
|
164 | 166 | |
|
165 | 167 | web2 = RhodeCodeUi() |
|
166 | 168 | web2.ui_section = 'web' |
|
167 | 169 | web2.ui_key = 'allow_archive' |
|
168 | 170 | web2.ui_value = 'gz zip bz2' |
|
169 | 171 | |
|
170 | 172 | web3 = RhodeCodeUi() |
|
171 | 173 | web3.ui_section = 'web' |
|
172 | 174 | web3.ui_key = 'allow_push' |
|
173 | 175 | web3.ui_value = '*' |
|
174 | 176 | |
|
175 | 177 | web4 = RhodeCodeUi() |
|
176 | 178 | web4.ui_section = 'web' |
|
177 | 179 | web4.ui_key = 'baseurl' |
|
178 | 180 | web4.ui_value = '/' |
|
179 | 181 | |
|
180 | 182 | paths = RhodeCodeUi() |
|
181 | 183 | paths.ui_section = 'paths' |
|
182 | 184 | paths.ui_key = '/' |
|
183 | 185 | paths.ui_value = path |
|
184 | 186 | |
|
185 | 187 | |
|
186 | 188 | hgsettings1 = RhodeCodeSettings('realm', 'RhodeCode authentication') |
|
187 | 189 | hgsettings2 = RhodeCodeSettings('title', 'RhodeCode') |
|
188 | 190 | |
|
189 | 191 | |
|
190 | 192 | try: |
|
191 | 193 | self.sa.add(hooks1) |
|
192 | 194 | self.sa.add(hooks2) |
|
193 | 195 | self.sa.add(hooks3) |
|
194 | 196 | self.sa.add(hooks4) |
|
195 | 197 | self.sa.add(web1) |
|
196 | 198 | self.sa.add(web2) |
|
197 | 199 | self.sa.add(web3) |
|
198 | 200 | self.sa.add(web4) |
|
199 | 201 | self.sa.add(paths) |
|
200 | 202 | self.sa.add(hgsettings1) |
|
201 | 203 | self.sa.add(hgsettings2) |
|
202 | 204 | self.sa.add(dotencode_disable) |
|
203 | 205 | for k in ['ldap_active', 'ldap_host', 'ldap_port', 'ldap_ldaps', |
|
204 | 206 | 'ldap_dn_user', 'ldap_dn_pass', 'ldap_base_dn']: |
|
205 | 207 | |
|
206 | 208 | setting = RhodeCodeSettings(k, '') |
|
207 | 209 | self.sa.add(setting) |
|
208 | 210 | |
|
209 | 211 | self.sa.commit() |
|
210 | 212 | except: |
|
211 | 213 | self.sa.rollback() |
|
212 | 214 | raise |
|
213 | 215 | log.info('created ui config') |
|
214 | 216 | |
|
215 | 217 | def create_user(self, username, password, email='', admin=False): |
|
216 | 218 | log.info('creating administrator user %s', username) |
|
217 | 219 | new_user = User() |
|
218 | 220 | new_user.username = username |
|
219 | 221 | new_user.password = get_crypt_password(password) |
|
220 | 222 | new_user.name = 'RhodeCode' |
|
221 | 223 | new_user.lastname = 'Admin' |
|
222 | 224 | new_user.email = email |
|
223 | 225 | new_user.admin = admin |
|
224 | 226 | new_user.active = True |
|
225 | 227 | |
|
226 | 228 | try: |
|
227 | 229 | self.sa.add(new_user) |
|
228 | 230 | self.sa.commit() |
|
229 | 231 | except: |
|
230 | 232 | self.sa.rollback() |
|
231 | 233 | raise |
|
232 | 234 | |
|
233 | 235 | def create_default_user(self): |
|
234 | 236 | log.info('creating default user') |
|
235 | 237 | #create default user for handling default permissions. |
|
236 | 238 | def_user = User() |
|
237 | 239 | def_user.username = 'default' |
|
238 | 240 | def_user.password = get_crypt_password(str(uuid.uuid1())[:8]) |
|
239 | 241 | def_user.name = 'Anonymous' |
|
240 | 242 | def_user.lastname = 'User' |
|
241 | 243 | def_user.email = 'anonymous@rhodecode.org' |
|
242 | 244 | def_user.admin = False |
|
243 | 245 | def_user.active = False |
|
244 | 246 | try: |
|
245 | 247 | self.sa.add(def_user) |
|
246 | 248 | self.sa.commit() |
|
247 | 249 | except: |
|
248 | 250 | self.sa.rollback() |
|
249 | 251 | raise |
|
250 | 252 | |
|
251 | 253 | def create_permissions(self): |
|
252 | 254 | #module.(access|create|change|delete)_[name] |
|
253 | 255 | #module.(read|write|owner) |
|
254 | 256 | perms = [('repository.none', 'Repository no access'), |
|
255 | 257 | ('repository.read', 'Repository read access'), |
|
256 | 258 | ('repository.write', 'Repository write access'), |
|
257 | 259 | ('repository.admin', 'Repository admin access'), |
|
258 | 260 | ('hg.admin', 'Hg Administrator'), |
|
259 | 261 | ('hg.create.repository', 'Repository create'), |
|
260 | 262 | ('hg.create.none', 'Repository creation disabled'), |
|
261 | 263 | ('hg.register.none', 'Register disabled'), |
|
262 | 264 | ('hg.register.manual_activate', 'Register new user with rhodecode without manual activation'), |
|
263 | 265 | ('hg.register.auto_activate', 'Register new user with rhodecode without auto activation'), |
|
264 | 266 | ] |
|
265 | 267 | |
|
266 | 268 | for p in perms: |
|
267 | 269 | new_perm = Permission() |
|
268 | 270 | new_perm.permission_name = p[0] |
|
269 | 271 | new_perm.permission_longname = p[1] |
|
270 | 272 | try: |
|
271 | 273 | self.sa.add(new_perm) |
|
272 | 274 | self.sa.commit() |
|
273 | 275 | except: |
|
274 | 276 | self.sa.rollback() |
|
275 | 277 | raise |
|
276 | 278 | |
|
277 | 279 | def populate_default_permissions(self): |
|
278 | 280 | log.info('creating default user permissions') |
|
279 | 281 | |
|
280 | 282 | default_user = self.sa.query(User)\ |
|
281 | 283 | .filter(User.username == 'default').scalar() |
|
282 | 284 | |
|
283 | 285 | reg_perm = UserToPerm() |
|
284 | 286 | reg_perm.user = default_user |
|
285 | 287 | reg_perm.permission = self.sa.query(Permission)\ |
|
286 | 288 | .filter(Permission.permission_name == 'hg.register.manual_activate')\ |
|
287 | 289 | .scalar() |
|
288 | 290 | |
|
289 | 291 | create_repo_perm = UserToPerm() |
|
290 | 292 | create_repo_perm.user = default_user |
|
291 | 293 | create_repo_perm.permission = self.sa.query(Permission)\ |
|
292 | 294 | .filter(Permission.permission_name == 'hg.create.repository')\ |
|
293 | 295 | .scalar() |
|
294 | 296 | |
|
295 | 297 | default_repo_perm = UserToPerm() |
|
296 | 298 | default_repo_perm.user = default_user |
|
297 | 299 | default_repo_perm.permission = self.sa.query(Permission)\ |
|
298 | 300 | .filter(Permission.permission_name == 'repository.read')\ |
|
299 | 301 | .scalar() |
|
300 | 302 | |
|
301 | 303 | try: |
|
302 | 304 | self.sa.add(reg_perm) |
|
303 | 305 | self.sa.add(create_repo_perm) |
|
304 | 306 | self.sa.add(default_repo_perm) |
|
305 | 307 | self.sa.commit() |
|
306 | 308 | except: |
|
307 | 309 | self.sa.rollback() |
|
308 | 310 | raise |
|
309 | 311 |
@@ -1,26 +1,25 b'' | |||
|
1 | 1 | """Setup the rhodecode application""" |
|
2 | 2 | from rhodecode.config.environment import load_environment |
|
3 | 3 | from rhodecode.lib.db_manage import DbManage |
|
4 | 4 | import logging |
|
5 | 5 | import os |
|
6 | 6 | |
|
7 | 7 | log = logging.getLogger(__name__) |
|
8 | 8 | |
|
9 | 9 | def setup_app(command, conf, vars): |
|
10 | 10 | """Place any commands to setup rhodecode here""" |
|
11 |
db |
|
|
12 |
dbmanage = DbManage(log_sql=True, db |
|
|
13 | tests=False) | |
|
11 | dbconf = conf['sqlalchemy.db1.url'] | |
|
12 | dbmanage = DbManage(log_sql=True, dbconf=dbconf, root=conf['here'], tests=False) | |
|
14 | 13 | dbmanage.create_tables(override=True) |
|
15 | 14 | dbmanage.config_prompt(None) |
|
16 | 15 | dbmanage.create_default_user() |
|
17 | 16 | dbmanage.admin_prompt() |
|
18 | 17 | dbmanage.create_permissions() |
|
19 | 18 | dbmanage.populate_default_permissions() |
|
20 | 19 | |
|
21 | 20 | load_environment(conf.global_conf, conf.local_conf, initial=True) |
|
22 | 21 | |
|
23 | 22 | |
|
24 | 23 | |
|
25 | 24 | |
|
26 | 25 |
General Comments 0
You need to be logged in to leave comments.
Login now