googlecomputeexport

package
v1.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2023 License: MPL-2.0 Imports: 18 Imported by: 1

Documentation

Index

Constants

View Source
const BuilderId = "packer.post-processor.googlecompute-export"

Variables

View Source
var StartupScript string = fmt.Sprintf(`#!/bin/bash

GetMetadata () {
  echo "$(curl -f -H "Metadata-Flavor: Google" http://metadata/computeMetadata/v1/instance/attributes/$1 2> /dev/null)"
}

ZONE=$(basename $(GetMetadata zone))

SetMetadata () {
  gcloud compute instances add-metadata ${HOSTNAME} --metadata ${1}=${2} --zone ${ZONE}
}

STARTUPSCRIPT=$(GetMetadata attributes/%s)
STARTUPSCRIPTPATH=/packer-wrapped-startup-script
if [ -f "/var/log/startupscript.log" ]; then
  STARTUPSCRIPTLOGPATH=/var/log/startupscript.log
else
  STARTUPSCRIPTLOGPATH=/var/log/daemon.log
fi
STARTUPSCRIPTLOGDEST=$(GetMetadata attributes/startup-script-log-dest)

IMAGENAME=$(GetMetadata image_name)
NAME=$(GetMetadata name)
DISKNAME=${NAME}-toexport
PATHS=($(GetMetadata paths))

Exit () {
  for i in ${PATHS[@]}; do
    LOGDEST="${i}.exporter.log"
    echo "Uploading exporter log to ${LOGDEST}..."
    gsutil -h "Content-Type:text/plain" cp /var/log/daemon.log ${LOGDEST}
  done
  exit $1
}

echo "####### Export configuration #######"
echo "Image name - ${IMAGENAME}"
echo "Instance name - ${NAME}"
echo "Instance zone - ${ZONE}"
echo "Disk name - ${DISKNAME}"
echo "Export paths - ${PATHS}"
echo "####################################"

echo "Creating disk from image to be exported..."
if ! gcloud compute disks create ${DISKNAME} --image ${IMAGENAME} --zone ${ZONE}; then
  echo "Failed to create disk."
  Exit 1
fi

echo "Attaching disk..."
if ! gcloud compute instances attach-disk ${NAME} --disk ${DISKNAME} --device-name toexport --zone ${ZONE}; then
  echo "Failed to attach disk."
  Exit 1
fi

echo "GCEExport: Running export tool."
gce_export -gcs_path "${PATHS[0]}" -disk /dev/disk/by-id/google-toexport -y
if [ $? -ne 0 ]; then
  echo "ExportFailed: Failed to export disk source to ${PATHS[0]}."
  Exit 1
fi

echo "ExportSuccess"
sync

echo "Detaching disk..."
if ! gcloud compute instances detach-disk ${NAME} --disk ${DISKNAME} --zone ${ZONE}; then
  echo "Failed to detach disk."
fi

FAIL=0
echo "Deleting disk..."
if ! gcloud compute disks delete ${DISKNAME} --zone ${ZONE}; then
  echo "Failed to delete disk."
  FAIL=1
fi

for i in ${PATHS[@]:1}; do
  echo "Copying archive image to ${i}..."
  if ! gsutil -o GSUtil:parallel_composite_upload_threshold=100M cp ${PATHS[0]} ${i}; then
    echo "Failed to copy image to ${i}."
    FAIL=1
  fi
done

SetMetadata %s %s

Exit ${FAIL}
`, googlecompute.StartupWrappedScriptKey, googlecompute.StartupScriptStatusKey, googlecompute.StartupScriptStatusDone)

Functions

This section is empty.

Types

type Artifact

type Artifact struct {

	// StateData should store data such as GeneratedData
	// to be shared with post-processors
	StateData map[string]interface{}
	// contains filtered or unexported fields
}

func (*Artifact) BuilderId

func (*Artifact) BuilderId() string

func (*Artifact) Destroy

func (a *Artifact) Destroy() error

func (*Artifact) Files

func (a *Artifact) Files() []string

func (*Artifact) Id

func (*Artifact) Id() string

func (*Artifact) State

func (a *Artifact) State(name string) interface{}

func (*Artifact) String

func (a *Artifact) String() string

type Config

type Config struct {
	sdk_common.PackerConfig `mapstructure:",squash"`
	common.Authentication   `mapstructure:",squash"`

	// The service account scopes for launched exporter post-processor instance.
	// Defaults to:
	//
	// “`json
	// [
	//   "https://www.googleapis.com/auth/cloud-platform"
	// ]
	// “`
	Scopes []string `mapstructure:"scopes" required:"false"`
	//The size of the export instances disk.
	//The disk is unused for the export but a larger size will increase `pd-ssd` read speed.
	//This defaults to `200`, which is 200GB.
	DiskSizeGb int64 `mapstructure:"disk_size"`
	//Type of disk used to back the export instance, like
	//`pd-ssd` or `pd-standard`. Defaults to `pd-ssd`.
	DiskType string `mapstructure:"disk_type"`
	//The export instance machine type. Defaults to `"n1-highcpu-4"`.
	MachineType string `mapstructure:"machine_type"`
	//The Google Compute network id or URL to use for the export instance.
	//Defaults to `"default"`. If the value is not a URL, it
	//will be interpolated to `projects/((builder_project_id))/global/networks/((network))`.
	//This value is not required if a `subnet` is specified.
	Network string `mapstructure:"network"`
	//A list of GCS paths where the image will be exported.
	//For example `'gs://mybucket/path/to/file.tar.gz'`
	Paths []string `mapstructure:"paths" required:"true"`
	//The Google Compute subnetwork id or URL to use for
	//the export instance. Only required if the `network` has been created with
	//custom subnetting. Note, the region of the subnetwork must match the
	//`zone` in which the VM is launched. If the value is not a URL,
	//it will be interpolated to
	//`projects/((builder_project_id))/regions/((region))/subnetworks/((subnetwork))`
	Subnetwork string `mapstructure:"subnetwork"`
	//The zone in which to launch the export instance. Defaults
	//to `googlecompute` builder zone. Example: `"us-central1-a"`
	Zone                string `mapstructure:"zone"`
	IAP                 bool   `mapstructure-to-hcl2:",skip"`
	ServiceAccountEmail string `mapstructure:"service_account_email"`
	// contains filtered or unexported fields
}

func (*Config) FlatMapstructure

func (*Config) FlatMapstructure() interface{ HCL2Spec() map[string]hcldec.Spec }

FlatMapstructure returns a new FlatConfig. FlatConfig is an auto-generated flat version of Config. Where the contents a fields with a `mapstructure:,squash` tag are bubbled up.

type FlatConfig

type FlatConfig struct {
	PackerBuildName           *string           `mapstructure:"packer_build_name" cty:"packer_build_name" hcl:"packer_build_name"`
	PackerBuilderType         *string           `mapstructure:"packer_builder_type" cty:"packer_builder_type" hcl:"packer_builder_type"`
	PackerCoreVersion         *string           `mapstructure:"packer_core_version" cty:"packer_core_version" hcl:"packer_core_version"`
	PackerDebug               *bool             `mapstructure:"packer_debug" cty:"packer_debug" hcl:"packer_debug"`
	PackerForce               *bool             `mapstructure:"packer_force" cty:"packer_force" hcl:"packer_force"`
	PackerOnError             *string           `mapstructure:"packer_on_error" cty:"packer_on_error" hcl:"packer_on_error"`
	PackerUserVars            map[string]string `mapstructure:"packer_user_variables" cty:"packer_user_variables" hcl:"packer_user_variables"`
	PackerSensitiveVars       []string          `mapstructure:"packer_sensitive_variables" cty:"packer_sensitive_variables" hcl:"packer_sensitive_variables"`
	AccessToken               *string           `mapstructure:"access_token" required:"false" cty:"access_token" hcl:"access_token"`
	AccountFile               *string           `mapstructure:"account_file" required:"false" cty:"account_file" hcl:"account_file"`
	CredentialsFile           *string           `mapstructure:"credentials_file" required:"false" cty:"credentials_file" hcl:"credentials_file"`
	CredentialsJSON           *string           `mapstructure:"credentials_json" required:"false" cty:"credentials_json" hcl:"credentials_json"`
	ImpersonateServiceAccount *string           `` /* 127-byte string literal not displayed */
	VaultGCPOauthEngine       *string           `mapstructure:"vault_gcp_oauth_engine" cty:"vault_gcp_oauth_engine" hcl:"vault_gcp_oauth_engine"`
	Scopes                    []string          `mapstructure:"scopes" required:"false" cty:"scopes" hcl:"scopes"`
	DiskSizeGb                *int64            `mapstructure:"disk_size" cty:"disk_size" hcl:"disk_size"`
	DiskType                  *string           `mapstructure:"disk_type" cty:"disk_type" hcl:"disk_type"`
	MachineType               *string           `mapstructure:"machine_type" cty:"machine_type" hcl:"machine_type"`
	Network                   *string           `mapstructure:"network" cty:"network" hcl:"network"`
	Paths                     []string          `mapstructure:"paths" required:"true" cty:"paths" hcl:"paths"`
	Subnetwork                *string           `mapstructure:"subnetwork" cty:"subnetwork" hcl:"subnetwork"`
	Zone                      *string           `mapstructure:"zone" cty:"zone" hcl:"zone"`
	ServiceAccountEmail       *string           `mapstructure:"service_account_email" cty:"service_account_email" hcl:"service_account_email"`
}

FlatConfig is an auto-generated flat version of Config. Where the contents of a field with a `mapstructure:,squash` tag are bubbled up.

func (*FlatConfig) HCL2Spec

func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec

HCL2Spec returns the hcl spec of a Config. This spec is used by HCL to read the fields of Config. The decoded values from this spec will then be applied to a FlatConfig.

type PostProcessor

type PostProcessor struct {
	// contains filtered or unexported fields
}

func (*PostProcessor) ConfigSpec

func (p *PostProcessor) ConfigSpec() hcldec.ObjectSpec

func (*PostProcessor) Configure

func (p *PostProcessor) Configure(raws ...interface{}) error

func (*PostProcessor) PostProcess

func (p *PostProcessor) PostProcess(ctx context.Context, ui packersdk.Ui, artifact packersdk.Artifact) (packersdk.Artifact, bool, bool, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL