Wilken Rivera 9ec8b67392
Add golangci-lint to project (#8686)
* Add golangci-lint as linting tool

* Disable failing staticchecks to start; GitHub issue to handle coming soon

* Run `goimports -w` to repair all source files that have improperly
formatted imports

* makefile: Add ci-lint target to run on travis

This change adds a new make target for running golangci-lint on newly
added Go files only. This target is expected to run during Packer ci builds.

* .github/contributing: Add code linting instructions

* travis: Update job configuration to run parallel builds
2020-02-14 11:42:29 -05:00

81 lines
1.8 KiB
Go

//go:generate struct-markdown
//go:generate mapstructure-to-hcl2 -type RunConfig
package common
import (
"context"
"strings"
"github.com/hashicorp/packer/builder/vsphere/driver"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
)
type RunConfig struct {
// Priority of boot devices. Defaults to `disk,cdrom`
BootOrder string `mapstructure:"boot_order"` // example: "floppy,cdrom,ethernet,disk"
}
type StepRun struct {
Config *RunConfig
SetOrder bool
}
func (s *StepRun) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
vm := state.Get("vm").(*driver.VirtualMachine)
if s.Config.BootOrder != "" {
ui.Say("Set boot order...")
order := strings.Split(s.Config.BootOrder, ",")
if err := vm.SetBootOrder(order); err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
} else {
if s.SetOrder {
ui.Say("Set boot order temporary...")
if err := vm.SetBootOrder([]string{"disk", "cdrom"}); err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
}
}
ui.Say("Power on VM...")
err := vm.PowerOn()
if err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
return multistep.ActionContinue
}
func (s *StepRun) Cleanup(state multistep.StateBag) {
ui := state.Get("ui").(packer.Ui)
vm := state.Get("vm").(*driver.VirtualMachine)
if s.Config.BootOrder == "" && s.SetOrder {
ui.Say("Clear boot order...")
if err := vm.SetBootOrder([]string{"-"}); err != nil {
state.Put("error", err)
return
}
}
_, cancelled := state.GetOk(multistep.StateCancelled)
_, halted := state.GetOk(multistep.StateHalted)
if !cancelled && !halted {
return
}
ui.Say("Power off VM...")
err := vm.PowerOff()
if err != nil {
ui.Error(err.Error())
}
}