def get(execution_id): logger = get_logger() rest_host = utils.get_rest_host() client = utils.get_rest_client(rest_host) try: logger.info('Getting execution: ' '\'{0}\' [manager={1}]' .format(execution_id, rest_host)) execution = client.executions.get(execution_id) except exceptions.CloudifyClientError as e: if e.status_code != 404: raise raise CloudifyCliError('Execution {0} not found'.format(execution_id)) pt = utils.table(['id', 'workflow_id', 'status', 'deployment_id', 'created_at', 'error'], [execution]) pt.max_width = 50 utils.print_table('Executions:', pt) # print execution parameters logger.info('Execution Parameters:') for param_name, param_value in utils.decode_dict( execution.parameters).iteritems(): logger.info('\t{0}: \t{1}'.format(param_name, param_value)) if execution.status in (execution.CANCELLING, execution.FORCE_CANCELLING): logger.info(_STATUS_CANCELING_MESSAGE) logger.info('')
def create(blueprint_id, deployment_id, inputs): logger = get_logger() rest_host = utils.get_rest_host() inputs = utils.inputs_to_dict(inputs, 'inputs') logger.info('Creating new deployment from blueprint {0} at ' 'management server {1}' .format(blueprint_id, rest_host)) client = utils.get_rest_client(rest_host) try: deployment = client.deployments.create(blueprint_id, deployment_id, inputs=inputs) except MissingRequiredDeploymentInputError as e: logger.info('Unable to create deployment. Not all ' 'required inputs have been specified...') _print_deployment_inputs(client, blueprint_id) raise SuppressedCloudifyCliError(str(e)) except UnknownDeploymentInputError as e: logger.info( 'Unable to create deployment, an unknown input was specified...') _print_deployment_inputs(client, blueprint_id) raise SuppressedCloudifyCliError(str(e)) logger.info("Deployment created. The deployment's id is {0}".format( deployment.id))
def download(plugin_id, output): logger = get_logger() rest_host = utils.get_rest_host() logger.info("Downloading plugin '{0}' from management server {1}...".format(plugin_id, rest_host)) client = utils.get_rest_client(rest_host) target_file = client.plugins.download(plugin_id, output) logger.info("Plugin downloaded as {0}".format(target_file))
def ls(deployment_id, include_system_workflows, sort_by=None, descending=False): logger = get_logger() rest_host = utils.get_rest_host() client = utils.get_rest_client(rest_host) try: if deployment_id: logger.info('Listing executions for deployment: \'{0}\' ' '[manager={1}]'.format(deployment_id, rest_host)) else: logger.info( 'Listing all executions: [manager={0}]'.format( rest_host)) executions = client.executions.list( deployment_id=deployment_id, include_system_workflows=include_system_workflows, sort=sort_by, is_descending=descending) except exceptions.CloudifyClientError as e: if e.status_code != 404: raise raise CloudifyCliError('Deployment {0} does not exist'.format( deployment_id)) columns = ['id', 'workflow_id', 'deployment_id', 'status', 'created_at'] pt = utils.table(columns, executions) utils.print_table('Executions:', pt) if any(execution.status in (execution.CANCELLING, execution.FORCE_CANCELLING) for execution in executions): logger.info(_STATUS_CANCELING_MESSAGE)
def get(blueprint_id): logger = get_logger() rest_host = utils.get_rest_host() client = utils.get_rest_client(rest_host) logger.info('Retrieving blueprint: ' '\'{0}\' [manager={1}]' .format(blueprint_id, rest_host)) blueprint = client.blueprints.get(blueprint_id) deployments = client.deployments.list(_include=['id'], blueprint_id=blueprint_id) blueprint['#deployments'] = len(deployments) pt = utils.table(['id', 'main_file_name', 'created_at', 'updated_at', '#deployments'], [blueprint]) pt.max_width = 50 utils.print_table('Blueprint:', pt) logger.info('Description:') logger.info('{0}\n'.format(blueprint['description'] if blueprint['description'] is not None else '')) logger.info('Existing deployments:') logger.info('{0}\n'.format(json.dumps([d['id'] for d in deployments])))
def ls(sort_by=None, descending=False): logger = get_logger() rest_host = utils.get_rest_host() client = utils.get_rest_client(rest_host) logger.info('Getting blueprints list... [manager={0}]' .format(rest_host)) def trim_description(blueprint): if blueprint['description'] is not None: if len(blueprint['description']) >= DESCRIPTION_LIMIT: blueprint['description'] = '{0}..'.format( blueprint['description'][:DESCRIPTION_LIMIT - 2]) else: blueprint['description'] = '' return blueprint blueprints = [trim_description(b) for b in client.blueprints.list( sort=sort_by, is_descending=descending)] pt = utils.table(['id', 'description', 'main_file_name', 'created_at', 'updated_at'], data=blueprints) utils.print_table('Available blueprints:', pt)
def ls(blueprint_id, sort_by=None, descending=False): logger = get_logger() rest_host = utils.get_rest_host() client = utils.get_rest_client(rest_host) if blueprint_id: logger.info("Listing deployments for blueprint: " "'{0}'... [manager={1}]" .format(blueprint_id, rest_host)) else: logger.info('Listing all deployments...[manager={0}]' .format(rest_host)) deployments = client.deployments.list( sort=sort_by, is_descending=descending) if blueprint_id: deployments = filter(lambda deployment: deployment['blueprint_id'] == blueprint_id, deployments) pt = utils.table( ['id', 'blueprint_id', 'created_at', 'updated_at'], deployments) utils.print_table('Deployments:', pt)
def get(node_instance_id): logger = get_logger() rest_host = utils.get_rest_host() client = utils.get_rest_client(rest_host) logger.info('Retrieving node instance with ID: \'{0}\' [manager={1}]' .format(node_instance_id, rest_host)) try: node_instance = client.node_instances.get(node_instance_id) except CloudifyClientError as e: if e.status_code != 404: raise raise CloudifyCliError('Node instance {0} not found') columns = ['id', 'deployment_id', 'host_id', 'node_id', 'state'] pt = utils.table(columns, [node_instance]) pt.max_width = 50 utils.print_table('Instance:', pt) # print node instance runtime properties logger.info('Instance runtime properties:') for prop_name, prop_value in utils.decode_dict( node_instance.runtime_properties).iteritems(): logger.info('\t{0}: {1}'.format(prop_name, prop_value)) logger.info('')
def delete(blueprint_id): logger = get_logger() rest_host = utils.get_rest_host() logger.info('Deleting blueprint {0} from management server {1}' .format(blueprint_id, rest_host)) client = utils.get_rest_client(rest_host) client.blueprints.delete(blueprint_id) logger.info('Blueprint deleted')
def download(snapshot_id, output): logger = get_logger() rest_host = utils.get_rest_host() logger.info("Downloading snapshot '{0}'... [manager={1}]".format( snapshot_id, rest_host)) client = utils.get_rest_client(rest_host) target_file = client.snapshots.download(snapshot_id, output) logger.info('Snapshot downloaded as {0}'.format(target_file))
def delete(snapshot_id): logger = get_logger() rest_host = utils.get_rest_host() logger.info("Deleting snapshot '{0}' from management server {1}" .format(snapshot_id, rest_host)) client = utils.get_rest_client(rest_host) client.snapshots.delete(snapshot_id) logger.info('Snapshot deleted successfully')
def delete(deployment_id, ignore_live_nodes): logger = get_logger() rest_host = utils.get_rest_host() logger.info('Deleting deployment {0} from management server {1}' .format(deployment_id, rest_host)) client = utils.get_rest_client(rest_host) client.deployments.delete(deployment_id, ignore_live_nodes) logger.info("Deployment deleted")
def delete(plugin_id, force): logger = get_logger() rest_host = utils.get_rest_host() client = utils.get_rest_client(rest_host) logger.info("Deleting plugin '{0}' from management server {1}".format(plugin_id, rest_host)) client.plugins.delete(plugin_id=plugin_id, force=force) logger.info("Plugin deleted")
def upload(snapshot_path, snapshot_id): logger = get_logger() rest_host = utils.get_rest_host() logger.info("Uploading snapshot '{0}' to management server {1}" .format(snapshot_path.name, rest_host)) client = utils.get_rest_client(rest_host) snapshot = client.snapshots.upload(snapshot_path.name, snapshot_id) logger.info("Snapshot uploaded. The snapshot's id is {0}".format( snapshot.id))
def ls(sort_by=None, descending=False): logger = get_logger() rest_host = utils.get_rest_host() client = utils.get_rest_client(rest_host) logger.info('Listing all snapshots... [manager={0}]' .format(rest_host)) pt = utils.table(['id', 'created_at', 'status', 'error'], data=client.snapshots.list( sort=sort_by, is_descending=descending)) print_table('Snapshots:', pt)
def restore(snapshot_id, without_deployments_envs, force): logger = get_logger() rest_host = utils.get_rest_host() logger.info("Restoring snapshot '{0}' at management server {1}" .format(snapshot_id, rest_host)) client = utils.get_rest_client(rest_host) execution = client.snapshots.restore( snapshot_id, not without_deployments_envs, force) logger.info("Started workflow execution. The execution's id is {0}".format( execution.id))
def ls(sort_by=None, descending=False): logger = get_logger() rest_host = utils.get_rest_host() client = utils.get_rest_client(rest_host) logger.info("Listing all plugins... [manager={0}]".format(rest_host)) plugins = client.plugins.list(_include=fields, sort=sort_by, is_descending=descending) pt = utils.table(fields, data=plugins) print_table("Plugins:", pt)
def get(plugin_id): logger = get_logger() rest_host = utils.get_rest_host() client = utils.get_rest_client(rest_host) logger.info("Retrieving plugin {0}... [manager={1}]".format(plugin_id, rest_host)) plugin = client.plugins.get(plugin_id, _include=fields) pt = utils.table(fields, data=[plugin]) print_table("Plugin:", pt)
def get(deployment_id, workflow_id): logger = get_logger() rest_host = utils.get_rest_host() client = utils.get_rest_client(rest_host) try: logger.info('Retrieving workflow ' '\'{0}\' of deployment \'{1}\' [manager={2}]' .format(workflow_id, deployment_id, rest_host)) deployment = client.deployments.get(deployment_id) workflow = next((wf for wf in deployment.workflows if wf.name == workflow_id), None) if not workflow: raise CloudifyCliError( 'Workflow {0} not found'.format(workflow_id, deployment_id)) except CloudifyClientError as e: if e.status_code != 404: raise raise CloudifyCliError('Deployment {0} not found'.format( deployment_id)) pt = utils.table(['blueprint_id', 'deployment_id', 'name', 'created_at'], data=[workflow], defaults={'blueprint_id': deployment.blueprint_id, 'deployment_id': deployment.id}) utils.print_table('Workflows:', pt) # print workflow parameters mandatory_params = dict() optional_params = dict() for param_name, param in utils.decode_dict( workflow.parameters).iteritems(): params_group = optional_params if 'default' in param else \ mandatory_params params_group[param_name] = param logger.info('Workflow Parameters:') logger.info('\tMandatory Parameters:') for param_name, param in mandatory_params.iteritems(): if 'description' in param: logger.info('\t\t{0}\t({1})'.format(param_name, param['description'])) else: logger.info('\t\t{0}'.format(param_name)) logger.info('\tOptional Parameters:') for param_name, param in optional_params.iteritems(): if 'description' in param: logger.info('\t\t{0}: \t{1}\t({2})'.format( param_name, param['default'], param['description'])) else: logger.info('\t\t{0}: \t{1}'.format(param_name, param['default'])) logger.info('')
def create(snapshot_id, include_metrics, exclude_credentials): logger = get_logger() rest_host = utils.get_rest_host() logger.info("Creating snapshot '{0}' to management server {1}" .format(snapshot_id, rest_host)) client = utils.get_rest_client(rest_host) execution = client.snapshots.create(snapshot_id, include_metrics, not exclude_credentials) logger.info("Started workflow execution. The execution's id is {0}".format( execution.id))
def cancel(execution_id, force): logger = get_logger() rest_host = utils.get_rest_host() client = utils.get_rest_client(rest_host) logger.info( '{0}Cancelling execution {1} on management server {2}' .format('Force-' if force else '', execution_id, rest_host)) client.executions.cancel(execution_id, force) logger.info( 'A cancel request for execution {0} has been sent to management ' "server {1}. To track the execution's status, use:\n" "cfy executions get -e {0}" .format(execution_id, rest_host))
def upload(blueprint_path, blueprint_id, validate_blueprint): logger = get_logger() rest_host = utils.get_rest_host() if validate_blueprint: validate(blueprint_path) else: logger.debug("Skipping blueprint validation...") logger.info('Uploading blueprint {0} to management server {1}' .format(blueprint_path.name, rest_host)) client = utils.get_rest_client(rest_host) blueprint = client.blueprints.upload(blueprint_path.name, blueprint_id) logger.info("Blueprint uploaded. " "The blueprint's id is {0}".format(blueprint.id))
def ls(execution_id, include_logs, tail, json): logger = get_logger() rest_host = utils.get_rest_host() logger.info("Getting events from management server {0} for " "execution id '{1}' " "[include_logs={2}]".format(rest_host, execution_id, include_logs)) client = utils.get_rest_client(rest_host) try: execution_events = ExecutionEventsFetcher( client, execution_id, include_logs=include_logs) events_logger = get_events_logger(json) if tail: execution = wait_for_execution(client, client.executions.get(execution_id), events_handler=events_logger, include_logs=include_logs, timeout=None) # don't timeout ever if execution.error: logger.info('Execution of workflow {0} for deployment ' '{1} failed. [error={2}]'.format( execution.workflow_id, execution.deployment_id, execution.error)) raise SuppressedCloudifyCliError() else: logger.info('Finished executing workflow {0} on deployment ' '{1}'.format( execution.workflow_id, execution.deployment_id)) else: # don't tail, get only the events created until now and return events = execution_events.fetch_and_process_events( events_handler=events_logger) logger.info('\nTotal events: {0}'.format(events)) except CloudifyClientError as e: if e.status_code != 404: raise raise CloudifyCliError('Execution {0} not found'.format(execution_id))
def inputs(blueprint_id): logger = get_logger() rest_host = utils.get_rest_host() client = utils.get_rest_client(rest_host) logger.info('Retrieving inputs for blueprint {0}... [manager={1}]' .format(blueprint_id, rest_host)) blueprint = client.blueprints.get(blueprint_id) inputs = blueprint['plan']['inputs'] data = [{'name': name, 'type': input.get('type', '-'), 'default': input.get('default', '-'), 'description': input.get('description', '-')} for name, input in inputs.iteritems()] pt = utils.table(['name', 'type', 'default', 'description'], data=data) utils.print_table('Inputs:', pt)
def get(deployment_id, node_id): logger = get_logger() rest_host = utils.get_rest_host() client = utils.get_rest_client(rest_host) logger.info('Retrieving node: \'{0}\' for deployment with ID \'{1}\' ' '[manager={2}]'.format(node_id, deployment_id, rest_host)) try: node = client.nodes.get(deployment_id, node_id) except CloudifyClientError as e: if e.status_code != 404: raise raise CloudifyCliError('Node {0} was not found'.format(node_id)) logger.debug('Getting node instances for node with ID \'{0}\'' .format(node_id)) try: instances = client.node_instances.list(deployment_id, node_id) except CloudifyClientError as e: if e.status_code != 404: raise # print node parameters columns = ['id', 'deployment_id', 'blueprint_id', 'host_id', 'type', 'number_of_instances', 'planned_number_of_instances'] pt = utils.table(columns, [node]) pt.max_width = 50 utils.print_table('Node:', pt) # print node properties logger.info('Node properties:') for property_name, property_value in utils.decode_dict( node.properties).iteritems(): logger.info('\t{0}: {1}'.format(property_name, property_value)) logger.info('') # print node instances IDs logger.info('Node instance IDs:') if instances: for instance in instances: logger.info('\t{0}'.format(instance['id'])) else: logger.info('\tNo node instances')
def outputs(deployment_id): logger = get_logger() rest_host = utils.get_rest_host() client = utils.get_rest_client(rest_host) logger.info("Getting outputs for deployment: {0} [manager={1}]".format( deployment_id, rest_host)) dep = client.deployments.get(deployment_id, _include=['outputs']) outputs_def = dep.outputs response = client.deployments.outputs.get(deployment_id) outputs_ = StringIO() for output_name, output in response.outputs.iteritems(): outputs_.write(' - "{0}":{1}'.format(output_name, os.linesep)) description = outputs_def[output_name].get('description', '') outputs_.write(' Description: {0}{1}'.format(description, os.linesep)) outputs_.write(' Value: {0}{1}'.format(output, os.linesep)) logger.info(outputs_.getvalue())
def teardown(force, ignore_deployments): _validate_force(force) try: management_ip = utils.get_rest_host() except exceptions.CloudifyCliError: # management ip does not exist in the local context # this can mean one of two things: # 1. bootstrap was unsuccessful # 2. we are in the wrong directory try: bs.load_env() # this means we are probably in the right directory # which means the teardown was unsuccessful, try to teardown # anyway except BaseException: # this means we are in the wrong directory, have the user # execute the 'use' command to retrieve manager deployment, # because other wise we cannot bootstrap from here. If the # manager is down, the user must return to the original # directory in order to teardown raise exceptions.CloudifyCliError( "You are attempting to teardown from an " "invalid directory. Please execute `cfy use` before " "running this command. If the manager is " "unavailable, you must execute this command from the " "directory you initially bootstrapped from, or from the last " "directory a `cfy use` command was executed on this manager.") else: _do_teardown() else: # make sure we don't teardown the manager if there are running # deployments, unless the user explicitly specified it. _validate_deployments(ignore_deployments, management_ip) # update local provider context since the server ip might have # changed in case it has gone through a recovery process. _update_local_provider_context(management_ip) # execute teardown _do_teardown()
def publish_archive(archive_location, blueprint_filename, blueprint_id): logger = get_logger() rest_host = utils.get_rest_host() check_if_archive_type_is_supported(archive_location) archive_location, archive_location_type = \ determine_archive_type(archive_location) logger.info('Publishing blueprint archive from {0} {1} to management ' 'server {2}' .format(archive_location_type, archive_location, rest_host)) client = utils.get_rest_client(rest_host) blueprint = client.blueprints.publish_archive( archive_location, blueprint_id, blueprint_filename) logger.info("Blueprint archive published. " "The blueprint's id is {0}".format(blueprint.id))
def ls(deployment_id): logger = get_logger() rest_host = utils.get_rest_host() client = utils.get_rest_client(rest_host) logger.info('Listing workflows for deployment: ' '\'{0}\'... [manager={1}]' .format(deployment_id, rest_host)) deployment = client.deployments.get(deployment_id) workflows = deployment.workflows workflows = sorted(workflows, key=lambda w: w.name) pt = utils.table(['blueprint_id', 'deployment_id', 'name', 'created_at'], data=workflows, defaults={'blueprint_id': deployment.blueprint_id, 'deployment_id': deployment.id}) utils.print_table('Workflows:', pt)
def status(): logger = get_logger() rest_host = utils.get_rest_host() logger.info('Retrieving management services status... [ip={0}]' .format(rest_host)) client = utils.get_rest_client(rest_host) try: status_result = client.manager.get_status() maintenance_response = client.maintenance_mode.status() except UserUnauthorizedError: logger.info( "Failed to query manager services status: User is unauthorized") return False except CloudifyClientError as e: logger.info('REST service at management server ' '{0} is not responding! error: {1}' .format(rest_host, e)) return False services = [] for service in status_result['services']: state = service['instances'][0]['state'] \ if 'instances' in service and \ len(service['instances']) > 0 else 'unknown' services.append({ 'service': service['display_name'].ljust(30), 'status': state }) pt = utils.table(['service', 'status'], data=services) utils.print_table('Services:', pt) maintenance_status = maintenance_response.status if maintenance_status != 'deactivated': logger.info('Maintenance mode is {0}.\n'.format( maintenance_response.status)) return True