Show More
@@ -1,285 +1,284 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2012-2016 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 | |
|
27 | 27 | import os |
|
28 | 28 | import logging |
|
29 | 29 | |
|
30 | 30 | from celery.task import task |
|
31 | 31 | from pylons import config |
|
32 | 32 | |
|
33 | 33 | import rhodecode |
|
34 | 34 | from rhodecode.lib.celerylib import ( |
|
35 | 35 | run_task, dbsession, __get_lockkey, LockHeld, DaemonLock, |
|
36 | 36 | get_session, vcsconnection, RhodecodeCeleryTask) |
|
37 | 37 | from rhodecode.lib.hooks_base import log_create_repository |
|
38 | 38 | from rhodecode.lib.rcmail.smtp_mailer import SmtpMailer |
|
39 | 39 | from rhodecode.lib.utils import add_cache, action_logger |
|
40 | 40 | from rhodecode.lib.utils2 import safe_int, str2bool |
|
41 | 41 | from rhodecode.model.db import Repository, User |
|
42 | 42 | |
|
43 | 43 | |
|
44 | 44 | add_cache(config) # pragma: no cover |
|
45 | 45 | |
|
46 | 46 | |
|
47 | 47 | def get_logger(cls): |
|
48 | 48 | if rhodecode.CELERY_ENABLED: |
|
49 | 49 | try: |
|
50 | 50 | log = cls.get_logger() |
|
51 | 51 | except Exception: |
|
52 | 52 | log = logging.getLogger(__name__) |
|
53 | 53 | else: |
|
54 | 54 | log = logging.getLogger(__name__) |
|
55 | 55 | |
|
56 | 56 | return log |
|
57 | 57 | |
|
58 | 58 | |
|
59 | 59 | @task(ignore_result=True, base=RhodecodeCeleryTask) |
|
60 | 60 | @dbsession |
|
61 | 61 | def send_email(recipients, subject, body='', html_body='', email_config=None): |
|
62 | 62 | """ |
|
63 | 63 | Sends an email with defined parameters from the .ini files. |
|
64 | 64 | |
|
65 | 65 | :param recipients: list of recipients, it this is empty the defined email |
|
66 | 66 | address from field 'email_to' is used instead |
|
67 | 67 | :param subject: subject of the mail |
|
68 | 68 | :param body: body of the mail |
|
69 | 69 | :param html_body: html version of body |
|
70 | 70 | """ |
|
71 | 71 | log = get_logger(send_email) |
|
72 | 72 | |
|
73 | 73 | email_config = email_config or rhodecode.CONFIG |
|
74 | print email_config | |
|
75 | 74 | subject = "%s %s" % (email_config.get('email_prefix', ''), subject) |
|
76 | 75 | if not recipients: |
|
77 | 76 | # if recipients are not defined we send to email_config + all admins |
|
78 | 77 | admins = [ |
|
79 | 78 | u.email for u in User.query().filter(User.admin == True).all()] |
|
80 | 79 | recipients = [email_config.get('email_to')] + admins |
|
81 | 80 | |
|
82 | 81 | mail_server = email_config.get('smtp_server') or None |
|
83 | 82 | if mail_server is None: |
|
84 | 83 | log.error("SMTP server information missing. Sending email failed. " |
|
85 | 84 | "Make sure that `smtp_server` variable is configured " |
|
86 | 85 | "inside the .ini file") |
|
87 | 86 | return False |
|
88 | 87 | |
|
89 | 88 | mail_from = email_config.get('app_email_from', 'RhodeCode') |
|
90 | 89 | user = email_config.get('smtp_username') |
|
91 | 90 | passwd = email_config.get('smtp_password') |
|
92 | 91 | mail_port = email_config.get('smtp_port') |
|
93 | 92 | tls = str2bool(email_config.get('smtp_use_tls')) |
|
94 | 93 | ssl = str2bool(email_config.get('smtp_use_ssl')) |
|
95 | 94 | debug = str2bool(email_config.get('debug')) |
|
96 | 95 | smtp_auth = email_config.get('smtp_auth') |
|
97 | 96 | |
|
98 | 97 | try: |
|
99 | 98 | m = SmtpMailer(mail_from, user, passwd, mail_server, smtp_auth, |
|
100 | 99 | mail_port, ssl, tls, debug=debug) |
|
101 | 100 | m.send(recipients, subject, body, html_body) |
|
102 | 101 | except Exception: |
|
103 | 102 | log.exception('Mail sending failed') |
|
104 | 103 | return False |
|
105 | 104 | return True |
|
106 | 105 | |
|
107 | 106 | |
|
108 | 107 | @task(ignore_result=True, base=RhodecodeCeleryTask) |
|
109 | 108 | @dbsession |
|
110 | 109 | @vcsconnection |
|
111 | 110 | def create_repo(form_data, cur_user): |
|
112 | 111 | from rhodecode.model.repo import RepoModel |
|
113 | 112 | from rhodecode.model.user import UserModel |
|
114 | 113 | from rhodecode.model.settings import SettingsModel |
|
115 | 114 | |
|
116 | 115 | log = get_logger(create_repo) |
|
117 | 116 | DBS = get_session() |
|
118 | 117 | |
|
119 | 118 | cur_user = UserModel(DBS)._get_user(cur_user) |
|
120 | 119 | owner = cur_user |
|
121 | 120 | |
|
122 | 121 | repo_name = form_data['repo_name'] |
|
123 | 122 | repo_name_full = form_data['repo_name_full'] |
|
124 | 123 | repo_type = form_data['repo_type'] |
|
125 | 124 | description = form_data['repo_description'] |
|
126 | 125 | private = form_data['repo_private'] |
|
127 | 126 | clone_uri = form_data.get('clone_uri') |
|
128 | 127 | repo_group = safe_int(form_data['repo_group']) |
|
129 | 128 | landing_rev = form_data['repo_landing_rev'] |
|
130 | 129 | copy_fork_permissions = form_data.get('copy_permissions') |
|
131 | 130 | copy_group_permissions = form_data.get('repo_copy_permissions') |
|
132 | 131 | fork_of = form_data.get('fork_parent_id') |
|
133 | 132 | state = form_data.get('repo_state', Repository.STATE_PENDING) |
|
134 | 133 | |
|
135 | 134 | # repo creation defaults, private and repo_type are filled in form |
|
136 | 135 | defs = SettingsModel().get_default_repo_settings(strip_prefix=True) |
|
137 | 136 | enable_statistics = form_data.get( |
|
138 | 137 | 'enable_statistics', defs.get('repo_enable_statistics')) |
|
139 | 138 | enable_locking = form_data.get( |
|
140 | 139 | 'enable_locking', defs.get('repo_enable_locking')) |
|
141 | 140 | enable_downloads = form_data.get( |
|
142 | 141 | 'enable_downloads', defs.get('repo_enable_downloads')) |
|
143 | 142 | |
|
144 | 143 | try: |
|
145 | 144 | RepoModel(DBS)._create_repo( |
|
146 | 145 | repo_name=repo_name_full, |
|
147 | 146 | repo_type=repo_type, |
|
148 | 147 | description=description, |
|
149 | 148 | owner=owner, |
|
150 | 149 | private=private, |
|
151 | 150 | clone_uri=clone_uri, |
|
152 | 151 | repo_group=repo_group, |
|
153 | 152 | landing_rev=landing_rev, |
|
154 | 153 | fork_of=fork_of, |
|
155 | 154 | copy_fork_permissions=copy_fork_permissions, |
|
156 | 155 | copy_group_permissions=copy_group_permissions, |
|
157 | 156 | enable_statistics=enable_statistics, |
|
158 | 157 | enable_locking=enable_locking, |
|
159 | 158 | enable_downloads=enable_downloads, |
|
160 | 159 | state=state |
|
161 | 160 | ) |
|
162 | 161 | |
|
163 | 162 | action_logger(cur_user, 'user_created_repo', |
|
164 | 163 | repo_name_full, '', DBS) |
|
165 | 164 | DBS.commit() |
|
166 | 165 | |
|
167 | 166 | # now create this repo on Filesystem |
|
168 | 167 | RepoModel(DBS)._create_filesystem_repo( |
|
169 | 168 | repo_name=repo_name, |
|
170 | 169 | repo_type=repo_type, |
|
171 | 170 | repo_group=RepoModel(DBS)._get_repo_group(repo_group), |
|
172 | 171 | clone_uri=clone_uri, |
|
173 | 172 | ) |
|
174 | 173 | repo = Repository.get_by_repo_name(repo_name_full) |
|
175 | 174 | log_create_repository(created_by=owner.username, **repo.get_dict()) |
|
176 | 175 | |
|
177 | 176 | # update repo commit caches initially |
|
178 | 177 | repo.update_commit_cache() |
|
179 | 178 | |
|
180 | 179 | # set new created state |
|
181 | 180 | repo.set_state(Repository.STATE_CREATED) |
|
182 | 181 | DBS.commit() |
|
183 | 182 | except Exception as e: |
|
184 | 183 | log.warning('Exception %s occurred when creating repository, ' |
|
185 | 184 | 'doing cleanup...', e) |
|
186 | 185 | # rollback things manually ! |
|
187 | 186 | repo = Repository.get_by_repo_name(repo_name_full) |
|
188 | 187 | if repo: |
|
189 | 188 | Repository.delete(repo.repo_id) |
|
190 | 189 | DBS.commit() |
|
191 | 190 | RepoModel(DBS)._delete_filesystem_repo(repo) |
|
192 | 191 | raise |
|
193 | 192 | |
|
194 | 193 | # it's an odd fix to make celery fail task when exception occurs |
|
195 | 194 | def on_failure(self, *args, **kwargs): |
|
196 | 195 | pass |
|
197 | 196 | |
|
198 | 197 | return True |
|
199 | 198 | |
|
200 | 199 | |
|
201 | 200 | @task(ignore_result=True, base=RhodecodeCeleryTask) |
|
202 | 201 | @dbsession |
|
203 | 202 | @vcsconnection |
|
204 | 203 | def create_repo_fork(form_data, cur_user): |
|
205 | 204 | """ |
|
206 | 205 | Creates a fork of repository using internal VCS methods |
|
207 | 206 | |
|
208 | 207 | :param form_data: |
|
209 | 208 | :param cur_user: |
|
210 | 209 | """ |
|
211 | 210 | from rhodecode.model.repo import RepoModel |
|
212 | 211 | from rhodecode.model.user import UserModel |
|
213 | 212 | |
|
214 | 213 | log = get_logger(create_repo_fork) |
|
215 | 214 | DBS = get_session() |
|
216 | 215 | |
|
217 | 216 | cur_user = UserModel(DBS)._get_user(cur_user) |
|
218 | 217 | owner = cur_user |
|
219 | 218 | |
|
220 | 219 | repo_name = form_data['repo_name'] # fork in this case |
|
221 | 220 | repo_name_full = form_data['repo_name_full'] |
|
222 | 221 | repo_type = form_data['repo_type'] |
|
223 | 222 | description = form_data['description'] |
|
224 | 223 | private = form_data['private'] |
|
225 | 224 | clone_uri = form_data.get('clone_uri') |
|
226 | 225 | repo_group = safe_int(form_data['repo_group']) |
|
227 | 226 | landing_rev = form_data['landing_rev'] |
|
228 | 227 | copy_fork_permissions = form_data.get('copy_permissions') |
|
229 | 228 | fork_id = safe_int(form_data.get('fork_parent_id')) |
|
230 | 229 | |
|
231 | 230 | try: |
|
232 | 231 | fork_of = RepoModel(DBS)._get_repo(fork_id) |
|
233 | 232 | RepoModel(DBS)._create_repo( |
|
234 | 233 | repo_name=repo_name_full, |
|
235 | 234 | repo_type=repo_type, |
|
236 | 235 | description=description, |
|
237 | 236 | owner=owner, |
|
238 | 237 | private=private, |
|
239 | 238 | clone_uri=clone_uri, |
|
240 | 239 | repo_group=repo_group, |
|
241 | 240 | landing_rev=landing_rev, |
|
242 | 241 | fork_of=fork_of, |
|
243 | 242 | copy_fork_permissions=copy_fork_permissions |
|
244 | 243 | ) |
|
245 | 244 | action_logger(cur_user, 'user_forked_repo:%s' % repo_name_full, |
|
246 | 245 | fork_of.repo_name, '', DBS) |
|
247 | 246 | DBS.commit() |
|
248 | 247 | |
|
249 | 248 | base_path = Repository.base_path() |
|
250 | 249 | source_repo_path = os.path.join(base_path, fork_of.repo_name) |
|
251 | 250 | |
|
252 | 251 | # now create this repo on Filesystem |
|
253 | 252 | RepoModel(DBS)._create_filesystem_repo( |
|
254 | 253 | repo_name=repo_name, |
|
255 | 254 | repo_type=repo_type, |
|
256 | 255 | repo_group=RepoModel(DBS)._get_repo_group(repo_group), |
|
257 | 256 | clone_uri=source_repo_path, |
|
258 | 257 | ) |
|
259 | 258 | repo = Repository.get_by_repo_name(repo_name_full) |
|
260 | 259 | log_create_repository(created_by=owner.username, **repo.get_dict()) |
|
261 | 260 | |
|
262 | 261 | # update repo commit caches initially |
|
263 | 262 | config = repo._config |
|
264 | 263 | config.set('extensions', 'largefiles', '') |
|
265 | 264 | repo.update_commit_cache(config=config) |
|
266 | 265 | |
|
267 | 266 | # set new created state |
|
268 | 267 | repo.set_state(Repository.STATE_CREATED) |
|
269 | 268 | DBS.commit() |
|
270 | 269 | except Exception as e: |
|
271 | 270 | log.warning('Exception %s occurred when forking repository, ' |
|
272 | 271 | 'doing cleanup...', e) |
|
273 | 272 | # rollback things manually ! |
|
274 | 273 | repo = Repository.get_by_repo_name(repo_name_full) |
|
275 | 274 | if repo: |
|
276 | 275 | Repository.delete(repo.repo_id) |
|
277 | 276 | DBS.commit() |
|
278 | 277 | RepoModel(DBS)._delete_filesystem_repo(repo) |
|
279 | 278 | raise |
|
280 | 279 | |
|
281 | 280 | # it's an odd fix to make celery fail task when exception occurs |
|
282 | 281 | def on_failure(self, *args, **kwargs): |
|
283 | 282 | pass |
|
284 | 283 | |
|
285 | 284 | return True |
General Comments 0
You need to be logged in to leave comments.
Login now