1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 import os
17 import sys
18 import tempfile
19
20 from config_common import handler_base, utils, cfg_exceptions
21 from config_common.rhn_log import log_debug, die
22 from config_common.transactions import DeployTransaction, FailedRollback
23 from spacewalk.common.usix import raise_with_tb
24
25
27 print("Deploying %s" % path)
28
29
30 -class Handler(handler_base.HandlerBase):
31 _usage_options = "[options] file [ file ... ]"
32
33 _options_table = handler_base.HandlerBase._options_table + [
34 handler_base.HandlerBase._option_class(
35 '-c', '--channel', action="store",
36 help="Get file(s) from this config channel",
37 ),
38 handler_base.HandlerBase._option_class(
39 '-t', '--topdir', action="store",
40 help="Make all files relative to this string",
41 ),
42 handler_base.HandlerBase._option_class(
43 '-r', '--revision', action="store",
44 help="Get this file revision",
45 ),
46 ]
47
49 log_debug(2)
50 r = self.repository
51
52 channel = self.options.channel
53 if not channel:
54 die(6, "Config channel not specified")
55
56 topdir = self.options.topdir
57 if topdir:
58 if not os.path.isdir(self.options.topdir):
59 die(8, "--topdir specified, but `%s' not a directory" %
60 self.options.topdir)
61
62 if not self.args:
63 die(7, "No files specified")
64
65 revision = self.options.revision
66 if revision:
67 if len(self.args) > 1:
68 die(9, "--revision specified with multiple files")
69
70 dep_trans = None
71
72 if topdir:
73 dep_trans = DeployTransaction(transaction_root=topdir)
74 dep_trans.deploy_callback(deploying_mesg_callback)
75
76 for f in self.args:
77 try:
78 directory = topdir or tempfile.gettempdir()
79
80
81 (temp_file, info, dirs_created) = r.get_file_info(channel, f, revision=revision,
82 auto_delete=0, dest_directory=directory)
83
84 except cfg_exceptions.RepositoryFileMissingError:
85 if revision is not None:
86 die(2, "Error: file %s (revision %s) not in config "
87 "channel %s" % (f, revision, channel))
88 else:
89 die(2, "Error: file %s not in config channel %s" %
90 (f, channel))
91
92 if topdir:
93
94 dep_trans.add_preprocessed(f, temp_file, info, dirs_created, strict_ownership=0)
95 continue
96 elif info.get('filetype') == 'symlink':
97 print("%s -> %s" % (info['path'], info['symlink']))
98 continue
99 elif info.get('filetype') == 'directory':
100 print("%s is a directory entry, nothing to get" % info['path'])
101 continue
102 else:
103 print(open(temp_file).read())
104 os.unlink(temp_file)
105
106 if topdir:
107 try:
108 dep_trans.deploy()
109 except Exception:
110 try:
111 dep_trans.rollback()
112 except FailedRollback:
113 raise_with_tb(FailedRollback("FAILED ROLLBACK: "), sys.exc_info()[2])
114
115 except cfg_exceptions.UserNotFound:
116 raise
117
118 except cfg_exceptions.GroupNotFound:
119 raise
120 else:
121 raise_with_tb(Exception("Deploy failed, rollback successful: "), sys.exc_info()[2])
122