2013-12-13 00:38:34 -05:00
|
|
|
package googlecompute
|
|
|
|
|
2016-09-25 22:46:52 -04:00
|
|
|
import (
|
|
|
|
"crypto/rsa"
|
|
|
|
"time"
|
2019-05-05 11:12:30 -04:00
|
|
|
|
|
|
|
compute "google.golang.org/api/compute/v1"
|
2016-09-25 22:46:52 -04:00
|
|
|
)
|
|
|
|
|
2013-12-13 00:38:34 -05:00
|
|
|
// 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.
|
|
|
|
type Driver interface {
|
2014-11-24 11:36:14 -05:00
|
|
|
// CreateImage creates an image from the given disk in Google Compute
|
|
|
|
// Engine.
|
2019-05-05 11:12:30 -04:00
|
|
|
CreateImage(name, description, family, zone, disk string, image_labels map[string]string, image_licenses []string, image_encryption_key *compute.CustomerEncryptionKey) (<-chan *Image, <-chan error)
|
2013-12-13 22:03:10 -05:00
|
|
|
|
2013-12-13 22:07:10 -05:00
|
|
|
// DeleteImage deletes the image with the given name.
|
|
|
|
DeleteImage(name string) <-chan error
|
|
|
|
|
2014-11-24 11:36:14 -05:00
|
|
|
// DeleteInstance deletes the given instance, keeping the boot disk.
|
2013-12-13 01:34:47 -05:00
|
|
|
DeleteInstance(zone, name string) (<-chan error, error)
|
|
|
|
|
2014-11-24 11:36:14 -05:00
|
|
|
// DeleteDisk deletes the disk with the given name.
|
|
|
|
DeleteDisk(zone, name string) (<-chan error, error)
|
|
|
|
|
2016-11-13 10:53:45 -05:00
|
|
|
// 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)
|
Some googlecompute fixes and cleanup. Addresses https://github.com/mitchellh/packer/issues/3829. Changes:
- startup scripts don't run for Windows since it is isn't implemented yet.
- startup scripts use instance metadata instead of serial port output to flag when they are done.
- added licenses to Image data type (to check if an Image is a Windows Image).
- added GetImage and GetImageFromProject to googlecompute Drivers.
- changed some of the builder/googlecompute tests to use github.com/stretchr/testify/assert.
Tests:
- (in the Packer directory) `go test .`, `go test ./builder/googlecompute`, and `go test ./post-processor/googlecompute-export`
- manual run of `packer build packer_template.json` with the following files
--packer_template.json--
{
"builders": [
{
"type": "googlecompute",
"account_file": "creds.json",
"project_id": "google.com:packer-test",
"source_image": "debian-8-jessie-v20160629",
"zone": "us-central1-a",
"startup_script_file": "startup_script.sh",
"metadata": {
"startup-script": "#!/bin/sh\necho \"This should be overwritten.\"",
"startup-script-log-dest": "gs://packer-test.google.com.a.appspot.com/startup-script.log"
},
"image_name": "test-packer-modifications",
"ssh_username": "foo"
}
],
"post-processors": [
{
"type": "googlecompute-export",
"paths": [
"gs://packer-test.google.com.a.appspot.com/foo.tar.gz",
"gs://packer-test.google.com.a.appspot.com/bar.tar.gz"
],
"keep_input_artifact": true
}
]
}
--startup_script.sh--
\#!/bin/sh
echo "Hi, my name is Scott. I'm waiting 60 seconds!" >> /scott
sleep 60
echo "I'm done waiting!" >> /scott
2016-09-07 22:00:30 -04:00
|
|
|
|
|
|
|
// GetInstanceMetadata gets a metadata variable for the instance, name.
|
|
|
|
GetInstanceMetadata(zone, name, key string) (string, error)
|
|
|
|
|
2015-05-29 17:50:11 -04:00
|
|
|
// GetInternalIP gets the GCE-internal IP address for the instance.
|
|
|
|
GetInternalIP(zone, name string) (string, error)
|
|
|
|
|
2016-05-24 20:13:36 -04:00
|
|
|
// GetNatIP gets the NAT IP address for the instance.
|
|
|
|
GetNatIP(zone, name string) (string, error)
|
2016-09-29 17:13:04 -04:00
|
|
|
|
2016-05-24 20:13:36 -04:00
|
|
|
// GetSerialPortOutput gets the Serial Port contents for the instance.
|
|
|
|
GetSerialPortOutput(zone, name string) (string, error)
|
|
|
|
|
Some googlecompute fixes and cleanup. Addresses https://github.com/mitchellh/packer/issues/3829. Changes:
- startup scripts don't run for Windows since it is isn't implemented yet.
- startup scripts use instance metadata instead of serial port output to flag when they are done.
- added licenses to Image data type (to check if an Image is a Windows Image).
- added GetImage and GetImageFromProject to googlecompute Drivers.
- changed some of the builder/googlecompute tests to use github.com/stretchr/testify/assert.
Tests:
- (in the Packer directory) `go test .`, `go test ./builder/googlecompute`, and `go test ./post-processor/googlecompute-export`
- manual run of `packer build packer_template.json` with the following files
--packer_template.json--
{
"builders": [
{
"type": "googlecompute",
"account_file": "creds.json",
"project_id": "google.com:packer-test",
"source_image": "debian-8-jessie-v20160629",
"zone": "us-central1-a",
"startup_script_file": "startup_script.sh",
"metadata": {
"startup-script": "#!/bin/sh\necho \"This should be overwritten.\"",
"startup-script-log-dest": "gs://packer-test.google.com.a.appspot.com/startup-script.log"
},
"image_name": "test-packer-modifications",
"ssh_username": "foo"
}
],
"post-processors": [
{
"type": "googlecompute-export",
"paths": [
"gs://packer-test.google.com.a.appspot.com/foo.tar.gz",
"gs://packer-test.google.com.a.appspot.com/bar.tar.gz"
],
"keep_input_artifact": true
}
]
}
--startup_script.sh--
\#!/bin/sh
echo "Hi, my name is Scott. I'm waiting 60 seconds!" >> /scott
sleep 60
echo "I'm done waiting!" >> /scott
2016-09-07 22:00:30 -04:00
|
|
|
// ImageExists returns true if the specified image exists. If an error
|
|
|
|
// occurs calling the API, this method returns false.
|
|
|
|
ImageExists(name string) bool
|
|
|
|
|
2013-12-13 00:38:34 -05:00
|
|
|
// RunInstance takes the given config and launches an instance.
|
|
|
|
RunInstance(*InstanceConfig) (<-chan error, error)
|
2013-12-13 16:01:28 -05:00
|
|
|
|
|
|
|
// WaitForInstance waits for an instance to reach the given state.
|
|
|
|
WaitForInstance(state, zone, name string) <-chan error
|
2016-09-25 22:46:52 -04:00
|
|
|
|
|
|
|
// CreateOrResetWindowsPassword creates or resets the password for a user on an Windows instance.
|
|
|
|
CreateOrResetWindowsPassword(zone, name string, config *WindowsPasswordConfig) (<-chan error, error)
|
2013-12-13 00:38:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type InstanceConfig struct {
|
2018-03-07 18:35:01 -05:00
|
|
|
AcceleratorType string
|
|
|
|
AcceleratorCount int64
|
|
|
|
Address string
|
|
|
|
Description string
|
|
|
|
DisableDefaultServiceAccount bool
|
|
|
|
DiskSizeGb int64
|
|
|
|
DiskType string
|
|
|
|
Image *Image
|
|
|
|
Labels map[string]string
|
|
|
|
MachineType string
|
|
|
|
Metadata map[string]string
|
2018-08-21 04:09:30 -04:00
|
|
|
MinCpuPlatform string
|
2018-03-07 18:35:01 -05:00
|
|
|
Name string
|
|
|
|
Network string
|
|
|
|
NetworkProjectId string
|
|
|
|
OmitExternalIP bool
|
|
|
|
OnHostMaintenance string
|
|
|
|
Preemptible bool
|
|
|
|
Region string
|
|
|
|
ServiceAccountEmail string
|
|
|
|
Scopes []string
|
|
|
|
Subnetwork string
|
|
|
|
Tags []string
|
|
|
|
Zone string
|
2013-12-13 00:38:34 -05:00
|
|
|
}
|
2016-09-25 22:46:52 -04:00
|
|
|
|
2018-03-13 23:23:56 -04:00
|
|
|
// WindowsPasswordConfig is the data structure that GCE needs to encrypt the created
|
2016-09-25 22:46:52 -04:00
|
|
|
// windows password.
|
|
|
|
type WindowsPasswordConfig struct {
|
|
|
|
key *rsa.PrivateKey
|
|
|
|
password string
|
|
|
|
UserName string `json:"userName"`
|
|
|
|
Modulus string `json:"modulus"`
|
|
|
|
Exponent string `json:"exponent"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
ExpireOn time.Time `json:"expireOn"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type windowsPasswordResponse struct {
|
|
|
|
UserName string `json:"userName"`
|
|
|
|
PasswordFound bool `json:"passwordFound"`
|
|
|
|
EncryptedPassword string `json:"encryptedPassword"`
|
|
|
|
Modulus string `json:"modulus"`
|
|
|
|
Exponent string `json:"exponent"`
|
|
|
|
ErrorMessage string `json:"errorMessage"`
|
|
|
|
}
|