def reboot(nova_client, **_): server = get_server_by_context(nova_client) ctx.logger.info('Rebooting machine') nova_client.servers.reboot(server) i = 0 vm_started = False while i < VM_ATTEMPTS and not vm_started: ctx.logger.info('Waiting for vm to reboot, attempt {0}'.format(i + 1)) time.sleep(INTERVAL) server = get_server_by_context(nova_client) vm_started = server.status == SERVER_STATUS_ACTIVE i += 1 if vm_started: ctx.logger.info('Machine rebooted') else: raise NonRecoverableError('Could not reboot machine') agent_name = ctx.instance.runtime_properties['cloudify_agent']['name'] i = 0 agent_alive = False amqp_client = get_client(amqp_vhost=ctx.tenant['rabbitmq_vhost'], amqp_user=ctx.tenant['rabbitmq_username'], amqp_pass=ctx.tenant['rabbitmq_password']) while i < AGENT_ATTEMPTS and not agent_alive: ctx.logger.info('Waiting for agent, attempt {0}'.format(i + 1)) time.sleep(INTERVAL) agent_alive = is_agent_alive(agent_name, amqp_client) i += 1 if agent_alive: ctx.logger.info('Agent started') else: raise NonRecoverableError('Agent did not start')
def reboot(nova_client, **_): server = get_server_by_context(nova_client) ctx.logger.info('Rebooting machine') nova_client.servers.reboot(server) i = 0 vm_started = False while i < VM_ATTEMPTS and not vm_started: ctx.logger.info('Waiting for vm to reboot, attempt {0}'.format(i + 1)) time.sleep(INTERVAL) server = get_server_by_context(nova_client) vm_started = server.status == SERVER_STATUS_ACTIVE i += 1 if vm_started: ctx.logger.info('Machine rebooted') else: raise NonRecoverableError('Could not reboot machine') agent_name = ctx.instance.runtime_properties['cloudify_agent']['name'] i = 0 agent_alive = False amqp_client = get_client( amqp_vhost=ctx.tenant['rabbitmq_vhost'], amqp_user=ctx.tenant['rabbitmq_username'], amqp_pass=ctx.tenant['rabbitmq_password'] ) while i < AGENT_ATTEMPTS and not agent_alive: ctx.logger.info('Waiting for agent, attempt {0}'.format(i + 1)) time.sleep(INTERVAL) agent_alive = is_agent_alive(agent_name, amqp_client) i += 1 if agent_alive: ctx.logger.info('Agent started') else: raise NonRecoverableError('Agent did not start')
def _get_amqp_client(agent): delete_cert_path = False if agent.get('broker_config'): broker_config = agent['broker_config'] ssl_cert_path = _get_ssl_cert_path(broker_config) # Using a temp path, so we need to delete it delete_cert_path = True else: broker_config = _get_broker_config(agent) ssl_cert_path = get_local_rest_certificate() tenant = get_tenant() try: yield amqp_client.get_client( amqp_host=broker_config.get('broker_ip'), amqp_user=tenant.get('rabbitmq_username'), amqp_port=broker_config.get('broker_port'), amqp_pass=tenant.get('rabbitmq_password'), amqp_vhost=tenant.get('rabbitmq_vhost'), ssl_enabled=broker_config.get('broker_ssl_enabled'), ssl_cert_path=ssl_cert_path ) finally: if delete_cert_path and ssl_cert_path: os.remove(ssl_cert_path)
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(exchange=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')
def main(args): postgres_publisher = PostgreSQLPublisher( host=args.postgres_hostname, db=args.postgres_db, user=args.postgres_user, password=args.postgres_password ) # This arg is a string for ease of use with the manager blueprint if args.amqp_ssl_enabled.lower() == 'true': ssl_enabled = True amqp_port = BROKER_PORT_SSL else: ssl_enabled = False amqp_port = BROKER_PORT_NO_SSL amqp_client = get_client( amqp_host=args.amqp_hostname, amqp_user=args.amqp_username, amqp_pass=args.amqp_password, amqp_vhost='/', amqp_port=amqp_port, ssl_enabled=ssl_enabled, ssl_cert_path=args.amqp_ca_cert_path ) amqp_consumer = AMQPLogsEventsConsumer( message_processor=postgres_publisher.process ) amqp_client.add_handler(amqp_consumer) with postgres_publisher: amqp_client.consume()
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')
def start(cloudify_agent, **_): """ Only called in "init_script"/"plugin" mode, where the agent is started externally (e.g. userdata script), and all we have to do is wait for it """ update_agent_record(cloudify_agent, AgentState.STARTING) tenant = cloudify_utils.get_tenant() client = get_client( amqp_user=tenant['rabbitmq_username'], amqp_pass=tenant['rabbitmq_password'], amqp_vhost=tenant['rabbitmq_vhost'] ) agent_alive = utils.is_agent_alive(cloudify_agent['queue'], client) if not agent_alive: if ctx.operation.retry_number > 3: ctx.logger.warning('Waiting too long for Agent to start') update_agent_record(cloudify_agent, AgentState.NONRESPONSIVE) return ctx.operation.retry( message='Waiting for Agent to start...') ctx.logger.info('Agent has started') update_agent_record(cloudify_agent, AgentState.STARTED) if not cloudify_agent.is_provided: script.cleanup_scripts()
def _get_client(self): return get_client(self.broker_ip, self.broker_user, self.broker_pass, self.broker_port, self.broker_vhost, self.broker_ssl_enabled, self.broker_ssl_cert_path, name=self.name)
def _get_amqp_client(): client = get_client(amqp_host=config.instance.amqp_host, amqp_user=config.instance.amqp_username, amqp_pass=config.instance.amqp_password, amqp_port=BROKER_PORT_SSL, amqp_vhost='/', ssl_enabled=True, ssl_cert_path=config.instance.amqp_ca_path) return client
def get_amqp_client(tenant=None): vhost = '/' if tenant is None else tenant.rabbitmq_vhost client = get_client(amqp_host=config.instance.amqp_host, amqp_user=config.instance.amqp_username, amqp_pass=config.instance.amqp_password, amqp_port=BROKER_PORT_SSL, amqp_vhost=vhost, ssl_enabled=True, ssl_cert_path=config.instance.amqp_ca_path) return client
def _get_client(self): return get_client( self.broker_ip, self.broker_user, self.broker_pass, self.broker_port, self.broker_vhost, self.broker_ssl_enabled, self.broker_ssl_cert_path, name=self.name )
def get_amqp_client(): return get_client( amqp_host=config.instance.amqp_host, amqp_user=config.instance.amqp_username, amqp_pass=config.instance.amqp_password, amqp_port=BROKER_PORT_SSL, amqp_vhost='/', ssl_enabled=True, ssl_cert_path=config.instance.amqp_ca_path, connect_timeout=3, )
def _get_amqp_client(): client = get_client( amqp_host=config.instance.amqp_host, amqp_user=config.instance.amqp_username, amqp_pass=config.instance.amqp_password, amqp_port=BROKER_PORT_SSL, amqp_vhost='/', ssl_enabled=True, ssl_cert_path=config.instance.amqp_ca_path ) return client
def delete_exchange(host, user, password, vhost, cert_path): client = amqp_client.get_client(amqp_host=host, amqp_user=user, amqp_port=5671, amqp_pass=password, amqp_vhost=vhost, ssl_enabled=True, ssl_cert_path=cert_path) client.connect() with client.channel() as channel: channel.exchange_delete('cloudify-events')
def _send_mgmtworker_task(message, routing_key='workflow'): """Send a message to the mgmtworker exchange""" client = get_client(amqp_host=config.instance.amqp_host, amqp_user=config.instance.amqp_username, amqp_pass=config.instance.amqp_password, amqp_port=BROKER_SSL_PORT, amqp_vhost='/', ssl_enabled=True, ssl_cert_path=config.instance.amqp_ca_path) send_handler = SendHandler(MGMTWORKER_QUEUE, routing_key=routing_key) client.add_handler(send_handler) with client: send_handler.publish(message)
def _send_cancel_task(self, target, execution_id): """Send a cancel-operation task to the agent given by `target`""" message = { 'service_task': { 'task_name': 'cancel-operation', 'kwargs': {'execution_id': execution_id} } } if target == MGMTWORKER_QUEUE: client = get_client() else: tenant = workflow_ctx.tenant client = get_client( amqp_user=tenant['rabbitmq_username'], amqp_pass=tenant['rabbitmq_password'], amqp_vhost=tenant['rabbitmq_vhost'] ) handler = SendHandler(exchange=target, routing_key='service') client.add_handler(handler) with client: handler.publish(message)
def _send_cancel_task(self, target, execution_id): """Send a cancel-operation task to the agent given by `target`""" message = { 'service_task': { 'task_name': 'cancel-operation', 'kwargs': { 'execution_id': execution_id } } } if target == MGMTWORKER_QUEUE: client = get_client() else: tenant = workflow_ctx.tenant client = get_client(amqp_user=tenant['rabbitmq_username'], amqp_pass=tenant['rabbitmq_password'], amqp_vhost=tenant['rabbitmq_vhost']) handler = SendHandler(exchange=target, routing_key='service') client.add_handler(handler) with client: handler.publish(message)
def start(cloudify_agent, **_): """ Only called in "init_script"/"plugin" mode, where the agent is started externally (e.g. userdata script), and all we have to do is wait for it """ tenant = cloudify_utils.get_tenant() client = get_client( amqp_user=tenant['rabbitmq_username'], amqp_pass=tenant['rabbitmq_password'], amqp_vhost=tenant['rabbitmq_vhost'] ) agent_alive = utils.is_agent_alive(cloudify_agent['queue'], client) if not agent_alive: return ctx.operation.retry( message='Waiting for Agent to start...') ctx.logger.info('Agent has started') if not cloudify_agent.is_provided: script.cleanup_scripts()
def _create_connections(): acks_queue = Queue.Queue() cfy_config = config.instance port = BROKER_PORT_SSL if cfy_config.amqp_ca_path else BROKER_PORT_NO_SSL amqp_client = get_client(amqp_host=cfy_config.amqp_host, amqp_user=cfy_config.amqp_username, amqp_pass=cfy_config.amqp_password, amqp_vhost='/', amqp_port=port, ssl_enabled=bool(cfy_config.amqp_ca_path), ssl_cert_path=cfy_config.amqp_ca_path, cls=AckingAMQPConnection) amqp_client.acks_queue = acks_queue db_publisher = DBLogEventPublisher(config.instance, amqp_client) amqp_consumer = AMQPLogsEventsConsumer( message_processor=db_publisher.process) amqp_client.add_handler(amqp_consumer) db_publisher.start() return amqp_client, db_publisher
def broker_is_healthy(): client = get_client( amqp_host=config.instance.amqp_host, amqp_user=config.instance.amqp_username, amqp_pass=config.instance.amqp_password, amqp_port=BROKER_PORT_SSL, amqp_vhost='/', ssl_enabled=True, ssl_cert_path=config.instance.amqp_ca_path, connect_timeout=3, ) try: with client: return True except Exception as err: current_app.logger.error( 'Broker check failed with {err_type}: {err_msg}'.format( err_type=type(err), err_msg=str(err), )) return False
def start(cloudify_agent, **_): """ Only called in "init_script"/"plugin" mode, where the agent is started externally (e.g. userdata script), and all we have to do is wait for it """ update_agent_record(cloudify_agent, AgentState.STARTING) tenant = cloudify_utils.get_tenant() client = get_client(amqp_user=tenant['rabbitmq_username'], amqp_pass=tenant['rabbitmq_password'], amqp_vhost=tenant['rabbitmq_vhost']) agent_alive = utils.is_agent_alive(cloudify_agent['queue'], client) if not agent_alive: if ctx.operation.retry_number > 3: ctx.logger.warning('Waiting too long for Agent to start') update_agent_record(cloudify_agent, AgentState.NONRESPONSIVE) return ctx.operation.retry(message='Waiting for Agent to start...') ctx.logger.info('Agent has started') update_agent_record(cloudify_agent, AgentState.STARTED) if not cloudify_agent.is_provided: script.cleanup_scripts()
def _create_connections(): acks_queue = Queue.Queue() cfy_config = config.instance port = BROKER_PORT_SSL if cfy_config.amqp_ca_path else BROKER_PORT_NO_SSL amqp_client = get_client( amqp_host=cfy_config.amqp_host, amqp_user=cfy_config.amqp_username, amqp_pass=cfy_config.amqp_password, amqp_vhost='/', amqp_port=port, ssl_enabled=bool(cfy_config.amqp_ca_path), ssl_cert_path=cfy_config.amqp_ca_path, cls=AckingAMQPConnection ) amqp_client.acks_queue = acks_queue db_publisher = DBLogEventPublisher(config.instance, amqp_client) amqp_consumer = AMQPLogsEventsConsumer( message_processor=db_publisher.process ) amqp_client.add_handler(amqp_consumer) db_publisher.start() return amqp_client, db_publisher
def _get_amqp_client(agent): delete_cert_path = False if agent.get('broker_config'): broker_config = agent['broker_config'] ssl_cert_path = _get_ssl_cert_path(broker_config) # Using a temp path, so we need to delete it delete_cert_path = True else: broker_config = _get_broker_config(agent) ssl_cert_path = get_local_rest_certificate() try: yield amqp_client.get_client( amqp_host=broker_config.get('broker_ip'), amqp_user=ctx.tenant.get('rabbitmq_username'), amqp_port=broker_config.get('broker_port'), amqp_pass=ctx.tenant.get('rabbitmq_password'), amqp_vhost=ctx.tenant.get('rabbitmq_vhost'), ssl_enabled=broker_config.get('broker_ssl_enabled'), ssl_cert_path=ssl_cert_path) finally: if delete_cert_path and ssl_cert_path: os.remove(ssl_cert_path)
def get_amqp_client(agent): yield get_client()
def _is_agent_alive(self, name, timeout=10): return agent_utils.is_agent_alive( name, get_client(), timeout=timeout)
def _is_agent_alive(self, name, timeout=10): return agent_utils.is_agent_alive(name, get_client(), timeout=timeout)