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"
|
2013-12-08 17:37:36 -05:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/common"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
2014-01-31 11:55:58 -05:00
|
|
|
"log"
|
|
|
|
"time"
|
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) {
|
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(
|
2014-09-05 12:47:20 -04:00
|
|
|
ui, b.config.ProjectId, &b.config.account, &b.config.clientSecrets)
|
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{
|
|
|
|
&StepCreateSSHKey{
|
|
|
|
Debug: b.config.PackerDebug,
|
|
|
|
DebugKeyPath: fmt.Sprintf("gce_%s.pem", b.config.PackerBuildName),
|
|
|
|
},
|
|
|
|
&StepCreateInstance{
|
|
|
|
Debug: b.config.PackerDebug,
|
|
|
|
},
|
|
|
|
&StepInstanceInfo{
|
|
|
|
Debug: b.config.PackerDebug,
|
|
|
|
},
|
|
|
|
&common.StepConnectSSH{
|
|
|
|
SSHAddress: sshAddress,
|
|
|
|
SSHConfig: sshConfig,
|
|
|
|
SSHWaitTimeout: 5 * time.Minute,
|
|
|
|
},
|
|
|
|
new(common.StepProvision),
|
2014-07-25 17:21:12 -04:00
|
|
|
new(StepUpdateGcloud),
|
2014-01-31 11:55:58 -05:00
|
|
|
new(StepCreateImage),
|
|
|
|
new(StepUploadImage),
|
|
|
|
new(StepRegisterImage),
|
|
|
|
}
|
2013-12-12 23:00:58 -05:00
|
|
|
|
2013-12-08 17:37:36 -05:00
|
|
|
// Run the steps.
|
|
|
|
if b.config.PackerDebug {
|
|
|
|
b.runner = &multistep.DebugRunner{
|
|
|
|
Steps: steps,
|
|
|
|
PauseFn: common.MultistepDebugFn(ui),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
b.runner = &multistep.BasicRunner{Steps: steps}
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
if _, ok := state.GetOk("image_name"); !ok {
|
|
|
|
log.Println("Failed to find image_name in state. Bug?")
|
|
|
|
return nil, nil
|
|
|
|
}
|
2013-12-12 23:00:58 -05:00
|
|
|
|
2013-12-08 17:37:36 -05:00
|
|
|
artifact := &Artifact{
|
|
|
|
imageName: state.Get("image_name").(string),
|
2013-12-13 00:41:14 -05:00
|
|
|
driver: driver,
|
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()
|
|
|
|
}
|
|
|
|
}
|