2014-12-09 11:42:33 -05:00
|
|
|
package googlecompute
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2014-12-09 11:42:33 -05:00
|
|
|
"fmt"
|
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2014-12-09 11:42:33 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// StepCheckExistingImage represents a Packer build step that checks if the
|
|
|
|
// target image already exists, and aborts immediately if so.
|
|
|
|
type StepCheckExistingImage int
|
|
|
|
|
|
|
|
// Run executes the Packer build step that checks if the image already exists.
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepCheckExistingImage) 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)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2014-12-09 11:42:33 -05:00
|
|
|
|
2016-09-24 15:12:25 -04:00
|
|
|
ui.Say("Checking image does not exist...")
|
2016-09-23 14:38:28 -04:00
|
|
|
c.imageAlreadyExists = d.ImageExists(c.ImageName)
|
|
|
|
if !c.PackerForce && c.imageAlreadyExists {
|
|
|
|
err := fmt.Errorf("Image %s already exists.\n"+
|
|
|
|
"Use the force flag to delete it prior to building.", c.ImageName)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
2014-12-09 11:42:33 -05:00
|
|
|
}
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup.
|
|
|
|
func (s *StepCheckExistingImage) Cleanup(state multistep.StateBag) {}
|