Package actions ::
Module configfiles
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 import os
17 import time
18 import sys
19
20 from config_common import local_config, file_utils, utils, cfg_exceptions
21 from config_common.rhn_log import set_debug_level, get_debug_level, set_logfile, log_to_file
22 from config_common.transactions import DeployTransaction, FailedRollback
23
24 from config_client import rpc_cli_repository
25
26 from up2date_client import config
27
28 from spacewalk.common.usix import StringType, UnicodeType
29
30
31
32 __rhnexport__ = [
33 'mtime_upload',
34 'upload',
35 'deploy',
36 'verify',
37 'diff',
38 ]
39
40
41
42 ACTION_VERSION = 2
43
44
45
46
47 _permission_root_dir = '/etc/sysconfig/rhn/allowed-actions'
65
67 return (42, "Local permission not set for action type %s" % action_type, {})
68
69
71
72 matches = params['matches']
73 info = params['info']
74 ignore_dirs = params['ignore']
75 now = params['now']
76
77 i = 0
78 while i < len(names):
79 full_path = os.path.join(dirname, names[i])
80 is_dir = os.path.isdir(full_path)
81
82 if is_dir:
83 if full_path in ignore_dirs:
84
85
86 del names[i]
87 else:
88 i = i + 1
89
90
91 ignore_dirs[full_path] = None
92 continue
93
94 if not os.path.exists(full_path):
95 i = i + 1
96 continue
97
98 mtime = os.path.getmtime(full_path)
99
100
101 if (now - mtime) <= (info['now'] - info['start_date']):
102 if info['end_date']:
103 if (now - mtime) >= (info['now'] - info['end_date']):
104 matches.append(full_path)
105 else:
106 matches.append(full_path)
107
108 i = i + 1
109
110
132
133
134
135
136
137
139 if cache_only:
140 return (0, "no-ops for caching", {})
141
142 action_type = 'configfiles.mtime_upload'
143 if not _local_permission_check(action_type):
144 log_to_file(0, "permissions error: " + str(action_type))
145 return _perm_error(action_type)
146
147 _init()
148
149 file_matches = []
150 now = time.time()
151 upload_contents = None
152 ignore_dirs = {'/proc':None, '/dev':None}
153
154 if params['info']['import_contents'] == 'Y':
155 upload_contents = 1
156
157 for to_ignore in params['ignore']:
158 ignore_dirs[utils.normalize_path(to_ignore)] = 1
159
160 for search_path in params['search']:
161 for dirname, dirs, names in os.walk(utils.normalize_path(search_path)):
162 _visit_dir({
163 'matches' : file_matches,
164 'info' : params['info'],
165 'ignore' : ignore_dirs,
166 'now' : now,
167 }, dirname, names)
168
169 if not file_matches:
170 return 0, "No files found", {}
171
172 r = rpc_cli_repository.ClientRepository()
173 result = r.put_files(action_id, file_matches, upload_contents=upload_contents)
174
175 formatted_result = format_result(result, file_matches)
176 log_to_file(0, formatted_result)
177 return formatted_result
178
179
180 -def upload(action_id, params, cache_only=None):
200
201
202 -def deploy(params, topdir=None, cache_only=None):
203 if cache_only:
204 return (0, "no-ops for caching", {})
205
206 action_type = 'configfiles.deploy'
207 if not _local_permission_check(action_type):
208 log_to_file(0, "permissions error: " + str(action_type))
209 return _perm_error(action_type)
210
211 _init()
212 files = params.get('files') or []
213 dep_trans = DeployTransaction(transaction_root=topdir, auto_rollback=0)
214
215 for file in files:
216 dep_trans.add(file)
217
218 try:
219 dep_trans.deploy()
220
221 except cfg_exceptions.UserNotFound:
222 e = sys.exc_info()[1]
223 try:
224 dep_trans.rollback()
225 except FailedRollback:
226 log_to_file(0, "Failed deployment and rollback, information on user '%s' could not be found" % (e[0], ))
227 return (44, "Failed deployment and rollback, information on user '%s' could not be found" % (e[0], ), {})
228
229 except cfg_exceptions.UserNotFound:
230 f = sys.exc_info()[1]
231 log_to_file(0, "Failed deployment and rollback, information on user '%s' could not be found" % (f[0], ))
232 return (50, "Failed deployment and rollback, information on user '%s' could not be found" % (f[0], ), {})
233
234 except cfg_exceptions.GroupNotFound:
235 f = sys.exc_info()[1]
236 log_to_file(0, "Failed deployment and rollback, group '%s' could not be found" % (f[0],))
237 return (51, "Failed deployment and rollback, group '%s' could not be found" % (f[0],), {})
238 else:
239 log_to_file(0, "Failed deployment and rollback, information on user '%s' could not be found" % (e[0], ))
240 return (50, "Failed deployment and rollback, information on user '%s' could not be found" % (e[0], ), {})
241 except cfg_exceptions.GroupNotFound:
242 e = sys.exc_info()[1]
243 try:
244 dep_trans.rollback()
245 except FailedRollback:
246 log_to_file(0, "Failed deployment and rollback, information on user '%s' could not be found" % (e[0], ))
247 return (44, "Failed deployment and rollback, information on user '%s' could not be found" % (e[0], ), {})
248
249 except cfg_exceptions.UserNotFound:
250 f = sys.exc_info()[1]
251 log_to_file(0, "Failed deployment and rollback, information on user '%s' could not be found" % (f[0], ) )
252 return (50, "Failed deployment and rollback, information on user '%s' could not be found" % (f[0], ), {})
253
254 except cfg_exceptions.GroupNotFound:
255 f = sys.exc_info()[1]
256 log_to_file(0, "Failed deployment and rollback, group '%s' could not be found" % (f[0],))
257 return (51, "Failed deployment and rollback, group '%s' could not be found" % (f[0],), {})
258 else:
259 log_to_file(0, "Failed deployment and rollback, group '%s' could not be found" % (e[0], ))
260 return (51, "Failed deployment and rollback, group '%s' could not be found" % (e[0], ), {})
261 except cfg_exceptions.FileEntryIsDirectory:
262 e = sys.exc_info()[1]
263 try:
264 dep_trans.rollback()
265 except FailedRollback:
266 log_to_file(0, "Failed deployment and rollback, %s already exists as a directory" % (e[0], ))
267 return (44, "Failed deployment and rollback, %s already exists as a directory" % (e[0], ), {})
268
269 except cfg_exceptions.UserNotFound:
270 f = sys.exc_info()[1]
271 log_to_file(0, "Failed deployment and rollback, information on user '%s' could not be found" % (f[0], ))
272 return (50, "Failed deployment and rollback, information on user '%s' could not be found" % (f[0], ), {})
273
274 except cfg_exceptions.GroupNotFound:
275 f = sys.exc_info()[1]
276 log_to_file(0, "Failed deployment and rollback, group '%s' could not be found" % (f[0],))
277 return (51, "Failed deployment and rollback, group '%s' could not be found" % (f[0],), {})
278 else:
279 log_to_file(0, "Failed deployment, %s already exists as a directory" % (e[0], ))
280 return (45, "Failed deployment, %s already exists as a directory" % (e[0], ), {})
281 except cfg_exceptions.DirectoryEntryIsFile:
282 e = sys.exc_info()[1]
283 try:
284 dep_trans.rollback()
285 except FailedRollback:
286 log_to_file(0, "Failed deployment and rollback, %s already exists as a file" % (e[0], ))
287 return (46, "Failed deployment and rollback, %s already exists as a file" % (e[0], ), {})
288
289 except cfg_exceptions.UserNotFound:
290 f = sys.exc_info()[1]
291 log_to_file(0, "Failed deployment and rollback, information on user '%s' could not be found" % (f[0], ))
292 return (50, "Failed deployment and rollback, information on user '%s' could not be found" % (f[0], ), {})
293
294 except cfg_exceptions.GroupNotFound:
295 f = sys.exc_info()[1]
296 log_to_file(0, "Failed deployment and rollback, group '%s' could not be found" % (f[0],))
297 return (51, "Failed deployment and rollback, group '%s' could not be found" % (f[0],), {})
298 else:
299 log_to_file(0, "Failed deployment, %s already exists as a file" % (e[0], ))
300 return (47, "Failed deployment, %s already exists as a file" % (e[0], ), {})
301
302 except Exception:
303 e = sys.exc_info()[1]
304 print(e)
305 try:
306 dep_trans.rollback()
307 except FailedRollback:
308 e2 = sys.exc_info()[1]
309 log_to_file(0, "Failed deployment, failed rollback: %s" % e2)
310 return (48, "Failed deployment, failed rollback: %s" % e2, {})
311
312 except cfg_exceptions.UserNotFound:
313 f = sys.exc_info()[1]
314 log_to_file(0, "Failed deployment and rollback, information on user '%s' could not be found" % (f[0]))
315 return (50, "Failed deployment and rollback, information on user '%s' could not be found" % (f[0]), {})
316
317 except cfg_exceptions.GroupNotFound:
318 f = sys.exc_info()[1]
319 log_to_file(0, "Failed deployment and rollback, group '%s' could not be found" % (f[0],))
320 return (51, "Failed deployment and rollback, group '%s' could not be found" % (f[0],), {})
321 else:
322 log_to_file(0, "Failed deployment, rolled back: %s" % e)
323 return (49, "Failed deployment, rolled back: %s" % e, {})
324
325 extras = {}
326 log_to_file(0, "Files successfully deployed: %s %s" % (format_file_string(files, create_key_list()), str(extras)))
327 return 0, "Files successfully deployed", extras
328
329
330 -def diff(params, cache_only=None):
331 def is_utf8(in_string):
332 """Returns true if input is a valid UTF-8 string, False otherwise."""
333 if isinstance(in_string, UnicodeType):
334 return True
335 elif isinstance(in_string, StringType):
336 try:
337 in_string.decode('utf-8')
338 return True
339 except UnicodeDecodeError:
340 return False
341 return False
342
343 if cache_only:
344 return (0, "no-ops for caching", {})
345
346 action_type = 'configfiles.diff'
347 if not _local_permission_check(action_type):
348 log_to_file(0, "permissions error: " + str(action_type))
349 return _perm_error(action_type)
350
351 _init()
352 files = params.get('files') or []
353 fp = file_utils.FileProcessor()
354 missing_files = []
355 diffs = {}
356 exists = hasattr(os.path, 'lexists') and os.path.lexists or os.path.exists
357 for file in files:
358 path = file['path']
359 if not exists(path):
360 missing_files.append(path)
361 continue
362
363 diff = fp.diff(file)
364 if diff:
365 diffs[path] = diff
366
367 extras = {}
368 if missing_files:
369 extras['missing_files'] = missing_files
370
371 if diffs:
372 for file in diffs.keys():
373 if not is_utf8(diffs[file]):
374 diffs[file] = "%s: binary files differ" % file
375 extras['diffs'] = diffs
376
377 log_to_file(0, "Files successfully diffed: %s %s" % (format_file_string(files, create_key_list()), str(extras)))
378 return 0, "Files successfully diffed", extras
379
380 verify = diff
381
382
383
384
385
386
397
398
399
400
402
403
404
405 key_list = [
406 'path',
407 'revision',
408 'config_channel',
409 'filemode',
410 'filetype',
411 'encoding',
412 'username',
413 'groupname',
414 'delim_start',
415 'delim_end',
416 'md5sum',
417 'checksum_type',
418 'checksum',
419 'verify_contents',
420 'file_contents',
421 ]
422
423
424 debug_levels = {
425 0 : 2,
426 1 : 4,
427 2 : 6,
428 3 : 8,
429 4 : 10,
430 5 : 15,
431 }
432 curr_debug = get_debug_level()
433 if curr_debug > 5:
434 curr_debug = 5
435 if curr_debug < 0:
436 curr_debug = 0
437 if not curr_debug in debug_levels.keys():
438 curr_debug = 0
439 return key_list[:debug_levels[curr_debug]]
440
451