googlecompute

package
v0.0.0-...-9eb14d7 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2022 License: MPL-2.0 Imports: 32 Imported by: 0

Documentation

Overview

The googlecompute package contains a packer.Builder implementation that builds images for Google Compute Engine.

Index

Constants

View Source
const BuilderId = "packer.googlecompute"

The unique ID for this builder.

View Source
const StartupScriptKey string = "startup-script"
View Source
const StartupScriptStatusDone string = "done"
View Source
const StartupScriptStatusError string = "error"
View Source
const StartupScriptStatusKey string = "startup-script-status"
View Source
const StartupScriptStatusNotDone string = "notdone"
View Source
const StartupWrappedScriptKey string = "packer-wrapped-startup-script"

Variables

View Source
var DriverScopes = []string{"https://www.googleapis.com/auth/compute", "https://www.googleapis.com/auth/devstorage.full_control"}
View Source
var StartupScriptLinux string = fmt.Sprintf(`#!/usr/bin/env bash
echo "Packer startup script starting."
RETVAL=0
BASEMETADATAURL=http://metadata/computeMetadata/v1/instance/

GetMetadata () {
  echo "$(curl -f -H "Metadata-Flavor: Google" ${BASEMETADATAURL}/${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)

if [[ ! -z $STARTUPSCRIPT ]]; then
  echo "Executing user-provided startup script..."
  echo "${STARTUPSCRIPT}" > ${STARTUPSCRIPTPATH}
  chmod +x ${STARTUPSCRIPTPATH}
  ${STARTUPSCRIPTPATH}
  RETVAL=$?

  if [[ ! -z $STARTUPSCRIPTLOGDEST ]]; then
    echo "Uploading user-provided startup script log to ${STARTUPSCRIPTLOGDEST}..."
    gsutil -h "Content-Type:text/plain" cp ${STARTUPSCRIPTLOGPATH} ${STARTUPSCRIPTLOGDEST}
  fi

  rm ${STARTUPSCRIPTPATH}
fi

echo "Packer startup script done."
SetMetadata %s %s
exit $RETVAL
`, StartupWrappedScriptKey, StartupScriptStatusKey, StartupScriptStatusDone)
View Source
var StartupScriptWindows string = ""
View Source
var TemplateFuncs = template.FuncMap{
	"clean_image_name": templateCleanImageName,
}

Functions

func ProcessAccountFile

func ProcessAccountFile(account_file *AccountFile, text string) error

Types

type AccountFile

type AccountFile struct {
	PrivateKeyId string `json:"private_key_id"`
	PrivateKey   string `json:"private_key"`
	ClientEmail  string `json:"client_email"`
	ClientId     string `json:"client_id"`
}

accountFile represents the structure of the account file JSON file.

type Artifact

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

Artifact represents a GCE image as the result of a Packer build.

func (*Artifact) BuilderId

func (*Artifact) BuilderId() string

BuilderId returns the builder Id.

func (*Artifact) Destroy

func (a *Artifact) Destroy() error

Destroy destroys the GCE image represented by the artifact.

func (*Artifact) Files

func (*Artifact) Files() []string

Files returns the files represented by the artifact.

func (*Artifact) Id

func (a *Artifact) Id() string

Id returns the GCE image name.

func (*Artifact) State

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

func (*Artifact) String

func (a *Artifact) String() string

String returns the string representation of the artifact.

type Builder

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

Builder represents a Packer Builder.

func (*Builder) Cancel

func (b *Builder) Cancel()

Cancel.

func (*Builder) Prepare

func (b *Builder) Prepare(raws ...interface{}) ([]string, error)

Prepare processes the build configuration parameters.

func (*Builder) Run

func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error)

Run executes a googlecompute Packer build and returns a packer.Artifact representing a GCE machine image.

type Config

type Config struct {
	common.PackerConfig `mapstructure:",squash"`
	Comm                communicator.Config `mapstructure:",squash"`

	AccountFile string `mapstructure:"account_file"`
	ProjectId   string `mapstructure:"project_id"`

	AcceleratorType      string            `mapstructure:"accelerator_type"`
	AcceleratorCount     int64             `mapstructure:"accelerator_count"`
	Address              string            `mapstructure:"address"`
	DiskName             string            `mapstructure:"disk_name"`
	DiskSizeGb           int64             `mapstructure:"disk_size"`
	DiskType             string            `mapstructure:"disk_type"`
	ImageName            string            `mapstructure:"image_name"`
	ImageDescription     string            `mapstructure:"image_description"`
	ImageFamily          string            `mapstructure:"image_family"`
	ImageLabels          map[string]string `mapstructure:"image_labels"`
	InstanceName         string            `mapstructure:"instance_name"`
	Labels               map[string]string `mapstructure:"labels"`
	MachineType          string            `mapstructure:"machine_type"`
	Metadata             map[string]string `mapstructure:"metadata"`
	Network              string            `mapstructure:"network"`
	NetworkProjectId     string            `mapstructure:"network_project_id"`
	OmitExternalIP       bool              `mapstructure:"omit_external_ip"`
	OnHostMaintenance    string            `mapstructure:"on_host_maintenance"`
	Preemptible          bool              `mapstructure:"preemptible"`
	RawStateTimeout      string            `mapstructure:"state_timeout"`
	Region               string            `mapstructure:"region"`
	Scopes               []string          `mapstructure:"scopes"`
	SourceImage          string            `mapstructure:"source_image"`
	SourceImageFamily    string            `mapstructure:"source_image_family"`
	SourceImageProjectId string            `mapstructure:"source_image_project_id"`
	StartupScriptFile    string            `mapstructure:"startup_script_file"`
	Subnetwork           string            `mapstructure:"subnetwork"`
	Tags                 []string          `mapstructure:"tags"`
	UseInternalIP        bool              `mapstructure:"use_internal_ip"`
	Zone                 string            `mapstructure:"zone"`

	Account AccountFile
	// contains filtered or unexported fields
}

Config is the configuration structure for the GCE builder. It stores both the publicly settable state as well as the privately generated state of the config object.

func NewConfig

func NewConfig(raws ...interface{}) (*Config, []string, error)

func (*Config) CalcTimeout

func (c *Config) CalcTimeout() error

type Driver

type Driver interface {
	// CreateImage creates an image from the given disk in Google Compute
	// Engine.
	CreateImage(name, description, family, zone, disk string, image_labels map[string]string) (<-chan *Image, <-chan error)

	// DeleteImage deletes the image with the given name.
	DeleteImage(name string) <-chan error

	// DeleteInstance deletes the given instance, keeping the boot disk.
	DeleteInstance(zone, name string) (<-chan error, error)

	// DeleteDisk deletes the disk with the given name.
	DeleteDisk(zone, name string) (<-chan error, error)

	// GetImage gets an image; tries the default and public projects. If
	// fromFamily is true, name designates an image family instead of a
	// particular image.
	GetImage(name string, fromFamily bool) (*Image, error)

	// GetImageFromProject gets an image from a specific project. If fromFamily
	// is true, name designates an image family instead of a particular image.
	GetImageFromProject(project, name string, fromFamily bool) (*Image, error)

	// GetInstanceMetadata gets a metadata variable for the instance, name.
	GetInstanceMetadata(zone, name, key string) (string, error)

	// GetInternalIP gets the GCE-internal IP address for the instance.
	GetInternalIP(zone, name string) (string, error)

	// GetNatIP gets the NAT IP address for the instance.
	GetNatIP(zone, name string) (string, error)

	// GetSerialPortOutput gets the Serial Port contents for the instance.
	GetSerialPortOutput(zone, name string) (string, error)

	// ImageExists returns true if the specified image exists. If an error
	// occurs calling the API, this method returns false.
	ImageExists(name string) bool

	// RunInstance takes the given config and launches an instance.
	RunInstance(*InstanceConfig) (<-chan error, error)

	// WaitForInstance waits for an instance to reach the given state.
	WaitForInstance(state, zone, name string) <-chan error

	// CreateOrResetWindowsPassword creates or resets the password for a user on an Windows instance.
	CreateOrResetWindowsPassword(zone, name string, config *WindowsPasswordConfig) (<-chan error, error)
}

Driver is the interface that has to be implemented to communicate with GCE. The Driver interface exists mostly to allow a mock implementation to be used to test the steps.

func NewDriverGCE

func NewDriverGCE(ui packer.Ui, p string, a *AccountFile) (Driver, error)

type DriverMock

type DriverMock struct {
	CreateImageName            string
	CreateImageDesc            string
	CreateImageFamily          string
	CreateImageLabels          map[string]string
	CreateImageZone            string
	CreateImageDisk            string
	CreateImageResultLicenses  []string
	CreateImageResultProjectId string
	CreateImageResultSelfLink  string
	CreateImageResultSizeGb    int64
	CreateImageErrCh           <-chan error
	CreateImageResultCh        <-chan *Image

	DeleteImageName  string
	DeleteImageErrCh <-chan error

	DeleteInstanceZone  string
	DeleteInstanceName  string
	DeleteInstanceErrCh <-chan error
	DeleteInstanceErr   error

	DeleteDiskZone  string
	DeleteDiskName  string
	DeleteDiskErrCh <-chan error
	DeleteDiskErr   error

	GetImageName       string
	GetImageFromFamily bool
	GetImageResult     *Image
	GetImageErr        error

	GetImageFromProjectProject    string
	GetImageFromProjectName       string
	GetImageFromProjectFromFamily bool
	GetImageFromProjectResult     *Image
	GetImageFromProjectErr        error

	GetInstanceMetadataZone   string
	GetInstanceMetadataName   string
	GetInstanceMetadataKey    string
	GetInstanceMetadataResult string
	GetInstanceMetadataErr    error

	GetNatIPZone   string
	GetNatIPName   string
	GetNatIPResult string
	GetNatIPErr    error

	GetInternalIPZone   string
	GetInternalIPName   string
	GetInternalIPResult string
	GetInternalIPErr    error

	GetSerialPortOutputZone   string
	GetSerialPortOutputName   string
	GetSerialPortOutputResult string
	GetSerialPortOutputErr    error

	ImageExistsName   string
	ImageExistsResult bool

	RunInstanceConfig *InstanceConfig
	RunInstanceErrCh  <-chan error
	RunInstanceErr    error

	CreateOrResetWindowsPasswordZone     string
	CreateOrResetWindowsPasswordInstance string
	CreateOrResetWindowsPasswordConfig   *WindowsPasswordConfig
	CreateOrResetWindowsPasswordErr      error
	CreateOrResetWindowsPasswordErrCh    <-chan error

	WaitForInstanceState string
	WaitForInstanceZone  string
	WaitForInstanceName  string
	WaitForInstanceErrCh <-chan error
}

DriverMock is a Driver implementation that is a mocked out so that it can be used for tests.

func (*DriverMock) CreateImage

func (d *DriverMock) CreateImage(name, description, family, zone, disk string, image_labels map[string]string) (<-chan *Image, <-chan error)

func (*DriverMock) CreateOrResetWindowsPassword

func (d *DriverMock) CreateOrResetWindowsPassword(instance, zone string, c *WindowsPasswordConfig) (<-chan error, error)

func (*DriverMock) DeleteDisk

func (d *DriverMock) DeleteDisk(zone, name string) (<-chan error, error)

func (*DriverMock) DeleteImage

func (d *DriverMock) DeleteImage(name string) <-chan error

func (*DriverMock) DeleteInstance

func (d *DriverMock) DeleteInstance(zone, name string) (<-chan error, error)

func (*DriverMock) GetImage

func (d *DriverMock) GetImage(name string, fromFamily bool) (*Image, error)

func (*DriverMock) GetImageFromProject

func (d *DriverMock) GetImageFromProject(project, name string, fromFamily bool) (*Image, error)

func (*DriverMock) GetInstanceMetadata

func (d *DriverMock) GetInstanceMetadata(zone, name, key string) (string, error)

func (*DriverMock) GetInternalIP

func (d *DriverMock) GetInternalIP(zone, name string) (string, error)

func (*DriverMock) GetNatIP

func (d *DriverMock) GetNatIP(zone, name string) (string, error)

func (*DriverMock) GetSerialPortOutput

func (d *DriverMock) GetSerialPortOutput(zone, name string) (string, error)

func (*DriverMock) GetWindowsPassword

func (d *DriverMock) GetWindowsPassword() (string, error)

func (*DriverMock) ImageExists

func (d *DriverMock) ImageExists(name string) bool

func (*DriverMock) RunInstance

func (d *DriverMock) RunInstance(c *InstanceConfig) (<-chan error, error)

func (*DriverMock) WaitForInstance

func (d *DriverMock) WaitForInstance(state, zone, name string) <-chan error

type Image

type Image struct {
	Labels    map[string]string
	Licenses  []string
	Name      string
	ProjectId string
	SelfLink  string
	SizeGb    int64
}

func (*Image) IsWindows

func (i *Image) IsWindows() bool

type InstanceConfig

type InstanceConfig struct {
	AcceleratorType   string
	AcceleratorCount  int64
	Address           string
	Description       string
	DiskSizeGb        int64
	DiskType          string
	Image             *Image
	Labels            map[string]string
	MachineType       string
	Metadata          map[string]string
	Name              string
	Network           string
	NetworkProjectId  string
	OmitExternalIP    bool
	OnHostMaintenance string
	Preemptible       bool
	Region            string
	Scopes            []string
	Subnetwork        string
	Tags              []string
	Zone              string
}

type StepCheckExistingImage

type StepCheckExistingImage int

StepCheckExistingImage represents a Packer build step that checks if the target image already exists, and aborts immediately if so.

func (*StepCheckExistingImage) Cleanup

func (s *StepCheckExistingImage) Cleanup(state multistep.StateBag)

Cleanup.

func (*StepCheckExistingImage) Run

Run executes the Packer build step that checks if the image already exists.

type StepCreateImage

type StepCreateImage int

StepCreateImage represents a Packer build step that creates GCE machine images.

func (*StepCreateImage) Cleanup

func (s *StepCreateImage) Cleanup(state multistep.StateBag)

Cleanup.

func (*StepCreateImage) Run

Run executes the Packer build step that creates a GCE machine image.

The image is created from the persistent disk used by the instance. The instance must be deleted and the disk retained before doing this step.

type StepCreateInstance

type StepCreateInstance struct {
	Debug bool
}

StepCreateInstance represents a Packer build step that creates GCE instances.

func (*StepCreateInstance) Cleanup

func (s *StepCreateInstance) Cleanup(state multistep.StateBag)

Cleanup destroys the GCE instance created during the image creation process.

func (*StepCreateInstance) Run

Run executes the Packer build step that creates a GCE instance.

type StepCreateSSHKey

type StepCreateSSHKey struct {
	Debug          bool
	DebugKeyPath   string
	PrivateKeyFile string
}

StepCreateSSHKey represents a Packer build step that generates SSH key pairs.

func (*StepCreateSSHKey) Cleanup

func (s *StepCreateSSHKey) Cleanup(state multistep.StateBag)

Nothing to clean up. SSH keys are associated with a single GCE instance.

func (*StepCreateSSHKey) Run

Run executes the Packer build step that generates SSH key pairs. The key pairs are added to the multistep state as "ssh_private_key" and "ssh_public_key".

type StepCreateWindowsPassword

type StepCreateWindowsPassword struct {
	Debug        bool
	DebugKeyPath string
}

StepCreateWindowsPassword represents a Packer build step that sets the windows password on a Windows GCE instance.

func (*StepCreateWindowsPassword) Cleanup

func (s *StepCreateWindowsPassword) Cleanup(state multistep.StateBag)

Nothing to clean up. The windows password is only created on the single instance.

func (*StepCreateWindowsPassword) Run

Run executes the Packer build step that sets the windows password on a Windows GCE instance.

type StepInstanceInfo

type StepInstanceInfo struct {
	Debug bool
}

stepInstanceInfo represents a Packer build step that gathers GCE instance info.

func (*StepInstanceInfo) Cleanup

func (s *StepInstanceInfo) Cleanup(state multistep.StateBag)

Cleanup.

func (*StepInstanceInfo) Run

Run executes the Packer build step that gathers GCE instance info. This adds "instance_ip" to the multistep state.

type StepTeardownInstance

type StepTeardownInstance struct {
	Debug bool
}

StepTeardownInstance represents a Packer build step that tears down GCE instances.

func (*StepTeardownInstance) Cleanup

func (s *StepTeardownInstance) Cleanup(state multistep.StateBag)

Deleting the instance does not remove the boot disk. This cleanup removes the disk.

func (*StepTeardownInstance) Run

Run executes the Packer build step that tears down a GCE instance.

type StepWaitStartupScript

type StepWaitStartupScript int

func (*StepWaitStartupScript) Cleanup

func (s *StepWaitStartupScript) Cleanup(state multistep.StateBag)

Cleanup.

func (*StepWaitStartupScript) Run

Run reads the instance metadata and looks for the log entry indicating the startup script finished.

type WindowsPasswordConfig

type WindowsPasswordConfig struct {
	UserName string    `json:"userName"`
	Modulus  string    `json:"modulus"`
	Exponent string    `json:"exponent"`
	Email    string    `json:"email"`
	ExpireOn time.Time `json:"expireOn"`
	// contains filtered or unexported fields
}

WindowsPasswordConfig is the data structue that GCE needs to encrypt the created windows password.

Jump to

Keyboard shortcuts

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