##// END OF EJS Templates
installed new hooks for #48...
marcink -
r657:212f97c2 beta
parent child Browse files
Show More
@@ -1,287 +1,300
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 44 def __init__(self, log_sql, dbname, root, tests=False):
45 45 self.dbname = dbname
46 46 self.tests = tests
47 47 self.root = root
48 48 dburi = 'sqlite:////%s' % jn(self.root, self.dbname)
49 49 engine = create_engine(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 56 log.info('checking for existing db in %s', db_path)
57 57 if os.path.isfile(db_path):
58 58 self.db_exists = True
59 59 if not override:
60 60 raise Exception('database already exists')
61 61
62 62 def create_tables(self, override=False):
63 63 """
64 64 Create a auth database
65 65 """
66 66 self.check_for_db(override)
67 67 if self.db_exists:
68 68 log.info("database exist and it's going to be destroyed")
69 69 if self.tests:
70 70 destroy = True
71 71 else:
72 72 destroy = ask_ok('Are you sure to destroy old database ? [y/n]')
73 73 if not destroy:
74 74 sys.exit()
75 75 if self.db_exists and destroy:
76 76 os.remove(jn(self.root, self.dbname))
77 77 checkfirst = not override
78 78 meta.Base.metadata.create_all(checkfirst=checkfirst)
79 79 log.info('Created tables for %s', self.dbname)
80 80
81 81 def admin_prompt(self, second=False):
82 82 if not self.tests:
83 83 import getpass
84 84
85 85
86 86 def get_password():
87 87 password = getpass.getpass('Specify admin password (min 6 chars):')
88 88 confirm = getpass.getpass('Confirm password:')
89 89
90 90 if password != confirm:
91 91 log.error('passwords mismatch')
92 92 return False
93 93 if len(password) < 6:
94 94 log.error('password is to short use at least 6 characters')
95 95 return False
96 96
97 97 return password
98 98
99 99 username = raw_input('Specify admin username:')
100 100
101 101 password = get_password()
102 102 if not password:
103 103 #second try
104 104 password = get_password()
105 105 if not password:
106 106 sys.exit()
107 107
108 108 email = raw_input('Specify admin email:')
109 109 self.create_user(username, password, email, True)
110 110 else:
111 111 log.info('creating admin and regular test users')
112 112 self.create_user('test_admin', 'test12', 'test_admin@mail.com', True)
113 113 self.create_user('test_regular', 'test12', 'test_regular@mail.com', False)
114 114 self.create_user('test_regular2', 'test12', 'test_regular2@mail.com', False)
115 115
116 116
117 117
118 118 def config_prompt(self, test_repo_path=''):
119 119 log.info('Setting up repositories config')
120 120
121 121 if not self.tests and not test_repo_path:
122 122 path = raw_input('Specify valid full path to your repositories'
123 123 ' you can change this later in application settings:')
124 124 else:
125 125 path = test_repo_path
126 126
127 127 if not os.path.isdir(path):
128 128 log.error('You entered wrong path: %s', path)
129 129 sys.exit()
130 130
131 131 hooks1 = RhodeCodeUi()
132 132 hooks1.ui_section = 'hooks'
133 133 hooks1.ui_key = 'changegroup.update'
134 134 hooks1.ui_value = 'hg update >&2'
135 135 hooks1.ui_active = False
136 136
137 137 hooks2 = RhodeCodeUi()
138 138 hooks2.ui_section = 'hooks'
139 139 hooks2.ui_key = 'changegroup.repo_size'
140 140 hooks2.ui_value = 'python:rhodecode.lib.hooks.repo_size'
141
142 hooks3 = RhodeCodeUi()
143 hooks3.ui_section = 'hooks'
144 hooks3.ui_key = 'pretxnchangegroup.push_logger'
145 hooks3.ui_value = 'python:rhodecode.lib.hooks.log_push_action'
146
147 hooks4 = RhodeCodeUi()
148 hooks4.ui_section = 'hooks'
149 hooks4.ui_key = 'preoutgoing.pull_logger'
150 hooks4.ui_value = 'python:rhodecode.lib.hooks.log_pull_action'
151
141 152
142 153 web1 = RhodeCodeUi()
143 154 web1.ui_section = 'web'
144 155 web1.ui_key = 'push_ssl'
145 156 web1.ui_value = 'false'
146 157
147 158 web2 = RhodeCodeUi()
148 159 web2.ui_section = 'web'
149 160 web2.ui_key = 'allow_archive'
150 161 web2.ui_value = 'gz zip bz2'
151 162
152 163 web3 = RhodeCodeUi()
153 164 web3.ui_section = 'web'
154 165 web3.ui_key = 'allow_push'
155 166 web3.ui_value = '*'
156 167
157 168 web4 = RhodeCodeUi()
158 169 web4.ui_section = 'web'
159 170 web4.ui_key = 'baseurl'
160 171 web4.ui_value = '/'
161 172
162 173 paths = RhodeCodeUi()
163 174 paths.ui_section = 'paths'
164 175 paths.ui_key = '/'
165 176 paths.ui_value = path
166 177
167 178
168 179 hgsettings1 = RhodeCodeSettings()
169 180
170 181 hgsettings1.app_settings_name = 'realm'
171 182 hgsettings1.app_settings_value = 'RhodeCode authentication'
172 183
173 184 hgsettings2 = RhodeCodeSettings()
174 185 hgsettings2.app_settings_name = 'title'
175 186 hgsettings2.app_settings_value = 'RhodeCode'
176 187
177 188 try:
178 189 self.sa.add(hooks1)
179 190 self.sa.add(hooks2)
191 self.sa.add(hooks3)
192 self.sa.add(hooks4)
180 193 self.sa.add(web1)
181 194 self.sa.add(web2)
182 195 self.sa.add(web3)
183 196 self.sa.add(web4)
184 197 self.sa.add(paths)
185 198 self.sa.add(hgsettings1)
186 199 self.sa.add(hgsettings2)
187 200 self.sa.commit()
188 201 except:
189 202 self.sa.rollback()
190 203 raise
191 204 log.info('created ui config')
192 205
193 206 def create_user(self, username, password, email='', admin=False):
194 207 log.info('creating administrator user %s', username)
195 208 new_user = User()
196 209 new_user.username = username
197 210 new_user.password = get_crypt_password(password)
198 211 new_user.name = 'RhodeCode'
199 212 new_user.lastname = 'Admin'
200 213 new_user.email = email
201 214 new_user.admin = admin
202 215 new_user.active = True
203 216
204 217 try:
205 218 self.sa.add(new_user)
206 219 self.sa.commit()
207 220 except:
208 221 self.sa.rollback()
209 222 raise
210 223
211 224 def create_default_user(self):
212 225 log.info('creating default user')
213 226 #create default user for handling default permissions.
214 227 def_user = User()
215 228 def_user.username = 'default'
216 229 def_user.password = get_crypt_password(str(uuid.uuid1())[:8])
217 230 def_user.name = 'default'
218 231 def_user.lastname = 'default'
219 232 def_user.email = 'default@default.com'
220 233 def_user.admin = False
221 234 def_user.active = False
222 235 try:
223 236 self.sa.add(def_user)
224 237 self.sa.commit()
225 238 except:
226 239 self.sa.rollback()
227 240 raise
228 241
229 242 def create_permissions(self):
230 243 #module.(access|create|change|delete)_[name]
231 244 #module.(read|write|owner)
232 245 perms = [('repository.none', 'Repository no access'),
233 246 ('repository.read', 'Repository read access'),
234 247 ('repository.write', 'Repository write access'),
235 248 ('repository.admin', 'Repository admin access'),
236 249 ('hg.admin', 'Hg Administrator'),
237 250 ('hg.create.repository', 'Repository create'),
238 251 ('hg.create.none', 'Repository creation disabled'),
239 252 ('hg.register.none', 'Register disabled'),
240 253 ('hg.register.manual_activate', 'Register new user with rhodecode without manual activation'),
241 254 ('hg.register.auto_activate', 'Register new user with rhodecode without auto activation'),
242 255 ]
243 256
244 257 for p in perms:
245 258 new_perm = Permission()
246 259 new_perm.permission_name = p[0]
247 260 new_perm.permission_longname = p[1]
248 261 try:
249 262 self.sa.add(new_perm)
250 263 self.sa.commit()
251 264 except:
252 265 self.sa.rollback()
253 266 raise
254 267
255 268 def populate_default_permissions(self):
256 269 log.info('creating default user permissions')
257 270
258 271 default_user = self.sa.query(User)\
259 272 .filter(User.username == 'default').scalar()
260 273
261 274 reg_perm = UserToPerm()
262 275 reg_perm.user = default_user
263 276 reg_perm.permission = self.sa.query(Permission)\
264 277 .filter(Permission.permission_name == 'hg.register.manual_activate')\
265 278 .scalar()
266 279
267 280 create_repo_perm = UserToPerm()
268 281 create_repo_perm.user = default_user
269 282 create_repo_perm.permission = self.sa.query(Permission)\
270 283 .filter(Permission.permission_name == 'hg.create.repository')\
271 284 .scalar()
272 285
273 286 default_repo_perm = UserToPerm()
274 287 default_repo_perm.user = default_user
275 288 default_repo_perm.permission = self.sa.query(Permission)\
276 289 .filter(Permission.permission_name == 'repository.read')\
277 290 .scalar()
278 291
279 292 try:
280 293 self.sa.add(reg_perm)
281 294 self.sa.add(create_repo_perm)
282 295 self.sa.add(default_repo_perm)
283 296 self.sa.commit()
284 297 except:
285 298 self.sa.rollback()
286 299 raise
287 300
@@ -1,140 +1,136
1 from rhodecode.model.meta import Base
2 from sqlalchemy import *
3 from sqlalchemy.orm import relation, backref
4 from sqlalchemy.orm.session import Session
5 from vcs.utils.lazy import LazyProperty
6 import logging
1 from rhodecode.model.meta import Base from sqlalchemy import * from
2 sqlalchemy.orm import relation, backref from sqlalchemy.orm.session import
3 Session from vcs.utils.lazy import LazyProperty import logging
7 4
8 5 log = logging.getLogger(__name__)
9 6
10 7 class RhodeCodeSettings(Base):
11 8 __tablename__ = 'rhodecode_settings'
12 9 __table_args__ = (UniqueConstraint('app_settings_name'), {'useexisting':True})
13 10 app_settings_id = Column("app_settings_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True)
14 11 app_settings_name = Column("app_settings_name", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
15 12 app_settings_value = Column("app_settings_value", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
16 13
17 14 class RhodeCodeUi(Base):
18 15 __tablename__ = 'rhodecode_ui'
19 16 __table_args__ = {'useexisting':True}
20 17 ui_id = Column("ui_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True)
21 18 ui_section = Column("ui_section", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
22 19 ui_key = Column("ui_key", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
23 20 ui_value = Column("ui_value", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
24 21 ui_active = Column("ui_active", BOOLEAN(), nullable=True, unique=None, default=True)
25 22
26 23
27 24 class User(Base):
28 25 __tablename__ = 'users'
29 26 __table_args__ = (UniqueConstraint('username'), UniqueConstraint('email'), {'useexisting':True})
30 27 user_id = Column("user_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True)
31 28 username = Column("username", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
32 29 password = Column("password", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
33 30 active = Column("active", BOOLEAN(), nullable=True, unique=None, default=None)
34 31 admin = Column("admin", BOOLEAN(), nullable=True, unique=None, default=False)
35 32 name = Column("name", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
36 33 lastname = Column("lastname", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
37 34 email = Column("email", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
38 35 last_login = Column("last_login", DATETIME(timezone=False), nullable=True, unique=None, default=None)
39 36
40 37 user_log = relation('UserLog')
41 38 user_perms = relation('UserToPerm', primaryjoin="User.user_id==UserToPerm.user_id")
42 39
43 40 @LazyProperty
44 41 def full_contact(self):
45 42 return '%s %s <%s>' % (self.name, self.lastname, self.email)
46 43
47 44 def __repr__(self):
48 45 return "<User('id:%s:%s')>" % (self.user_id, self.username)
49 46
50 47 def update_lastlogin(self):
51 48 """Update user lastlogin"""
52 49 import datetime
53 50
54 51 try:
55 52 session = Session.object_session(self)
56 53 self.last_login = datetime.datetime.now()
57 54 session.add(self)
58 55 session.commit()
59 56 log.debug('updated user %s lastlogin', self.username)
60 57 except Exception:
61 58 session.rollback()
62 59
63 60
64 61 class UserLog(Base):
65 62 __tablename__ = 'user_logs'
66 63 __table_args__ = {'useexisting':True}
67 64 user_log_id = Column("user_log_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True)
68 65 user_id = Column("user_id", INTEGER(), ForeignKey(u'users.user_id'), nullable=False, unique=None, default=None)
69 66 repository_id = Column("repository_id", INTEGER(length=None, convert_unicode=False, assert_unicode=None), ForeignKey(u'repositories.repo_id'), nullable=False, unique=None, default=None)
70 67 repository_name = Column("repository_name", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
71 68 user_ip = Column("user_ip", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
72 69 action = Column("action", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
73 70 action_date = Column("action_date", DATETIME(timezone=False), nullable=True, unique=None, default=None)
74 revision = Column('revision', TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
75 71
76 72 user = relation('User')
77 73 repository = relation('Repository')
78 74
79 75 class Repository(Base):
80 76 __tablename__ = 'repositories'
81 77 __table_args__ = (UniqueConstraint('repo_name'), {'useexisting':True},)
82 78 repo_id = Column("repo_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True)
83 79 repo_name = Column("repo_name", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None)
84 80 repo_type = Column("repo_type", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=False, default=None)
85 81 user_id = Column("user_id", INTEGER(), ForeignKey(u'users.user_id'), nullable=False, unique=False, default=None)
86 82 private = Column("private", BOOLEAN(), nullable=True, unique=None, default=None)
87 83 description = Column("description", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
88 84 fork_id = Column("fork_id", INTEGER(), ForeignKey(u'repositories.repo_id'), nullable=True, unique=False, default=None)
89 85
90 86 user = relation('User')
91 87 fork = relation('Repository', remote_side=repo_id)
92 88 repo_to_perm = relation('RepoToPerm', cascade='all')
93 89
94 90 def __repr__(self):
95 91 return "<Repository('id:%s:%s')>" % (self.repo_id, self.repo_name)
96 92
97 93 class Permission(Base):
98 94 __tablename__ = 'permissions'
99 95 __table_args__ = {'useexisting':True}
100 96 permission_id = Column("permission_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True)
101 97 permission_name = Column("permission_name", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
102 98 permission_longname = Column("permission_longname", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
103 99
104 100 def __repr__(self):
105 101 return "<Permission('%s:%s')>" % (self.permission_id, self.permission_name)
106 102
107 103 class RepoToPerm(Base):
108 104 __tablename__ = 'repo_to_perm'
109 105 __table_args__ = (UniqueConstraint('user_id', 'repository_id'), {'useexisting':True})
110 106 repo_to_perm_id = Column("repo_to_perm_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True)
111 107 user_id = Column("user_id", INTEGER(), ForeignKey(u'users.user_id'), nullable=False, unique=None, default=None)
112 108 permission_id = Column("permission_id", INTEGER(), ForeignKey(u'permissions.permission_id'), nullable=False, unique=None, default=None)
113 109 repository_id = Column("repository_id", INTEGER(), ForeignKey(u'repositories.repo_id'), nullable=False, unique=None, default=None)
114 110
115 111 user = relation('User')
116 112 permission = relation('Permission')
117 113 repository = relation('Repository')
118 114
119 115 class UserToPerm(Base):
120 116 __tablename__ = 'user_to_perm'
121 117 __table_args__ = (UniqueConstraint('user_id', 'permission_id'), {'useexisting':True})
122 118 user_to_perm_id = Column("user_to_perm_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True)
123 119 user_id = Column("user_id", INTEGER(), ForeignKey(u'users.user_id'), nullable=False, unique=None, default=None)
124 120 permission_id = Column("permission_id", INTEGER(), ForeignKey(u'permissions.permission_id'), nullable=False, unique=None, default=None)
125 121
126 122 user = relation('User')
127 123 permission = relation('Permission')
128 124
129 125 class Statistics(Base):
130 126 __tablename__ = 'statistics'
131 127 __table_args__ = (UniqueConstraint('repository_id'), {'useexisting':True})
132 128 stat_id = Column("stat_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True)
133 129 repository_id = Column("repository_id", INTEGER(), ForeignKey(u'repositories.repo_id'), nullable=False, unique=True, default=None)
134 130 stat_on_revision = Column("stat_on_revision", INTEGER(), nullable=False)
135 131 commit_activity = Column("commit_activity", BLOB(), nullable=False)#JSON data
136 132 commit_activity_combined = Column("commit_activity_combined", BLOB(), nullable=False)#JSON data
137 133 languages = Column("languages", BLOB(), nullable=False)#JSON data
138 134
139 135 repository = relation('Repository')
140 136
General Comments 0
You need to be logged in to leave comments. Login now