##// END OF EJS Templates
emails: ensure none empty emails are fetched from admin accounts in recipinets aren't specified....
marcink -
r2896:5e035500 default
parent child Browse files
Show More
@@ -1,301 +1,307 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2012-2018 RhodeCode GmbH
4 4 #
5 5 # This program is free software: you can redistribute it and/or modify
6 6 # it under the terms of the GNU Affero General Public License, version 3
7 7 # (only), as published by the Free Software Foundation.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU Affero General Public License
15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16 #
17 17 # This program is dual-licensed. If you wish to learn more about the
18 18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 20
21 21 """
22 22 RhodeCode task modules, containing all task that suppose to be run
23 23 by celery daemon
24 24 """
25 25
26 26 import os
27 27 import time
28 28
29 29 import rhodecode
30 30 from rhodecode.lib import audit_logger
31 31 from rhodecode.lib.celerylib import get_logger, async_task, RequestContextTask
32 32 from rhodecode.lib.hooks_base import log_create_repository
33 33 from rhodecode.lib.rcmail.smtp_mailer import SmtpMailer
34 34 from rhodecode.lib.utils2 import safe_int, str2bool
35 from rhodecode.model.db import Session, IntegrityError, Repository, User
35 from rhodecode.model.db import Session, IntegrityError, Repository, User, true
36 36
37 37
38 38 @async_task(ignore_result=True, base=RequestContextTask)
39 39 def send_email(recipients, subject, body='', html_body='', email_config=None):
40 40 """
41 41 Sends an email with defined parameters from the .ini files.
42 42
43 43 :param recipients: list of recipients, it this is empty the defined email
44 44 address from field 'email_to' is used instead
45 45 :param subject: subject of the mail
46 46 :param body: body of the mail
47 47 :param html_body: html version of body
48 48 """
49 49 log = get_logger(send_email)
50 50
51 51 email_config = email_config or rhodecode.CONFIG
52 52 subject = "%s %s" % (email_config.get('email_prefix', ''), subject)
53 53 if not recipients:
54 54 # if recipients are not defined we send to email_config + all admins
55 admins = [
56 u.email for u in User.query().filter(User.admin == True).all()]
57 recipients = [email_config.get('email_to')] + admins
55 admins = []
56 for u in User.query().filter(User.admin == true()).all():
57 if u.email:
58 admins.append(u.email)
59 recipients = []
60 config_email = email_config.get('email_to')
61 if config_email:
62 recipients += [config_email]
63 recipients += admins
58 64
59 65 mail_server = email_config.get('smtp_server') or None
60 66 if mail_server is None:
61 67 log.error("SMTP server information missing. Sending email failed. "
62 68 "Make sure that `smtp_server` variable is configured "
63 69 "inside the .ini file")
64 70 return False
65 71
66 72 mail_from = email_config.get('app_email_from', 'RhodeCode')
67 73 user = email_config.get('smtp_username')
68 74 passwd = email_config.get('smtp_password')
69 75 mail_port = email_config.get('smtp_port')
70 76 tls = str2bool(email_config.get('smtp_use_tls'))
71 77 ssl = str2bool(email_config.get('smtp_use_ssl'))
72 78 debug = str2bool(email_config.get('debug'))
73 79 smtp_auth = email_config.get('smtp_auth')
74 80
75 81 try:
76 82 m = SmtpMailer(mail_from, user, passwd, mail_server, smtp_auth,
77 83 mail_port, ssl, tls, debug=debug)
78 84 m.send(recipients, subject, body, html_body)
79 85 except Exception:
80 86 log.exception('Mail sending failed')
81 87 return False
82 88 return True
83 89
84 90
85 91 @async_task(ignore_result=True, base=RequestContextTask)
86 92 def create_repo(form_data, cur_user):
87 93 from rhodecode.model.repo import RepoModel
88 94 from rhodecode.model.user import UserModel
89 95 from rhodecode.model.settings import SettingsModel
90 96
91 97 log = get_logger(create_repo)
92 98
93 99 cur_user = UserModel()._get_user(cur_user)
94 100 owner = cur_user
95 101
96 102 repo_name = form_data['repo_name']
97 103 repo_name_full = form_data['repo_name_full']
98 104 repo_type = form_data['repo_type']
99 105 description = form_data['repo_description']
100 106 private = form_data['repo_private']
101 107 clone_uri = form_data.get('clone_uri')
102 108 repo_group = safe_int(form_data['repo_group'])
103 109 landing_rev = form_data['repo_landing_rev']
104 110 copy_fork_permissions = form_data.get('copy_permissions')
105 111 copy_group_permissions = form_data.get('repo_copy_permissions')
106 112 fork_of = form_data.get('fork_parent_id')
107 113 state = form_data.get('repo_state', Repository.STATE_PENDING)
108 114
109 115 # repo creation defaults, private and repo_type are filled in form
110 116 defs = SettingsModel().get_default_repo_settings(strip_prefix=True)
111 117 enable_statistics = form_data.get(
112 118 'enable_statistics', defs.get('repo_enable_statistics'))
113 119 enable_locking = form_data.get(
114 120 'enable_locking', defs.get('repo_enable_locking'))
115 121 enable_downloads = form_data.get(
116 122 'enable_downloads', defs.get('repo_enable_downloads'))
117 123
118 124 try:
119 125 repo = RepoModel()._create_repo(
120 126 repo_name=repo_name_full,
121 127 repo_type=repo_type,
122 128 description=description,
123 129 owner=owner,
124 130 private=private,
125 131 clone_uri=clone_uri,
126 132 repo_group=repo_group,
127 133 landing_rev=landing_rev,
128 134 fork_of=fork_of,
129 135 copy_fork_permissions=copy_fork_permissions,
130 136 copy_group_permissions=copy_group_permissions,
131 137 enable_statistics=enable_statistics,
132 138 enable_locking=enable_locking,
133 139 enable_downloads=enable_downloads,
134 140 state=state
135 141 )
136 142 Session().commit()
137 143
138 144 # now create this repo on Filesystem
139 145 RepoModel()._create_filesystem_repo(
140 146 repo_name=repo_name,
141 147 repo_type=repo_type,
142 148 repo_group=RepoModel()._get_repo_group(repo_group),
143 149 clone_uri=clone_uri,
144 150 )
145 151 repo = Repository.get_by_repo_name(repo_name_full)
146 152 log_create_repository(created_by=owner.username, **repo.get_dict())
147 153
148 154 # update repo commit caches initially
149 155 repo.update_commit_cache()
150 156
151 157 # set new created state
152 158 repo.set_state(Repository.STATE_CREATED)
153 159 repo_id = repo.repo_id
154 160 repo_data = repo.get_api_data()
155 161
156 162 audit_logger.store(
157 163 'repo.create', action_data={'data': repo_data},
158 164 user=cur_user,
159 165 repo=audit_logger.RepoWrap(repo_name=repo_name, repo_id=repo_id))
160 166
161 167 Session().commit()
162 168 except Exception as e:
163 169 log.warning('Exception occurred when creating repository, '
164 170 'doing cleanup...', exc_info=True)
165 171 if isinstance(e, IntegrityError):
166 172 Session().rollback()
167 173
168 174 # rollback things manually !
169 175 repo = Repository.get_by_repo_name(repo_name_full)
170 176 if repo:
171 177 Repository.delete(repo.repo_id)
172 178 Session().commit()
173 179 RepoModel()._delete_filesystem_repo(repo)
174 180 log.info('Cleanup of repo %s finished', repo_name_full)
175 181 raise
176 182
177 183 return True
178 184
179 185
180 186 @async_task(ignore_result=True, base=RequestContextTask)
181 187 def create_repo_fork(form_data, cur_user):
182 188 """
183 189 Creates a fork of repository using internal VCS methods
184 190 """
185 191 from rhodecode.model.repo import RepoModel
186 192 from rhodecode.model.user import UserModel
187 193
188 194 log = get_logger(create_repo_fork)
189 195
190 196 cur_user = UserModel()._get_user(cur_user)
191 197 owner = cur_user
192 198
193 199 repo_name = form_data['repo_name'] # fork in this case
194 200 repo_name_full = form_data['repo_name_full']
195 201 repo_type = form_data['repo_type']
196 202 description = form_data['description']
197 203 private = form_data['private']
198 204 clone_uri = form_data.get('clone_uri')
199 205 repo_group = safe_int(form_data['repo_group'])
200 206 landing_rev = form_data['landing_rev']
201 207 copy_fork_permissions = form_data.get('copy_permissions')
202 208 fork_id = safe_int(form_data.get('fork_parent_id'))
203 209
204 210 try:
205 211 fork_of = RepoModel()._get_repo(fork_id)
206 212 RepoModel()._create_repo(
207 213 repo_name=repo_name_full,
208 214 repo_type=repo_type,
209 215 description=description,
210 216 owner=owner,
211 217 private=private,
212 218 clone_uri=clone_uri,
213 219 repo_group=repo_group,
214 220 landing_rev=landing_rev,
215 221 fork_of=fork_of,
216 222 copy_fork_permissions=copy_fork_permissions
217 223 )
218 224
219 225 Session().commit()
220 226
221 227 base_path = Repository.base_path()
222 228 source_repo_path = os.path.join(base_path, fork_of.repo_name)
223 229
224 230 # now create this repo on Filesystem
225 231 RepoModel()._create_filesystem_repo(
226 232 repo_name=repo_name,
227 233 repo_type=repo_type,
228 234 repo_group=RepoModel()._get_repo_group(repo_group),
229 235 clone_uri=source_repo_path,
230 236 )
231 237 repo = Repository.get_by_repo_name(repo_name_full)
232 238 log_create_repository(created_by=owner.username, **repo.get_dict())
233 239
234 240 # update repo commit caches initially
235 241 config = repo._config
236 242 config.set('extensions', 'largefiles', '')
237 243 repo.update_commit_cache(config=config)
238 244
239 245 # set new created state
240 246 repo.set_state(Repository.STATE_CREATED)
241 247
242 248 repo_id = repo.repo_id
243 249 repo_data = repo.get_api_data()
244 250 audit_logger.store(
245 251 'repo.fork', action_data={'data': repo_data},
246 252 user=cur_user,
247 253 repo=audit_logger.RepoWrap(repo_name=repo_name, repo_id=repo_id))
248 254
249 255 Session().commit()
250 256 except Exception as e:
251 257 log.warning('Exception occurred when forking repository, '
252 258 'doing cleanup...', exc_info=True)
253 259 if isinstance(e, IntegrityError):
254 260 Session().rollback()
255 261
256 262 # rollback things manually !
257 263 repo = Repository.get_by_repo_name(repo_name_full)
258 264 if repo:
259 265 Repository.delete(repo.repo_id)
260 266 Session().commit()
261 267 RepoModel()._delete_filesystem_repo(repo)
262 268 log.info('Cleanup of repo %s finished', repo_name_full)
263 269 raise
264 270
265 271 return True
266 272
267 273
268 274 @async_task(ignore_result=True)
269 275 def repo_maintenance(repoid):
270 276 from rhodecode.lib import repo_maintenance as repo_maintenance_lib
271 277 log = get_logger(repo_maintenance)
272 278 repo = Repository.get_by_id_or_repo_name(repoid)
273 279 if repo:
274 280 maintenance = repo_maintenance_lib.RepoMaintenance()
275 281 tasks = maintenance.get_tasks_for_repo(repo)
276 282 log.debug('Executing %s tasks on repo `%s`', tasks, repoid)
277 283 executed_types = maintenance.execute(repo)
278 284 log.debug('Got execution results %s', executed_types)
279 285 else:
280 286 log.debug('Repo `%s` not found or without a clone_url', repoid)
281 287
282 288
283 289 @async_task(ignore_result=True)
284 290 def check_for_update():
285 291 from rhodecode.model.update import UpdateModel
286 292 update_url = UpdateModel().get_update_url()
287 293 cur_ver = rhodecode.__version__
288 294
289 295 try:
290 296 data = UpdateModel().get_update_data(update_url)
291 297 latest = data['versions'][0]
292 298 UpdateModel().store_version(latest['version'])
293 299 except Exception:
294 300 pass
295 301
296 302
297 303 @async_task(ignore_result=False)
298 304 def beat_check(*args, **kwargs):
299 305 log = get_logger(beat_check)
300 306 log.info('Got args: %r and kwargs %r', args, kwargs)
301 307 return time.time()
General Comments 0
You need to be logged in to leave comments. Login now