コード例 #1
0
def bootstrap_openstack():
    try:
        input_params = request.get_json() or request.form.to_dict()
        base_config = bootstrapper_utils.build_base_configs(input_params)
        base_config = bootstrapper_utils.build_openstack_heat(base_config,
                                                              input_params,
                                                              archive=True)

        archive = archive_utils.create_archive(base_config,
                                               input_params['hostname'])
        mime_type = 'application/zip'

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

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

    except (BadRequest, RequiredParametersError):
        abort(400, 'Invalid input parameters')
    except TemplateNotFoundError:
        print('Could not load templates!')
        abort(500, 'Could not load template!')
コード例 #2
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
    """

    try:
        posted_json = request.get_json(force=True)
        base_config = bootstrapper_utils.build_base_configs(posted_json)

    except (BadRequest, RequiredParametersError):
        abort(400, 'Invalid input parameters')
    except TemplateNotFoundError:
        print('Could not load tempaltes!')
        abort(500, 'Could not load template!')

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

    if 'hostname' not in posted_json:
        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 = posted_json.get('archive_type', 'zip')

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

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

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

    return send_file(archive, mimetype=mime_type)
コード例 #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)