def test_0040_check_invalid_installation(self):
     """Tests cse check against config files that are invalid/have not been
     used for installation.
     """
     try:
         check_cse_installation(self._config)
         print("cse check passed when it should have failed.")
         assert False
     except EntityNotFoundException:
         pass
def check(ctx, config, check_install, template):
    """Validate CSE configuration."""
    try:
        config_dict = get_validated_config(config)
    except (KeyError, ValueError, Exception):
        # TODO replace Exception with specific (see validate_amqp_config)
        click.secho(f"Config file '{config}' is invalid", fg='red')
        return
    if check_install:
        try:
            check_cse_installation(config_dict, check_template=template)
        except EntityNotFoundException:
            click.secho("CSE installation is invalid", fg='red')
Example #3
0
    def run(self):
        self.config = get_validated_config(self.config_file)
        if self.should_check_config:
            check_cse_installation(self.config)

        log_file = 'cse.log'
        handler = RotatingFileHandler(log_file,
                                      maxBytes=10 * 1024 * 1024,
                                      backupCount=10)
        logging.basicConfig(level=logging.DEBUG,
                            format='%(asctime)s %(name)-12s %(lineno)s '
                            '%(levelname)-8s %(message)s',
                            datefmt='%m-%d %H:%M:%S',
                            handlers=(handler, ))
        logging.getLogger("pika").setLevel(logging.WARNING)

        message = """
Container Service Extension for vCloud Director running
config file: {config_file}
see file log file for details: {log_file}
waiting for requests, press Ctrl+C to finish
""".format(config_file=self.config_file, log_file=log_file)

        signal.signal(signal.SIGINT, signal_handler)
        click.secho(message)
        LOGGER.info(message)

        amqp = self.config['amqp']
        num_consumers = self.config['service']['listeners']

        for n in range(num_consumers):
            try:
                c = MessageConsumer(amqp['host'], amqp['port'], amqp['ssl'],
                                    amqp['vhost'], amqp['username'],
                                    amqp['password'], amqp['exchange'],
                                    amqp['routing_key'], self.config,
                                    self.config['vcd']['verify'],
                                    self.config['vcd']['log'])
                name = 'MessageConsumer-%s' % n
                t = Thread(name=name, target=consumer_thread, args=(c, ))
                t.daemon = True
                t.start()
                LOGGER.info('started thread %s', t.ident)
                self.threads.append(t)
                self.consumers.append(c)
                time.sleep(0.25)
            except KeyboardInterrupt:
                break
            except Exception:
                print(traceback.format_exc())

        LOGGER.info('num of threads started: %s', len(self.threads))

        self.is_enabled = True

        while True:
            try:
                time.sleep(1)
                if self.should_stop and self.active_requests_count() == 0:
                    break
            except KeyboardInterrupt:
                break
            except Exception:
                click.secho(traceback.format_exc())
                sys.exit(1)

        LOGGER.info('stop detected')
        LOGGER.info('closing connections...')
        for c in self.consumers:
            try:
                c.stop()
            except Exception:
                pass
        LOGGER.info('done')
Example #4
0
    def run(self):
        self.config = get_validated_config(self.config_file)
        if self.should_check_config:
            check_cse_installation(self.config)

        configure_server_logger()

        message = f"Container Service Extension for vCloudDirector" \
                  f"\nServer running using config file: {self.config_file}" \
                  f"\nLog files: {SERVER_INFO_LOG_FILEPATH}, " \
                  f"{SERVER_DEBUG_LOG_FILEPATH}" \
                  f"\nwaiting for requests (ctrl+c to close)"

        signal.signal(signal.SIGINT, signal_handler)
        click.secho(message)
        LOGGER.info(message)

        amqp = self.config['amqp']
        num_consumers = self.config['service']['listeners']

        for n in range(num_consumers):
            try:
                c = MessageConsumer(amqp['host'], amqp['port'], amqp['ssl'],
                                    amqp['vhost'], amqp['username'],
                                    amqp['password'], amqp['exchange'],
                                    amqp['routing_key'], self.config,
                                    self.config['vcd']['verify'],
                                    self.config['vcd']['log'])
                name = 'MessageConsumer-%s' % n
                t = Thread(name=name, target=consumer_thread, args=(c, ))
                t.daemon = True
                t.start()
                LOGGER.info('started thread %s', t.ident)
                self.threads.append(t)
                self.consumers.append(c)
                time.sleep(0.25)
            except KeyboardInterrupt:
                break
            except Exception:
                print(traceback.format_exc())

        LOGGER.info('num of threads started: %s', len(self.threads))

        self.is_enabled = True

        while True:
            try:
                time.sleep(1)
                if self.should_stop and self.active_requests_count() == 0:
                    break
            except KeyboardInterrupt:
                break
            except Exception:
                click.secho(traceback.format_exc())
                sys.exit(1)

        LOGGER.info('stop detected')
        LOGGER.info('closing connections...')
        for c in self.consumers:
            try:
                c.stop()
            except Exception:
                pass
        LOGGER.info('done')
    def test_0080_install_cleanup_true(self):
        """Tests that installation deletes temp vapps when 'cleanup' is True.
        Tests that '--amqp/--ext config' configures vcd amqp and registers cse.

        command: cse install --config cse_test_config.yaml
        expected: temp vapps are deleted
        """
        for template_config in self._config['broker']['templates']:
            assert template_config['cleanup']

        result = self._runner.invoke(cli, [
            'install', '--config', ACTIVE_CONFIG_FILEPATH, '--amqp', 'config',
            '--ext', 'config'
        ],
                                     catch_exceptions=False)
        assert result.exit_code == 0

        # reloads required due to inability to wait for catalog deletion.
        # vdc can't find vapp unless we reload
        self._org.reload()
        self._vdc.reload()

        # check that amqp was configured
        assert not diff_amqp_settings(self._amqp_service, self._config['amqp'])

        # check that cse was registered
        try:
            self._api_extension.get_extension(CSE_NAME,
                                              namespace=CSE_NAMESPACE)
        except MissingRecordException:
            print('CSE is not registered as an extension when it should be.')
            assert False

        for template_config in self._config['broker']['templates']:
            # check that vapp template exists
            try:
                self._org.get_catalog_item(self._config['broker']['catalog'],
                                           template_config['catalog_item'])
            except EntityNotFoundException:
                print('vApp template does not exist when it should')
                assert False

            # check that temp vapp does not exist (cleanup: true)
            try:
                self._vdc.get_vapp(template_config['temp_vapp'])
                print('Temp vapp should not exist (cleanup: True')
                assert False
            except EntityNotFoundException:
                pass

        # sub-test to make sure `cse check` works for valid installation
        try:
            check_cse_installation(self._config)
        except EntityNotFoundException:
            print("cse check failed when it should have passed.")
            assert False

        # sub-test to make sure `cse check` fails for config file with
        # invalid templates.
        # change config file to make template names invalid
        for i, template_dict in enumerate(self._config['broker']['templates']):
            self._config['broker']['templates'][i]['catalog_item'] = f"_bad{i}"

        try:
            check_cse_installation(self._config)
            print("cse check passed when it should have failed.")
            assert False
        except EntityNotFoundException:
            pass