예제 #1
0
    def test_option_invalid_nodeset(self):
        '''Test if nodeset/group source is invalid'''
        mop = McOptionParser()
        mop.configure_mop()
        self.assertRaises(InvalidOptionError, mop.parse_args,
                                    ['status', '-n', '@bad:group'])

        mop = McOptionParser()
        mop.configure_mop()
        self.assertRaises(InvalidOptionError, mop.parse_args,
                                    ['status', '-n', 'bad_node[set'])
예제 #2
0
    def test_option_configdir(self):
        '''Test usage of the configdir option'''
        mop = McOptionParser()
        mop.configure_mop()
        (options, args) = \
            mop.parse_args(['-c', '/usr/bin'])
        self.assertEqual(options.config_dir, '/usr/bin')

        mop = McOptionParser()
        mop.configure_mop()
        self.assertRaises(InvalidOptionError,
            mop.parse_args, ['-c', '/duke/'])
예제 #3
0
    def test_debug_config(self):
        '''Test configuration of the debug mode'''
        mop = McOptionParser()
        mop.configure_mop()
        (options, args) = mop.parse_args(['-d'])
        self.assertEqual(options.verbosity, 5)
        self.assertTrue(options.debug)

        mop = McOptionParser()
        mop.configure_mop()
        (options, args) = mop.parse_args(['-d'])
        self.assertEqual(options.verbosity, 5)
        self.assertTrue(options.debug)

        mop = McOptionParser()
        mop.configure_mop()
        (options, args) = mop.parse_args(['-vvv'])
        self.assertEqual(options.verbosity, 4)
        self.assertFalse(options.debug)
예제 #4
0
    def test_option_onlynodes(self):
        '''Test usage of the only-nodes option'''
        mop = McOptionParser()
        mop.configure_mop()
        (options, args) = mop.parse_args(['-n', 'foo8'])
        self.assertTrue('foo8' in options.only_nodes)

        mop = McOptionParser()
        mop.configure_mop()
        (options, args) = \
            mop.parse_args(['service', 'start', '-n', 'foo1,foo2'])
        self.assertTrue(isinstance(options.only_nodes, NodeSet))
        self.assertTrue('foo1' in options.only_nodes)
        self.assertTrue('foo2' in options.only_nodes)
        self.assertTrue('service' in args and 'start' in args)

        mop = McOptionParser()
        mop.configure_mop()
        self.assertRaises(InvalidOptionError,
            mop.parse_args, ['service', 'start','-n', '[foo5]'])
예제 #5
0
    def test_option_excluded_nodes(self):
        '''Test usage of the excluded_nodes option'''
        mop = McOptionParser()
        mop.configure_mop()
        (options, args) = \
        mop.parse_args(['service', 'start',
            '-n', 'foo[8-15]', '-x', 'foo[8-12]'])
        self.assertTrue('foo[13-15]' in options.only_nodes)
        self.assertFalse('foo[8-9]'  in options.only_nodes)
        self.assertTrue('foo[8-12]' in options.excluded_nodes)

        mop.parse_args(['service', 'start',
            '-x', 'foo[8-12]', '-n', 'foo[8-15]'])
        self.assertTrue('foo[13-15]' in options.only_nodes)
        self.assertFalse('foo[8-9]'  in options.only_nodes)
        self.assertTrue('foo[8-12]' in options.excluded_nodes)
예제 #6
0
 def test_option_version(self):
     '''Test usage of option --version'''
     mop = McOptionParser()
     mop.configure_mop()
     self.assertRaises(SystemExit, mop.parse_args, ['--version'])
예제 #7
0
 def test_instanciation(self):
     '''Test creation of an McOptionParser'''
     self.assertTrue(McOptionParser())
예제 #8
0
 def setUp(self):
     self.mop = McOptionParser()
     self.mop.configure_mop()
예제 #9
0
    def execute(self, command_line):
        '''
        Ask for the manager to execute orders given by the command line.
        '''
        self._mop = McOptionParser()
        self._mop.configure_mop()
        retcode = RC_OK

        try:
            (self._options, self._args) = self._mop.parse_args(command_line)

            self._conf = ConfigParser(self._options)

            # Configure ActionManager
            action_manager_self().default_fanout = self._conf['fanout']
            action_manager_self().dryrun = self._conf['dryrun']

            self.manager = self.manager or ServiceManager()
            # Case 0: build the graph
            if self._conf.get('graph', False):
                self.manager.load_config(self._conf['config_dir'])
                # Deps graph generation
                self._console.output(
                    self.manager.output_graph(
                        self._args, self._conf.get('excluded_svc', [])))
            # Case 1 : call services referenced in the manager with
            # the required action
            elif self._args:
                # Compute all services with the required action
                services = self._args[:-1]
                action = self._args[-1]

                # Ask for confirmation if the configuration requests it.
                if action in self._conf['confirm_actions'] and \
                   not self._conf['assumeyes'] and \
                   not Terminal.confirm("Are you sure to run %s action?" % action):
                    raise UserError('Execution aborted by user')

                # Create a thread in interactive mode to manage
                # current running status
                if self.interactive:
                    self.inter_thread.start()

                # Run tasks
                self.manager.call_services(services, action, conf=self._conf)
                retcode = self.retcode()

                if self._conf.get('report', 'no').lower() != 'no':
                    r_type = self._conf.get('report', 'default')
                    self._console.print_summary(self.actions, report=r_type)

            # Case 2 : Check configuration
            elif self._conf.get('config_dir', False):
                self._console.output("No actions specified, "
                                     "checking configuration...")
                self.manager.load_config(self._conf['config_dir'])
                self._console.output("%s seems good" %
                                     self._conf['config_dir'])
            # Case 3: Nothing to do so just print MilkCheck help
            else:
                self._mop.print_help()
        except (ServiceNotFoundError, ActionNotFoundError,
                InvalidVariableError, UndefinedVariableError,
                VariableAlreadyExistError, DependencyAlreadyReferenced,
                UnknownDependencyError, IllegalDependencyTypeError,
                ConfigError, ScannerError, UserError) as exc:
            self._logger.error(str(exc))
            retcode = RC_EXCEPTION
        except InvalidOptionError as exc:
            self._logger.critical('Invalid options: %s\n' % exc)
            self._mop.print_help()
            retcode = RC_EXCEPTION
        except KeyboardInterrupt as exc:
            self._logger.error('Keyboard Interrupt')
            retcode = (128 + SIGINT)
        except ScannerError as exc:
            self._logger.error('Bad syntax in config file :\n%s' % exc)
            retcode = RC_EXCEPTION
        except ImportError as exc:
            self._logger.error('Missing python dependency: %s' % exc)
            for line in traceback.format_exc().splitlines()[-3:-1]:
                self._logger.error(line)
            retcode = RC_EXCEPTION
        except Exception as exc:
            # In high verbosity mode, propagate the error
            if (not self._conf or self._conf.get('verbosity') >= 5):
                traceback.print_exc(file=sys.stdout)
            else:
                self._logger.error('Unexpected Exception : %s' % exc)
            retcode = RC_UNKNOWN_EXCEPTION

        # Quit the interactive thread
        self.inter_thread.quit()
        self.inter_thread.join()

        return retcode