Ejemplo n.º 1
0
def _cfy_agent_attributes_no_defaults(cloudify_agent):
    if not cloudify_agent.get('process_management'):
        cloudify_agent['process_management'] = {}

    if not cloudify_agent['process_management'].get('name'):
        # user did not specify process management configuration, choose the
        # default one according to os type.
        if cloudify_agent['windows']:
            name = 'nssm'
        else:
            name = 'init.d'
        cloudify_agent['process_management']['name'] = name

    if not cloudify_agent.get('name'):
        if cloudify_agent['local']:
            workflows_worker = cloudify_agent.get('workflows_worker', False)
            suffix = '_workflows' if workflows_worker else ''
            name = '{0}{1}'.format(ctx.deployment.id, suffix)
        else:
            name = ctx.instance.id
        cloudify_agent['name'] = name

    service_name = cloudify_agent.get('service_name')
    if service_name:
        # service_name takes precedence over name (which is deprecated)
        cloudify_agent['name'] = service_name

    if not cloudify_agent.get('queue'):
        # by default, queue of the agent is the same as the name
        cloudify_agent['queue'] = cloudify_agent['name']

    if not cloudify_agent.get('rest_host'):
        cloudify_agent['rest_host'] = \
            cloudify_utils.get_manager_rest_service_host()

    if not cloudify_agent.get('rest_port'):
        cloudify_agent['rest_port'] = \
            cloudify_utils.get_manager_rest_service_port()

    if not cloudify_agent.get('rest_token'):
        cloudify_agent['rest_token'] = \
            cloudify_utils.get_rest_token()

    if not cloudify_agent.get('rest_tenant'):
        cloudify_agent['rest_tenant'] = \
            cloudify_utils.get_tenant_name()

    if not cloudify_agent.get('bypass_maintenance'):
        cloudify_agent['bypass_maintenance_mode'] = \
            cloudify_utils.get_is_bypass_maintenance()
Ejemplo n.º 2
0
 def set_default_values(self):
     self._set_process_management()
     self._set_name()
     self._set_network()
     self.setdefault('queue', self['name'])
     self.setdefault('rest_token', cloudify_utils.get_rest_token())
     self.setdefault('rest_tenant', cloudify_utils.get_tenant())
     self.setdefault('rest_port',
                     cloudify_utils.get_manager_rest_service_port())
     self.setdefault('bypass_maintenance',
                     cloudify_utils.get_is_bypass_maintenance())
     self.setdefault('min_workers', 0)
     self.setdefault('max_workers', 5)
     self.setdefault('disable_requiretty', True)
     self.setdefault('env', {})
     self.setdefault('fabric_env', {})
     self.setdefault('system_python', 'python')
Ejemplo n.º 3
0
def _get_cloudify_context(agent, task_name):
    """
    Return the cloudify context that would be set in tasks sent to the old
    agent
    """
    return {
        '__cloudify_context': {
            'type': 'operation',
            'task_name': task_name,
            'task_target': agent['queue'],
            'node_id': ctx.instance.id,
            'workflow_id': ctx.workflow_id,
            'execution_id': ctx.execution_id,
            'tenant': ctx.tenant,
            'rest_token': get_rest_token()
        }
    }
Ejemplo n.º 4
0
def _get_cloudify_context(agent, task_name):
    """
    Return the cloudify context that would be set in tasks sent to the old
    agent
    """
    return {
        '__cloudify_context': {
            'type': 'operation',
            'task_name': task_name,
            'task_target': agent['queue'],
            'node_id': ctx.instance.id,
            'workflow_id': ctx.workflow_id,
            'execution_id': ctx.execution_id,
            'tenant': ctx.tenant,
            'rest_token': get_rest_token()
        }
    }
def check_liveness(nodes_to_monitor,depl_id):
    c = CloudifyClient(host=utils.get_manager_ip(),
                       port=utils.get_manager_rest_service_port(),
                       protocol='https',
                       cert=utils.get_local_rest_certificate(),
                       token= utils.get_rest_token(),
                       tenant= utils.get_tenant_name())

    c_influx = InfluxDBClient(host='localhost', port=8086, database='cloudify')
    log ('nodes_to_monitor: {0}'.format(nodes_to_monitor))

    # compare influx data (monitoring) to cloudify desired state

    for node_name in nodes_to_monitor:
        instances=c.node_instances.list(depl_id,node_name)
        for instance in instances:
            q_string='SELECT MEAN(value) FROM /' + depl_id + '\.' + node_name + '\.' + instance.id + '\.cpu_total_system/ GROUP BY time(10s) '\
                   'WHERE  time > now() - 40s'
            log ('query string is {0}'.format(q_string))
            try:
               result=c_influx.query(q_string)
               log ('result is {0}'.format(result))
               if not result:
                  executions=c.executions.list(depl_id)
                  has_pending_execution = False
                  if executions and len(executions)>0:
                      for execution in executions:
                      # log("Execution {0} : {1}".format(execution.id, execution.status))
                          if execution.status not in execution.END_STATES:
                              has_pending_execution = True
                  if not has_pending_execution:
                      log ('Setting state to error for instance {0} and its children'.format(instance.id))
                      update_nodes_tree_state(c, depl_id, instance, 'error')
                      params = {'node_instance_id': instance.id}
                      log ('Calling Auto-healing workflow for container instance {0}'.format(instance.id))
                      c.executions.start(depl_id, 'a4c_heal', params)
                  else:
                      log ('pending executions on the deployment...waiting for the end before calling heal workflow...')
            except InfluxDBClientError as ee:
                log ('DBClienterror {0}'.format(str(ee)), level='ERROR')
                log ('instance id is {0}'.format(instance), level='ERROR')
            except Exception as e:
                log (str(e), level='ERROR')
Ejemplo n.º 6
0
def get_rest_client(tenant=None, api_token=None):
    """
    :param tenant: optional tenant name to connect as
    :param api_token: optional api_token to authenticate with (instead of
            using REST token)
    :returns: A REST client configured to connect to the manager in context
    :rtype: cloudify_rest_client.CloudifyClient
    """
    cluster_settings = get_cluster_settings()
    if cluster_settings:
        client_class = CloudifyClusterClient
    else:
        client_class = CloudifyClient

    if not tenant:
        tenant = utils.get_tenant_name()

    # Handle maintenance mode
    headers = {}
    if utils.get_is_bypass_maintenance():
        headers['X-BYPASS-MAINTENANCE'] = 'True'

    # If api_token or execution_token was provided no need to use REST token
    token = None
    execution_token = utils.get_execution_token()
    if execution_token:
        headers[constants.CLOUDIFY_EXECUTION_TOKEN_HEADER] = execution_token
    elif api_token:
        headers[constants.CLOUDIFY_API_AUTH_TOKEN_HEADER] = api_token
    else:
        token = utils.get_rest_token()

    return client_class(headers=headers,
                        host=utils.get_manager_rest_service_host(),
                        port=utils.get_manager_rest_service_port(),
                        tenant=tenant,
                        token=token,
                        protocol=constants.SECURED_PROTOCOL,
                        cert=utils.get_local_rest_certificate(),
                        kerberos_env=utils.get_kerberos_indication(
                            os.environ.get(constants.KERBEROS_ENV_KEY)))
Ejemplo n.º 7
0
def _get_cloudify_context(agent, task_name, new_agent_connection=None):
    """
    Return the cloudify context that would be set in tasks sent to the old
    agent
    """
    cloudify_context = {
        'type': 'operation',
        'task_name': task_name,
        'task_target': agent['queue'],
        'node_id': ctx.instance.id,
        'workflow_id': ctx.workflow_id,
        'execution_id': ctx.execution_id,
        'tenant': ctx.tenant,
        'rest_token': get_rest_token(),
        'rest_host': agent['rest_host']
    }

    if new_agent_connection:
        cloudify_context['rest_host'] = new_agent_connection['rest_host']
        cloudify_context['rest_ssl_cert'] = \
            new_agent_connection['rest_ssl_cert']

    return {'__cloudify_context': cloudify_context}
    while host is not None:
        entity = host
        host = get_host(entity)
    return get_attribute(entity, attribute_name)

from cloudify import utils
from cloudify_rest_client import CloudifyClient
from cloudify.state import ctx_parameters as inputs

import os

client = CloudifyClient(host=utils.get_manager_ip(),
                        port=utils.get_manager_rest_service_port(),
                        protocol='https',
                        cert=utils.get_local_rest_certificate(),
                        token= utils.get_rest_token(),
                        tenant= utils.get_tenant_name())

def convert_env_value_to_string(envDict):
    for key, value in envDict.items():
        envDict[str(key)] = str(envDict.pop(key))


def parse_output(output):
    # by convention, the last output is the result of the operation
    last_output = None
    outputs = {}
    pattern = re.compile('EXPECTED_OUTPUT_(\w+)=(.*)')
    for line in output.splitlines():
        match = pattern.match(line)
        if match is None:
Ejemplo n.º 9
0
def _cfy_agent_attributes_no_defaults(cloudify_agent):

    if not cloudify_agent.get('process_management'):
        cloudify_agent['process_management'] = {}

    if not cloudify_agent['process_management'].get('name'):
        # user did not specify process management configuration, choose the
        # default one according to os type.
        if cloudify_agent['windows']:
            name = 'nssm'
        else:
            name = 'init.d'
        cloudify_agent['process_management']['name'] = name

    if not cloudify_agent.get('name'):
        if cloudify_agent['local']:
            workflows_worker = cloudify_agent.get('workflows_worker', False)
            suffix = '_workflows' if workflows_worker else ''
            name = '{0}{1}'.format(ctx.deployment.id, suffix)
        else:
            name = ctx.instance.id
        cloudify_agent['name'] = name

    if not cloudify_agent.get('queue'):
        # by default, queue of the agent is the same as the name
        cloudify_agent['queue'] = cloudify_agent['name']

    if not cloudify_agent.get('file_server_host'):
        cloudify_agent['file_server_host'] = \
            cloudify_utils.get_manager_file_server_host()

    if not cloudify_agent.get('file_server_port'):
        cloudify_agent['file_server_port'] = \
            cloudify_utils.get_manager_file_server_port()

    if not cloudify_agent.get('file_server_protocol'):
        cloudify_agent['file_server_protocol'] = \
            cloudify_utils.get_manager_file_server_protocol()

    if not cloudify_agent.get('rest_host'):
        cloudify_agent['rest_host'] = \
            cloudify_utils.get_manager_rest_service_host()

    if not cloudify_agent.get('security_enabled'):
        cloudify_agent['security_enabled'] = \
            cloudify_utils.is_security_enabled()

    if not cloudify_agent.get('rest_protocol'):
        cloudify_agent['rest_protocol'] = \
            cloudify_utils.get_manager_rest_service_protocol()

    if not cloudify_agent.get('rest_port'):
        cloudify_agent['rest_port'] = \
            cloudify_utils.get_manager_rest_service_port()

    if not cloudify_agent.get('rest_username'):
        cloudify_agent['rest_username'] = \
            cloudify_utils.get_rest_username()

    if not cloudify_agent.get('rest_password'):
        cloudify_agent['rest_password'] = \
            cloudify_utils.get_rest_password()

    if not cloudify_agent.get('rest_token'):
        cloudify_agent['rest_token'] = \
            cloudify_utils.get_rest_token()

    if not cloudify_agent.get('rest_cert_content'):
        cloudify_agent['rest_cert_content'] = \
            cloudify_utils.get_rest_cert_content()

    if not cloudify_agent.get('verify_rest_certificate'):
        cloudify_agent['verify_rest_certificate'] = \
            cloudify_utils.is_verify_rest_certificate()

    if not cloudify_agent.get('bypass_maintenance'):
        cloudify_agent['bypass_maintenance_mode'] = \
            cloudify_utils.get_is_bypass_maintenance()
Ejemplo n.º 10
0
    def _create_agent_env(self):
        # Try to get broker credentials from the tenant. If they aren't set
        # get them from the broker_config module
        tenant = get_tenant()
        tenant_name = tenant.get('name', defaults.DEFAULT_TENANT_NAME)
        broker_user = tenant.get('rabbitmq_username',
                                 broker_config.broker_username)
        broker_pass = tenant.get('rabbitmq_password',
                                 broker_config.broker_password)
        broker_vhost = tenant.get('rabbitmq_vhost', broker_config.broker_vhost)

        execution_env = {
            # mandatory values calculated before the agent
            # is actually created
            env.CLOUDIFY_DAEMON_QUEUE:
            self.cloudify_agent['queue'],
            env.CLOUDIFY_DAEMON_NAME:
            self.cloudify_agent['name'],
            env.CLOUDIFY_REST_HOST:
            self.cloudify_agent['rest_host'],
            env.CLOUDIFY_BROKER_IP:
            self.cloudify_agent['broker_ip'],

            # Optional broker values
            env.CLOUDIFY_BROKER_USER:
            broker_user,
            env.CLOUDIFY_BROKER_PASS:
            broker_pass,
            env.CLOUDIFY_BROKER_VHOST:
            broker_vhost,
            env.CLOUDIFY_BROKER_SSL_ENABLED:
            broker_config.broker_ssl_enabled,
            env.CLOUDIFY_BROKER_SSL_CERT_PATH:
            self.cloudify_agent['broker_ssl_cert_path'],
            env.CLOUDIFY_HEARTBEAT:
            self.cloudify_agent.get('heartbeat'),

            # these are variables that have default values that will be set
            # by the agent on the remote host if not set here
            env.CLOUDIFY_DAEMON_USER:
            self.cloudify_agent.get('user'),
            env.CLOUDIFY_REST_PORT:
            self.cloudify_agent.get('rest_port'),
            env.CLOUDIFY_REST_TOKEN:
            get_rest_token(),
            env.CLOUDIFY_REST_TENANT:
            tenant_name,
            env.CLOUDIFY_DAEMON_MAX_WORKERS:
            self.cloudify_agent.get('max_workers'),
            env.CLOUDIFY_DAEMON_MIN_WORKERS:
            self.cloudify_agent.get('min_workers'),
            env.CLOUDIFY_DAEMON_PROCESS_MANAGEMENT:
            self.cloudify_agent['process_management']['name'],
            env.CLOUDIFY_DAEMON_WORKDIR:
            self.cloudify_agent['workdir'],
            env.CLOUDIFY_DAEMON_EXTRA_ENV:
            self.create_custom_env_file_on_target(
                self.cloudify_agent.get('env', {})),
            env.CLOUDIFY_BYPASS_MAINTENANCE_MODE:
            get_is_bypass_maintenance(),
            env.CLOUDIFY_LOCAL_REST_CERT_PATH:
            self.cloudify_agent['agent_rest_cert_path'],
            env.CLOUDIFY_CLUSTER_NODES:
            base64.b64encode(json.dumps(self.cloudify_agent.get('cluster',
                                                                []))),
            env.CLOUDIFY_NETWORK_NAME:
            self.cloudify_agent.get('network')
        }

        execution_env = utils.purge_none_values(execution_env)
        execution_env = utils.stringify_values(execution_env)

        self.logger.debug('Cloudify Agent will be created using the following '
                          'environment: {0}'.format(execution_env))

        return execution_env
Ejemplo n.º 11
0
    def _create_agent_env(self):
        tenant = get_tenant()
        tenant_name = tenant.get('name', defaults.DEFAULT_TENANT_NAME)
        tenant_user = tenant.get('rabbitmq_username',
                                 broker_config.broker_username)
        tenant_pass = tenant.get('rabbitmq_password',
                                 broker_config.broker_password)
        broker_vhost = tenant.get('rabbitmq_vhost',
                                  broker_config.broker_vhost)
        # Get the agent's broker credentials
        broker_user = self.cloudify_agent.get('broker_user', tenant_user)
        broker_pass = self.cloudify_agent.get('broker_pass', tenant_pass)

        manager_ip = self.cloudify_agent.get_manager_ip()
        network = self.cloudify_agent.get('network')
        execution_env = {
            # mandatory values calculated before the agent
            # is actually created
            env.CLOUDIFY_DAEMON_QUEUE: self.cloudify_agent['queue'],
            env.CLOUDIFY_DAEMON_NAME: self.cloudify_agent['name'],
            env.CLOUDIFY_REST_HOST: manager_ip,
            env.CLOUDIFY_BROKER_IP: ','.join(
                broker.networks[network] for broker in
                ctx.get_brokers(network=network)
            ),

            # Optional broker values
            env.CLOUDIFY_BROKER_USER: broker_user,
            env.CLOUDIFY_BROKER_PASS: broker_pass,
            env.CLOUDIFY_BROKER_VHOST: broker_vhost,
            env.CLOUDIFY_BROKER_SSL_ENABLED: broker_config.broker_ssl_enabled,
            env.CLOUDIFY_BROKER_SSL_CERT_PATH: (
                self.cloudify_agent['broker_ssl_cert_path']
            ),
            env.CLOUDIFY_HEARTBEAT: (
                self.cloudify_agent.get('heartbeat')
            ),

            # these are variables that have default values that will be set
            # by the agent on the remote host if not set here
            env.CLOUDIFY_DAEMON_USER: self.cloudify_agent.get('user'),
            env.CLOUDIFY_REST_PORT: self.cloudify_agent.get('rest_port'),
            env.CLOUDIFY_REST_TOKEN: get_rest_token(),
            env.CLOUDIFY_REST_TENANT: tenant_name,
            env.CLOUDIFY_DAEMON_MAX_WORKERS: self.cloudify_agent.get(
                'max_workers'),
            env.CLOUDIFY_DAEMON_MIN_WORKERS: self.cloudify_agent.get(
                'min_workers'),
            env.CLOUDIFY_DAEMON_PROCESS_MANAGEMENT:
            self.cloudify_agent['process_management']['name'],
            env.CLOUDIFY_DAEMON_WORKDIR: self.cloudify_agent['workdir'],
            env.CLOUDIFY_DAEMON_EXTRA_ENV:
            self.create_custom_env_file_on_target(
                self.cloudify_agent.get('env', {})),
            env.CLOUDIFY_BYPASS_MAINTENANCE_MODE: get_is_bypass_maintenance(),
            env.CLOUDIFY_LOCAL_REST_CERT_PATH: (
                self.cloudify_agent['agent_rest_cert_path']
            ),
            env.CLOUDIFY_CLUSTER_NODES: base64.b64encode(json.dumps(
                self.cloudify_agent.get('cluster', []))),
            env.CLOUDIFY_NETWORK_NAME: network,
            ENV_CFY_EXEC_TEMPDIR: self.cloudify_agent.get(
                'executable_temp_path'),
            ENV_AGENT_LOG_LEVEL: self.cloudify_agent.get('log_level'),
            ENV_AGENT_LOG_MAX_BYTES: self.cloudify_agent.get('log_max_bytes'),
            ENV_AGENT_LOG_MAX_HISTORY: self.cloudify_agent.get(
                'log_max_history')
        }

        execution_env = utils.purge_none_values(execution_env)
        execution_env = utils.stringify_values(execution_env)

        self.logger.debug('Cloudify Agent will be created using the following '
                          'environment: {0}'.format(execution_env))

        return execution_env
Ejemplo n.º 12
0
    def _create_agent_env(self):
        # pop to not leave creds around
        tenant = self.cloudify_agent.pop('tenant')
        tenant_name = tenant.get('name', defaults.DEFAULT_TENANT_NAME)
        tenant_user = tenant.get('rabbitmq_username',
                                 broker_config.broker_username)
        tenant_pass = tenant.get('rabbitmq_password',
                                 broker_config.broker_password)
        broker_vhost = tenant.get('rabbitmq_vhost', broker_config.broker_vhost)
        # Get the agent's broker credentials
        broker_user = self.cloudify_agent.get('broker_user', tenant_user)
        broker_pass = self.cloudify_agent.get('broker_pass', tenant_pass)

        network = self.cloudify_agent.get('network')
        execution_env = {
            # mandatory values calculated before the agent
            # is actually created
            env.CLOUDIFY_DAEMON_QUEUE:
            self.cloudify_agent['queue'],
            env.AGENT_NAME:
            self.cloudify_agent['name'],
            env.CLOUDIFY_REST_HOST:
            ','.join(self.cloudify_agent['rest_host']),
            env.CLOUDIFY_BROKER_IP:
            ','.join(self.cloudify_agent['broker_ip']),

            # Optional broker values
            env.CLOUDIFY_BROKER_USER:
            broker_user,
            env.CLOUDIFY_BROKER_PASS:
            broker_pass,
            env.CLOUDIFY_BROKER_VHOST:
            broker_vhost,
            env.CLOUDIFY_BROKER_SSL_ENABLED:
            broker_config.broker_ssl_enabled,
            env.CLOUDIFY_BROKER_SSL_CERT_PATH:
            (self.cloudify_agent['broker_ssl_cert_path']),
            env.CLOUDIFY_HEARTBEAT: (self.cloudify_agent.get('heartbeat')),

            # these are variables that have default values that will be set
            # by the agent on the remote host if not set here
            env.CLOUDIFY_DAEMON_USER:
            self.cloudify_agent.get('user'),
            env.CLOUDIFY_REST_PORT:
            self.cloudify_agent.get('rest_port'),
            env.CLOUDIFY_REST_TOKEN:
            get_rest_token(),
            env.CLOUDIFY_REST_TENANT:
            tenant_name,
            env.CLOUDIFY_DAEMON_MAX_WORKERS:
            self.cloudify_agent.get('max_workers'),
            env.CLOUDIFY_DAEMON_MIN_WORKERS:
            self.cloudify_agent.get('min_workers'),
            env.CLOUDIFY_DAEMON_PROCESS_MANAGEMENT:
            self.cloudify_agent['process_management']['name'],
            env.CLOUDIFY_DAEMON_WORKDIR:
            self.cloudify_agent['workdir'],
            env.CLOUDIFY_DAEMON_EXTRA_ENV:
            self.create_custom_env_file_on_target(
                self.cloudify_agent.get('env', {})),
            env.CLOUDIFY_BYPASS_MAINTENANCE_MODE:
            get_is_bypass_maintenance(),
            env.CLOUDIFY_LOCAL_REST_CERT_PATH:
            (self.cloudify_agent['agent_rest_cert_path']),
            env.CLOUDIFY_NETWORK_NAME:
            network,
            ENV_CFY_EXEC_TEMPDIR:
            self.cloudify_agent.get('executable_temp_path'),
            ENV_AGENT_LOG_LEVEL:
            self.cloudify_agent.get('log_level'),
            ENV_AGENT_LOG_MAX_BYTES:
            self.cloudify_agent.get('log_max_bytes'),
            ENV_AGENT_LOG_MAX_HISTORY:
            self.cloudify_agent.get('log_max_history')
        }

        execution_env = utils.purge_none_values(execution_env)
        execution_env = utils.stringify_values(execution_env)

        self.logger.debug('Cloudify Agent will be created using the following '
                          'environment: {0}'.format(execution_env))

        return execution_env