Package config_common :: Module rhn_main
[hide private]
[frames] | no frames]

Source Code for Module config_common.rhn_main

  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   
 19  from config_common import local_config 
 20  from config_common import rhn_log 
 21  from config_common import utils 
 22  from config_common import cfg_exceptions 
 23   
 24  try: 
 25      # python3 
 26      from urllib.parse import urlsplit 
 27      from configparser import InterpolationError 
 28  except ImportError: #python2 
 29      from urlparse import urlsplit 
 30      from ConfigParser import InterpolationError 
 31   
 32  try: 
 33      from socket import gaierror 
 34  except: 
 35      from socket import error 
 36      gaierror = error 
 37   
 38  from up2date_client import config 
 39   
40 -class BaseMain:
41 modes = [] 42 repository_class_name = "Repository" 43 plugins_dir = 'config_common' 44 mode_prefix = None 45 config_section = None 46
47 - def usage(self, exit_code):
48 print("Usage: %s MODE [ --config config_file ] [ --server-name name ] [ params ]" % sys.argv[0]) 49 print("Valid modes are:") 50 for mode in self.modes: 51 print("\t%s" % mode) 52 sys.exit(exit_code)
53
54 - def main(self):
55 args = [] 56 57 show_help = None 58 debug_level = 3 59 mode = None 60 61 dict_name_opt={'--server-name': None,'--password': None,'--username': None, '--config': None,} 62 for index in range(1,len(sys.argv)): 63 arg=sys.argv[index] 64 param = [x for x in dict_name_opt.items() if x[1] == 0] 65 if param: 66 if arg.startswith('-') or arg in self.modes: 67 # not perfect, but at least a little bit better 68 print("Option %s requires an argument" % dict_name_opt[param[0][0]]) 69 return 1 70 dict_name_opt[param[0][0]] = arg 71 continue 72 73 if arg in ('--help', '-h'): 74 show_help = 1 75 continue 76 77 param = [s for s in dict_name_opt.keys() if arg.startswith(s)] 78 if param: 79 rarg = arg[len(param[0]):] 80 if not rarg: 81 dict_name_opt[param[0]] = 0 82 if index == len(sys.argv) - 1: 83 print("Option %s requires an argument" % param[0]) 84 return 1 85 continue 86 if rarg[0] == '=': 87 if len(rarg) == 1: 88 print("Option %s requires an argument" % param[0]) 89 return 1 90 91 dict_name_opt[param[0]] = rarg[1:] 92 continue 93 print("Unknown option %s" % arg) 94 return 1 95 96 if mode is None: 97 # This should be the mode 98 mode = arg 99 if mode == '': 100 # Bad 101 self.usage(1) 102 103 if mode[0] == '-': 104 # Mode can't be an option 105 self.usage(1) 106 107 if mode not in self.modes: 108 print("Unknown mode %s" % mode) 109 self.usage(1) 110 111 continue 112 113 args.append(arg) 114 115 server_name = dict_name_opt['--server-name'] 116 password = dict_name_opt['--password'] 117 username = dict_name_opt['--username'] 118 config_file_override = dict_name_opt['--config'] 119 120 if config_file_override and not os.path.isfile(config_file_override): 121 rhn_log.die(1, "Config file %s does not exist.", config_file_override) 122 123 rhn_log.set_debug_level(debug_level) 124 125 if mode is None: 126 # No args were specified 127 self.usage(0) 128 129 execname = os.path.basename(sys.argv[0]) 130 # Class names cannot have dot in them, so strip the extension 131 execname = execname.split('.', 1)[0] 132 133 mode_module = mode.replace('-', '_') 134 module_name = "%s_%s" % (self.mode_prefix, mode_module) 135 full_module_name = "%s.%s" % (self.plugins_dir, module_name) 136 137 try: 138 module = __import__(full_module_name) 139 except ImportError: 140 e = sys.exc_info()[1] 141 rhn_log.die(1, "Unable to load plugin for mode '%s': %s" % (mode, e)) 142 143 module = getattr(module, module_name) 144 145 if show_help: 146 # Display the per-module help 147 handler = module.Handler(args, None, mode=mode, exec_name=execname) 148 handler.usage() 149 return 0 150 151 cfg = config.initUp2dateConfig() 152 up2date_cfg = dict(cfg.items()) 153 154 if server_name: 155 local_config.init(self.config_section, defaults=up2date_cfg, config_file_override=config_file_override, server_url="https://" + server_name) 156 else: 157 local_config.init(self.config_section, defaults=up2date_cfg, config_file_override=config_file_override) 158 159 try: 160 server_name = local_config.get('server_url') 161 except InterpolationError: 162 e = sys.exc_info()[1] 163 if e.option == 'server_url': 164 server_name = config.getServerlURL() 165 up2date_cfg['proto'] = urlsplit(server_name[0])[0] 166 if up2date_cfg['proto'] == '': 167 up2date_cfg['proto'] = 'https' 168 up2date_cfg['server_list'] = [urlsplit(x)[2] for x in server_name] 169 else: 170 up2date_cfg['server_list'] = [urlsplit(x)[1] for x in server_name] 171 server_name = (up2date_cfg['server_list'])[0] 172 local_config.init(self.config_section, defaults=up2date_cfg, config_file_override=config_file_override, server_name=server_name) 173 174 print("Using server name %s" % server_name) 175 176 # set the debug level through the config 177 rhn_log.set_debug_level(int(local_config.get('debug_level') or 0)) 178 rhn_log.set_logfile(local_config.get('logfile') or "/var/log/rhncfg") 179 180 # Multi-level import - __import__("foo.bar") does not return the bar 181 # module, but the foo module with bar loaded 182 # XXX Error checking 183 repo_class = local_config.get('repository_type') 184 if repo_class is None: 185 rhn_log.die(1, "repository_type not set (missing configuration file?)") 186 187 repo_module_name = "%s.%s" % (self.plugins_dir, repo_class) 188 try: 189 repo_module = __import__(repo_module_name) 190 except ImportError: 191 e = sys.exc_info()[1] 192 rhn_log.die(1, "Unable to load repository module: %s" % e) 193 194 try: 195 repo_module = getattr(repo_module, repo_class) 196 except AttributeError: 197 rhn_log.die(1, "Malformed repository module") 198 199 try: 200 repo = getattr(repo_module, self.repository_class_name)() 201 except AttributeError: 202 rhn_log.die(1, "Missing repository class") 203 except InterpolationError: 204 e = sys.exc_info()[1] 205 if e.option == 'server_url': 206 #pkilambi: bug#179367# backtic is replaced with single quote 207 rhn_log.die(1, "Missing 'server_url' configuration variable; please refer to the config file") 208 raise 209 except cfg_exceptions.ConfigurationError: 210 e = sys.exc_info()[1] 211 rhn_log.die(e) 212 except gaierror: 213 e = sys.exc_info()[1] 214 print("Socket Error: %s" % (e.args[1],)) 215 sys.exit(1) 216 217 handler = module.Handler(args, repo, mode=mode, exec_name=execname) 218 try: 219 try: 220 handler.authenticate(username, password) 221 handler.run() 222 except cfg_exceptions.AuthenticationError: 223 e = sys.exc_info()[1] 224 rhn_log.die(1, "Authentication failed: %s" % e) 225 except Exception: 226 raise 227 finally: 228 repo.cleanup() 229 return 0
230