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"
|
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/common"
|
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
// 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) {
|
2017-10-17 00:48:15 -04:00
|
|
|
c, warnings, errs := NewConfig(raws...)
|
2013-12-12 19:41:44 -05:00
|
|
|
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
|
|
|
|
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.
|
Use the hashicorp/go-getter to download files
* removed packer.Cache and references since packer.Cache is never used except in the download step. The download step now uses the new func packer.CachePath(targetPath) for this, the behavior is the same.
* removed download code from packer that was reimplemented into the go-getter library: progress bar, http download restart, checksuming from file, skip already downloaded files, symlinking, make a download cancellable by context.
* on windows if packer is running without symlinking rights and we are getting a local file, the file will be copied instead to avoid errors.
* added unit tests for step_download that are now CI tested on windows, mac & linux.
* files are now downloaded under cache dir `sha1(filename + "?checksum=" + checksum) + file_extension`
* since the output dir is based on the source url and the checksum, when the checksum fails, the file is auto deleted.
* a download file is protected and locked by a file lock,
* updated docs
* updated go modules and vendors
2019-03-13 07:11:58 -04:00
|
|
|
func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (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{
|
2018-08-28 11:47:38 -04:00
|
|
|
Debug: b.config.PackerDebug,
|
|
|
|
DebugKeyPath: fmt.Sprintf("gce_%s.pem", b.config.PackerBuildName),
|
2014-01-31 11:55:58 -05:00
|
|
|
},
|
|
|
|
&StepCreateInstance{
|
|
|
|
Debug: b.config.PackerDebug,
|
|
|
|
},
|
2016-09-25 22:46:52 -04:00
|
|
|
&StepCreateWindowsPassword{
|
|
|
|
Debug: b.config.PackerDebug,
|
|
|
|
DebugKeyPath: fmt.Sprintf("gce_windows_%s.pem", b.config.PackerBuildName),
|
|
|
|
},
|
2014-01-31 11:55:58 -05:00
|
|
|
&StepInstanceInfo{
|
|
|
|
Debug: b.config.PackerDebug,
|
|
|
|
},
|
2015-06-13 18:30:16 -04:00
|
|
|
&communicator.StepConnect{
|
2016-09-25 22:46:52 -04:00
|
|
|
Config: &b.config.Comm,
|
|
|
|
Host: commHost,
|
2018-08-22 11:02:23 -04:00
|
|
|
SSHConfig: b.config.Comm.SSHConfigFunc(),
|
2016-09-25 22:46:52 -04:00
|
|
|
WinRMConfig: winrmConfig,
|
2014-01-31 11:55:58 -05:00
|
|
|
},
|
|
|
|
new(common.StepProvision),
|
2018-09-14 14:03:23 -04:00
|
|
|
&common.StepCleanupTempKeys{
|
|
|
|
Comm: &b.config.Comm,
|
|
|
|
},
|
2014-01-31 11:55:58 -05:00
|
|
|
}
|
2016-11-02 17:35:06 -04:00
|
|
|
if _, exists := b.config.Metadata[StartupScriptKey]; exists || b.config.StartupScriptFile != "" {
|
|
|
|
steps = append(steps, new(StepWaitStartupScript))
|
|
|
|
}
|
|
|
|
steps = append(steps, new(StepTeardownInstance), 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()
|
|
|
|
}
|
|
|
|
}
|