def ssh(ssh_plain_mode, ssh_command): logger = get_logger() ssh_path = spawn.find_executable('ssh') logger.debug('SSH executable path: {0}'.format(ssh_path or 'Not found')) if not ssh_path and platform.system() == 'Windows': msg = messages.SSH_WIN_NOT_FOUND raise CloudifyCliError(msg) elif not ssh_path: msg = messages.SSH_LINUX_NOT_FOUND raise CloudifyCliError(msg) else: command = [ ssh_path, '{0}@{1}'.format(get_management_user(), get_management_server_ip()) ] if get_global_verbosity(): command.append('-v') if not ssh_plain_mode: command.extend(['-i', os.path.expanduser(get_management_key())]) if ssh_command: command.extend(['--', ssh_command]) logger.debug('executing command: {0}'.format(' '.join(command))) logger.info('Trying to connect...') from subprocess import call call(command)
def publish_archive(archive_location, blueprint_filename, blueprint_id): logger = get_logger() management_ip = utils.get_management_server_ip() for archive_type in SUPPORTED_ARCHIVE_TYPES: if archive_location.endswith('.{0}'.format(archive_type)): break else: raise CloudifyCliError( "Can't publish archive {0} - it's of an unsupported archive type. " "Supported archive types: {1}".format(archive_location, SUPPORTED_ARCHIVE_TYPES)) archive_location_type = 'URL' if not urlparse.urlparse(archive_location).scheme: # archive_location is not a URL - validate it's a file path if not os.path.isfile(archive_location): raise CloudifyCliError( "Can't publish archive {0} - it's not a valid URL nor a path " "to an archive file".format(archive_location)) archive_location_type = 'path' archive_location = os.path.expanduser(archive_location) logger.info('Publishing blueprint archive from {0} {1} to management ' 'server {2}'.format(archive_location_type, archive_location, management_ip)) client = utils.get_rest_client(management_ip) blueprint = client.blueprints.publish_archive(archive_location, blueprint_id, blueprint_filename) logger.info("Published blueprint archive, blueprint's id is: {0}".format( blueprint.id))
def get_provider_module(provider_name): try: module_or_pkg_desc = imp.find_module(provider_name) if not module_or_pkg_desc[1]: # module_or_pkg_desc[1] is the pathname of found module/package, # if it's empty none were found msg = ('Provider {0} not found.'.format(provider_name)) raise CloudifyCliError(msg) module = imp.load_module(provider_name, *module_or_pkg_desc) if not module_or_pkg_desc[0]: # module_or_pkg_desc[0] is None and module_or_pkg_desc[1] is not # empty only when we've loaded a package rather than a module. # Re-searching for the module inside the now-loaded package # with the same name. module = imp.load_module( provider_name, *imp.find_module(provider_name, module.__path__)) return module except ImportError: msg = ('Could not import module {0}. ' 'maybe {0} provider module was not installed?'.format( provider_name)) raise CloudifyCliError(msg)
def inputs_to_dict(resource, resource_name): if not resource: return None try: # parse resource as string representation of a dictionary parsed_dict = plain_string_to_dict(resource) except CloudifyCliError: try: # if resource is a path - parse as a yaml file if os.path.exists(resource): with open(resource, 'r') as f: parsed_dict = yaml.load(f.read()) else: # parse resource content as yaml parsed_dict = yaml.load(resource) except yaml.error.YAMLError as e: msg = ("'{0}' is not a valid YAML. {1}".format( resource_name, str(e))) raise CloudifyCliError(msg) if isinstance(parsed_dict, dict): return parsed_dict else: msg = "Invalid input: {0}. {1} must represent a dictionary. Valid " \ "values can either be a path to a YAML file, a string " \ "formatted as YAML or a string formatted as " \ "key1=value1;key2=value2" \ .format(resource, resource_name) raise CloudifyCliError(msg)
def raise_uninitialized(): error = CloudifyCliError('Not initialized: Cannot find {0} in {1}, ' 'or in any of its parent directories'.format( CLOUDIFY_WD_SETTINGS_DIRECTORY_NAME, get_cwd())) error.possible_solutions = ["Run 'cfy init' in this directory"] raise error
def validate(plugin_path): logger = get_logger() logger.info( messages.VALIDATING_PLUGIN.format(plugin_path.name)) if not tarfile.is_tarfile(plugin_path.name): raise CloudifyCliError('Archive {0} is of an unsupported archive type.' ' Only tar.gz is allowed' .format(plugin_path.name)) with tarfile.open(plugin_path.name, 'r') as tar: tar_members = tar.getmembers() package_json_path = '{0}/package.json'.format(tar_members[0].name) try: package_member = tar.getmember(package_json_path) except KeyError: raise CloudifyCliError(messages.VALIDATING_PLUGIN_FAILED .format(plugin_path, 'package.json was not ' 'found in archive')) try: tar.extractfile(package_member).read() except: raise CloudifyCliError(messages.VALIDATING_PLUGIN_FAILED .format(plugin_path, 'unable to read ' 'package.json')) logger.info(messages.VALIDATING_PLUGIN_SUCCEEDED)
def raise_uninitialized(): error = CloudifyCliError( 'Not initialized: Cannot find {0} in {1}, ' 'or in any of its parent directories' .format(constants.CLOUDIFY_WD_SETTINGS_DIRECTORY_NAME, get_cwd())) error.possible_solutions = [ "Run 'cfy init' in this directory" ] raise error
def _execute_task(ip, task, tasks, task_args): task = task.replace('-', '_') args, kwargs = _parse_task_args(task_args) task_function = tasks.get(task) if not task_function: raise CloudifyCliError('task: "{0}" not found'.format(task)) try: with settings(host_string=ip): task_function(*args, **kwargs) except Exception as e: raise CloudifyCliError('failed to execute: "{0}" ' '({1}) '.format(task, str(e)))
def get_default_rest_cert_local_path(): init_path = get_init_path() if init_path: return os.path.join(init_path, constants.PUBLIC_REST_CERT) error = CloudifyCliError( 'The default path for REST certificates cannot be calculated ' 'because a working directory is not set.') error.possible_solutions = [ "Run 'cfy init' or 'cfy use' to initialize a working directory" ] raise error
def ls(execution_id, include_logs): logger = get_logger() management_ip = utils.get_management_server_ip() logger.info("Getting events from management server {0} for " "execution id '{1}' " "[include_logs={2}]".format(management_ip, execution_id, include_logs)) client = utils.get_rest_client(management_ip) try: execution_events = ExecutionEventsFetcher( client, execution_id, include_logs=include_logs) events = execution_events.fetch_all() events_logger = get_events_logger() events_logger(events) logger.info('\nTotal events: {0}'.format(len(events))) except CloudifyClientError, e: if e.status_code != 404: raise msg = ("Execution '{0}' not found on management server" .format(execution_id)) raise CloudifyCliError(msg)
def protected_provider_call(): try: yield except Exception, ex: trace = sys.exc_info()[2] msg = 'Exception occurred in provider: {0}'.format(str(ex)) raise CloudifyCliError(msg), None, trace
def use(management_ip, provider, rest_port): logger = get_logger() # first check this server is available. client = utils.get_rest_client(manager_ip=management_ip, rest_port=rest_port) try: status_result = client.manager.get_status() except CloudifyClientError: status_result = None if not status_result: msg = ("Can't use management server {0}: No response.".format( management_ip)) raise CloudifyCliError(msg) # check if cloudify was initialized. if not utils.is_initialized(): utils.dump_cloudify_working_dir_settings() utils.dump_configuration_file() try: response = utils.get_rest_client(management_ip).manager.get_context() provider_name = response['name'] provider_context = response['context'] except CloudifyClientError: provider_name = None provider_context = None with utils.update_wd_settings() as wd_settings: wd_settings.set_management_server(management_ip) wd_settings.set_provider_context(provider_context) wd_settings.set_provider(provider_name) wd_settings.set_rest_port(rest_port) wd_settings.set_is_provider_config(provider) logger.info('Using management server {0} with port {1}'.format( management_ip, rest_port))
def get_context_path(): init_path = get_init_path() if init_path is None: raise_uninitialized() context_path = os.path.join(init_path, CLOUDIFY_WD_SETTINGS_FILE_NAME) if not os.path.exists(context_path): raise CloudifyCliError('File {0} does not exist'.format(context_path)) return context_path
def exec_tasks_file(tasks_file=None): tasks_file = tasks_file or 'tasks.py' exec_globals = exec_env.exec_globals(tasks_file) try: execfile(tasks_file, exec_globals) except Exception, e: raise CloudifyCliError('Failed evaluating {0} ({1}:{2}' .format(tasks_file, type(e).__name__, e))
def use(management_ip, rest_port): logger = get_logger() # determine SSL mode by port if rest_port == constants.SECURED_REST_PORT: protocol = constants.SECURED_PROTOCOL else: protocol = constants.DEFAULT_PROTOCOL client = utils.get_rest_client(manager_ip=management_ip, rest_port=rest_port, protocol=protocol) try: # first check this server is available. client.manager.get_status() except UserUnauthorizedError: msg = "Can't use management server {0}: User is unauthorized.".format( management_ip) raise CloudifyCliError(msg) except CloudifyClientError: msg = "Can't use management server {0}: No response.".format( management_ip) raise CloudifyCliError(msg) # check if cloudify was initialized. if not utils.is_initialized(): utils.dump_cloudify_working_dir_settings() utils.dump_configuration_file() try: response = client.manager.get_context() provider_context = response['context'] except CloudifyClientError: provider_context = None with utils.update_wd_settings() as wd_settings: wd_settings.set_management_server(management_ip) wd_settings.set_provider_context(provider_context) wd_settings.set_rest_port(rest_port) wd_settings.set_protocol(protocol) logger.info('Using management server {0} with port {1}'.format( management_ip, rest_port)) # delete the previous manager deployment if exists. bs.delete_workdir()
def get_management_server_ip(): cosmo_wd_settings = load_cloudify_working_dir_settings() management_ip = cosmo_wd_settings.get_management_server() if management_ip: return management_ip msg = ("Must either first run 'cfy use' command for a " "management server or provide a management " "server ip explicitly") raise CloudifyCliError(msg)
def validate(blueprint_path): logger = get_logger() logger.info(messages.VALIDATING_BLUEPRINT.format(blueprint_path.name)) try: parse_from_path(blueprint_path.name) except DSLParsingException as ex: msg = (messages.VALIDATING_BLUEPRINT_FAILED.format( blueprint_path.name, str(ex))) raise CloudifyCliError(msg) logger.info(messages.VALIDATING_BLUEPRINT_SUCCEEDED)
def validate(blueprint_path): logger = get_logger() logger.info(messages.VALIDATING_BLUEPRINT.format(blueprint_path.name)) try: resolver = utils.get_import_resolver() parse_from_path(dsl_file_path=blueprint_path.name, resolver=resolver) except DSLParsingException as ex: msg = (messages.VALIDATING_BLUEPRINT_FAILED.format( blueprint_path.name, str(ex))) raise CloudifyCliError(msg) logger.info(messages.VALIDATING_BLUEPRINT_SUCCEEDED)
def json_to_dict(json_resource, json_resource_name): if not json_resource: return None try: if os.path.exists(json_resource): with open(json_resource, 'r') as f: return json.loads(f.read()) else: return json.loads(json_resource) except ValueError, e: msg = ("'{0}' must be a valid JSON. {1}".format( json_resource_name, str(e))) raise CloudifyCliError(msg)
def get(deployment_id, workflow_id): logger = get_logger() management_ip = utils.get_management_server_ip() client = utils.get_rest_client(management_ip) try: logger.info('Getting workflow ' '\'{0}\' of deployment \'{1}\' [manager={2}]'.format( workflow_id, deployment_id, management_ip)) deployment = client.deployments.get(deployment_id) workflow = next( (wf for wf in deployment.workflows if wf.name == workflow_id), None) if not workflow: msg = ("Workflow '{0}' not found on management server for " "deployment {1}".format(workflow_id, deployment_id)) raise CloudifyCliError(msg) except CloudifyClientError, e: if e.status_code != 404: raise msg = ("Deployment '{0}' not found on management server".format( deployment_id)) raise CloudifyCliError(msg)
def get(node_instance_id): logger = get_logger() management_ip = utils.get_management_server_ip() client = utils.get_rest_client(management_ip) logger.info('Getting node instance with ID: \'{0}\' [manager={1}]'.format( node_instance_id, management_ip)) try: node_instance = client.node_instances.get(node_instance_id) except CloudifyClientError, e: if e.status_code != 404: raise msg = ("Node instance with ID '{0}' was not found on the management " "server".format(node_instance_id)) raise CloudifyCliError(msg)
def get(execution_id): logger = get_logger() management_ip = utils.get_management_server_ip() client = utils.get_rest_client(management_ip) try: logger.info('Getting execution: ' '\'{0}\' [manager={1}]'.format(execution_id, management_ip)) execution = client.executions.get(execution_id) except exceptions.CloudifyClientError, e: if e.status_code != 404: raise msg = ("Execution '{0}' not found on management server".format( execution_id)) raise CloudifyCliError(msg)
def ls(deployment_id): logger = get_logger() management_ip = utils.get_management_server_ip() client = utils.get_rest_client(management_ip) try: if deployment_id: logger.info('Getting nodes list for deployment: \'{0}\' ' '[manager={1}]'.format(deployment_id, management_ip)) else: logger.info('Getting a list of all nodes: [manager={0}]'.format( management_ip)) nodes = client.nodes.list(deployment_id=deployment_id) except CloudifyClientError, e: if not e.status_code != 404: raise msg = ('Deployment {0} does not exist on management server'.format( deployment_id)) raise CloudifyCliError(msg)
def plain_string_to_dict(input_string): input_string = input_string.strip() input_dict = {} mapped_inputs = input_string.split(';') for mapped_input in mapped_inputs: mapped_input = mapped_input.strip() if not mapped_input: continue split_mapping = mapped_input.split('=') if len(split_mapping) == 2: key = split_mapping[0].strip() value = split_mapping[1].strip() input_dict[key] = value else: msg = "Invalid input format: {0}, the expected format is: " \ "key1=value1;key2=value2".format(input_string) raise CloudifyCliError(msg) return input_dict
def ls(deployment_id, include_system_workflows): logger = get_logger() management_ip = utils.get_management_server_ip() client = utils.get_rest_client(management_ip) try: if deployment_id: logger.info('Getting executions list for deployment: \'{0}\' ' '[manager={1}]'.format(deployment_id, management_ip)) else: logger.info( 'Getting a list of all executions: [manager={0}]'.format( management_ip)) executions = client.executions.list( deployment_id=deployment_id, include_system_workflows=include_system_workflows) except exceptions.CloudifyClientError, e: if not e.status_code != 404: raise msg = ('Deployment {0} does not exist on management server'.format( deployment_id)) raise CloudifyCliError(msg)
def get_management_key(): cosmo_wd_settings = load_cloudify_working_dir_settings() if cosmo_wd_settings.get_management_key(): return cosmo_wd_settings.get_management_key() msg = 'Management Key is not set in working directory settings' raise CloudifyCliError(msg)