def _stop_old_agent(new_agent, old_agent, timeout): ctx.logger.info('Stopping old Cloudify agent...') _, script_url = stop_agent_script_download_link( new_agent, old_agent_name=old_agent['name']) _run_script(new_agent, script_url, timeout) ctx.logger.info('Old Cloudify agent stopped') update_agent_record(old_agent, AgentState.STOPPED)
def stop(cloudify_agent, installer, **_): """ Only called in "remote" mode - other modes stop via AMQP """ ctx.logger.info('Stopping Agent {0}'.format(cloudify_agent['name'])) update_agent_record(cloudify_agent, AgentState.STOPPING) installer.stop_agent() update_agent_record(cloudify_agent, AgentState.STOPPED) script.cleanup_scripts()
def _stop_old_agent(new_agent, old_agent, timeout): ctx.logger.info('Stopping old Cloudify agent...') _, script_url = stop_agent_script_download_link( new_agent, old_agent_name=old_agent['name'] ) _run_script(new_agent, script_url, timeout) ctx.logger.info('Old Cloudify agent stopped') update_agent_record(old_agent, AgentState.STOPPED)
def delete(cloudify_agent, installer, **_): update_agent_record(cloudify_agent, AgentState.DELETING) # delete the runtime properties set on create if cloudify_agent.has_installer: ctx.logger.info('Deleting Agent {0}'.format(cloudify_agent['name'])) installer.delete_agent() ctx.instance.runtime_properties.pop('cloudify_agent', None) ctx.instance.update() update_agent_record(cloudify_agent, AgentState.DELETED) # TODO: Delete the RabbitMQ queue after deleting the agent delete_agent_rabbitmq_user(cloudify_agent)
def create_agent_amqp(install_agent_timeout=300, manager_ip=None, manager_certificate=None, stop_old_agent=False, **_): """ Installs a new agent on a host machine. :param install_agent_timeout: operation's timeout. :param manager_ip: the private IP of the current leader (master) Manager. This IP is used to connect to the Manager's RabbitMQ. (relevant only in HA cluster) :param manager_certificate: the SSL certificate of the current leader (master) Manager. (relevant only in HA cluster) :param stop_old_agent: if set, stop the old agent after successfully installing the new one """ old_agent = _validate_agent() update_agent_record(old_agent, AgentState.UPGRADING) _update_broker_config(old_agent, manager_ip, manager_certificate) agents = _run_install_script(old_agent, install_agent_timeout) new_agent = agents['new'] ctx.logger.info('Installed agent {0}'.format(new_agent['name'])) create_agent_record(new_agent, AgentState.STARTING) result = _validate_current_amqp(new_agent) if not result['agent_alive']: update_agent_record(new_agent, AgentState.FAILED) raise RecoverableError('New agent did not start and connect') update_agent_record(new_agent, AgentState.STARTED) update_agent_record(old_agent, AgentState.UPGRADED) if stop_old_agent: _stop_old_diamond(old_agent, install_agent_timeout) _stop_old_agent(new_agent, old_agent, install_agent_timeout) # Setting old_cloudify_agent in order to uninstall it later. ctx.instance.runtime_properties['old_cloudify_agent'] = agents['old'] update_agent_runtime_properties(new_agent)
def delete(cloudify_agent, installer, **_): update_agent_record(cloudify_agent, AgentState.DELETING) # delete the runtime properties set on create if cloudify_agent.has_installer: ctx.logger.info('Deleting Agent {0}'.format(cloudify_agent['name'])) installer.delete_agent() ctx.instance.runtime_properties.pop('cloudify_agent', None) ctx.instance.update() update_agent_record(cloudify_agent, AgentState.DELETED) delete_agent_rabbitmq_user(cloudify_agent) try: delete_agent_exchange(cloudify_agent) delete_agent_queues(cloudify_agent) except KeyError as e: # this would happen for malformed cloudify_agent which is missing # eg. the tenant data. We'd be possibly leaving queues around, # but without that info, we can't really do anything else ctx.logger.error('Could not delete agent queues: %s', e)
def create(cloudify_agent, installer, **_): # When not in "remote" mode, this operation is called only to set the # agent_config dict in the runtime properties create_agent_record(cloudify_agent) if cloudify_agent.has_installer: with script.install_script_path(cloudify_agent) as script_path: ctx.logger.info('Creating Agent {0}'.format( cloudify_agent['name'])) try: installer.runner.run_script(script_path) except (CommandExecutionError, CommandExecutionException): ctx.logger.error("Failed creating agent; marking agent as " "failed") update_agent_record(cloudify_agent, AgentState.FAILED) raise ctx.logger.info( 'Agent created, configured and started successfully' ) update_agent_record(cloudify_agent, AgentState.STARTED) elif cloudify_agent.is_proxied: ctx.logger.info('Working in "proxied" mode') elif cloudify_agent.is_provided: ctx.logger.info('Working in "provided" mode') _, install_script_download_link = script.install_script_download_link( cloudify_agent ) ctx.logger.info( 'Agent config created. To configure/start the agent, download the ' 'following script: {0}'.format(install_script_download_link) ) cloudify_agent['install_script_download_link'] = \ install_script_download_link update_agent_runtime_properties(cloudify_agent) update_agent_record(cloudify_agent, AgentState.CREATED)
def create(cloudify_agent, installer, **_): # When not in "remote" mode, this operation is called only to set the # agent_config dict in the runtime properties create_agent_record(cloudify_agent) if cloudify_agent.has_installer: with script.install_script_path(cloudify_agent) as script_path: ctx.logger.info('Creating Agent {0}'.format( cloudify_agent['name'])) try: installer.runner.run_script(script_path) except (CommandExecutionError, CommandExecutionException): ctx.logger.error("Failed creating agent; marking agent as " "failed") update_agent_record(cloudify_agent, AgentState.FAILED) raise ctx.logger.info( 'Agent created, configured and started successfully') update_agent_record(cloudify_agent, AgentState.STARTED) elif cloudify_agent.is_proxied: ctx.logger.info('Working in "proxied" mode') elif cloudify_agent.is_provided: ctx.logger.info('Working in "provided" mode') _, install_script_download_link = script.install_script_download_link( cloudify_agent) ctx.logger.info( 'Agent config created. To configure/start the agent, download the ' 'following script: {0}'.format(install_script_download_link)) cloudify_agent['install_script_download_link'] = \ install_script_download_link update_agent_runtime_properties(cloudify_agent) update_agent_record(cloudify_agent, AgentState.CREATED)
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_agent_amqp(install_agent_timeout=300, manager_ip=None, manager_certificate=None, stop_old_agent=False, **_): """ Installs a new agent on a host machine. :param install_agent_timeout: operation's timeout. :param manager_ip: the private IP of the current leader (master) Manager. This IP is used to connect to the Manager's RabbitMQ. (relevant only in HA cluster) :param manager_certificate: the SSL certificate of the current leader (master) Manager. (relevant only in HA cluster) :param stop_old_agent: if set, stop the old agent after successfully installing the new one """ old_agent = _validate_agent() original_agent = copy.deepcopy(old_agent) update_agent_record(old_agent, AgentState.UPGRADING) _update_broker_config(old_agent, manager_ip, manager_certificate) try: agents = _run_install_script(old_agent, install_agent_timeout) except Exception: update_agent_runtime_properties(original_agent) raise new_agent = agents['new'] ctx.logger.info('Installed agent {0}'.format(new_agent['name'])) create_agent_record(new_agent, AgentState.STARTING) result = _validate_current_amqp(new_agent) if not result['agent_alive']: update_agent_record(new_agent, AgentState.FAILED) update_agent_runtime_properties(original_agent) raise RecoverableError('New agent did not start and connect') update_agent_record(new_agent, AgentState.STARTED) update_agent_record(old_agent, AgentState.UPGRADED) if stop_old_agent: _stop_old_diamond(old_agent, install_agent_timeout) _stop_old_agent(new_agent, old_agent, install_agent_timeout) # Setting old_cloudify_agent in order to uninstall it later. ctx.instance.runtime_properties['old_cloudify_agent'] = agents['old'] update_agent_runtime_properties(new_agent)
def configure(cloudify_agent, installer, **_): ctx.logger.info('Configuring Agent {0}'.format(cloudify_agent['name'])) update_agent_record(cloudify_agent, AgentState.CONFIGURING) try: installer.configure_agent() except CommandExecutionError as e: ctx.logger.error(str(e)) update_agent_record(cloudify_agent, AgentState.FAILED) raise update_agent_record(cloudify_agent, AgentState.CONFIGURED)
def restart(cloudify_agent, installer, **_): # no need to handling remote_execution False because this operation is # not invoked in that case ctx.logger.info('Restarting Agent {0}'.format(cloudify_agent['name'])) update_agent_record(cloudify_agent, AgentState.RESTARTING) try: installer.restart_agent() except CommandExecutionError as e: ctx.logger.error(str(e)) update_agent_record(cloudify_agent, AgentState.FAILED) raise update_agent_record(cloudify_agent, AgentState.RESTARTED)
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()