googlecompute

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2016 License: MPL-2.0 Imports: 30 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(`#!/bin/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=$(GetMetadata zone | grep -oP "[^/]*$")

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 = ""

Functions

func ProcessAccountFile added in v0.11.0

func ProcessAccountFile(account_file *AccountFile, text string) error

Types

type AccountFile added in v0.11.0

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 added in v0.7.2

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"`

	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"`
	InstanceName         string            `mapstructure:"instance_name"`
	MachineType          string            `mapstructure:"machine_type"`
	Metadata             map[string]string `mapstructure:"metadata"`
	Network              string            `mapstructure:"network"`
	OmitExternalIP       bool              `mapstructure:"omit_external_ip"`
	Preemptible          bool              `mapstructure:"preemptible"`
	RawStateTimeout      string            `mapstructure:"state_timeout"`
	Region               string            `mapstructure:"region"`
	SourceImage          string            `mapstructure:"source_image"`
	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 added in v0.11.0

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) (<-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.
	GetImage(name string) (*Image, error)

	// GetImageFromProject gets an image from a specific project.
	GetImageFromProject(project, name string) (*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
	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
	GetImageResult *Image
	GetImageErr    error

	GetImageFromProjectProject string
	GetImageFromProjectName    string
	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) (<-chan *Image, <-chan error)

func (*DriverMock) CreateOrResetWindowsPassword added in v0.11.0

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

func (*DriverMock) DeleteDisk added in v0.7.5

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 added in v0.11.0

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

func (*DriverMock) GetImageFromProject added in v0.11.0

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

func (*DriverMock) GetInstanceMetadata added in v0.11.0

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

func (*DriverMock) GetInternalIP added in v0.8.0

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

func (*DriverMock) GetNatIP

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

func (*DriverMock) GetSerialPortOutput added in v0.11.0

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

func (*DriverMock) GetWindowsPassword added in v0.11.0

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

func (*DriverMock) ImageExists added in v0.7.5

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 added in v0.7.0

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

func (*Image) IsWindows added in v0.11.0

func (i *Image) IsWindows() bool

type InstanceConfig

type InstanceConfig struct {
	Address             string
	Description         string
	DiskSizeGb          int64
	DiskType            string
	Image               *Image
	MachineType         string
	Metadata            map[string]string
	Name                string
	Network             string
	OmitExternalIP      bool
	Preemptible         bool
	Region              string
	ServiceAccountEmail string
	Subnetwork          string
	Tags                []string
	Zone                string
}

type StepCheckExistingImage added in v0.7.5

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 added in v0.7.5

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

Cleanup.

func (*StepCheckExistingImage) Run added in v0.7.5

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
}

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 added in v0.11.0

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 added in v0.11.0

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

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

func (*StepCreateWindowsPassword) Run added in v0.11.0

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

type StepInstanceInfo

type StepInstanceInfo struct {
	Debug bool
	// contains filtered or unexported fields
}

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 added in v0.7.5

type StepTeardownInstance struct {
	Debug bool
}

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

func (*StepTeardownInstance) Cleanup added in v0.7.5

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

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

func (*StepTeardownInstance) Run added in v0.7.5

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

type StepWaitInstanceStartup added in v0.11.0

type StepWaitInstanceStartup int

func (*StepWaitInstanceStartup) Cleanup added in v0.11.0

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

Cleanup.

func (*StepWaitInstanceStartup) Run added in v0.11.0

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

type WindowsPasswordConfig added in v0.11.0

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