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

Source Code for Module config_management.rhncfg_upload_channel

  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   
 18  from config_common import handler_base, utils, cfg_exceptions 
 19  from config_common.rhn_log import log_debug, log_error, die 
 20   
21 -class Handler(handler_base.HandlerBase):
22 _usage_options = "[options] [ config_channel ... ]" 23 24 _options_table = handler_base.HandlerBase._options_table + [ 25 handler_base.HandlerBase._option_class( 26 '-t', '--topdir', 27 action="store", 28 default="./", 29 help="Directory all the file paths are relative to", 30 ), 31 handler_base.HandlerBase._option_class( 32 '-c', '--channel', 33 action='store', 34 default=None, 35 help="List of channels the config info will be uploaded into. Channels delimited by ','.\nExample: --channel=foo,bar,baz", 36 ), 37 handler_base.HandlerBase._option_class( 38 '', '--disable-selinux', 39 action='store_true', 40 default=None, 41 help="Don't upload SELinux contexts", 42 ), 43 ] 44
45 - def run(self):
46 log_debug(2) 47 #5/12/05 wregglej - 149034 changed r into a instance variable 48 self.r = self.repository 49 50 topdir = self.options.topdir 51 if not topdir: 52 die(7, "--topdir not specified") 53 54 if not os.path.isdir(topdir): 55 die(8, "--topdir specified, but `%s' not a directory" % 56 topdir) 57 58 topdir = utils.normalize_path(topdir) 59 60 #5/12/05 wregglej - 149034 allowing the channel name and the directory name to vary independently. 61 if not self.options.channel is None: 62 #Get the list of channels with leading and trailing whitespace removed. 63 channels = [x.strip() for x in self.options.channel.split(',') if x] 64 65 #Get the list of directories to upload. At this point it's the list of arguments. 66 dirs = self.args 67 elif not self.args: 68 #If we get to this point, then --channel wasn't used and nothing was included as arguments. 69 #Assumes that the directories in topdir are the ones we want to upload, and since no channels were 70 #specified that each directory is it's own channel. 71 channels = os.listdir(topdir) 72 dirs = None 73 print("No config channels specified, using %s" % channels) 74 else: 75 #At this point, --channel wasn't used but there was something included as an argument. 76 #The name of the channel is assumed to be the same as the name of the directory. 77 channels = self.args 78 dirs = None 79 80 #If dirs isn't None, then each directory needs to be uploaded into each channel. 81 if dirs: 82 for channel in channels: 83 for directory in dirs: 84 self.upload_config_channel(topdir, channel, directory) 85 #If dirs is None, then each channel is it's own channel. 86 else: 87 for channel in channels: 88 self.upload_config_channel(topdir, channel, channel)
89
90 - def upload_config_channel(self, topdir, channel, directory_name):
91 if not self.r.config_channel_exists(channel): 92 die(6, "Error: config channel %s does not exist" % channel) 93 94 if self.options.disable_selinux: 95 selinux_ctx = '' 96 else: 97 selinux_ctx = None 98 99 print("Using config channel %s" % channel) 100 101 channel_dir = utils.join_path(topdir, directory_name) 102 103 if not os.path.exists(channel_dir): 104 die(6, "Error: channel directory %s does not exist" % channel_dir) 105 106 flist = list_files_recursive(channel_dir) 107 108 for (dirname, filenames) in flist: 109 assert dirname.startswith(channel_dir) 110 remote_dirname = dirname[len(channel_dir):] 111 112 for f in filenames: 113 local_file = utils.join_path(dirname, f) 114 remote_file = utils.join_path(remote_dirname, f) 115 116 print("Uploading %s from %s" % (remote_file, local_file)) 117 try: 118 self.r.put_file(channel, remote_file, local_file, is_first_revision=0, 119 selinux_ctx = selinux_ctx) 120 except cfg_exceptions.RepositoryFilePushError: 121 e = sys.exc_info()[1] 122 log_error(e)
123 124 128
129 -def list_files_recursive(d):
130 file_list = [] 131 for dirname, dirs, names in os.walk(d): 132 file_list.append((dirname, filter(lambda x, d=dirname: is_file_or_link(d, x), 133 names))) 134 return file_list
135