Beispiel #1
0
def save_credentials(provider, client, key_raw, force_file):
    """

    Save the credentials if are valid

    :param provider:
    :param client:
    :param key: Is an string of a file path
    :return:
    """
    if os.path.isfile(key_raw) or force_file:
        maccli.logger.info("%s is an existing path, opening file" % key_raw)
        with open(key_raw, "r") as myfile:
            key = myfile.read()
            maccli.logger.debug("%s read, contents are:\n%s" % (key_raw, key))
    else:
        key = key_raw

    server_status, response = maccli.dao.api_provider.credentials(
        provider, client, key)

    if server_status == 409:
        show_error(
            "The credentials provided has been rejected by the supplier. Please make sure that you are using a correct 'clientid' and 'key'"
        )

    if server_status == 200:
        show(
            "Credentials validated and saved. The provider '%s' has been activated."
            % provider)

    return response
Beispiel #2
0
def save_credentials(provider, client, key_raw, force_file):
    """

    Save the credentials if are valid

    :param provider:
    :param client:
    :param key: Is an string of a file path
    :return:
    """
    if os.path.isfile(key_raw) or force_file:
        maccli.logger.info("%s is an existing path, opening file" % key_raw)
        with open(key_raw, "r") as myfile:
            key = myfile.read()
            maccli.logger.debug("%s read, contents are:\n%s" % (key_raw, key))
    else:
        key = key_raw

    server_status, response = maccli.dao.api_provider.credentials(provider, client, key)

    if server_status == 409:
        show_error("The credentials provided has been rejected by the supplier. Please make sure that you are using a correct 'clientid' and 'key'")

    if server_status == 200:
        show("Credentials validated and saved. The provider '%s' has been activated." % provider)

    return response
Beispiel #3
0
def delete(resource):

    json_request = json.dumps(resource)

    status_code, json_response, raw = maccli.helper.http.send_request("DELETE", "/resource", data=json_request)

    if status_code == 400:
        show_error("Error while building request: " + raw)

    return json_response
def destroy(instanceid):

    status_code, json_response, raw = maccli.helper.http.send_request("DELETE", "/instance/%s" % instanceid)

    if status_code == 404:
        show_error("Server %s not found" % instanceid)

    if status_code == 400:
        show_error("Error with parameters: %s " % raw)

    return json_response
def log(instance_id):

    status_code, json_response, raw = maccli.helper.http.send_request("GET", "/log/%s" % instance_id)

    if status_code == 404:
        show_error("Server not found")

    if status_code == 400:
        show_error("There is a problem with the input parameters")

    return json_response
def create(cookbook_tag, bootstrap, deployment, location, servername, provider, release, release_version,
           branch, hardware, lifespan, environments, hd, port, net, metadata, applyChanges):
    """
     Creates a new instance

    :param cookbook_tag:
    :param bootstrap:
    :param deployment:
    :param location:
    :param servername:
    :param provider:
    :param release:
    :param release_version:
    :param branch:
    :param hardware:
    :param lifespan:
    :param environments:
    :param hd:
    :return:
    """

    params = {
        'cookbook_tag': cookbook_tag,
        'bootstrap': bootstrap,
        'deployment': deployment,
        'location': location,
        'servername': servername,
        'provider': provider,
        'release': release,
        'release_version': release_version,
        'branch': branch,
        'hardware': hardware,
        'lifespan': lifespan,
        'environments': environments,
        'hd': hd,
        'port': port,
        'net': net,
        'metadata': json.dumps(metadata),
        'apply_changes': applyChanges
    }

    json_request = json.dumps(params)

    status_code, json_response, raw = maccli.helper.http.send_request("POST", "/instance", data=json_request)

    if status_code == 400:
        show_error("Error while building request: " + raw)

    if status_code == 409:
        show_error("Resources limit reached. If you need more resources please contact "
                   "[email protected]. Detailed output:" + raw)

    return json_response
def facts(instance_id):

    status_code, json_response, raw = maccli.helper.http.send_request("GET", "/facts/%s" % instance_id)

    if status_code == 404:
        show_error("Server not found")

    if status_code == 400:
        show_error("There is a problem with the input parameters")

    if status_code == 503:
        raise FactError("Facts are not available yet.")

    return json_response
def update(instance_id, lifespan):

    params = {
        'lifespan': lifespan
    }

    json_request = json.dumps(params)

    status_code, json_response, raw = maccli.helper.http.send_request("PUT", "/instance/%s" % instance_id,
                                                                      data=json_request)

    if status_code == 404:
        show_error("Server not found")

    if status_code == 400:
        show_error("There is a problem with the input parameters")

    return json_response
Beispiel #9
0
def parse_macfile(raw):
    """

    Parse and validates the macfile and returns structured contents

    :param raw: R
    :return: root, role and infrastructure contents

    TODO remove "exit" from this method and raise exceptions
    """
    # validate root
    root_params = ['mac', 'version', 'name', 'description', 'infrastructures']
    root_params_optional = ['actions', 'resources', 'roles']
    raw_root_keys = raw.keys()
    try:
        validate_param(raw_root_keys, root_params, root_params_optional)
    except MacParamValidationError, e:
        show_error(e.message)
        exit(1)
def parse_macfile(raw):
    """

    Parse and validates the macfile and returns structured contents

    :param raw: R
    :return: root, role and infrastructure contents

    TODO remove "exit" from this method and raise exceptions
    """
    # validate root
    root_params = ['mac', 'version', 'name', 'description', 'infrastructures']
    root_params_optional = ['actions', 'resources', 'roles']
    raw_root_keys = raw.keys()
    try:
        validate_param(raw_root_keys, root_params, root_params_optional)
    except MacParamValidationError, e:
        show_error(e.message)
        exit(1)
def update_configuration(cookbook_tag, instance_id, new_metadata):
    """
     Creates a new instance

    :param cookbook_tag:
    :param instance_id:
    :return:
    """

    params = {
        'cookbook_tag': cookbook_tag,
        'instance_id': instance_id,
        'metadata': new_metadata
    }

    json_request = json.dumps(params)

    status_code, json_response, raw = maccli.helper.http.send_request("PUT", "/instance", data=json_request)

    if status_code == 400:
        show_error("Error while building request: " + raw)

    if status_code == 404:
        show_error("Error while building request: " + raw)

    if status_code == 401:
        show_error("Error while building request: " + raw)

    return json_response
Beispiel #12
0
def create(name, create_bash, rc, stderr, stdout, destroy_bash, metadata):
    """
     Creates a new resource
    """
    params = {
        'name': name,
        'create_cmd': create_bash,
        'create_rc': rc,
        'create_stderr': stderr,
        'create_stdout': stdout,
        'destroy_cmd': destroy_bash,
        'metadata': json.dumps(metadata),
    }

    json_request = json.dumps(params)

    status_code, json_response, raw = maccli.helper.http.send_request("POST", "/resource", data=json_request)

    if status_code == 400:
        show_error("Error while building request: " + raw)

    return json_response
def credentials(provider, client, key):

    params = {'provider': provider, 'client': client, 'key': key}

    json_request = json.dumps(params)

    status_code, json_raw, raw = maccli.helper.http.send_request(
        "POST", "/provider/credentials", data=json_request)

    if status_code == 400:
        show_error("Error while building request: " + raw)

    if status_code == 404:
        show_error("Error while building request: " + raw)

    if status_code == 401:
        show_error("Error while building request: " + raw)

    return status_code, json_raw
Beispiel #14
0
def credentials(provider, client, key):

    params = {
        'provider': provider,
        'client': client,
        'key': key
    }

    json_request = json.dumps(params)

    status_code, json_raw, raw = maccli.helper.http.send_request("POST", "/provider/credentials", data=json_request)

    if status_code == 400:
        show_error("Error while building request: " + raw)

    if status_code == 404:
        show_error("Error while building request: " + raw)

    if status_code == 401:
        show_error("Error while building request: " + raw)

    return status_code, json_raw
Beispiel #15
0
    except MacParamValidationError, e:
        show_error(e.message)
        exit(1)

    # validate roles
    expected_roles = []
    role_root_params = ["instance create"]
    role_params = []  # mandatory parameters
    role_optional_params = [
        'branch', 'hd', 'lifespan', 'environment', 'configuration',
        'bootstrap bash'
    ]

    if 'roles' in raw:
        if raw['roles'] is None:
            show_error("Roles section defined but empty")
            exit(1)
        else:
            raw_role_root_keys = raw['roles'].keys()
            for key_role_root in raw_role_root_keys:
                expected_roles.append(key_role_root)
                raw_role_keys = raw['roles'][key_role_root].keys()
                try:
                    validate_param(raw_role_keys, role_root_params)
                    for key_role in raw_role_keys:
                        raw_role = raw['roles'][key_role_root][key_role].keys()
                        validate_param(raw_role, role_params,
                                       role_optional_params)
                except MacParamValidationError, e:
                    show_error(e.message)
                    exit(1)
Beispiel #16
0
    raw_root_keys = raw.keys()
    try:
        validate_param(raw_root_keys, root_params, root_params_optional)
    except MacParamValidationError, e:
        show_error(e.message)
        exit(1)

    # validate roles
    expected_roles = []
    role_root_params = ["instance create"]
    role_params = []  # mandatory parameters
    role_optional_params = ['branch', 'hd', 'lifespan', 'environment', 'configuration', 'bootstrap bash']

    if 'roles' in raw:
        if raw['roles'] is None:
            show_error("Roles section defined but empty")
            exit(1)
        else:
            raw_role_root_keys = raw['roles'].keys()
            for key_role_root in raw_role_root_keys:
                expected_roles.append(key_role_root)
                raw_role_keys = raw['roles'][key_role_root].keys()
                try:
                    validate_param(raw_role_keys, role_root_params)
                    for key_role in raw_role_keys:
                        raw_role = raw['roles'][key_role_root][key_role].keys()
                        validate_param(raw_role, role_params, role_optional_params)
                except MacParamValidationError, e:
                    show_error(e.message)
                    exit(1)
Beispiel #17
0
def dispatch_cmds(args):

    if args.debug:
        logging.basicConfig(level=logging.DEBUG)
    elif args.verbose:
        logging.basicConfig(level=logging.INFO)
    else:
        logging.basicConfig(level=logging.WARN)

    maccli.quiet = args.quiet
    maccli.disable_strict_host_check = args.disable_strict_host

    maccli.logger.debug("Args options %s: " % args)

    if args.cmd == 'login':
        maccli.command_cli.login()

    elif maccli.user is None:
        maccli.command_cli.no_credentials()

    elif args.cmd == 'instance' or args.cmd == 'ins':

        if args.subcmd == 'create':
            if args.yaml:
                maccli.command_cli.convert_to_yaml(args)
            else:
                maccli.command_cli.instance_create(args.configuration, args.bootstrap, args.deployment, args.location, args.name,
                                                   #args.provider, args.release, args.release_version, args.branch, args.hardware, args.lifespan,
                                                   args.provider, args.release, args.release_version, "master", args.hardware, args.lifespan,
                                                   args.environment, args.hd, args.net, args.port)
        elif args.subcmd == 'update':
            if args.id is None:
                show_error("Parameter 'id' is required.")
                maccli.command_cli.instance_update_help()
            else:
                maccli.command_cli.instance_update(args.id, args.configuration, args.bootstrap)

        elif args.subcmd == 'destroy':
            if args.id is None:
                show_error("Parameter 'id' is required.")
                maccli.command_cli.instance_destroy_help()
            else:
                maccli.command_cli.instance_destroy(args.id)

        elif args.subcmd == 'list':
            maccli.command_cli.instance_list()

        elif args.subcmd == 'facts':
            maccli.command_cli.instance_fact(args.id)

        elif args.subcmd == 'log':
            maccli.command_cli.instance_log(args.id, args.follow)

        elif args.subcmd == 'lifespan':
            maccli.command_cli.instance_lifespan(args.id, args.amount)

        elif args.subcmd == 'ssh':
            if args.id is None:
                show_error("Parameter 'id' is required.")
                maccli.command_cli.instance_ssh_help()
            else:
                if args.command is not None:
                    command = " ".join(args.command)
                else:
                    command = args.command

                maccli.command_cli.instance_ssh(args.id, command)

    elif args.cmd == "configuration":
        if args.subcmd == 'list':
            maccli.command_cli.configuration_list()
        elif args.subcmd == 'search':
            maccli.command_cli.configuration_search(args.keyword, args.url)

    elif args.cmd == "infrastructure" or args.cmd == 'infra':
        if args.subcmd == 'list':
            maccli.command_cli.infrastructure_list()
        elif args.subcmd == 'macfile':
            maccli.command_cli.process_macfile(args.file[0], args.resume, args.param, args.quiet, args.on_failure)
        elif args.subcmd == 'items':
            maccli.command_cli.infrastructure_search(args.name, args.version)
        elif args.subcmd == 'destroy':
            maccli.command_cli.infrastructure_destroy(args.name, args.version)
        elif args.subcmd == 'update':
            maccli.command_cli.infrastructure_update(args.name, args.version, args.configuration)
        elif args.subcmd == 'lifespan':
            maccli.command_cli.infrastructure_lifespan(args.amount, args.name, args.version)
        elif args.subcmd == 'sshkey':
            maccli.command_cli.infrastructure_ssh_keys(args.name, args.version, args.known_host)

    elif args.cmd == "provider":
        if args.subcmd == 'credential':
            maccli.command_cli.credentials(args.provider, args.clientid, args.key, args.force_file)

    elif args.cmd == "resource":
        if args.subcmd == 'get_stdout':
            maccli.command_cli.resouce_get_stdout(args.infrastructure_name, args.infrastructure_version, args.resource_name, args.key)
Beispiel #18
0
def dispatch_cmds(args):

    if args.debug:
        logging.basicConfig(level=logging.DEBUG)
    elif args.verbose:
        logging.basicConfig(level=logging.INFO)
    else:
        logging.basicConfig(level=logging.WARN)

    maccli.logger.debug("Args options %s: " % args)

    if args.cmd == 'login':
        maccli.command_cli.login()

    elif maccli.user is None:
        maccli.command_cli.no_credentials()

    elif args.cmd == 'instance':

        if args.subcmd == 'create':
            if args.yaml:
                maccli.command_cli.convert_to_yaml(args)
            else:
                maccli.command_cli.instance_create(args.configuration, args.deployment, args.location, args.name,
                                                   args.provider, args.release, args.branch, args.hardware, args.lifespan,
                                                   args.environment, args.hd, args.port)
        elif args.subcmd == 'destroy':
            if args.id is None:
                show_error("Parameter 'id' is required.")
                maccli.command_cli.instance_destroy_help()
            else:
                maccli.command_cli.instance_destroy(args.id)

        elif args.subcmd == 'list':
            maccli.command_cli.instance_list()

        elif args.subcmd == 'facts':
            maccli.command_cli.instance_fact(args.id)

        elif args.subcmd == 'log':
            maccli.command_cli.instance_log(args.id)

        elif args.subcmd == 'lifespan':
            maccli.command_cli.instance_lifespan(args.id, args.amount)

        elif args.subcmd == 'ssh':
            if args.id is None:
                show_error("Parameter 'id' is required.")
                maccli.command_cli.instance_ssh_help()
            else:
                if args.command is not None:
                    command = " ".join(args.command)
                else:
                    command = args.command

                maccli.command_cli.instance_ssh(args.id, command)

    elif args.cmd == "configuration":
        if args.subcmd == 'list':
            maccli.command_cli.configuration_list()
        elif args.subcmd == 'search':
            maccli.command_cli.configuration_search(args.keyword, args.url)

    elif args.cmd == "infrastructure":
        if args.subcmd == 'list':
            maccli.command_cli.infrastructure_list()
        elif args.subcmd == 'macfile':
            maccli.command_cli.process_macfile(args.file[0], args.resume, args.param, args.quiet, args.on_failure)
        elif args.subcmd == 'instance':
            maccli.command_cli.infrastructure_search(args.name, args.version)
        elif args.subcmd == 'lifespan':
            maccli.command_cli.infrastructure_lifespan(args.amount, args.name, args.version)
Beispiel #19
0
def dispatch_cmds(args):

    if args.debug:
        logging.basicConfig(level=logging.DEBUG)
    elif args.verbose:
        logging.basicConfig(level=logging.INFO)
    else:
        logging.basicConfig(level=logging.WARN)

    maccli.quiet = args.quiet
    maccli.disable_strict_host_check = args.disable_strict_host

    maccli.logger.debug("Args options %s: " % args)

    if args.cmd == 'login':
        maccli.command_cli.login()

    elif maccli.user is None:
        maccli.command_cli.no_credentials()

    elif args.cmd == 'instance' or args.cmd == 'ins':

        if args.subcmd == 'create':
            if args.yaml:
                maccli.command_cli.convert_to_yaml(args)
            else:
                maccli.command_cli.instance_create(
                    args.configuration,
                    args.bootstrap,
                    args.deployment,
                    args.location,
                    args.name,
                    #args.provider, args.release, args.release_version, args.branch, args.hardware, args.lifespan,
                    args.provider,
                    args.release,
                    args.release_version,
                    "master",
                    args.hardware,
                    args.lifespan,
                    args.environment,
                    args.hd,
                    args.net,
                    args.port)
        elif args.subcmd == 'update':
            if args.id is None:
                show_error("Parameter 'id' is required.")
                maccli.command_cli.instance_update_help()
            else:
                maccli.command_cli.instance_update(args.id, args.configuration,
                                                   args.bootstrap)

        elif args.subcmd == 'destroy':
            if args.id is None:
                show_error("Parameter 'id' is required.")
                maccli.command_cli.instance_destroy_help()
            else:
                maccli.command_cli.instance_destroy(args.id)

        elif args.subcmd == 'list':
            maccli.command_cli.instance_list()

        elif args.subcmd == 'facts':
            maccli.command_cli.instance_fact(args.id)

        elif args.subcmd == 'log':
            maccli.command_cli.instance_log(args.id, args.follow)

        elif args.subcmd == 'lifespan':
            maccli.command_cli.instance_lifespan(args.id, args.amount)

        elif args.subcmd == 'ssh':
            if args.id is None:
                show_error("Parameter 'id' is required.")
                maccli.command_cli.instance_ssh_help()
            else:
                if args.command is not None:
                    command = " ".join(args.command)
                else:
                    command = args.command

                maccli.command_cli.instance_ssh(args.id, command)

    elif args.cmd == "configuration":
        if args.subcmd == 'list':
            maccli.command_cli.configuration_list()
        elif args.subcmd == 'search':
            maccli.command_cli.configuration_search(args.keyword, args.url)

    elif args.cmd == "infrastructure" or args.cmd == 'infra':
        if args.subcmd == 'list':
            maccli.command_cli.infrastructure_list()
        elif args.subcmd == 'macfile':
            maccli.command_cli.process_macfile(args.file[0], args.resume,
                                               args.param, args.quiet,
                                               args.on_failure)
        elif args.subcmd == 'items':
            maccli.command_cli.infrastructure_search(args.name, args.version)
        elif args.subcmd == 'destroy':
            maccli.command_cli.infrastructure_destroy(args.name, args.version)
        elif args.subcmd == 'update':
            maccli.command_cli.infrastructure_update(args.name, args.version,
                                                     args.configuration)
        elif args.subcmd == 'lifespan':
            maccli.command_cli.infrastructure_lifespan(args.amount, args.name,
                                                       args.version)
        elif args.subcmd == 'sshkey':
            maccli.command_cli.infrastructure_ssh_keys(args.name, args.version,
                                                       args.known_host)

    elif args.cmd == "provider":
        if args.subcmd == 'credential':
            maccli.command_cli.credentials(args.provider, args.clientid,
                                           args.key, args.force_file)

    elif args.cmd == "resource":
        if args.subcmd == 'get_stdout':
            maccli.command_cli.resouce_get_stdout(args.infrastructure_name,
                                                  args.infrastructure_version,
                                                  args.resource_name, args.key)
    raw_root_keys = raw.keys()
    try:
        validate_param(raw_root_keys, root_params, root_params_optional)
    except MacParamValidationError, e:
        show_error(e.message)
        exit(1)

    # validate roles
    expected_roles = []
    role_root_params = ["instance create"]
    role_params = []  # mandatory parameters
    role_optional_params = ['branch', 'hd', 'lifespan', 'environment', 'configuration', 'bootstrap bash']

    if 'roles' in raw:
        if raw['roles'] is None:
            show_error("Roles section defined but empty")
            exit(1)
        else:
            raw_role_root_keys = raw['roles'].keys()
            for key_role_root in raw_role_root_keys:
                expected_roles.append(key_role_root)
                raw_role_keys = raw['roles'][key_role_root].keys()
                try:
                    validate_param(raw_role_keys, role_root_params)
                    for key_role in raw_role_keys:
                        raw_role = raw['roles'][key_role_root][key_role].keys()
                        validate_param(raw_role, role_params, role_optional_params)
                except MacParamValidationError, e:
                    show_error(e.message)
                    exit(1)