From e91ac3ef1b4267d336f0734e0f6489f7f5a5d7ae Mon Sep 17 00:00:00 2001 From: Andrei Tonkikh Date: Wed, 24 Jan 2018 14:04:39 +0300 Subject: [PATCH] Move clone builder to a separate module --- build.sh | 6 ++-- builder.go => clone/builder.go | 32 ++++++++++--------- .../builder_acc_test.go | 9 +++--- builder_test.go => clone/builder_test.go | 2 +- config.go => clone/config.go | 21 ++++++------ config_test.go => clone/config_test.go | 2 +- main.go => clone/main.go | 2 +- step_clone.go => clone/step_clone.go | 2 +- step_hardware.go => clone/step_hardware.go | 2 +- clone/test.sh | 4 +++ artifact.go => common/artifact.go | 2 +- ssh.go => common/ssh.go | 19 +++++------ step_connect.go => common/step_connect.go | 14 ++++---- step_run.go => common/step_run.go | 2 +- step_shutdown.go => common/step_shutdown.go | 14 ++++---- step_snapshot.go => common/step_snapshot.go | 6 ++-- step_template.go => common/step_template.go | 2 +- test.sh | 4 +-- 18 files changed, 77 insertions(+), 68 deletions(-) rename builder.go => clone/builder.go (73%) rename builder_acc_test.go => clone/builder_acc_test.go (98%) rename builder_test.go => clone/builder_test.go (94%) rename config.go => clone/config.go (61%) rename config_test.go => clone/config_test.go (99%) rename main.go => clone/main.go (93%) rename step_clone.go => clone/step_clone.go (99%) rename step_hardware.go => clone/step_hardware.go (99%) create mode 100755 clone/test.sh rename artifact.go => common/artifact.go (97%) rename ssh.go => common/ssh.go (61%) rename step_connect.go => common/step_connect.go (81%) rename step_run.go => common/step_run.go (98%) rename step_shutdown.go => common/step_shutdown.go (86%) rename step_snapshot.go => common/step_snapshot.go (91%) rename step_template.go => common/step_template.go (97%) diff --git a/build.sh b/build.sh index b64c7ec69..efcf7e2b8 100755 --- a/build.sh +++ b/build.sh @@ -8,6 +8,6 @@ export GOARCH=amd64 mkdir -p bin rm -f bin/* -GOOS=darwin go build -o bin/packer-builder-vsphere.macos -GOOS=linux go build -o bin/packer-builder-vsphere.linux -GOOS=windows go build -o bin/packer-builder-vsphere.exe +GOOS=darwin go build -o bin/packer-builder-vsphere-clone.macos ./clone +GOOS=linux go build -o bin/packer-builder-vsphere-clone.linux ./clone +GOOS=windows go build -o bin/packer-builder-vsphere-clone.exe ./clone diff --git a/builder.go b/clone/builder.go similarity index 73% rename from builder.go rename to clone/builder.go index 8d6ec8fff..53f3a1d4b 100644 --- a/builder.go +++ b/clone/builder.go @@ -1,11 +1,12 @@ -package main +package clone import ( "errors" - "github.com/hashicorp/packer/common" + packerCommon "github.com/hashicorp/packer/common" "github.com/hashicorp/packer/helper/communicator" "github.com/hashicorp/packer/packer" + "github.com/jetbrains-infra/packer-builder-vsphere/common" "github.com/jetbrains-infra/packer-builder-vsphere/driver" "github.com/mitchellh/multistep" ) @@ -28,14 +29,15 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) { state := new(multistep.BasicStateBag) state.Put("config", b.config) + state.Put("comm", &b.config.Comm) state.Put("hook", hook) state.Put("ui", ui) var steps []multistep.Step steps = append(steps, - &StepConnect{ - config: &b.config.ConnectConfig, + &common.StepConnect{ + Config: &b.config.ConnectConfig, }, &StepCloneVM{ config: &b.config.CloneConfig, @@ -47,30 +49,30 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe if b.config.Comm.Type != "none" { steps = append(steps, - &StepRun{}, + &common.StepRun{}, &communicator.StepConnect{ Config: &b.config.Comm, - Host: commHost, - SSHConfig: sshConfig, + Host: common.CommHost, + SSHConfig: common.SshConfig, }, - &common.StepProvision{}, - &StepShutdown{ - config: &b.config.ShutdownConfig, + &packerCommon.StepProvision{}, + &common.StepShutdown{ + Config: &b.config.ShutdownConfig, }, ) } steps = append(steps, - &StepCreateSnapshot{ - createSnapshot: b.config.CreateSnapshot, + &common.StepCreateSnapshot{ + CreateSnapshot: b.config.CreateSnapshot, }, - &StepConvertToTemplate{ + &common.StepConvertToTemplate{ ConvertToTemplate: b.config.ConvertToTemplate, }, ) // Run! - b.runner = common.NewRunner(steps, b.config.PackerConfig, ui) + b.runner = packerCommon.NewRunner(steps, b.config.PackerConfig, ui) b.runner.Run(state) // If there was an error, return that @@ -87,7 +89,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe return nil, errors.New("Build was halted.") } - artifact := &Artifact{ + artifact := &common.Artifact{ Name: b.config.VMName, VM: state.Get("vm").(*driver.VirtualMachine), } diff --git a/builder_acc_test.go b/clone/builder_acc_test.go similarity index 98% rename from builder_acc_test.go rename to clone/builder_acc_test.go index ea4f861f0..66e4d2a2d 100644 --- a/builder_acc_test.go +++ b/clone/builder_acc_test.go @@ -1,10 +1,11 @@ -package main +package clone import ( "encoding/json" "fmt" builderT "github.com/hashicorp/packer/helper/builder/testing" "github.com/hashicorp/packer/packer" + "github.com/jetbrains-infra/packer-builder-vsphere/common" "github.com/jetbrains-infra/packer-builder-vsphere/driver" "math/rand" "testing" @@ -111,7 +112,7 @@ func checkArtifact(t *testing.T) builderT.TestCheckFunc { } artifactRaw := artifacts[0] - _, ok := artifactRaw.(*Artifact) + _, ok := artifactRaw.(*common.Artifact) if !ok { t.Fatalf("unknown artifact: %#v", artifactRaw) } @@ -388,7 +389,7 @@ func TestBuilderAcc_sshKey(t *testing.T) { func sshKeyConfig() string { config := defaultConfig() config["ssh_password"] = "" - config["ssh_private_key_file"] = "test-key.pem" + config["ssh_private_key_file"] = "../test-key.pem" config["linked_clone"] = true // speed up return renderConfig(config) } @@ -490,7 +491,7 @@ func testConn(t *testing.T) *driver.Driver { func getVM(t *testing.T, d *driver.Driver, artifacts []packer.Artifact) *driver.VirtualMachine { artifactRaw := artifacts[0] - artifact, _ := artifactRaw.(*Artifact) + artifact, _ := artifactRaw.(*common.Artifact) vm, err := d.FindVM(artifact.Name) if err != nil { diff --git a/builder_test.go b/clone/builder_test.go similarity index 94% rename from builder_test.go rename to clone/builder_test.go index 20243b659..51ebf0a31 100644 --- a/builder_test.go +++ b/clone/builder_test.go @@ -1,4 +1,4 @@ -package main +package clone import ( "github.com/hashicorp/packer/packer" diff --git a/config.go b/clone/config.go similarity index 61% rename from config.go rename to clone/config.go index 5c25da4d1..720672eb4 100644 --- a/config.go +++ b/clone/config.go @@ -1,22 +1,23 @@ -package main +package clone import ( - "github.com/hashicorp/packer/common" + packerCommon "github.com/hashicorp/packer/common" "github.com/hashicorp/packer/helper/communicator" "github.com/hashicorp/packer/helper/config" "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/template/interpolate" + "github.com/jetbrains-infra/packer-builder-vsphere/common" ) type Config struct { - common.PackerConfig `mapstructure:",squash"` - ConnectConfig `mapstructure:",squash"` - CloneConfig `mapstructure:",squash"` - HardwareConfig `mapstructure:",squash"` - Comm communicator.Config `mapstructure:",squash"` - ShutdownConfig `mapstructure:",squash"` - CreateSnapshot bool `mapstructure:"create_snapshot"` - ConvertToTemplate bool `mapstructure:"convert_to_template"` + packerCommon.PackerConfig `mapstructure:",squash"` + common.ConnectConfig `mapstructure:",squash"` + CloneConfig `mapstructure:",squash"` + HardwareConfig `mapstructure:",squash"` + Comm communicator.Config `mapstructure:",squash"` + common.ShutdownConfig `mapstructure:",squash"` + CreateSnapshot bool `mapstructure:"create_snapshot"` + ConvertToTemplate bool `mapstructure:"convert_to_template"` ctx interpolate.Context } diff --git a/config_test.go b/clone/config_test.go similarity index 99% rename from config_test.go rename to clone/config_test.go index ecc7677e0..21cf5474c 100644 --- a/config_test.go +++ b/clone/config_test.go @@ -1,4 +1,4 @@ -package main +package clone import ( "testing" diff --git a/main.go b/clone/main.go similarity index 93% rename from main.go rename to clone/main.go index 9beb821a0..3a710c3d7 100644 --- a/main.go +++ b/clone/main.go @@ -1,4 +1,4 @@ -package main +package clone import "github.com/hashicorp/packer/packer/plugin" diff --git a/step_clone.go b/clone/step_clone.go similarity index 99% rename from step_clone.go rename to clone/step_clone.go index 0c4543ff6..ca166ba36 100644 --- a/step_clone.go +++ b/clone/step_clone.go @@ -1,4 +1,4 @@ -package main +package clone import ( "github.com/mitchellh/multistep" diff --git a/step_hardware.go b/clone/step_hardware.go similarity index 99% rename from step_hardware.go rename to clone/step_hardware.go index ed7a01c48..b308ba1ab 100644 --- a/step_hardware.go +++ b/clone/step_hardware.go @@ -1,4 +1,4 @@ -package main +package clone import ( "github.com/mitchellh/multistep" diff --git a/clone/test.sh b/clone/test.sh new file mode 100755 index 000000000..dd91fee3a --- /dev/null +++ b/clone/test.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +export PACKER_ACC=1 +go test -v "$@" diff --git a/artifact.go b/common/artifact.go similarity index 97% rename from artifact.go rename to common/artifact.go index ec5842b3b..c9ae1a36d 100644 --- a/artifact.go +++ b/common/artifact.go @@ -1,4 +1,4 @@ -package main +package common import ( "github.com/jetbrains-infra/packer-builder-vsphere/driver" diff --git a/ssh.go b/common/ssh.go similarity index 61% rename from ssh.go rename to common/ssh.go index 863ce4e58..5ca854068 100644 --- a/ssh.go +++ b/common/ssh.go @@ -1,4 +1,4 @@ -package main +package common import ( "fmt" @@ -7,19 +7,20 @@ import ( packerssh "github.com/hashicorp/packer/communicator/ssh" "github.com/mitchellh/multistep" "golang.org/x/crypto/ssh" + "github.com/hashicorp/packer/helper/communicator" ) -func commHost(state multistep.StateBag) (string, error) { +func CommHost(state multistep.StateBag) (string, error) { return state.Get("ip").(string), nil } -func sshConfig(state multistep.StateBag) (*ssh.ClientConfig, error) { - config := state.Get("config").(*Config) +func SshConfig(state multistep.StateBag) (*ssh.ClientConfig, error) { + comm := state.Get("comm").(*communicator.Config) var auth []ssh.AuthMethod - if config.Comm.SSHPrivateKey != "" { - privateKey, err := ioutil.ReadFile(config.Comm.SSHPrivateKey) + if comm.SSHPrivateKey != "" { + privateKey, err := ioutil.ReadFile(comm.SSHPrivateKey) if err != nil { return nil, fmt.Errorf("Error loading configured private key file: %s", err) } @@ -32,14 +33,14 @@ func sshConfig(state multistep.StateBag) (*ssh.ClientConfig, error) { auth = []ssh.AuthMethod{ssh.PublicKeys(signer)} } else { auth = []ssh.AuthMethod{ - ssh.Password(config.Comm.SSHPassword), + ssh.Password(comm.SSHPassword), ssh.KeyboardInteractive( - packerssh.PasswordKeyboardInteractive(config.Comm.SSHPassword)), + packerssh.PasswordKeyboardInteractive(comm.SSHPassword)), } } clientConfig := &ssh.ClientConfig{ - User: config.Comm.SSHUsername, + User: comm.SSHUsername, HostKeyCallback: ssh.InsecureIgnoreHostKey(), } clientConfig.Auth = auth diff --git a/step_connect.go b/common/step_connect.go similarity index 81% rename from step_connect.go rename to common/step_connect.go index 57bce17ba..ae6b8fbbb 100644 --- a/step_connect.go +++ b/common/step_connect.go @@ -1,4 +1,4 @@ -package main +package common import ( "github.com/mitchellh/multistep" @@ -31,16 +31,16 @@ func (c *ConnectConfig) Prepare() []error { } type StepConnect struct { - config *ConnectConfig + Config *ConnectConfig } func (s *StepConnect) Run(state multistep.StateBag) multistep.StepAction { d, err := driver.NewDriver(&driver.ConnectConfig{ - VCenterServer: s.config.VCenterServer, - Username: s.config.Username, - Password: s.config.Password, - InsecureConnection: s.config.InsecureConnection, - Datacenter: s.config.Datacenter, + VCenterServer: s.Config.VCenterServer, + Username: s.Config.Username, + Password: s.Config.Password, + InsecureConnection: s.Config.InsecureConnection, + Datacenter: s.Config.Datacenter, }) if err != nil { state.Put("error", err) diff --git a/step_run.go b/common/step_run.go similarity index 98% rename from step_run.go rename to common/step_run.go index 5fc98fec5..340c226ad 100644 --- a/step_run.go +++ b/common/step_run.go @@ -1,4 +1,4 @@ -package main +package common import ( "github.com/mitchellh/multistep" diff --git a/step_shutdown.go b/common/step_shutdown.go similarity index 86% rename from step_shutdown.go rename to common/step_shutdown.go index 78bf2fbe6..8f9a4f4d2 100644 --- a/step_shutdown.go +++ b/common/step_shutdown.go @@ -1,4 +1,4 @@ -package main +package common import ( "github.com/mitchellh/multistep" @@ -34,7 +34,7 @@ func (c *ShutdownConfig) Prepare() []error { } type StepShutdown struct { - config *ShutdownConfig + Config *ShutdownConfig } func (s *StepShutdown) Run(state multistep.StateBag) multistep.StepAction { @@ -42,13 +42,13 @@ func (s *StepShutdown) Run(state multistep.StateBag) multistep.StepAction { comm := state.Get("communicator").(packer.Communicator) vm := state.Get("vm").(*driver.VirtualMachine) - if s.config.Command != "" { + if s.Config.Command != "" { ui.Say("Executing shutdown command...") - log.Printf("Shutdown command: %s", s.config.Command) + log.Printf("Shutdown command: %s", s.Config.Command) var stdout, stderr bytes.Buffer cmd := &packer.RemoteCmd{ - Command: s.config.Command, + Command: s.Config.Command, Stdout: &stdout, Stderr: &stderr, } @@ -67,8 +67,8 @@ func (s *StepShutdown) Run(state multistep.StateBag) multistep.StepAction { } } - log.Printf("Waiting max %s for shutdown to complete", s.config.Timeout) - err := vm.WaitForShutdown(s.config.Timeout) + log.Printf("Waiting max %s for shutdown to complete", s.Config.Timeout) + err := vm.WaitForShutdown(s.Config.Timeout) if err != nil { state.Put("error", err) return multistep.ActionHalt diff --git a/step_snapshot.go b/common/step_snapshot.go similarity index 91% rename from step_snapshot.go rename to common/step_snapshot.go index 4b2a86355..fbf69170c 100644 --- a/step_snapshot.go +++ b/common/step_snapshot.go @@ -1,4 +1,4 @@ -package main +package common import ( "github.com/mitchellh/multistep" @@ -7,14 +7,14 @@ import ( ) type StepCreateSnapshot struct{ - createSnapshot bool + CreateSnapshot bool } func (s *StepCreateSnapshot) Run(state multistep.StateBag) multistep.StepAction { ui := state.Get("ui").(packer.Ui) vm := state.Get("vm").(*driver.VirtualMachine) - if s.createSnapshot { + if s.CreateSnapshot { ui.Say("Creating snapshot...") err := vm.CreateSnapshot("Created by Packer") diff --git a/step_template.go b/common/step_template.go similarity index 97% rename from step_template.go rename to common/step_template.go index 0bed2c062..e805ea83d 100644 --- a/step_template.go +++ b/common/step_template.go @@ -1,4 +1,4 @@ -package main +package common import ( "github.com/mitchellh/multistep" diff --git a/test.sh b/test.sh index dd91fee3a..e69b13b42 100755 --- a/test.sh +++ b/test.sh @@ -1,4 +1,4 @@ #!/bin/sh -export PACKER_ACC=1 -go test -v "$@" +(cd driver && ./test.sh) +(cd clone && ./test.sh)