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 (
|
2019-03-22 09:53:28 -04:00
|
|
|
"context"
|
2014-01-31 11:55:58 -05:00
|
|
|
"fmt"
|
2015-06-09 00:13:25 -04:00
|
|
|
"log"
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
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 {
|
2019-12-17 05:25:56 -05:00
|
|
|
config Config
|
2013-12-08 17:37:36 -05:00
|
|
|
runner multistep.Runner
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (b *Builder) ConfigSpec() hcldec.ObjectSpec { return b.config.FlatMapstructure().HCL2Spec() }
|
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
|
2019-12-17 05:25:56 -05:00
|
|
|
warnings, errs := b.config.Prepare(raws...)
|
2013-12-12 19:41:44 -05:00
|
|
|
if errs != nil {
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, warnings, errs
|
2013-12-08 17:37:36 -05:00
|
|
|
}
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, 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.
|
2019-03-22 09:53:28 -04:00
|
|
|
func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
|
2013-12-13 00:41:14 -05:00
|
|
|
driver, err := NewDriverGCE(
|
2019-10-14 10:02:19 -04:00
|
|
|
ui, b.config.ProjectId, b.config.account, b.config.VaultGCPOauthEngine)
|
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)
|
2019-12-17 05:25:56 -05:00
|
|
|
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,
|
2020-01-30 18:22:22 -05:00
|
|
|
Host: communicator.CommHost(b.config.Comm.Host(), "instance_ip"),
|
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)
|
2019-03-22 09:53:28 -04:00
|
|
|
b.runner.Run(ctx, 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{
|
2020-01-30 05:27:58 -05:00
|
|
|
image: state.Get("image").(*Image),
|
|
|
|
driver: driver,
|
|
|
|
config: &b.config,
|
|
|
|
StateData: map[string]interface{}{"generated_data": state.Get("generated_data")},
|
2013-12-08 17:37:36 -05:00
|
|
|
}
|
|
|
|
return artifact, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel.
|