2013-12-08 17:37:36 -05:00
|
|
|
package googlecompute
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Artifact represents a GCE image as the result of a Packer build.
|
|
|
|
type Artifact struct {
|
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
|
|
|
image *Image
|
2016-07-07 17:50:46 -04:00
|
|
|
driver Driver
|
|
|
|
config *Config
|
2013-12-08 17:37:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// BuilderId returns the builder Id.
|
|
|
|
func (*Artifact) BuilderId() string {
|
|
|
|
return BuilderId
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy destroys the GCE image represented by the artifact.
|
|
|
|
func (a *Artifact) Destroy() error {
|
2016-05-24 20:13:36 -04:00
|
|
|
log.Printf("Destroying image: %s", a.image.Name)
|
|
|
|
errCh := a.driver.DeleteImage(a.image.Name)
|
2013-12-13 22:07:10 -05:00
|
|
|
return <-errCh
|
2013-12-08 17:37:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Files returns the files represented by the artifact.
|
|
|
|
func (*Artifact) Files() []string {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Id returns the GCE image name.
|
|
|
|
func (a *Artifact) Id() string {
|
2016-05-24 20:13:36 -04:00
|
|
|
return a.image.Name
|
2013-12-08 17:37:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// String returns the string representation of the artifact.
|
|
|
|
func (a *Artifact) String() string {
|
2016-05-24 20:13:36 -04:00
|
|
|
return fmt.Sprintf("A disk image was created: %v", a.image.Name)
|
2013-12-08 17:37:36 -05:00
|
|
|
}
|
2014-01-19 13:32:44 -05:00
|
|
|
|
|
|
|
func (a *Artifact) State(name string) interface{} {
|
2016-07-07 17:50:46 -04:00
|
|
|
switch name {
|
2016-09-29 17:13:04 -04:00
|
|
|
case "ImageName":
|
|
|
|
return a.image.Name
|
|
|
|
case "ImageSizeGb":
|
|
|
|
return a.image.SizeGb
|
|
|
|
case "AccountFilePath":
|
|
|
|
return a.config.AccountFile
|
|
|
|
case "ProjectId":
|
|
|
|
return a.config.ProjectId
|
|
|
|
case "BuildZone":
|
|
|
|
return a.config.Zone
|
2016-07-07 17:50:46 -04:00
|
|
|
}
|
2014-01-19 13:32:44 -05:00
|
|
|
return nil
|
|
|
|
}
|