Package config_management :: Module rhncfg_add
[hide private]
[frames] | no frames]

Source Code for Module config_management.rhncfg_add

  1  # 
  2  # Copyright (c) 2008--2016 Red Hat, Inc. 
  3  # 
  4  # This software is licensed to you under the GNU General Public License, 
  5  # version 2 (GPLv2). There is NO WARRANTY for this software, express or 
  6  # implied, including the implied warranties of MERCHANTABILITY or FITNESS 
  7  # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 
  8  # along with this software; if not, see 
  9  # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 
 10  # 
 11  # Red Hat trademarks are not licensed under GPLv2. No permission is 
 12  # granted to use or replicate Red Hat trademarks that are incorporated 
 13  # in this software or its documentation. 
 14  # 
 15   
 16  import os 
 17  import sys 
 18  from config_common import handler_base, utils, cfg_exceptions 
 19  from config_common.rhn_log import log_debug, die, log_error 
 20   
21 -class Handler(handler_base.HandlerBase):
22 _usage_options = "[options] file [ file ... ]" 23 24 is_first_revision=1 25 26 _options_table = handler_base.HandlerBase._options_table + [ 27 handler_base.HandlerBase._option_class( 28 '-c', '--channel', action="store", 29 help="Upload files in this config channel", 30 ), 31 handler_base.HandlerBase._option_class( 32 '-d', '--dest-file', action="store", 33 help="Upload the file as this path", 34 ), 35 handler_base.HandlerBase._option_class( 36 '-t', '--topdir', action="store", 37 help="Make all files relative to this string", 38 ), 39 handler_base.HandlerBase._option_class( 40 '--delim-start', action="store", 41 help="Start delimiter for variable interpolation", 42 ), 43 handler_base.HandlerBase._option_class( 44 '--delim-end', action="store", 45 help="End delimiter for variable interpolation", 46 ), 47 handler_base.HandlerBase._option_class( 48 '-i', '--ignore-missing', action="store_true", 49 help="Ignore missing local files", 50 ), 51 handler_base.HandlerBase._option_class( 52 '--selinux-context', action="store", 53 help="Overwrite the SELinux context", 54 ), 55 ] 56
57 - def run(self):
58 log_debug(2) 59 60 if self.options.dest_file and self.options.topdir: 61 die(6, "Error: conflicting options --dest-file and --topdir") 62 63 if len(self.args) == 0: 64 die(0, "No files supplied (use --help for help)") 65 66 channel = self.options.channel 67 68 if not channel: 69 die(6, "Config channel not specified") 70 71 r = self.repository 72 if not r.config_channel_exists(channel): 73 die(6, "Error: config channel %s does not exist" % channel) 74 75 files = [utils.normalize_path(x) for x in self.args] 76 files_to_push = [] 77 if self.options.dest_file: 78 if len(files) != 1: 79 die(7, "--dest-file accepts a single file") 80 if not (self.options.dest_file[0] == os.sep): 81 die(7, "--dest-file argument must begin with " + os.sep) 82 files_to_push.append((files[0], self.options.dest_file)) 83 elif self.options.topdir: 84 if not os.path.isdir(self.options.topdir): 85 die(8, "--topdir specified, but `%s' not a directory" % 86 self.options.topdir) 87 88 #5/11/05 wregglej - 141790 remove the trailing slash from topdir 89 self.options.topdir = utils.rm_trailing_slash(self.options.topdir) 90 91 for f in files: 92 if not f.startswith(self.options.topdir): 93 die(8, "--topdir %s specified, but file `%s' doesn't comply" 94 % (self.options.topdir, f)) 95 files_to_push.append((f, f[len(self.options.topdir):])) 96 else: 97 for f in files: 98 #if a file is given w/o a full path, then use the abspath of the 99 #file as name of the file to be uploaded into the channel 100 if not (f[0] == os.sep): 101 files_to_push.append((f, os.path.abspath(f))) 102 else: 103 files_to_push.append((f, f)) 104 105 for (local_file, remote_file) in files_to_push: 106 if not os.path.exists(local_file): 107 if self.options.ignore_missing: 108 files_to_push.remove((local_file,remote_file)) 109 print("Local file %s does not exist. Ignoring file..." %(local_file)) 110 else: 111 die(9, "No such file `%s'" % local_file) 112 113 print("Pushing to channel %s:" % (channel, )) 114 115 delim_start = self.options.delim_start 116 delim_end = self.options.delim_end 117 118 selinux_ctx = None 119 if type(self.options.selinux_context) != None: 120 selinux_ctx = self.options.selinux_context 121 122 for (local_file, remote_file) in files_to_push: 123 try: 124 r.put_file(channel, remote_file, local_file, 125 is_first_revision=self.is_first_revision, 126 delim_start=delim_start, 127 delim_end=delim_end, 128 selinux_ctx=selinux_ctx) 129 except cfg_exceptions.RepositoryFileExistsError: 130 e = sys.exc_info()[1] 131 log_error("Error: %s is already in channel %s" % 132 (remote_file, channel)) 133 except cfg_exceptions.RepositoryFilePushError: 134 e = sys.exc_info()[1] 135 log_error("Error pushing file: %s" % e) 136 else: 137 print("Local file %s -> remote file %s" % (local_file, remote_file))
138