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