Ejemplo n.º 1
0
    def cluster_update_task(self, brokers, broker_ca, managers, manager_ca):
        """Update the running agent with the new cluster.

        When a node is added or removed from the cluster, the agent will
        receive the current cluster nodes in this task. We need to update
        both the current process envvars, the cert files, and all the
        daemon config files.
        """
        if not self.name:
            raise RuntimeError('cluster-update sent to agent with no name set')
        factory = DaemonFactory()
        daemon = factory.load(self.name)

        os.environ[constants.REST_HOST_KEY] = ','.join(managers)

        with open(daemon.local_rest_cert_file, 'w') as f:
            f.write(manager_ca)
        with open(daemon.broker_ssl_cert_path, 'w') as f:
            f.write(broker_ca)

        daemon.rest_host = managers
        daemon.broker_ip = brokers
        daemon.create_broker_conf()
        daemon.create_config()

        factory.save(daemon)
Ejemplo n.º 2
0
def create(**params):

    """
    Creates and stores the daemon parameters.

    """
    attributes = dict(**params)
    attributes.update(_parse_security_settings(attributes))
    custom_arg = attributes.pop('custom_options', ())
    attributes.update(_parse_custom_options(custom_arg))

    click.echo('Creating...')

    _create_rest_ssl_cert(attributes)
    # _create_rest_ssl_cert called before get_broker_configuration because it
    # might be required for the rest call to succeed
    if attributes['broker_get_settings_from_manager']:
        broker = api_utils.internal.get_broker_configuration(attributes)
        attributes.update(broker)
    # _create_broker_ssl_cert called after get_broker_configuration because the
    # cert might be retrieved there
    _create_broker_ssl_cert(attributes)

    from cloudify_agent.shell.main import get_logger
    daemon = DaemonFactory().new(
        logger=get_logger(),
        **attributes
    )

    daemon.create()
    _save_daemon(daemon)
    click.echo('Successfully created daemon: {0}'
               .format(daemon.name))
Ejemplo n.º 3
0
def create(**params):

    """
    Creates and stores the daemon parameters.

    """

    attributes = dict(**params)
    custom_arg = attributes.pop('custom_options', ())
    attributes.update(_parse_custom_options(custom_arg))
    click.echo('Creating...')
    from cloudify_agent.shell.main import get_logger

    if attributes['broker_get_settings_from_manager']:
        broker = api_utils.internal.get_broker_configuration(attributes)
        attributes.update(broker)

    daemon = DaemonFactory().new(
        logger=get_logger(),
        **attributes
    )

    daemon.create()
    _save_daemon(daemon)
    click.echo('Successfully created daemon: {0}'
               .format(daemon.name))
Ejemplo n.º 4
0
 def cluster_update(nodes):
     cluster.set_cluster_nodes(nodes)
     factory = DaemonFactory()
     daemon = factory.load(daemon_name)
     network_name = daemon.network
     daemon.cluster = [n['networks'][network_name] for n in nodes]
     factory.save(daemon)
Ejemplo n.º 5
0
 def __init__(self, tmp_path, logger, ssl_cert):
     self.daemons = []
     self.temp_folder = str(tmp_path)
     self.username = getpass.getuser()
     self.logger = logger
     self.rest_cert_path = ssl_cert.local_cert_path()
     self.factory = DaemonFactory()
     self.runner = LocalCommandRunner(logger=logger)
Ejemplo n.º 6
0
 def cluster_update_task(self, nodes):
     if not self.name:
         raise RuntimeError('cluster-update sent to agent with no name set')
     factory = DaemonFactory()
     daemon = factory.load(self.name)
     network_name = daemon.network
     nodes = [n['networks'][network_name] for n in nodes]
     daemon.cluster = nodes
     factory.save(daemon)
Ejemplo n.º 7
0
 def cluster_update_task(self, nodes):
     if not self.name:
         raise RuntimeError('cluster-update sent to agent with no name set')
     factory = DaemonFactory()
     daemon = factory.load(self.name)
     network_name = daemon.network
     nodes = [n['networks'][network_name] for n in nodes]
     daemon.cluster = nodes
     factory.save(daemon)
Ejemplo n.º 8
0
def _set_master(daemon_name, node_ip):
    factory = DaemonFactory()
    try:
        daemon = factory.load(daemon_name)
    except exceptions.DaemonNotFoundError:
        return
    daemon.broker_ip = node_ip
    factory.save(daemon)
    cluster.set_cluster_active(node_ip)
Ejemplo n.º 9
0
def _set_master(daemon_name, node):
    factory = DaemonFactory()
    try:
        daemon = factory.load(daemon_name)
    except exceptions.DaemonNotFoundError:
        return
    daemon.broker_ip = node['broker_ip']
    daemon.broker_ssl_cert_path = node.get('internal_cert_path')
    with _cluster_settings_lock(daemon_name):
        factory.save(daemon)
        cluster.set_cluster_active(node)
Ejemplo n.º 10
0
def create(**params):
    """
    Creates and stores the daemon parameters.

    """
    attributes = dict(**params)
    custom_arg = attributes.pop('custom_options', ())
    attributes.update(_parse_custom_options(custom_arg))

    click.echo('Creating...')
    from cloudify_agent.shell.main import get_logger
    daemon = DaemonFactory().new(logger=get_logger(), **attributes)
    daemon.create()
    _save_daemon(daemon)
    click.echo('Successfully created daemon: {0}'.format(daemon.name))
Ejemplo n.º 11
0
 def setUp(self):
     super(TestDaemonFactory, self).setUp()
     self.daemon_name = 'test-daemon-{0}'.format(uuid.uuid4())
     self.factory = DaemonFactory(storage=get_storage_directory())
     self.daemon_params = {
         'process_management': 'init.d',
         'name': self.daemon_name,
         'queue': 'queue',
         'rest_host': '127.0.0.1',
         'broker_ip': '127.0.0.1',
         'user': '******',
         'broker_url': '127.0.0.1',
         'broker_ssl_enabled': True,
         'local_rest_cert_file': self._rest_cert_path
     }
Ejemplo n.º 12
0
def restart(new_name=None, delay_period=5, **_):

    cloudify_agent = ctx.instance.runtime_properties['cloudify_agent']
    if new_name is None:
        new_name = utils.internal.generate_new_agent_name(
            cloudify_agent.get('name', 'agent'))

    # update agent name in runtime properties so that the workflow will
    # what the name of the worker handling tasks to this instance.
    # the update cannot be done by setting a nested property directly
    # because they are not recognized as 'dirty'
    cloudify_agent['name'] = new_name
    ctx.instance.runtime_properties['cloudify_agent'] = cloudify_agent

    # must update instance here because the process may shutdown before
    # the decorator has a chance to do it.
    ctx.instance.update()

    daemon = _load_daemon(logger=ctx.logger)

    # make the current master stop listening to the current queue
    # to avoid a situation where we have two masters listening on the
    # same queue.
    app = get_celery_app(tenant=cloudify_agent['rest_tenant'])
    app.control.cancel_consumer(queue=daemon.queue,
                                destination=['celery@{0}'.format(daemon.name)])

    # clone the current daemon to preserve all the attributes
    attributes = utils.internal.daemon_to_dict(daemon)

    # give the new daemon the new name
    attributes['name'] = new_name

    # remove the log file and pid file so that new ones will be created
    # for the new agent
    del attributes['log_file']
    del attributes['pid_file']

    # Get the broker credentials for the daemon
    attributes.update(ctx.bootstrap_context.broker_config())

    new_daemon = DaemonFactory().new(logger=ctx.logger, **attributes)

    # create the new daemon
    new_daemon.create()
    _save_daemon(new_daemon)

    # configure the new daemon
    new_daemon.configure()
    new_daemon.start()

    # start a thread that will kill the current master.
    # this is done in a thread so that the current task will not result in
    # a failure
    thread = threading.Thread(target=shutdown_current_master,
                              args=[delay_period, ctx.logger])
    thread.daemon = True
    thread.start()
Ejemplo n.º 13
0
    def _strategy(initial_brokers):
        logger.debug('Failover strategy: searching for a new rabbitmq server')
        initial_brokers = [broker for broker in initial_brokers if broker]
        brokers = itertools.cycle(initial_brokers)
        while True:
            if cluster.is_cluster_configured():
                nodes = cluster.get_cluster_nodes()
                for node_ip in nodes:
                    _set_master(daemon_name, node_ip)

                    daemon = DaemonFactory().load(daemon_name)

                    broker_url = 'amqp://{0}:{1}@{2}:{3}/{4}'.format(
                        daemon.broker_user, daemon.broker_pass, node_ip,
                        daemon.broker_port, daemon.broker_vhost)

                    logger.debug('Trying broker at {0}'.format(broker_url))
                    yield broker_url
            else:
                logger.debug('cluster config file does not exist')
                broker_url = next(brokers)
                if len(initial_brokers) > 1:
                    logger.debug('writing config file')
                    cluster.config_from_broker_urls(broker_url,
                                                    initial_brokers)
                    _set_master(daemon_name, cluster.get_cluster_active())
                logger.debug('Trying broker at {0}'.format(broker_url))
                yield broker_url
Ejemplo n.º 14
0
    def replace_ca_certs_task(self, new_manager_ca, new_broker_ca):
        """Update the running agent with new CAs."""
        self._assert_name('replace-ca-certs')
        factory = DaemonFactory()
        daemon = factory.load(self.name)

        if new_broker_ca:
            with open(daemon.broker_ssl_cert_path, 'w') as f:
                f.write(new_broker_ca)
            daemon.create_broker_conf()

        if new_manager_ca:
            with open(daemon.local_rest_cert_file, 'w') as f:
                f.write(new_manager_ca)
            daemon.create_config()

        factory.save(daemon)
Ejemplo n.º 15
0
def restart(new_name=None, delay_period=5, **_):

    cloudify_agent = ctx.instance.runtime_properties['cloudify_agent']
    if new_name is None:
        new_name = utils.internal.generate_new_agent_name(
            cloudify_agent.get('name', 'agent'))

    # update agent name in runtime properties so that the workflow will
    # what the name of the worker handling tasks to this instance.
    # the update cannot be done by setting a nested property directly
    # because they are not recognized as 'dirty'
    cloudify_agent['name'] = new_name
    ctx.instance.runtime_properties['cloudify_agent'] = cloudify_agent

    # must update instance here because the process may shutdown before
    # the decorator has a chance to do it.
    ctx.instance.update()

    daemon = _load_daemon(logger=ctx.logger)

    # make the current master stop listening to the current queue
    # to avoid a situation where we have two masters listening on the
    # same queue.
    app.control.cancel_consumer(
        queue=daemon.queue,
        destination=['celery@{0}'.format(daemon.name)]
    )

    # clone the current daemon to preserve all the attributes
    attributes = utils.internal.daemon_to_dict(daemon)

    # give the new daemon the new name
    attributes['name'] = new_name

    # remove the log file and pid file so that new ones will be created
    # for the new agent
    del attributes['log_file']
    del attributes['pid_file']

    # Get the broker credentials for the daemon
    attributes.update(ctx.bootstrap_context.broker_config())

    new_daemon = DaemonFactory().new(logger=ctx.logger, **attributes)

    # create the new daemon
    new_daemon.create()
    _save_daemon(new_daemon)

    # configure the new daemon
    new_daemon.configure()
    new_daemon.start()

    # start a thread that will kill the current master.
    # this is done in a thread so that the current task will not result in
    # a failure
    thread = threading.Thread(target=shutdown_current_master,
                              args=[delay_period, ctx.logger])
    thread.daemon = True
    thread.start()
Ejemplo n.º 16
0
def ls():
    """
    List all existing daemons.

    """

    from cloudify_agent.shell.main import get_logger
    daemons = DaemonFactory().load_all(logger=get_logger())
    for daemon in daemons:
        click.echo(daemon.name)
Ejemplo n.º 17
0
def delete(name):
    """
    Deletes the daemon.

    """

    click.echo('Deleting...')
    daemon = _load_daemon(name)
    daemon.delete()
    DaemonFactory().delete(name)
    click.echo('Successfully deleted daemon: {0}'.format(name))
Ejemplo n.º 18
0
def create(**params):

    """
    Creates and stores the daemon parameters.

    """
    attributes = dict(**params)
    custom_arg = attributes.pop('custom_options', ())
    attributes.update(_parse_custom_options(custom_arg))

    click.echo('Creating...')
    from cloudify_agent.shell.main import get_logger
    daemon = DaemonFactory().new(
        logger=get_logger(),
        **attributes
    )
    daemon.create()
    _save_daemon(daemon)
    click.echo('Successfully created daemon: {0}'
               .format(daemon.name))
Ejemplo n.º 19
0
def create(**params):
    """
    Creates and stores the daemon parameters.

    """
    attributes = dict(**params)
    custom_arg = attributes.pop('custom_options', ())
    attributes.update(_parse_custom_options(custom_arg))

    click.echo('Creating...')

    if attributes['broker_get_settings_from_manager']:
        broker = api_utils.internal.get_broker_configuration(attributes)
        attributes.update(broker)

    from cloudify_agent.shell.main import get_logger
    daemon = DaemonFactory().new(logger=get_logger(), **attributes)

    daemon.create()
    _save_daemon(daemon)
    click.echo('Successfully created daemon: {0}'.format(daemon.name))
Ejemplo n.º 20
0
def restart(new_name=None, delay_period=5, **_):

    cloudify_agent = ctx.instance.runtime_properties['cloudify_agent']
    if new_name is None:
        new_name = utils.internal.generate_new_agent_name(
            cloudify_agent.get('name', 'agent'))

    # update agent name in runtime properties so that the workflow will
    # what the name of the worker handling tasks to this instance.
    # the update cannot be done by setting a nested property directly
    # because they are not recognized as 'dirty'
    cloudify_agent['name'] = new_name
    update_agent_runtime_properties(cloudify_agent)

    daemon = _load_daemon(logger=ctx.logger)

    # make the current master stop listening to the current queue
    # to avoid a situation where we have two masters listening on the
    # same queue.
    app = get_celery_app(tenant=ctx.tenant)
    app.control.cancel_consumer(queue=daemon.queue,
                                destination=['celery@{0}'.format(daemon.name)])

    # clone the current daemon to preserve all the attributes
    attributes = utils.internal.daemon_to_dict(daemon)

    # give the new daemon the new name
    attributes['name'] = new_name

    # remove the log file and pid file so that new ones will be created
    # for the new agent
    del attributes['log_file']
    del attributes['pid_file']

    # Get the broker credentials for the daemon
    attributes.update(ctx.bootstrap_context.broker_config())

    new_daemon = DaemonFactory().new(logger=ctx.logger, **attributes)

    # create the new daemon
    new_daemon.create()
    _save_daemon(new_daemon)

    # configure the new daemon
    new_daemon.configure()
    new_daemon.start()

    # ..and stop the old agent
    daemon.before_self_stop()
    raise StopAgent()
Ejemplo n.º 21
0
    def create_daemon(self, **attributes):
        name = utils.internal.generate_agent_name()

        params = {
            'rest_host': ['127.0.0.1'],
            'broker_ip': ['127.0.0.1'],
            'user': self.username,
            'workdir': self.temp_folder,
            'logger': self.logger,
            'name': name,
            'queue': '{0}-queue'.format(name),
            'local_rest_cert_file': self._rest_cert_path,
            'broker_ssl_enabled': False,  # No SSL on the CI machines
        }
        params.update(attributes)

        factory = DaemonFactory()
        daemon = self.daemon_cls(**params)
        factory.save(daemon)
        self.addCleanup(factory.delete, daemon.name)
        self.daemons.append(daemon)
        return daemon
Ejemplo n.º 22
0
    def create_daemon(self, **attributes):
        name = utils.internal.generate_agent_name()

        params = {
            'rest_host': '127.0.0.1',
            'broker_ip': '127.0.0.1',
            'user': self.username,
            'workdir': self.temp_folder,
            'logger': self.logger,
            'name': name,
            'queue': '{0}-queue'.format(name),
            'local_rest_cert_file': self._rest_cert_path,
            'broker_ssl_enabled': False,  # No SSL on the CI machines
        }
        params.update(attributes)

        factory = DaemonFactory()
        daemon = self.daemon_cls(**params)
        factory.save(daemon)
        self.addCleanup(factory.delete, daemon.name)
        self.daemons.append(daemon)
        return daemon
Ejemplo n.º 23
0
 def setUp(self):
     super(TestDaemonFactory, self).setUp()
     self.daemon_name = 'test-daemon-{0}'.format(uuid.uuid4())
     self.factory = DaemonFactory(storage=get_storage_directory())
     self.daemon_params = {
         'process_management': 'init.d',
         'name': self.daemon_name,
         'queue': 'queue',
         'rest_host': '127.0.0.1',
         'broker_ip': '127.0.0.1',
         'user': '******',
         'broker_url': '127.0.0.1',
         'broker_ssl_enabled': True,
         'local_rest_cert_file': self._rest_cert_path
     }
Ejemplo n.º 24
0
def install(source, args):
    """
    Install a cloudify plugin into the current virtualenv. This will also
    register the plugin to all daemons created from this virtualenv.
    """

    from cloudify_agent.shell.main import get_logger
    click.echo('Installing plugin from {0}'.format(source))
    installer = PluginInstaller(logger=get_logger())
    name = installer.install(source, args)

    daemons = DaemonFactory().load_all(logger=get_logger())
    for daemon in daemons:
        click.echo('Registering plugin {0} to {1}'.format(name, daemon.name))
        if daemon.virtualenv == VIRTUALENV:
            daemon.register(name)
            _save_daemon(daemon)

    click.echo('Successfully installed plugin: {0}'.format(name))
Ejemplo n.º 25
0
def uninstall(plugin):
    """
    Install a cloudify plugin into the current virtualenv. This will also
    register the plugin to all daemons created from this virtualenv.
    """

    from cloudify_agent.shell.main import get_logger
    click.echo('Uninstalling plugin {0}'.format(plugin))
    installer = PluginInstaller(logger=get_logger())
    installer.uninstall(plugin)

    daemons = DaemonFactory().load_all(logger=get_logger())
    for daemon in daemons:
        click.echo('Un-registering plugin {0} from {1}'.format(
            plugin, daemon.name))
        if daemon.virtualenv == VIRTUALENV:
            daemon.unregister(plugin)
            _save_daemon(daemon)

    click.echo('Successfully installed plugin: {0}'.format(plugin))
Ejemplo n.º 26
0
    def _strategy(initial_brokers):
        logger.debug('Failover strategy: searching for a new rabbitmq server')
        initial_brokers = [broker for broker in initial_brokers if broker]
        brokers = itertools.cycle(initial_brokers)
        while True:
            if cluster.is_cluster_configured():
                nodes = cluster.get_cluster_nodes()
                for node in nodes:
                    with _cluster_settings_lock(daemon_name):
                        _set_master(daemon_name, node)

                    daemon = DaemonFactory().load(daemon_name)

                    broker_url = 'amqp://{0}:{1}@{2}:{3}/{4}'.format(
                        daemon.broker_user, daemon.broker_pass,
                        node['broker_ip'], daemon.broker_port,
                        daemon.broker_vhost)

                    if daemon.broker_ssl_enabled:
                        # use a different cert for each node in the cluster -
                        # can't pass that in the amqp url
                        broker_ssl_cert_path = node.get('internal_cert_path')
                        if broker_ssl_cert_path:
                            app.conf['BROKER_USE_SSL']['ca_certs'] =\
                                broker_ssl_cert_path

                    logger.debug('Trying broker at {0}'.format(broker_url))
                    yield broker_url
            else:
                logger.debug('cluster config file does not exist')
                broker_url = next(brokers)
                if len(initial_brokers) > 1:
                    logger.debug('writing config file')
                    with _cluster_settings_lock(daemon_name):
                        cluster.config_from_broker_urls(
                            broker_url, initial_brokers)
                        _set_master(daemon_name, cluster.get_cluster_active())
                logger.debug('Trying broker at {0}'.format(broker_url))
                yield broker_url
Ejemplo n.º 27
0
 def setUp(self):
     super(TestDaemonFactory, self).setUp()
     self.daemon_name = 'test-daemon-{0}'.format(uuid.uuid4())
     self.factory = DaemonFactory(storage=get_storage_directory())
Ejemplo n.º 28
0
def _save_daemon(daemon):
    factory = DaemonFactory(
        username=utils.internal.get_daemon_user(),
        storage=utils.internal.get_daemon_storage_dir())
    factory.save(daemon)
Ejemplo n.º 29
0
def _load_daemon(logger):
    factory = DaemonFactory(
        username=utils.internal.get_daemon_user(),
        storage=utils.internal.get_daemon_storage_dir())
    return factory.load(utils.internal.get_daemon_name(), logger=logger)
Ejemplo n.º 30
0
class TestDaemonFactory(BaseTest, unittest.TestCase):

    def setUp(self):
        super(TestDaemonFactory, self).setUp()
        self.daemon_name = 'test-daemon-{0}'.format(uuid.uuid4())
        self.factory = DaemonFactory(storage=get_storage_directory())
        self.daemon_params = {
            'process_management': 'init.d',
            'name': self.daemon_name,
            'queue': 'queue',
            'rest_host': '127.0.0.1',
            'broker_ip': '127.0.0.1',
            'user': '******',
            'broker_url': '127.0.0.1',
            'broker_ssl_enabled': True,
            'local_rest_cert_file': self._rest_cert_path
        }

    def test_new_initd(self):
        daemon = self.factory.new(**self.daemon_params)
        self.assertEqual(self.daemon_name, daemon.name)
        self.assertEqual('queue', daemon.queue)
        self.assertEqual('127.0.0.1', daemon.rest_host)
        self.assertEqual('user', daemon.user)
        self.assertEqual(self._rest_cert_path,
                         daemon.local_rest_cert_file)

    def test_new_no_implementation(self):
        self.assertRaises(exceptions.DaemonNotImplementedError,
                          self.factory.new,
                          process_management='no-impl')

    def test_save_load_delete(self):

        daemon = self.factory.new(**self.daemon_params)

        self.factory.save(daemon)
        loaded = self.factory.load(self.daemon_name)
        self.assertEqual('init.d', loaded.PROCESS_MANAGEMENT)
        self.assertEqual(self.daemon_name, loaded.name)
        self.assertEqual('queue', loaded.queue)
        self.assertEqual('127.0.0.1', loaded.rest_host)
        self.assertEqual('user', loaded.user)
        self.factory.delete(daemon.name)
        self.assertRaises(exceptions.DaemonNotFoundError,
                          self.factory.load, daemon.name)

    def test_load_non_existing(self):
        self.assertRaises(exceptions.DaemonNotFoundError,
                          self.factory.load,
                          'non_existing_name')

    def test_load_all(self):

        def _save_daemon(name):
            params = self.daemon_params.copy()
            params['name'] = name
            daemon = self.factory.new(**params)
            self.factory.save(daemon)

        if os.path.exists(get_storage_directory()):
            shutil.rmtree(get_storage_directory())

        daemons = self.factory.load_all()
        self.assertEquals(0, len(daemons))
        _save_daemon(utils.internal.generate_agent_name())
        _save_daemon(utils.internal.generate_agent_name())
        _save_daemon(utils.internal.generate_agent_name())

        daemons = self.factory.load_all()
        self.assertEquals(3, len(daemons))

    def test_new_existing_agent(self):
        daemon = self.factory.new(**self.daemon_params)

        self.factory.save(daemon)

        # without no_overwrite, this will overwrite the existing daemon
        daemon = self.factory.new(**self.daemon_params)

        self.assertRaises(exceptions.DaemonAlreadyExistsError,
                          self.factory.new,
                          no_overwrite=True,
                          **self.daemon_params)
Ejemplo n.º 31
0
def _load_daemon(name, user=None):
    from cloudify_agent.shell.main import get_logger
    return DaemonFactory(username=user).load(name, logger=get_logger())
Ejemplo n.º 32
0
def daemon_factory(tmp_path):
    yield DaemonFactory(storage=get_daemon_storage(str(tmp_path)))
Ejemplo n.º 33
0
def _load_daemon(logger):
    factory = DaemonFactory(username=utils.internal.get_daemon_user(),
                            storage=utils.internal.get_daemon_storage_dir())
    return factory.load(utils.internal.get_daemon_name(), logger=logger)
Ejemplo n.º 34
0
def _save_daemon(daemon):
    factory = DaemonFactory(username=utils.internal.get_daemon_user(),
                            storage=utils.internal.get_daemon_storage_dir())
    factory.save(daemon)
Ejemplo n.º 35
0
class TestDaemonFactory(BaseShellTest):
    def setUp(self):
        super(TestDaemonFactory, self).setUp()
        self.daemon_name = 'test-daemon-{0}'.format(uuid.uuid4())
        self.factory = DaemonFactory(storage=get_storage_directory())

    def test_new_initd(self):
        daemon = self.factory.new(process_management='init.d',
                                  name=self.daemon_name,
                                  queue='queue',
                                  manager_ip='127.0.0.1',
                                  user='******',
                                  broker_url='127.0.0.1')
        self.assertEqual(self.daemon_name, daemon.name)
        self.assertEqual('queue', daemon.queue)
        self.assertEqual('127.0.0.1', daemon.manager_ip)
        self.assertEqual('amqp://*****:*****@127.0.0.1:5672//',
                         daemon.broker_url)
        self.assertEqual('user', daemon.user)

    def test_new_wrong_includes_attribute(self):
        self.assertRaises(ValueError,
                          self.factory.new,
                          process_management='init.d',
                          queue='{0}-queue'.format(self.daemon_name),
                          name=self.daemon_name,
                          manager_ip='127.0.0.1',
                          includes=set(['plugin']))

    def test_new_no_implementation(self):
        self.assertRaises(exceptions.DaemonNotImplementedError,
                          self.factory.new,
                          process_management='no-impl')

    def test_save_load_delete(self):

        daemon = self.factory.new(process_management='init.d',
                                  name=self.daemon_name,
                                  queue='queue',
                                  manager_ip='127.0.0.1',
                                  user='******',
                                  broker_url='127.0.0.1')

        self.factory.save(daemon)
        loaded = self.factory.load(self.daemon_name)
        self.assertEqual('init.d', loaded.PROCESS_MANAGEMENT)
        self.assertEqual(self.daemon_name, loaded.name)
        self.assertEqual('queue', loaded.queue)
        self.assertEqual('127.0.0.1', loaded.manager_ip)
        self.assertEqual('user', loaded.user)
        self.assertEqual('amqp://*****:*****@127.0.0.1:5672//',
                         daemon.broker_url)
        self.factory.delete(daemon.name)
        self.assertRaises(exceptions.DaemonNotFoundError, self.factory.load,
                          daemon.name)

    def test_load_non_existing(self):
        self.assertRaises(exceptions.DaemonNotFoundError, self.factory.load,
                          'non_existing_name')

    def test_load_all(self):
        def _save_daemon(name):
            daemon = self.factory.new(process_management='init.d',
                                      name=name,
                                      queue='queue',
                                      manager_ip='127.0.0.1',
                                      user='******',
                                      broker_url='127.0.0.1')
            self.factory.save(daemon)

        if os.path.exists(get_storage_directory()):
            shutil.rmtree(get_storage_directory())

        daemons = self.factory.load_all()
        self.assertEquals(0, len(daemons))

        _save_daemon(utils.internal.generate_agent_name())
        _save_daemon(utils.internal.generate_agent_name())
        _save_daemon(utils.internal.generate_agent_name())

        daemons = self.factory.load_all()
        self.assertEquals(3, len(daemons))

    def test_new_existing_agent(self):

        daemon = self.factory.new(process_management='init.d',
                                  name=self.daemon_name,
                                  queue='queue',
                                  manager_ip='127.0.0.1',
                                  user='******',
                                  broker_url='127.0.0.1')

        self.factory.save(daemon)

        self.assertRaises(exceptions.DaemonAlreadyExistsError,
                          self.factory.new,
                          process_management='init.d',
                          name=self.daemon_name,
                          queue='queue',
                          manager_ip='127.0.0.1',
                          user='******',
                          broker_url='127.0.0.1')
Ejemplo n.º 36
0
class TestDaemonFactory(BaseTest, unittest.TestCase):
    def setUp(self):
        super(TestDaemonFactory, self).setUp()
        self.daemon_name = 'test-daemon-{0}'.format(uuid.uuid4())
        self.factory = DaemonFactory(storage=get_storage_directory())
        self.daemon_params = {
            'process_management': 'init.d',
            'name': self.daemon_name,
            'queue': 'queue',
            'rest_host': '127.0.0.1',
            'broker_ip': '127.0.0.1',
            'user': '******',
            'broker_url': '127.0.0.1',
            'broker_ssl_enabled': True,
            'local_rest_cert_file': self._rest_cert_path
        }

    def test_new_initd(self):
        daemon = self.factory.new(**self.daemon_params)
        self.assertEqual(self.daemon_name, daemon.name)
        self.assertEqual('queue', daemon.queue)
        self.assertEqual('127.0.0.1', daemon.rest_host)
        self.assertEqual('user', daemon.user)
        self.assertEqual(self._rest_cert_path, daemon.local_rest_cert_file)

    def test_new_no_implementation(self):
        self.assertRaises(exceptions.DaemonNotImplementedError,
                          self.factory.new,
                          process_management='no-impl')

    def test_save_load_delete(self):

        daemon = self.factory.new(**self.daemon_params)

        self.factory.save(daemon)
        loaded = self.factory.load(self.daemon_name)
        self.assertEqual('init.d', loaded.PROCESS_MANAGEMENT)
        self.assertEqual(self.daemon_name, loaded.name)
        self.assertEqual('queue', loaded.queue)
        self.assertEqual('127.0.0.1', loaded.rest_host)
        self.assertEqual('user', loaded.user)
        self.factory.delete(daemon.name)
        self.assertRaises(exceptions.DaemonNotFoundError, self.factory.load,
                          daemon.name)

    def test_load_non_existing(self):
        self.assertRaises(exceptions.DaemonNotFoundError, self.factory.load,
                          'non_existing_name')

    def test_load_all(self):
        def _save_daemon(name):
            params = self.daemon_params.copy()
            params['name'] = name
            daemon = self.factory.new(**params)
            self.factory.save(daemon)

        if os.path.exists(get_storage_directory()):
            shutil.rmtree(get_storage_directory())

        daemons = self.factory.load_all()
        self.assertEquals(0, len(daemons))
        _save_daemon(utils.internal.generate_agent_name())
        _save_daemon(utils.internal.generate_agent_name())
        _save_daemon(utils.internal.generate_agent_name())

        daemons = self.factory.load_all()
        self.assertEquals(3, len(daemons))

    def test_new_existing_agent(self):
        daemon = self.factory.new(**self.daemon_params)

        self.factory.save(daemon)

        # without no_overwrite, this will overwrite the existing daemon
        daemon = self.factory.new(**self.daemon_params)

        self.assertRaises(exceptions.DaemonAlreadyExistsError,
                          self.factory.new,
                          no_overwrite=True,
                          **self.daemon_params)
Ejemplo n.º 37
0
 def setUp(self):
     super(TestDaemonFactory, self).setUp()
     self.daemon_name = 'test-daemon-{0}'.format(uuid.uuid4())
     self.factory = DaemonFactory(storage=get_storage_directory())
Ejemplo n.º 38
0
class TestDaemonFactory(BaseShellTest):

    def setUp(self):
        super(TestDaemonFactory, self).setUp()
        self.daemon_name = 'test-daemon-{0}'.format(uuid.uuid4())
        self.factory = DaemonFactory(storage=get_storage_directory())

    def test_new_initd(self):
        daemon = self.factory.new(
            process_management='init.d',
            name=self.daemon_name,
            queue='queue',
            rest_host='127.0.0.1',
            broker_ip='127.0.0.1',
            file_server_host='127.0.0.1',
            user='******',
            broker_url='127.0.0.1')
        self.assertEqual(self.daemon_name, daemon.name)
        self.assertEqual('queue', daemon.queue)
        self.assertEqual('127.0.0.1', daemon.rest_host)
        self.assertEqual('amqp://*****:*****@127.0.0.1:5672//',
                         daemon.broker_url)
        self.assertEqual('user', daemon.user)

    def test_new_no_implementation(self):
        self.assertRaises(exceptions.DaemonNotImplementedError,
                          self.factory.new,
                          process_management='no-impl')

    def test_save_load_delete(self):

        daemon = self.factory.new(
            process_management='init.d',
            name=self.daemon_name,
            queue='queue',
            rest_host='127.0.0.1',
            broker_ip='127.0.0.1',
            file_server_host='127.0.0.1',
            user='******',
            broker_url='127.0.0.1')

        self.factory.save(daemon)
        loaded = self.factory.load(self.daemon_name)
        self.assertEqual('init.d', loaded.PROCESS_MANAGEMENT)
        self.assertEqual(self.daemon_name, loaded.name)
        self.assertEqual('queue', loaded.queue)
        self.assertEqual('127.0.0.1', loaded.rest_host)
        self.assertEqual('user', loaded.user)
        self.assertEqual('amqp://*****:*****@127.0.0.1:5672//',
                         daemon.broker_url)
        self.factory.delete(daemon.name)
        self.assertRaises(exceptions.DaemonNotFoundError,
                          self.factory.load, daemon.name)

    def test_load_non_existing(self):
        self.assertRaises(exceptions.DaemonNotFoundError,
                          self.factory.load,
                          'non_existing_name')

    def test_load_all(self):

        def _save_daemon(name):
            daemon = self.factory.new(
                process_management='init.d',
                name=name,
                queue='queue',
                rest_host='127.0.0.1',
                broker_ip='127.0.0.1',
                file_server_host='127.0.0.1',
                user='******',
                broker_url='127.0.0.1')
            self.factory.save(daemon)

        if os.path.exists(get_storage_directory()):
            shutil.rmtree(get_storage_directory())

        daemons = self.factory.load_all()
        self.assertEquals(0, len(daemons))

        _save_daemon(utils.internal.generate_agent_name())
        _save_daemon(utils.internal.generate_agent_name())
        _save_daemon(utils.internal.generate_agent_name())

        daemons = self.factory.load_all()
        self.assertEquals(3, len(daemons))

    def test_new_existing_agent(self):

        daemon = self.factory.new(
            process_management='init.d',
            name=self.daemon_name,
            queue='queue',
            rest_host='127.0.0.1',
            broker_ip='127.0.0.1',
            file_server_host='127.0.0.1',
            user='******',
            broker_url='127.0.0.1')

        self.factory.save(daemon)

        self.assertRaises(exceptions.DaemonAlreadyExistsError,
                          self.factory.new,
                          process_management='init.d',
                          name=self.daemon_name,
                          queue='queue',
                          rest_host='127.0.0.1',
                          broker_ip='127.0.0.1',
                          file_server_host='127.0.0.1',
                          user='******',
                          broker_url='127.0.0.1')
Ejemplo n.º 39
0
 def cluster_update(nodes):
     cluster.set_cluster_nodes(nodes)
     factory = DaemonFactory()
     daemon = factory.load(daemon_name)
     daemon.cluster = nodes
     factory.save(daemon)
Ejemplo n.º 40
0
def _save_daemon(daemon):
    DaemonFactory(username=daemon.user).save(daemon)
Ejemplo n.º 41
0
class BaseDaemon:
    def __init__(self, tmp_path, logger, ssl_cert):
        self.daemons = []
        self.temp_folder = str(tmp_path)
        self.username = getpass.getuser()
        self.logger = logger
        self.rest_cert_path = ssl_cert.local_cert_path()
        self.factory = DaemonFactory()
        self.runner = LocalCommandRunner(logger=logger)

    @property
    def daemon_cls(self):
        raise NotImplementedError('Must be implemented by sub-class')

    def create_daemon(self, **attributes):
        name = utils.internal.generate_agent_name()

        params = {
            'rest_host': ['127.0.0.1'],
            'broker_ip': ['127.0.0.1'],
            'user': self.username,
            'workdir': self.temp_folder,
            'logger': self.logger,
            'name': name,
            'queue': '{0}-queue'.format(name),
            'local_rest_cert_file': self.rest_cert_path,
            'broker_ssl_enabled': False,  # No SSL on the CI machines
        }
        params.update(attributes)

        daemon = self.daemon_cls(**params)
        self.factory.save(daemon)
        self.daemons.append(daemon)
        return daemon

    @staticmethod
    def plugin_struct(plugin_name='mock-plugin'):
        return {
            'source': os.path.join(resources.get_resource('plugins'),
                                   plugin_name),
            'package_name': PLUGIN_NAME
        }

    def send_task(self,
                  task_name,
                  queue,
                  deployment_id=None,
                  args=None,
                  kwargs=None,
                  timeout=10,
                  execution_env=None):
        cloudify_context = test_utils.op_context(task_name,
                                                 task_target=queue,
                                                 plugin_name=PLUGIN_NAME,
                                                 execution_env=execution_env,
                                                 deployment_id=deployment_id)
        kwargs = kwargs or {}
        kwargs['__cloudify_context'] = cloudify_context
        handler = amqp_client.BlockingRequestResponseHandler(queue)
        client = amqp_client.get_client()
        client.add_handler(handler)
        with client:
            task = {'cloudify_task': {'kwargs': kwargs}}
            result = handler.publish(task,
                                     routing_key='operation',
                                     timeout=timeout)
        error = result.get('error')
        if error:
            raise deserialize_known_exception(error)
        else:
            return result.get('result')