2013-12-08 17:37:36 -05:00
|
|
|
package googlecompute
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-12-13 00:38:34 -05:00
|
|
|
"errors"
|
2013-12-08 17:37:36 -05:00
|
|
|
"fmt"
|
2016-05-24 20:13:36 -04:00
|
|
|
"io/ioutil"
|
2020-03-24 03:33:55 -04:00
|
|
|
"strings"
|
2013-12-13 00:38:34 -05:00
|
|
|
"time"
|
2013-12-08 17:37:36 -05:00
|
|
|
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-12-08 17:37:36 -05:00
|
|
|
)
|
|
|
|
|
2013-12-13 00:38:34 -05:00
|
|
|
// StepCreateInstance represents a Packer build step that creates GCE instances.
|
|
|
|
type StepCreateInstance struct {
|
2014-02-21 18:10:09 -05:00
|
|
|
Debug bool
|
2013-12-08 17:37:36 -05:00
|
|
|
}
|
|
|
|
|
2019-06-11 11:31:26 -04:00
|
|
|
func (c *Config) createInstanceMetadata(sourceImage *Image, sshPublicKey string) (map[string]string, error) {
|
2014-10-06 17:18:34 -04:00
|
|
|
instanceMetadata := make(map[string]string)
|
2016-08-02 16:43:04 -04:00
|
|
|
var err error
|
2019-06-11 10:38:03 -04:00
|
|
|
var errs *packer.MultiError
|
2019-06-11 07:06:16 -04:00
|
|
|
|
2016-05-24 20:13:36 -04:00
|
|
|
// Copy metadata from config.
|
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
|
|
|
for k, v := range c.Metadata {
|
2014-10-06 17:18:34 -04:00
|
|
|
instanceMetadata[k] = v
|
|
|
|
}
|
|
|
|
|
2016-11-02 16:24:48 -04:00
|
|
|
// Merge any existing ssh keys with our public key, unless there is no
|
|
|
|
// supplied public key. This is possible if a private_key_file was
|
|
|
|
// specified.
|
|
|
|
if sshPublicKey != "" {
|
2020-03-24 03:33:55 -04:00
|
|
|
sshMetaKey := "ssh-keys"
|
|
|
|
sshPublicKey = strings.TrimSuffix(sshPublicKey, "\n")
|
|
|
|
sshKeys := fmt.Sprintf("%s:%s %s", c.Comm.SSHUsername, sshPublicKey, c.Comm.SSHUsername)
|
2016-11-02 16:24:48 -04:00
|
|
|
if confSshKeys, exists := instanceMetadata[sshMetaKey]; exists {
|
|
|
|
sshKeys = fmt.Sprintf("%s\n%s", sshKeys, confSshKeys)
|
|
|
|
}
|
|
|
|
instanceMetadata[sshMetaKey] = sshKeys
|
2014-10-06 17:18:34 -04:00
|
|
|
}
|
2016-09-29 17:13:04 -04:00
|
|
|
|
2020-06-25 21:21:10 -04:00
|
|
|
startupScript := instanceMetadata[StartupScriptKey]
|
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
|
|
|
if c.StartupScriptFile != "" {
|
2016-05-24 20:13:36 -04:00
|
|
|
var content []byte
|
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
|
|
|
content, err = ioutil.ReadFile(c.StartupScriptFile)
|
2019-06-11 07:06:16 -04:00
|
|
|
if err != nil {
|
2019-06-11 11:31:26 -04:00
|
|
|
return nil, err
|
2019-06-11 07:06:16 -04:00
|
|
|
}
|
2020-06-25 21:21:10 -04:00
|
|
|
startupScript = string(content)
|
|
|
|
}
|
|
|
|
instanceMetadata[StartupScriptKey] = startupScript
|
|
|
|
|
|
|
|
// Wrap any found startup script with our own startup script wrapper.
|
|
|
|
if startupScript != "" && c.WrapStartupScriptFile.True() {
|
|
|
|
instanceMetadata[StartupScriptKey] = StartupScriptLinux
|
|
|
|
instanceMetadata[StartupWrappedScriptKey] = startupScript
|
|
|
|
instanceMetadata[StartupScriptStatusKey] = StartupScriptStatusNotDone
|
|
|
|
}
|
|
|
|
|
|
|
|
if sourceImage.IsWindows() {
|
|
|
|
// Windows startup script support is not yet implemented so clear any script data and set status to done
|
|
|
|
instanceMetadata[StartupScriptKey] = StartupScriptWindows
|
|
|
|
instanceMetadata[StartupScriptStatusKey] = StartupScriptStatusDone
|
2016-05-24 20:13:36 -04:00
|
|
|
}
|
2019-06-06 01:43:17 -04:00
|
|
|
|
2020-07-08 06:30:25 -04:00
|
|
|
// If UseOSLogin is true, force `enable-oslogin` in metadata
|
|
|
|
// In the event that `enable-oslogin` is not enabled at project level
|
|
|
|
if c.UseOSLogin {
|
|
|
|
instanceMetadata[EnableOSLoginKey] = "TRUE"
|
|
|
|
}
|
|
|
|
|
2019-06-06 06:44:57 -04:00
|
|
|
for key, value := range c.MetadataFiles {
|
2019-06-06 01:43:17 -04:00
|
|
|
var content []byte
|
2019-06-06 06:44:57 -04:00
|
|
|
content, err = ioutil.ReadFile(value)
|
2019-06-11 07:06:16 -04:00
|
|
|
if err != nil {
|
2019-06-11 10:38:03 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, err)
|
2019-06-11 07:06:16 -04:00
|
|
|
}
|
2019-06-06 06:44:57 -04:00
|
|
|
instanceMetadata[key] = string(content)
|
2019-06-06 01:43:17 -04:00
|
|
|
}
|
|
|
|
|
2019-06-11 11:31:26 -04:00
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
|
|
|
return instanceMetadata, errs
|
|
|
|
}
|
|
|
|
return instanceMetadata, nil
|
2014-10-06 17:18:34 -04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
func getImage(c *Config, d Driver) (*Image, error) {
|
2016-11-13 10:53:45 -05:00
|
|
|
name := c.SourceImageFamily
|
|
|
|
fromFamily := true
|
|
|
|
if c.SourceImage != "" {
|
|
|
|
name = c.SourceImage
|
|
|
|
fromFamily = false
|
|
|
|
}
|
2020-02-06 04:27:01 -05:00
|
|
|
if len(c.SourceImageProjectId) == 0 {
|
2016-11-13 10:53:45 -05:00
|
|
|
return d.GetImage(name, fromFamily)
|
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
|
|
|
} else {
|
2020-02-06 04:27:01 -05:00
|
|
|
return d.GetImageFromProjects(c.SourceImageProjectId, name, fromFamily)
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-08 17:37:36 -05:00
|
|
|
// Run executes the Packer build step that creates a GCE instance.
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepCreateInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
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
|
|
|
c := state.Get("config").(*Config)
|
|
|
|
d := state.Get("driver").(Driver)
|
2018-08-23 11:27:31 -04:00
|
|
|
|
2013-12-13 00:38:34 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
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
|
|
|
sourceImage, err := getImage(c, d)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error getting source image for instance creation: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2020-03-30 09:05:16 -04:00
|
|
|
if c.EnableSecureBoot && !sourceImage.IsSecureBootCompatible() {
|
|
|
|
err := fmt.Errorf("Image: %s is not secure boot compatible. Please set 'enable_secure_boot' to false or choose another source image.", sourceImage.Name)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2016-11-26 09:06:43 -05:00
|
|
|
ui.Say(fmt.Sprintf("Using image: %s", sourceImage.Name))
|
|
|
|
|
2016-09-25 22:46:52 -04:00
|
|
|
if sourceImage.IsWindows() && c.Comm.Type == "winrm" && c.Comm.WinRMPassword == "" {
|
|
|
|
state.Put("create_windows_password", true)
|
|
|
|
}
|
|
|
|
|
2013-12-08 17:37:36 -05:00
|
|
|
ui.Say("Creating instance...")
|
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
|
|
|
name := c.InstanceName
|
2013-12-13 00:38:34 -05:00
|
|
|
|
2016-05-24 20:13:36 -04:00
|
|
|
var errCh <-chan error
|
|
|
|
var metadata map[string]string
|
2019-06-11 10:38:03 -04:00
|
|
|
metadata, errs := c.createInstanceMetadata(sourceImage, string(c.Comm.SSHPublicKey))
|
2019-06-11 11:31:26 -04:00
|
|
|
if errs != nil {
|
2019-06-11 10:38:03 -04:00
|
|
|
state.Put("error", errs.Error())
|
|
|
|
ui.Error(errs.Error())
|
2019-06-11 11:31:26 -04:00
|
|
|
return multistep.ActionHalt
|
2019-06-11 10:38:03 -04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
errCh, err = d.RunInstance(&InstanceConfig{
|
2018-03-07 18:35:01 -05:00
|
|
|
AcceleratorType: c.AcceleratorType,
|
|
|
|
AcceleratorCount: c.AcceleratorCount,
|
|
|
|
Address: c.Address,
|
|
|
|
Description: "New instance created by Packer",
|
|
|
|
DisableDefaultServiceAccount: c.DisableDefaultServiceAccount,
|
|
|
|
DiskSizeGb: c.DiskSizeGb,
|
|
|
|
DiskType: c.DiskType,
|
2020-03-30 09:05:16 -04:00
|
|
|
EnableSecureBoot: c.EnableSecureBoot,
|
|
|
|
EnableVtpm: c.EnableVtpm,
|
|
|
|
EnableIntegrityMonitoring: c.EnableIntegrityMonitoring,
|
2018-03-07 18:35:01 -05:00
|
|
|
Image: sourceImage,
|
|
|
|
Labels: c.Labels,
|
|
|
|
MachineType: c.MachineType,
|
|
|
|
Metadata: metadata,
|
2018-08-21 04:09:30 -04:00
|
|
|
MinCpuPlatform: c.MinCpuPlatform,
|
2018-03-07 18:35:01 -05:00
|
|
|
Name: name,
|
|
|
|
Network: c.Network,
|
|
|
|
NetworkProjectId: c.NetworkProjectId,
|
|
|
|
OmitExternalIP: c.OmitExternalIP,
|
|
|
|
OnHostMaintenance: c.OnHostMaintenance,
|
|
|
|
Preemptible: c.Preemptible,
|
|
|
|
Region: c.Region,
|
|
|
|
ServiceAccountEmail: c.ServiceAccountEmail,
|
|
|
|
Scopes: c.Scopes,
|
|
|
|
Subnetwork: c.Subnetwork,
|
|
|
|
Tags: c.Tags,
|
|
|
|
Zone: c.Zone,
|
2013-12-13 00:38:34 -05:00
|
|
|
})
|
2013-12-08 17:37:36 -05:00
|
|
|
|
2013-12-13 00:38:34 -05:00
|
|
|
if err == nil {
|
2013-12-13 01:23:00 -05:00
|
|
|
ui.Message("Waiting for creation operation to complete...")
|
2013-12-13 00:38:34 -05:00
|
|
|
select {
|
|
|
|
case err = <-errCh:
|
2019-10-31 10:49:34 -04:00
|
|
|
case <-time.After(c.StateTimeout):
|
2013-12-13 00:38:34 -05:00
|
|
|
err = errors.New("time out while waiting for instance to create")
|
|
|
|
}
|
2013-12-08 17:37:36 -05:00
|
|
|
}
|
2013-12-13 00:38:34 -05:00
|
|
|
|
2013-12-08 17:37:36 -05:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error creating instance: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2013-12-13 00:38:34 -05:00
|
|
|
|
|
|
|
ui.Message("Instance has been created!")
|
|
|
|
|
2014-01-31 11:55:58 -05:00
|
|
|
if s.Debug {
|
|
|
|
if name != "" {
|
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
|
|
|
ui.Message(fmt.Sprintf("Instance: %s started in %s", name, c.Zone))
|
2014-01-31 11:55:58 -05:00
|
|
|
}
|
|
|
|
}
|
2014-01-31 07:33:23 -05:00
|
|
|
|
2013-12-13 00:38:34 -05:00
|
|
|
// Things succeeded, store the name so we can remove it later
|
2013-12-08 17:37:36 -05:00
|
|
|
state.Put("instance_name", name)
|
2019-12-13 14:57:01 -05:00
|
|
|
// instance_id is the generic term used so that users can have access to the
|
|
|
|
// instance id inside of the provisioners, used in step_provision.
|
|
|
|
state.Put("instance_id", name)
|
2013-12-13 00:38:34 -05:00
|
|
|
|
2013-12-08 17:37:36 -05:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup destroys the GCE instance created during the image creation process.
|
2013-12-13 00:38:34 -05:00
|
|
|
func (s *StepCreateInstance) Cleanup(state multistep.StateBag) {
|
2014-11-24 11:36:14 -05:00
|
|
|
nameRaw, ok := state.GetOk("instance_name")
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
name := nameRaw.(string)
|
|
|
|
if name == "" {
|
2013-12-08 17:37:36 -05:00
|
|
|
return
|
|
|
|
}
|
2013-12-13 01:34:47 -05:00
|
|
|
|
|
|
|
config := state.Get("config").(*Config)
|
|
|
|
driver := state.Get("driver").(Driver)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
ui.Say("Deleting instance...")
|
2014-11-24 11:36:14 -05:00
|
|
|
errCh, err := driver.DeleteInstance(config.Zone, name)
|
2013-12-13 01:34:47 -05:00
|
|
|
if err == nil {
|
|
|
|
select {
|
|
|
|
case err = <-errCh:
|
2019-10-31 10:49:34 -04:00
|
|
|
case <-time.After(config.StateTimeout):
|
2013-12-13 01:34:47 -05:00
|
|
|
err = errors.New("time out while waiting for instance to delete")
|
2013-12-08 17:37:36 -05:00
|
|
|
}
|
2013-12-13 01:34:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf(
|
|
|
|
"Error deleting instance. Please delete it manually.\n\n"+
|
|
|
|
"Name: %s\n"+
|
2014-11-24 11:36:14 -05:00
|
|
|
"Error: %s", name, err))
|
2013-12-13 01:34:47 -05:00
|
|
|
}
|
|
|
|
|
2015-04-21 10:40:55 -04:00
|
|
|
ui.Message("Instance has been deleted!")
|
2014-11-24 11:36:14 -05:00
|
|
|
state.Put("instance_name", "")
|
2015-04-21 10:40:55 -04:00
|
|
|
|
|
|
|
// Deleting the instance does not remove the boot disk. This cleanup removes
|
|
|
|
// the disk.
|
|
|
|
ui.Say("Deleting disk...")
|
|
|
|
errCh, err = driver.DeleteDisk(config.Zone, config.DiskName)
|
|
|
|
if err == nil {
|
|
|
|
select {
|
|
|
|
case err = <-errCh:
|
2019-10-31 10:49:34 -04:00
|
|
|
case <-time.After(config.StateTimeout):
|
2015-04-21 10:40:55 -04:00
|
|
|
err = errors.New("time out while waiting for disk to delete")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf(
|
|
|
|
"Error deleting disk. Please delete it manually.\n\n"+
|
|
|
|
"Name: %s\n"+
|
|
|
|
"Error: %s", config.InstanceName, err))
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Message("Disk has been deleted!")
|
|
|
|
|
2013-12-08 17:37:36 -05:00
|
|
|
return
|
|
|
|
}
|