2013-12-08 17:37:36 -05:00
|
|
|
// The googlecompute package contains a packer.Builder implementation that
|
|
|
|
// builds images for Google Compute Engine.
|
|
|
|
package googlecompute
|
|
|
|
|
|
|
|
import (
|
2014-01-31 11:55:58 -05:00
|
|
|
"fmt"
|
2015-06-09 00:13:25 -04:00
|
|
|
"log"
|
|
|
|
|
2013-12-08 17:37:36 -05:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/common"
|
2015-06-13 18:30:16 -04:00
|
|
|
"github.com/mitchellh/packer/helper/communicator"
|
2013-12-08 17:37:36 -05:00
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
// The unique ID for this builder.
|
2013-12-12 18:11:03 -05:00
|
|
|
const BuilderId = "packer.googlecompute"
|
2013-12-08 17:37:36 -05:00
|
|
|
|
|
|
|
// Builder represents a Packer Builder.
|
|
|
|
type Builder struct {
|
2013-12-12 19:41:44 -05:00
|
|
|
config *Config
|
2013-12-08 17:37:36 -05:00
|
|
|
runner multistep.Runner
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare processes the build configuration parameters.
|
|
|
|
func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
|
2013-12-12 19:41:44 -05:00
|
|
|
c, warnings, errs := NewConfig(raws...)
|
|
|
|
if errs != nil {
|
|
|
|
return warnings, errs
|
2013-12-08 17:37:36 -05:00
|
|
|
}
|
2013-12-12 19:41:44 -05:00
|
|
|
b.config = c
|
2013-12-08 17:37:36 -05:00
|
|
|
|
2013-12-12 19:41:44 -05:00
|
|
|
return warnings, nil
|
2013-12-08 17:37:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run executes a googlecompute Packer build and returns a packer.Artifact
|
|
|
|
// representing a GCE machine image.
|
|
|
|
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
|
2013-12-13 00:41:14 -05:00
|
|
|
driver, err := NewDriverGCE(
|
2016-07-07 17:50:46 -04:00
|
|
|
ui, b.config.ProjectId, &b.config.Account)
|
2013-12-08 17:37:36 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-12-12 23:00:58 -05:00
|
|
|
|
2013-12-08 17:37:36 -05:00
|
|
|
// Set up the state.
|
|
|
|
state := new(multistep.BasicStateBag)
|
|
|
|
state.Put("config", b.config)
|
2013-12-13 00:41:14 -05:00
|
|
|
state.Put("driver", driver)
|
2013-12-08 17:37:36 -05:00
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
2013-12-12 23:00:58 -05:00
|
|
|
|
2013-12-08 17:37:36 -05:00
|
|
|
// Build the steps.
|
2014-01-31 11:55:58 -05:00
|
|
|
steps := []multistep.Step{
|
2014-12-09 11:42:33 -05:00
|
|
|
new(StepCheckExistingImage),
|
2014-01-31 11:55:58 -05:00
|
|
|
&StepCreateSSHKey{
|
|
|
|
Debug: b.config.PackerDebug,
|
|
|
|
DebugKeyPath: fmt.Sprintf("gce_%s.pem", b.config.PackerBuildName),
|
|
|
|
},
|
|
|
|
&StepCreateInstance{
|
|
|
|
Debug: b.config.PackerDebug,
|
|
|
|
},
|
|
|
|
&StepInstanceInfo{
|
|
|
|
Debug: b.config.PackerDebug,
|
|
|
|
},
|
2015-06-13 18:30:16 -04:00
|
|
|
&communicator.StepConnect{
|
2015-06-13 19:23:33 -04:00
|
|
|
Config: &b.config.Comm,
|
|
|
|
Host: commHost,
|
|
|
|
SSHConfig: sshConfig,
|
2014-01-31 11:55:58 -05:00
|
|
|
},
|
|
|
|
new(common.StepProvision),
|
2016-05-24 20:13:36 -04:00
|
|
|
new(StepWaitInstanceStartup),
|
2014-11-24 11:36:14 -05:00
|
|
|
new(StepTeardownInstance),
|
2014-01-31 11:55:58 -05:00
|
|
|
new(StepCreateImage),
|
|
|
|
}
|
2013-12-12 23:00:58 -05:00
|
|
|
|
2013-12-08 17:37:36 -05:00
|
|
|
// Run the steps.
|
2016-09-13 20:04:18 -04:00
|
|
|
b.runner = common.NewRunner(steps, b.config.PackerConfig, ui)
|
2013-12-08 17:37:36 -05:00
|
|
|
b.runner.Run(state)
|
2013-12-12 23:00:58 -05:00
|
|
|
|
2013-12-08 17:37:36 -05:00
|
|
|
// Report any errors.
|
|
|
|
if rawErr, ok := state.GetOk("error"); ok {
|
|
|
|
return nil, rawErr.(error)
|
|
|
|
}
|
2016-05-24 20:13:36 -04:00
|
|
|
if _, ok := state.GetOk("image"); !ok {
|
|
|
|
log.Println("Failed to find image in state. Bug?")
|
2013-12-08 17:37:36 -05:00
|
|
|
return nil, nil
|
|
|
|
}
|
2013-12-12 23:00:58 -05:00
|
|
|
|
2013-12-08 17:37:36 -05:00
|
|
|
artifact := &Artifact{
|
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: state.Get("image").(*Image),
|
2016-05-24 20:13:36 -04:00
|
|
|
driver: driver,
|
2016-07-07 17:50:46 -04:00
|
|
|
config: b.config,
|
2013-12-08 17:37:36 -05:00
|
|
|
}
|
|
|
|
return artifact, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel.
|
2013-12-12 23:00:58 -05:00
|
|
|
func (b *Builder) Cancel() {
|
|
|
|
if b.runner != nil {
|
|
|
|
log.Println("Cancelling the step runner...")
|
|
|
|
b.runner.Cancel()
|
|
|
|
}
|
|
|
|
}
|