def blueprint(cmd_proc, operation, blueprint, blueprint_file, include_plan): """Operations with Blueprints""" scoreclient = None if 'validate' != operation: scoreclient = _authorize(cmd_proc) else: scoreclient = Score(cmd_proc.host_score) Log.debug(cmd_proc.logger, 'using host score: %s' % cmd_proc.host_score) if 'validate' == operation: _validate(cmd_proc, blueprint_file, scoreclient) elif 'list' == operation: _list_blueprints(cmd_proc, scoreclient) elif 'upload' == operation: _upload(cmd_proc, blueprint, blueprint_file, scoreclient) elif 'delete' == operation: _delete_blueprint(cmd_proc, blueprint, scoreclient) elif 'info' == operation: _info_blueprint(cmd_proc, scoreclient, include_plan=include_plan) elif 'status' == operation: try: scoreclient = _authorize(cmd_proc) status = scoreclient.get_status() print_utils.print_dict(json.loads(status)) except exceptions.ClientException as e: utils.print_error("Unable to get blueprinting service status. " "Reason: {0}" .format(str(e)), cmd_proc)
def role(cmd_proc, operation): """Operations with Roles""" if cmd_proc.vca.service_type != VCA.VCA_SERVICE_TYPE_VCA: utils.print_message('Operation not supported ' 'in this service type') sys.exit(1) result = cmd_proc.re_login() if not result: utils.print_error('Not logged in', cmd_proc) sys.exit(1) if 'list' == operation: headers = ['Role Name', 'Description', 'Permissions'] table = [] for r in cmd_proc.vca.get_roles(): permissions = [] if len(r.get('rights')) > 0: for p in r.get('rigths'): permissions.append(str(p.get('name'))) table.append([ r.get('name'), r.get('description'), utils.beautified(permissions) ]) sorted_table = sorted(table, key=operator.itemgetter(0), reverse=False) utils.print_table( "Available roles in instance '%s'" ", profile '%s':" % (cmd_proc.instance, cmd_proc.profile), headers, sorted_table, cmd_proc) cmd_proc.save_current_config()
def _info_deployment( cmd_proc, operation, deployment_id, blueprint_id, input_file, workflow, show_events, execution_id, force_cancel, force_delete, scoreclient, ): try: d = scoreclient.deployments.get(deployment_id) e = scoreclient.executions.list(deployment_id) events = None e_id = None if show_events and e is not None and len(e) > 0: table = [] for execution in e: table.append([execution.get("created_at"), execution.get("id")]) sorted_table = sorted(table, key=operator.itemgetter(0), reverse=True) e_id = sorted_table[0][1] if execution_id is None else execution_id events = scoreclient.events.get(e_id) print_deployment_info(d, e, events, e_id) except exceptions.ClientException as e: utils.print_error( "Failed to get deployment info. Reason: {0}, {1}".format(str(e), scoreclient.response.content), cmd_proc )
def _info_blueprint(cmd_proc, operation, blueprint_id, blueprint_file, include_plan, scoreclient): try: b = scoreclient.blueprints.get(blueprint_id) if blueprint_id is None or len(blueprint_id) == 0: utils.print_error('specify blueprint id') sys.exit(1) headers = ['Id', 'Created'] table = cmd_proc.blueprints_to_table([b]) if cmd_proc.json_output: json_object = {'blueprint': utils.table_to_json(headers, table)} utils.print_json(json_object, cmd_proc=cmd_proc) else: utils.print_table("Details of blueprint '%s', profile '%s':" % (blueprint_id, cmd_proc.profile), headers, table, cmd_proc) if include_plan: utils.print_json(b['plan'], "Blueprint plan", cmd_proc) except exceptions.ClientException as e: utils.print_error("Blueprint not found. Reason: {0}, {1}". format(str(e), scoreclient.response.content), cmd_proc)
def _validate(cmd_proc, operation, blueprint_id, blueprint_file, include_plan, scoreclient): try: scoreclient.blueprints.validate(blueprint_file) utils.print_message("The blueprint is valid.", cmd_proc) except MissingRequiredInputError as mrie: utils.print_error('Invalid blueprint: ' + str(mrie)[str(mrie).rfind('}') + 1:]. strip()) except UnknownInputError as uie: utils.print_error('Invalid blueprint: ' + str(uie)[str(uie).rfind('}') + 1:]. strip()) except FunctionEvaluationError as fee: utils.print_error('Invalid blueprint: ' + str(fee)[str(fee).rfind('}') + 1:]. strip()) except DSLParsingException as dpe: utils.print_error('Invalid blueprint: ' + str(dpe)[str(dpe).rfind('}') + 1:]. strip()) except Exception as ex: utils.print_error('Failed to validate %s:\n %s' % (blueprint_file, str(ex)))
def role(cmd_proc, operation): """Operations with Roles""" if cmd_proc.vca.service_type != VCA.VCA_SERVICE_TYPE_VCA: utils.print_message('Operation not supported ' 'in this service type') sys.exit(1) result = cmd_proc.re_login() if not result: utils.print_error('Not logged in', cmd_proc) sys.exit(1) if 'list' == operation: headers = ['Role Name', 'Description', 'Permissions'] table = [] for r in cmd_proc.vca.get_roles(): permissions = [] if len(r.get('rights')) > 0: for p in r.get('rigths'): permissions.append(str(p.get('name'))) table.append([r.get('name'), r.get('description'), utils.beautified(permissions)]) sorted_table = sorted(table, key=operator.itemgetter(0), reverse=False) utils.print_table("Available roles in instance '%s'" ", profile '%s':" % (cmd_proc.instance, cmd_proc.profile), headers, sorted_table, cmd_proc) cmd_proc.save_current_config()
def user(cmd_proc, operation, username, user_id, password, new_password, first_name, last_name, roles, token): """Operations with Users""" if cmd_proc.vca.service_type != VCA.VCA_SERVICE_TYPE_VCA: utils.print_message('Operation not supported ' 'in this service type') sys.exit(1) result = cmd_proc.re_login() if not result: utils.print_error('Not logged in', cmd_proc) sys.exit(1) try: if 'list' == operation: headers = ['User Name', 'First', 'Last', 'Email', 'State', 'Id', 'Roles'] table = [] for u in cmd_proc.vca.get_users(): roles = [] for r in u.get('roles').get('roles'): roles.append(str(r.get('name'))) table.append([u.get('userName'), u.get('givenName'), u.get('familyName'), u.get('email'), u.get('state'), u.get('id'), utils.beautified(roles)]) sorted_table = sorted(table, key=operator.itemgetter(0), reverse=False) utils.print_table("Available users in instance '%s'" ", profile '%s':" % (cmd_proc.instance, cmd_proc.profile), headers, sorted_table, cmd_proc) elif 'info' == operation: cmd_proc.error_message = 'not implemented' sys.exit(1) elif 'create' == operation: roles_array = roles.split(',') result = cmd_proc.vca.add_user(username, first_name, last_name, roles_array) utils.print_message("User '%s' successfully created" % username) elif 'delete' == operation: result = cmd_proc.vca.del_user(user_id) utils.print_message("Successfully deleted user '%s'" % cmd_proc.vca.username) elif 'change-password' == operation: result = cmd_proc.vca.change_password(password, new_password) utils.print_message("Successfully changed password for user '%s'" % cmd_proc.vca.username) elif 'validate' == operation: result = cmd_proc.vca.validate_user(username, password, token) print result elif 'reset-password' == operation: result = cmd_proc.vca.reset_password(user_id) utils.print_message("Successfully reset password for user id" "'%s', check email to enter new password" % user_id) except: utils.print_error('An error has ocurred', cmd_proc) sys.exit(1) cmd_proc.save_current_config()
def _authorize(cmd_proc): result = cmd_proc.re_login() if not result: utils.print_error("Not logged in", cmd_proc) scoreclient = cmd_proc.vca.get_score_service(cmd_proc.host_score) if scoreclient is None: utils.print_error("Unable to access TOSCA service.", cmd_proc) return scoreclient
def _upload(cmd_proc, operation, blueprint_id, blueprint_file, include_plan, scoreclient): try: b = scoreclient.blueprints.upload(blueprint_file, blueprint_id) utils.print_message("Successfully uploaded blueprint '%s'." % b.get("id"), cmd_proc) except exceptions.ClientException as e: utils.print_error( "Failed to upload blueprint. Reason: {0}, {1}".format(str(e), scoreclient.response.content), cmd_proc )
def _delete_blueprint(cmd_proc, operation, blueprint_id, blueprint_file, include_plan, scoreclient): try: scoreclient.blueprints.delete(blueprint_id) utils.print_message("successfully deleted blueprint '%s'" % blueprint_id, cmd_proc) except exceptions.ClientException as e: utils.print_error( "Failed to delete blueprint. Reason: {0}, {1}".format(str(e), scoreclient.response.content), cmd_proc )
def _delete_blueprint(cmd_proc, blueprint_id, scoreclient): try: scoreclient.blueprints.delete(blueprint_id) utils.print_message("successfully deleted blueprint '%s'" % blueprint_id, cmd_proc) except exceptions.ClientException as e: utils.print_error("Failed to delete blueprint. Reason: %s." % str(e), cmd_proc)
def _upload(cmd_proc, blueprint, blueprint_file, scoreclient): try: b = scoreclient.blueprints.upload(blueprint_file, blueprint) utils.print_message("Successfully uploaded blueprint '%s'." % b.get('id'), cmd_proc) except exceptions.ClientException as e: utils.print_error("Failed to upload blueprint. Reason: %s." % str(e), cmd_proc)
def _authorize(cmd_proc): result = cmd_proc.re_login() if not result: utils.print_error('Not logged in', cmd_proc) scoreclient = cmd_proc.vca.get_score_service(cmd_proc.host_score) if scoreclient is None: utils.print_error('Unable to access TOSCA service.', cmd_proc) return scoreclient
def _validate(cmd_proc, operation, blueprint_id, blueprint_file, include_plan, scoreclient): try: scoreclient.blueprints.validate(blueprint_file) utils.print_message("The blueprint is valid.", cmd_proc) except Exception as ex: utils.print_error('Failed to validate %s:\n %s' % (blueprint_file, str(ex)))
def _status(cmd_proc, operation, blueprint_id, blueprint_file, include_plan, scoreclient): try: status = scoreclient.get_status() print_utils.print_dict(json.loads(status)) except exceptions.ClientException as e: utils.print_error( "Unable to get blueprinting service status. " "Reason: {0}, {1}".format(str(e), scoreclient.response.content), cmd_proc, )
def _delete_deployment(cmd_proc, scoreclient, deployment_to_delete, force_delete): try: scoreclient.deployments.delete(deployment_to_delete, force_delete=force_delete) utils.print_message("successfully deleted deployment '%s'" % deployment_to_delete, cmd_proc) except exceptions.ClientException as e: utils.print_error("Failed to delete deployment. Reason: %s." % str(e), cmd_proc)
def _cancel(cmd_proc, execution, force_cancel, scoreclient): if not execution: utils.print_error("execution id is not specified") return try: e = scoreclient.executions.cancel(execution, force_cancel) print_execution(e, None) if e else utils.print_message( str(scoreclient.response.content), cmd_proc) except exceptions.ClientException as e: utils.print_error("Failed to cancel workflow. Reasons: {0}." .format(str(e)), cmd_proc)
def _delete_blueprint(cmd_proc, operation, blueprint_id, blueprint_file, include_plan, scoreclient): try: scoreclient.blueprints.delete(blueprint_id) utils.print_message("successfully deleted blueprint '%s'" % blueprint_id, cmd_proc) except exceptions.ClientException as e: utils.print_error("Failed to delete blueprint. Reason: {0}, {1}" .format(str(e), scoreclient.response.content), cmd_proc)
def _status(cmd_proc, operation, blueprint_id, blueprint_file, include_plan, scoreclient): try: status = scoreclient.get_status() print_utils.print_dict(json.loads(status)) except exceptions.ClientException as e: utils.print_error("Unable to get blueprinting service status. " "Reason: {0}, {1}" .format(str(e), scoreclient.response.content), cmd_proc)
def _authorize(cmd_proc): result = cmd_proc.re_login() if not result: utils.print_error('Not logged in', cmd_proc) sys.exit(1) scoreclient = cmd_proc.vca.get_score_service(cmd_proc.host_score) if scoreclient is None: utils.print_error('Unable to login to the blueprinting service.', cmd_proc) sys.exit(1) return scoreclient
def _upload(cmd_proc, operation, blueprint_id, blueprint_file, include_plan, scoreclient): try: b = scoreclient.blueprints.upload(blueprint_file, blueprint_id) utils.print_message("Successfully uploaded blueprint '%s'." % b.get('id'), cmd_proc) except exceptions.ClientException as e: utils.print_error("Failed to upload blueprint. Reason: {0}, {1}" .format(str(e), scoreclient.response.content), cmd_proc)
def _info_deployment(cmd_proc, scoreclient, deployment_to_show, show_events=False): try: d = scoreclient.deployments.get(deployment_to_show) e = scoreclient.executions.list(deployment_to_show) events = None if show_events and e is not None and len(e) > 0: events = scoreclient.events.get(e[-1].get('id')) print_deployment_info(d, e, events) except exceptions.ClientException as e: utils.print_error("Failed to get deployment info. Reason: %s." % str(e), cmd_proc)
def _outputs(cmd_proc, operation, deployment_id, blueprint_id, input_file, workflow, show_events, execution_id, force_cancel, force_delete, scoreclient): try: deployment_outputs = scoreclient.deployments.outputs(deployment_id) utils.print_json(deployment_outputs) except exceptions.ClientException as e: utils.print_error("Failed to get deployment output. Reason: {0}, {1}" .format(str(e), scoreclient.response.content), cmd_proc)
def _create_deployment(cmd_proc, blueprint, deployment, input_file, scoreclient): try: inputs = None if input_file: inputs = yaml.load(input_file) scoreclient.deployments.create(blueprint, deployment, inputs) utils.print_message("Successfully created deployment '%s'." % deployment, cmd_proc) except exceptions.ClientException as e: utils.print_error("Failed to create deployment. Reason: %s" % str(e), cmd_proc)
def _delete_deployment(cmd_proc, operation, deployment_id, blueprint_id, input_file, workflow, show_events, execution_id, force_cancel, force_delete, scoreclient): try: scoreclient.deployments.delete(deployment_id, force_delete=force_delete) utils.print_message("successfully deleted deployment '{0}'" .format(deployment_id), cmd_proc) except exceptions.ClientException as e: utils.print_error("Failed to delete deployment. Reason: {0}, {1}" .format(str(e), scoreclient.response.content), cmd_proc)
def disk(cmd_proc, operation, vdc, disk_name, disk_size, disk_id): """Operations with Independent Disks""" result = cmd_proc.re_login() if not result: utils.print_error('Not logged in', cmd_proc) sys.exit(1) if vdc is None: vdc = cmd_proc.vdc_name the_vdc = cmd_proc.vca.get_vdc(vdc) if the_vdc is None: utils.print_error("VDC not found '%s'" % vdc, cmd_proc) sys.exit(1) if 'list' == operation: headers = ['Disk', 'Size GB', 'Id', 'Owner'] disks = cmd_proc.vca.get_disks(vdc) table = cmd_proc.disks_to_table(disks) if cmd_proc.json_output: json_object = {'disks': utils.table_to_json(headers, table)} utils.print_json(json_object, cmd_proc=cmd_proc) else: utils.print_table("Available independent disks in '%s'" ", profile '%s':" % (vdc, cmd_proc.profile), headers, table, cmd_proc) elif 'create' == operation: assert disk_name, "Disk name can't be empty" size = disk_size * cmd_proc.DISK_SIZE result = cmd_proc.vca.add_disk(vdc, disk_name, size) if result and len(result) > 0: if result[0]: utils.print_message('disk %s successfully created' % disk_name, cmd_proc) else: utils.print_error('disk %s could not be created' % disk_name, cmd_proc) elif 'delete' == operation: result = cmd_proc.vca.delete_disk(vdc, disk_name, disk_id) if result and len(result) > 0: if result[0]: utils.print_message('disk %s successfully deleted' % (disk_id if disk_id else disk_name), cmd_proc) else: utils.print_error('disk %s could not be deleted: %s' % (disk_name, result[1]), cmd_proc) sys.exit(1) else: utils.print_error('not implemented', cmd_proc) sys.exit(1) cmd_proc.save_current_config()
def disk(cmd_proc, operation, vdc, disk_name, disk_size, disk_id): """Operations with Independent Disks""" result = cmd_proc.re_login() if not result: utils.print_error('Not logged in', cmd_proc) sys.exit(1) if vdc is None: vdc = cmd_proc.vdc_name the_vdc = cmd_proc.vca.get_vdc(vdc) if the_vdc is None: utils.print_error("VDC not found '%s'" % vdc, cmd_proc) sys.exit(1) if 'list' == operation: headers = ['Disk', 'Size GB', 'Id', 'Owner'] disks = cmd_proc.vca.get_disks(vdc) table = cmd_proc.disks_to_table(disks) if cmd_proc.json_output: json_object = {'disks': utils.table_to_json(headers, table)} utils.print_json(json_object, cmd_proc=cmd_proc) else: utils.print_table( "Available independent disks in '%s'" ", profile '%s':" % (vdc, cmd_proc.profile), headers, table, cmd_proc) elif 'create' == operation: assert disk_name, "Disk name can't be empty" size = disk_size * cmd_proc.DISK_SIZE result = cmd_proc.vca.add_disk(vdc, disk_name, size) if result and len(result) > 0: if result[0]: utils.print_message('disk %s successfully created' % disk_name, cmd_proc) else: utils.print_error('disk %s could not be created' % disk_name, cmd_proc) elif 'delete' == operation: result = cmd_proc.vca.delete_disk(vdc, disk_name, disk_id) if result and len(result) > 0: if result[0]: utils.print_message( 'disk %s successfully deleted' % (disk_id if disk_id else disk_name), cmd_proc) else: utils.print_error( 'disk %s could not be deleted: %s' % (disk_name, result[1]), cmd_proc) sys.exit(1) else: utils.print_error('not implemented', cmd_proc) sys.exit(1) cmd_proc.save_current_config()
def _execute_workflow(cmd_proc, deployment_for_exection, workflow, scoreclient): try: if not deployment_for_exection or not workflow: utils.print_error("Deployment ID or Workflow ID " "was not specified.") return e = scoreclient.executions.start( deployment_for_exection, workflow) print_utils.print_dict(e) if e else utils.print_message( str(scoreclient.response.content), cmd_proc) except exceptions.ClientException as e: utils.print_error("Failed to execute workflow. Reasons: {0}." .format(str(e)), cmd_proc)
def _validate(cmd_proc, operation, blueprint_id, blueprint_file, include_plan, scoreclient): try: scoreclient.blueprints.validate(blueprint_file) utils.print_message("The blueprint is valid.", cmd_proc) except MissingRequiredInputError as mrie: utils.print_error("Invalid blueprint: " + str(mrie)[str(mrie).rfind("}") + 1 :].strip()) except UnknownInputError as uie: utils.print_error("Invalid blueprint: " + str(uie)[str(uie).rfind("}") + 1 :].strip()) except FunctionEvaluationError as fee: utils.print_error("Invalid blueprint: " + str(fee)[str(fee).rfind("}") + 1 :].strip()) except DSLParsingException as dpe: utils.print_error("Invalid blueprint: " + str(dpe)[str(dpe).rfind("}") + 1 :].strip()) except Exception as ex: utils.print_error("Failed to validate %s:\n %s" % (blueprint_file, str(ex)))
def operation_unknown( cmd_proc, operation, deployment_id, blueprint_id, input_file, workflow, show_events, execution_id, force_cancel, force_delete, scoreclient, ): utils.print_error("Operation '{0}' not supported.".format(operation), cmd_proc)
def event(cmd_proc, operation, execution_id, from_event, batch_size, show_logs): """Operations with Blueprint Events""" scoreclient = _authorize(cmd_proc) if "list" == operation: try: events = scoreclient.events.get( execution_id, from_event=from_event, batch_size=batch_size, include_logs=show_logs ) print_events(events) utils.print_message("Total events: {}".format(events[-1]), cmd_proc) except exceptions.ClientException as e: utils.print_error( "Can't find events for execution: {0}. " "Reason: {1}.".format(execution_id, str(e)), cmd_proc )
def _create_deployment(cmd_proc, operation, deployment_id, blueprint_id, input_file, workflow, show_events, execution_id, force_cancel, force_delete, scoreclient): try: inputs = None if input_file: inputs = yaml.load(input_file) scoreclient.deployments.create(blueprint_id, deployment_id, inputs) utils.print_message("Successfully created deployment '{0}'.".format( deployment_id), cmd_proc) except exceptions.ClientException as e: utils.print_error("Failed to create deployment. Reason: {0}, {1}" .format(str(e), scoreclient.response.content), cmd_proc)
def _cancel(cmd_proc, operation, deployment_id, blueprint_id, input_file, workflow, show_events, execution_id, force_cancel, force_delete, scoreclient): if not execution_id: utils.print_error("Execution id is not specified.") return try: e = scoreclient.executions.cancel(execution_id, force_cancel) print_execution(e, None) if e else utils.print_message( str(scoreclient.response.content), cmd_proc) except exceptions.ClientException as e: utils.print_error("Failed to cancel workflow. Reasons: {0}, {1}." .format(str(e), scoreclient.response.content), cmd_proc)
def _execute_workflow(cmd_proc, operation, deployment_id, blueprint_id, input_file, workflow, show_events, execution_id, force_cancel, force_delete, scoreclient): try: if not deployment_id or not workflow: utils.print_error("Deployment ID or Workflow ID " "was not specified.") return e = scoreclient.executions.start( deployment_id, workflow) print_utils.print_dict(e) if e else utils.print_message( str(scoreclient.response.content), cmd_proc) except exceptions.ClientException as e: utils.print_error("Failed to execute workflow. Reasons: {0}, {1}" .format(str(e), scoreclient.response.content), cmd_proc)
def _info_blueprint(cmd_proc, scoreclient, include_plan=False): try: b = scoreclient.blueprints.get(blueprint) headers = ['Id', 'Created'] table = cmd_proc.blueprints_to_table([b]) if cmd_proc.json_output: json_object = {'blueprint': utils.table_to_json(headers, table)} utils.print_json(json_object, cmd_proc=cmd_proc) else: utils.print_table("Details of blueprint '%s', profile '%s':" % (blueprint, cmd_proc.profile), headers, table, cmd_proc) if include_plan: utils.print_json(b['plan'], "Blueprint plan", cmd_proc) except exceptions.ClientException as e: utils.print_error("Blueprint not found. Reason: %s." % str(e))
def event(cmd_proc, operation, execution, from_event, batch_size, show_logs): """Operations with Blueprint Events""" scoreclient = _authorize(cmd_proc) if 'list' == operation: try: events = scoreclient.events.get(execution, from_event=from_event, batch_size=batch_size, include_logs=show_logs) print_table("Status:", 'status', events[0].keys(), [e.values() for e in events[:-1]], None) utils.print_message("Total events: {}".format(events[-1]), cmd_proc) except exceptions.ClientException as e: utils.print_error("Can't find events for execution: {0}. " "Reason: {1}.". format(execution, str(e)), cmd_proc)
def event(cmd_proc, operation, execution_id, from_event, batch_size, show_logs): """Operations with Blueprint Events""" scoreclient = _authorize(cmd_proc) if 'list' == operation: try: events = scoreclient.events.get(execution_id, from_event=from_event, batch_size=batch_size, include_logs=show_logs) print_events(events) utils.print_message("Total events: {}".format(events[-1]), cmd_proc) except exceptions.ClientException as e: utils.print_error("Can't find events for execution: {0}. " "Reason: {1}.". format(execution_id, str(e)), cmd_proc)
def _delete_deployment( cmd_proc, operation, deployment_id, blueprint_id, input_file, workflow, show_events, execution_id, force_cancel, force_delete, scoreclient, ): try: scoreclient.deployments.delete(deployment_id, force_delete=force_delete) utils.print_message("successfully deleted deployment '{0}'".format(deployment_id), cmd_proc) except exceptions.ClientException as e: utils.print_error( "Failed to delete deployment. Reason: {0}, {1}".format(str(e), scoreclient.response.content), cmd_proc )
def _info_blueprint(cmd_proc, operation, blueprint_id, blueprint_file, include_plan, scoreclient): try: b = scoreclient.blueprints.get(blueprint_id) if blueprint_id is None or len(blueprint_id) == 0: utils.print_error("specify blueprint id") sys.exit(1) headers = ["Id", "Created"] table = cmd_proc.blueprints_to_table([b]) if cmd_proc.json_output: json_object = {"blueprint": utils.table_to_json(headers, table)} utils.print_json(json_object, cmd_proc=cmd_proc) else: utils.print_table( "Details of blueprint '%s', profile '%s':" % (blueprint_id, cmd_proc.profile), headers, table, cmd_proc ) if include_plan: utils.print_json(b["plan"], "Blueprint plan", cmd_proc) except exceptions.ClientException as e: utils.print_error( "Blueprint not found. Reason: {0}, {1}".format(str(e), scoreclient.response.content), cmd_proc )
def vm(cmd_proc, operation, vdc, vapp): """Operations with VMs""" result = cmd_proc.re_login() if not result: utils.print_error('Not logged in', cmd_proc) sys.exit(1) if vdc is None: vdc = cmd_proc.vdc_name the_vdc = cmd_proc.vca.get_vdc(vdc) if the_vdc is None: utils.print_error("VDC not found '%s'" % vdc, cmd_proc) sys.exit(1) if 'list' == operation: headers = [ 'VM', "vApp", "Status", "IPs", "Networks", "vCPUs", "Memory (GB)", "CD/DVD", "OS", "Owner" ] table = cmd_proc.vms_to_table(the_vdc, vapp) if cmd_proc.json_output: json_object = {'vms': utils.table_to_json(headers, table)} utils.print_json(json_object, cmd_proc=cmd_proc) else: utils.print_table( "Available VMs in '%s', profile '%s':" % (vdc, cmd_proc.profile), headers, table, cmd_proc) else: utils.print_error('not implemented', cmd_proc) sys.exit(1) cmd_proc.save_current_config()
def vm(cmd_proc, operation, vdc, vapp): """Operations with VMs""" result = cmd_proc.re_login() if not result: utils.print_error('Not logged in', cmd_proc) sys.exit(1) if vdc is None: vdc = cmd_proc.vdc_name the_vdc = cmd_proc.vca.get_vdc(vdc) if the_vdc is None: utils.print_error("VDC not found '%s'" % vdc, cmd_proc) sys.exit(1) if 'list' == operation: headers = ['VM', "vApp", "Status", "IPs", "MACs", "Networks", "vCPUs", "Memory (GB)", "CD/DVD", "OS", "Owner"] table = cmd_proc.vms_to_table(the_vdc, vapp) if cmd_proc.json_output: json_object = {'vms': utils.table_to_json(headers, table)} utils.print_json(json_object, cmd_proc=cmd_proc) else: utils.print_table("Available VMs in '%s', profile '%s':" % (vdc, cmd_proc.profile), headers, table, cmd_proc) else: utils.print_error('not implemented', cmd_proc) sys.exit(1) cmd_proc.save_current_config()
def _outputs( cmd_proc, operation, deployment_id, blueprint_id, input_file, workflow, show_events, execution_id, force_cancel, force_delete, scoreclient, ): try: deployment_outputs = scoreclient.deployments.outputs(deployment_id) utils.print_json(deployment_outputs) except exceptions.ClientException as e: utils.print_error( "Failed to get deployment output. Reason: {0}, {1}".format(str(e), scoreclient.response.content), cmd_proc )
def _create_deployment( cmd_proc, operation, deployment_id, blueprint_id, input_file, workflow, show_events, execution_id, force_cancel, force_delete, scoreclient, ): try: inputs = None if input_file: inputs = yaml.load(input_file) scoreclient.deployments.create(blueprint_id, deployment_id, inputs) utils.print_message("Successfully created deployment '{0}'.".format(deployment_id), cmd_proc) except exceptions.ClientException as e: utils.print_error( "Failed to create deployment. Reason: {0}, {1}".format(str(e), scoreclient.response.content), cmd_proc )
def _execute_workflow( cmd_proc, operation, deployment_id, blueprint_id, input_file, workflow, show_events, execution_id, force_cancel, force_delete, scoreclient, ): try: if not deployment_id or not workflow: utils.print_error("Deployment ID or Workflow ID " "was not specified.") return e = scoreclient.executions.start(deployment_id, workflow) print_utils.print_dict(e) if e else utils.print_message(str(scoreclient.response.content), cmd_proc) except exceptions.ClientException as e: utils.print_error( "Failed to execute workflow. Reasons: {0}, {1}".format(str(e), scoreclient.response.content), cmd_proc )
def _info_deployment(cmd_proc, operation, deployment_id, blueprint_id, input_file, workflow, show_events, execution_id, force_cancel, force_delete, scoreclient): try: d = scoreclient.deployments.get(deployment_id) e = scoreclient.executions.list(deployment_id) events = None e_id = None if show_events and e is not None and len(e) > 0: table = [] for execution in e: table.append([execution.get('created_at'), execution.get('id')]) sorted_table = sorted(table, key=operator.itemgetter(0), reverse=True) e_id = sorted_table[0][1] if execution_id is None else execution_id events = scoreclient.events.get(e_id) print_deployment_info(d, e, events, e_id) except exceptions.ClientException as e: utils.print_error("Failed to get deployment info. Reason: {0}, {1}" .format(str(e), scoreclient.response.content), cmd_proc)
def firewall(cmd_proc, operation, vdc, gateway): """Operations with Edge Gateway Firewall Service""" result = cmd_proc.re_login() if not result: utils.print_error('Not logged in', cmd_proc) sys.exit(1) if vdc is None: vdc = cmd_proc.vdc_name the_vdc = cmd_proc.vca.get_vdc(vdc) if the_vdc is None: utils.print_error("VDC not found '%s'" % vdc, cmd_proc) sys.exit(1) if gateway is None: gateway = cmd_proc.gateway the_gateway = cmd_proc.vca.get_gateway(vdc, gateway) if the_gateway is None: utils.print_error("gateway not found '%s'" % gateway, cmd_proc) sys.exit(1) if 'list' == operation: headers = [ 'Source IP', 'Source Port', 'Destination IP', 'Destination Port', 'Protocol', 'Enabled' ] table = cmd_proc.firewall_rules_to_table(the_gateway) if cmd_proc.json_output: json_object = {'fw-rules': utils.table_to_json(headers, table)} utils.print_json(json_object, cmd_proc=cmd_proc) else: utils.print_table( "Firewall rules in gateway '%s', " "VDC '%s', profile '%s':" % (gateway, vdc, cmd_proc.profile), headers, table, cmd_proc) elif 'enable' == operation or 'disable' == operation: utils.print_message("%s firewall" % operation) the_gateway.enable_fw('enable' == operation) task = the_gateway.save_services_configuration() if task: utils.display_progress( task, cmd_proc, cmd_proc.vca.vcloud_session.get_vcloud_headers()) else: error = parseString(the_gateway.response.content, True) utils.print_error( "can't '%s' the firewall: " % operation + error.get_message(), cmd_proc) sys.exit(1) cmd_proc.save_current_config()
def firewall(cmd_proc, operation, vdc, gateway): """Operations with Edge Gateway Firewall Service""" result = cmd_proc.re_login() if not result: utils.print_error('Not logged in', cmd_proc) sys.exit(1) if vdc is None: vdc = cmd_proc.vdc_name the_vdc = cmd_proc.vca.get_vdc(vdc) if the_vdc is None: utils.print_error("VDC not found '%s'" % vdc, cmd_proc) sys.exit(1) if gateway is None: gateway = cmd_proc.gateway the_gateway = cmd_proc.vca.get_gateway(vdc, gateway) if the_gateway is None: utils.print_error("gateway not found '%s'" % gateway, cmd_proc) sys.exit(1) if 'list' == operation: headers = ['Source IP', 'Source Port', 'Destination IP', 'Destination Port', 'Protocol', 'Enabled'] table = cmd_proc.firewall_rules_to_table(the_gateway) if cmd_proc.json_output: json_object = {'fw-rules': utils.table_to_json(headers, table)} utils.print_json(json_object, cmd_proc=cmd_proc) else: utils.print_table("Firewall rules in gateway '%s', " "VDC '%s', profile '%s':" % (gateway, vdc, cmd_proc.profile), headers, table, cmd_proc) elif 'enable' == operation or 'disable' == operation: utils.print_message("%s firewall" % operation) the_gateway.enable_fw('enable' == operation) task = the_gateway.save_services_configuration() if task: utils.display_progress(task, cmd_proc, cmd_proc.vca.vcloud_session. get_vcloud_headers()) else: error = parseString(the_gateway.response.content, True) utils.print_error("can't '%s' the firewall: " % operation + error.get_message(), cmd_proc) sys.exit(1) cmd_proc.save_current_config()
def _cancel( cmd_proc, operation, deployment_id, blueprint_id, input_file, workflow, show_events, execution_id, force_cancel, force_delete, scoreclient, ): if not execution_id: utils.print_error("Execution id is not specified.") return try: e = scoreclient.executions.cancel(execution_id, force_cancel) print_execution(e, None) if e else utils.print_message(str(scoreclient.response.content), cmd_proc) except exceptions.ClientException as e: utils.print_error( "Failed to cancel workflow. Reasons: {0}, {1}.".format(str(e), scoreclient.response.content), cmd_proc )
def vdc(cmd_proc, operation, vdc, template, yes): """Operations with Virtual Data Centers""" result = cmd_proc.re_login() if not result: utils.print_error('Not logged in', cmd_proc) sys.exit(1) if 'list' == operation: headers = ['Virtual Data Center', "Selected"] table = ['', ''] if cmd_proc.vca.vcloud_session and \ cmd_proc.vca.vcloud_session.organization: links = (cmd_proc.vca.vcloud_session.organization.Link if cmd_proc.vca.vcloud_session.organization else []) table1 = [[ details.get_name(), '*' if details.get_name() == cmd_proc.vdc_name else '' ] for details in filter( lambda info: info.name and (info.type_ == 'application/vnd.vmware.vcloud.vdc+xml'), links) ] table = sorted(table1, key=operator.itemgetter(0), reverse=False) utils.print_table( "Available Virtual Data Centers in org '%s', profile '%s':" % (cmd_proc.vca.org, cmd_proc.profile), headers, table, cmd_proc) elif 'use' == operation: the_vdc = cmd_proc.vca.get_vdc(vdc) if the_vdc is not None: utils.print_message( "Using vdc '%s', profile '%s'" % (vdc, cmd_proc.profile), cmd_proc) cmd_proc.vdc_name = vdc cmd_proc.select_default_gateway() else: utils.print_error( "Unable to select vdc '%s' in profile '%s'" % (vdc, cmd_proc.profile), cmd_proc) sys.exit(1) elif 'info' == operation: if vdc is None: vdc = cmd_proc.vdc_name the_vdc = cmd_proc.vca.get_vdc(vdc) if the_vdc: gateways = cmd_proc.vca.get_gateways(vdc) headers1 = ['Type', 'Name'] table1 = cmd_proc.vdc_to_table(the_vdc, gateways) headers2 = [ 'Resource', 'Allocated', 'Limit', 'Reserved', 'Used', 'Overhead' ] table2 = cmd_proc.vdc_resources_to_table(the_vdc) headers3 = [ 'Name', 'External IPs', 'DHCP', 'Firewall', 'NAT', 'VPN', 'Routed Networks', 'Syslog', 'Uplinks' ] table3 = cmd_proc.gateways_to_table(gateways) if cmd_proc.json_output: json_object = { 'vdc_entities': utils.table_to_json(headers1, table1), 'vdc_resources': utils.table_to_json(headers2, table2), 'gateways': utils.table_to_json(headers3, table3) } utils.print_json(json_object, cmd_proc=cmd_proc) else: utils.print_table( "Details of Virtual Data Center '%s', profile '%s':" % (vdc, cmd_proc.profile), headers1, table1, cmd_proc) utils.print_table("Compute capacity:", headers2, table2, cmd_proc) utils.print_table('Gateways:', headers3, table3, cmd_proc) else: utils.print_error( "Unable to select VDC %s, profile '%s': " "VDC not found" % (vdc, cmd_proc.profile), cmd_proc) sys.exit(1) elif 'create' == operation: if vdc is None: utils.print_error('please enter new VDC name') sys.exit(1) task = cmd_proc.vca.create_vdc(vdc, template) if task: utils.display_progress( task, cmd_proc, cmd_proc.vca.vcloud_session.get_vcloud_headers()) else: utils.print_error("can't create the VDC", cmd_proc) sys.exit(1) elif 'delete' == operation: if vdc is None: vdc = cmd_proc.vdc_name the_vdc = cmd_proc.vca.get_vdc(vdc) if the_vdc: if not yes: confirmed = click.confirm("This will delete the VDC, its " "edge gateway and all its virtual " "machines.\n" "The action can't be undone.\n" "Do you want to delete VDC '%s'?" % vdc) if not confirmed: utils.print_message('Action cancelled, ' 'VDC will not be deleted') sys.exit(0) result = cmd_proc.vca.delete_vdc(vdc) if result[0]: utils.display_progress( result[1], cmd_proc, cmd_proc.vca.vcloud_session.get_vcloud_headers()) else: utils.print_error("can't delete the VDC", cmd_proc) sys.exit(1) else: utils.print_error( "Unable to delete VDC '%s', profile '%s': " "VDC not found" % (vdc, cmd_proc.profile), cmd_proc) sys.exit(1) cmd_proc.save_current_config()