Пример #1
0
def bootstrap_gcp():
    try:
        input_params = bootstrapper_utils.normalize_input_params(request)
        base_config = bootstrapper_utils.build_base_configs(input_params)

        response = archive_utils.create_gcp_bucket(
            base_config, input_params['hostname'],
            input_params['gcp_project_id'], input_params["gcp_access_token"])
        return jsonify(response=response)

    except KeyError as ke:
        print(ke)
        abort(
            400,
            'Invalid input parameters! Not all required parameters are present'
        )
    except (BadRequest, RequiredParametersError):
        abort(400, 'Invalid input parameters for basic configuration!')
    except TemplateNotFoundError:
        print('Could not load templates!')
        abort(500, 'Could not load template!')
Пример #2
0
def build_bootstrap_gcp(input_config):
    init_application()
    try:
        yaml_conf = input_config.read()
        payload = yaml.load(yaml_conf)
        print(payload)
        base_config = bootstrapper_utils.build_base_configs(payload)

        if not set(['GCP_PROJECT_ID', 'GCP_ACCESS_TOKEN']).issubset(
                set(os.environ)):
            print(
                'GCP bootstrap type requires GCP_PROJECT_ID and GCP_ACCESS_TOKEN'
            )
            sys.exit(1)

        project_id = os.environ['GCP_PROJECT_ID']
        access_token = os.environ['GCP_ACESSS_TOKEN']

        message = archive_utils.create_gcp_bucket(base_config,
                                                  payload['hostname'],
                                                  project_id, access_token)

        print(message)

    except IOError as ioe:
        print('Could not read from input')
        print(ioe)
        sys.exit(1)
    except yaml.YAMLError as ye:
        print('Could not load YAML input')
        print(ye)
        sys.exit(2)
    except (BadRequest, RequiredParametersError):
        sys.exit(3)
    except TemplateNotFoundError:
        print('Could not load templates!')
        sys.exit(4)
Пример #3
0
def generate_bootstrap_package():
    """
    Main function to build a bootstrap archive. You must post the following params:
    hostname: we cannot build an archive without at least a hostname
    deployment_type: openstack, kvm, vmware, etc.
    archive_type: zip, iso

    You must also supply all the variables required from included templates

    :return: binary package containing variable interpolated templates
    """
    input_params = dict()
    try:
        # input_params = request.get_json() or request.form.to_dict()
        input_params = bootstrapper_utils.normalize_input_params(request)
        base_config = bootstrapper_utils.build_base_configs(input_params)

    except (BadRequest, RequiredParametersError):
        err_string = '\nRequired variables: hostname'
        err_string += '\nOptional variables: '
        vs = bootstrapper_utils.get_bootstrap_variables(input_params)
        for v in vs:
            err_string += '%s ' % v
        print('aborting due to bad request, invalid params')
        abort(400, 'Invalid input parameters %s' % err_string)
    except TemplateNotFoundError:
        print('aborting, Could not load templates!')
        abort(500, 'Could not load template!')

    # if desired deployment type is openstack, then add the heat templates and whatnot
    if 'deployment_type' in input_params and input_params[
            'deployment_type'] == 'openstack':
        print('Including openstack')
        try:
            base_config = bootstrapper_utils.build_openstack_heat(base_config,
                                                                  input_params,
                                                                  archive=True)
        except RequiredParametersError:
            abort(400, 'Could not parse JSON data')

    if 'hostname' not in input_params:
        abort(400, 'No hostname found in posted data')

    # if the user supplies an 'archive_type' parameter we can return either a ZIP or ISO
    archive_type = input_params.get('archive_type', 'zip')

    # user has specified they want an ISO built
    if archive_type == 'iso':
        archive = archive_utils.create_iso(base_config,
                                           input_params['hostname'])
        mime_type = 'application/iso-image'

    elif archive_type == 'tgz':
        archive = archive_utils.create_tgz(base_config,
                                           input_params['hostname'])
        mime_type = 'application/gzip'

    elif archive_type == 's3':
        required_keys = {'aws_location', 'aws_secret', 'aws_key'}
        if not required_keys.issubset(input_params):
            r = jsonify(
                message=
                f"Not all required keys for archive_type: {archive_type} are "
                f"present. Required are {required_keys}",
                success=False,
                status_code=400)
            r.status_code = 400
            return r

        response = archive_utils.create_s3_bucket(base_config,
                                                  input_params['hostname'],
                                                  input_params['aws_location'],
                                                  input_params['aws_key'],
                                                  input_params['aws_secret'])
        return jsonify(response=response)

    elif archive_type == 'azure':
        required_keys = {'azure_account_name', 'azure_account_key'}
        if not required_keys.issubset(input_params):
            r = jsonify(
                message=
                f"Not all required keys for archive_type: {archive_type} are "
                f"present. Required are {required_keys}",
                success=False,
                status_code=400)
            r.status_code = 400
            return r

        response = archive_utils.create_azure_fileshare(
            base_config, input_params['hostname'],
            input_params['azure_account_name'],
            input_params['azure_account_key'])
        return jsonify(response=response)

    elif archive_type == 'gcp':
        required_keys = {'project_id', 'access_token'}
        if not required_keys.issubset(input_params):
            r = jsonify(
                message=
                f"Not all required keys for archive_type: {archive_type} are "
                f"present. Required are {required_keys}",
                success=False,
                status_code=400)
            r.status_code = 400
            return r
        response = archive_utils.create_gcp_bucket(
            base_config, input_params['hostname'], input_params['project_id'],
            input_params["access_token"])
        return jsonify(response=response)

    else:
        # no ISO required, just make a zip
        archive = archive_utils.create_archive(base_config,
                                               input_params['hostname'])
        mime_type = 'application/zip'

    print("archive path is: %s" % archive)
    if archive is None:
        print('Aborting with no archive created')
        abort(
            500,
            'Could not create archive! Check bootstrapper logs for more information'
        )

    return send_file(archive, mimetype=mime_type, as_attachment=True)