def test_sm_config_cmd(self): os.environ = {} sys.argv = ["virt-who", "--sam", "--libvirt"] logger, options = parseOptions() config = Config("env/cmdline", options.virtType, defaults={}, **options) config.checkOptions(logger) manager = Manager.fromOptions(logger, options, config) self.assertTrue(isinstance(manager, SubscriptionManager))
def test_sm_config_env(self): os.environ = { "VIRTWHO_SAM": '1', "VIRTWHO_LIBVIRT": '1' } sys.argv = ["virt-who"] logger, options = parseOptions() config = Config("env/cmdline", options.virtType, defaults={}, **options) config.checkOptions(logger) manager = Manager.fromOptions(logger, options, config) self.assertTrue(isinstance(manager, SubscriptionManager))
def test_satellite_config_cmd(self): os.environ = {} sys.argv = ["virt-who", "--satellite", "--satellite-server=sat.example.com", "--satellite-username=username", "--satellite-password=password", "--libvirt"] logger, options = parseOptions() config = Config("env/cmdline", options.virtType, defaults={}, **options) config.checkOptions(logger) manager = Manager.fromOptions(logger, options, config) self.assertTrue(isinstance(manager, Satellite))
def test_satellite_config_env(self): os.environ = { "VIRTWHO_SATELLITE": '1', "VIRTWHO_SATELLITE_SERVER": 'sat.example.com', "VIRTWHO_SATELLITE_USERNAME": '******', "VIRTWHO_SATELLITE_PASSWORD": '******', "VIRTWHO_LIBVIRT": '1' } sys.argv = ["virt-who"] logger, options = parseOptions() config = Config("env/cmdline", options.virtType, defaults={}, **options) config.checkOptions(logger) manager = Manager.fromOptions(logger, options, config) self.assertTrue(isinstance(manager, Satellite))
def test_satellite_config_cmd(self): os.environ = {} sys.argv = [ "virt-who", "--satellite", "--satellite-server=sat.example.com", "--satellite-username=username", "--satellite-password=password", "--libvirt" ] logger, options = parseOptions() config = Config("env/cmdline", options.virtType, defaults={}, **options) config.checkOptions(logger) manager = Manager.fromOptions(logger, options, config) self.assertTrue(isinstance(manager, Satellite))
def main(): logger = options = None try: logger, options = parseOptions() except OptionError as e: print >>sys.stderr, str(e) exit(1, status="virt-who can't be started: %s" % str(e)) lock = PIDLock(PIDFILE) if lock.is_locked(): msg = "virt-who seems to be already running. If not, remove %s" % PIDFILE print >>sys.stderr, msg exit(1, status=msg) global executor try: executor = Executor(logger, options) except (InvalidKeyFile, InvalidPasswordFormat) as e: logger.error(str(e)) exit(1, "virt-who can't be started: %s" % str(e)) if options.virtType is not None: config = Config("env/cmdline", options.virtType, executor.configManager._defaults, **options) try: config.checkOptions(logger) except InvalidOption as e: err = "virt-who can't be started: %s" % str(e) logger.error(err) exit(1, err) executor.configManager.addConfig(config) has_error = False for conffile in options.configs: try: executor.configManager.readFile(conffile) except InvalidPasswordFormat as e: err = "virt-who can't be started: %s" % str(e) logger.error(err) exit(1, err) except Exception as e: logger.error('Config file "%s" skipped because of an error: %s', conffile, str(e)) has_error = True if len(executor.configManager.configs) == 0: if has_error: err = "virt-who can't be started: no valid configuration found" logger.error(err) exit(1, err) # In order to keep compatibility with older releases of virt-who, # fallback to using libvirt as default virt backend logger.info("No configurations found, using libvirt as backend") executor.configManager.addConfig(Config("env/cmdline", "libvirt")) for config in executor.configManager.configs: if config.name is None: logger.info('Using commandline or sysconfig configuration ("%s" mode)', config.type) else: logger.info('Using configuration "%s" ("%s" mode)', config.name, config.type) logger.info("Using reporter_id='%s'", options.reporter_id) log.closeLogger(logger) if options.background: locker = lambda: daemon.DaemonContext(pidfile=lock) # flake8: noqa else: locker = lambda: lock # flake8: noqa with locker(): signal.signal(signal.SIGHUP, reload) signal.signal(signal.SIGTERM, atexit_fn) executor.logger = logger = log.getLogger(name='main', config=None, queue=True) sd_notify("READY=1\nMAINPID=%d" % os.getpid()) while True: try: return _main(executor) except ReloadRequest: logger.info("Reloading") continue