builder/qemu: dont fail on communicator - none

Signed-off-by: Vasiliy Tolstov <v.tolstov@selfip.ru>
This commit is contained in:
Vasiliy Tolstov 2016-07-06 18:52:40 +03:00
parent d170d5952b
commit 83f175cac7
3 changed files with 74 additions and 19 deletions

View File

@ -376,19 +376,40 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
HTTPPortMin: b.config.HTTPPortMin, HTTPPortMin: b.config.HTTPPortMin,
HTTPPortMax: b.config.HTTPPortMax, HTTPPortMax: b.config.HTTPPortMax,
}, },
new(stepForwardSSH), )
if b.config.Comm.Type != "none" {
steps = append(steps,
new(stepForwardSSH),
)
}
steps = append(steps,
new(stepConfigureVNC), new(stepConfigureVNC),
steprun, steprun,
&stepBootWait{}, &stepBootWait{},
&stepTypeBootCommand{}, &stepTypeBootCommand{},
&communicator.StepConnect{ )
Config: &b.config.Comm,
Host: commHost, if b.config.Comm.Type != "none" {
SSHConfig: sshConfig, steps = append(steps,
SSHPort: commPort, &communicator.StepConnect{
}, Config: &b.config.Comm,
Host: commHost,
SSHConfig: sshConfig,
SSHPort: commPort,
},
)
}
steps = append(steps,
new(common.StepProvision), new(common.StepProvision),
)
steps = append(steps,
new(stepShutdown), new(stepShutdown),
)
steps = append(steps,
new(stepConvertDisk), new(stepConvertDisk),
) )

View File

@ -63,7 +63,6 @@ func getCommandArgs(bootDrive string, state multistep.StateBag) ([]string, error
isoPath := state.Get("iso_path").(string) isoPath := state.Get("iso_path").(string)
vncIP := state.Get("vnc_ip").(string) vncIP := state.Get("vnc_ip").(string)
vncPort := state.Get("vnc_port").(uint) vncPort := state.Get("vnc_port").(uint)
sshHostPort := state.Get("sshHostPort").(uint)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
driver := state.Get("driver").(Driver) driver := state.Get("driver").(Driver)
@ -74,10 +73,16 @@ func getCommandArgs(bootDrive string, state multistep.StateBag) ([]string, error
defaultArgs := make(map[string]interface{}) defaultArgs := make(map[string]interface{})
var deviceArgs []string var deviceArgs []string
var driveArgs []string var driveArgs []string
var sshHostPort uint
defaultArgs["-name"] = vmName defaultArgs["-name"] = vmName
defaultArgs["-machine"] = fmt.Sprintf("type=%s", config.MachineType) defaultArgs["-machine"] = fmt.Sprintf("type=%s", config.MachineType)
defaultArgs["-netdev"] = fmt.Sprintf("user,id=user.0,hostfwd=tcp::%v-:%d", sshHostPort, config.Comm.Port()) if config.Comm.Type != "none" {
sshHostPort = state.Get("sshHostPort").(uint)
defaultArgs["-netdev"] = fmt.Sprintf("user,id=user.0,hostfwd=tcp::%v-:%d", sshHostPort, config.Comm.Port())
} else {
defaultArgs["-netdev"] = fmt.Sprintf("user,id=user.0")
}
qemuVersion, err := driver.Version() qemuVersion, err := driver.Version()
if err != nil { if err != nil {
@ -157,13 +162,23 @@ func getCommandArgs(bootDrive string, state multistep.StateBag) ([]string, error
httpPort := state.Get("http_port").(uint) httpPort := state.Get("http_port").(uint)
ctx := config.ctx ctx := config.ctx
ctx.Data = qemuArgsTemplateData{ if config.Comm.Type != "none" {
"10.0.2.2", ctx.Data = qemuArgsTemplateData{
httpPort, "10.0.2.2",
config.HTTPDir, httpPort,
config.OutputDir, config.HTTPDir,
config.VMName, config.OutputDir,
sshHostPort, config.VMName,
sshHostPort,
}
} else {
ctx.Data = qemuArgsTemplateData{
HTTPIP: "10.0.2.2",
HTTPPort: httpPort,
HTTPDir: config.HTTPDir,
OutputDir: config.OutputDir,
Name: config.VMName,
}
} }
newQemuArgs, err := processArgs(config.QemuArgs, &ctx) newQemuArgs, err := processArgs(config.QemuArgs, &ctx)
if err != nil { if err != nil {

View File

@ -3,10 +3,11 @@ package qemu
import ( import (
"errors" "errors"
"fmt" "fmt"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"log" "log"
"time" "time"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
) )
// This step shuts down the machine. It first attempts to do so gracefully, // This step shuts down the machine. It first attempts to do so gracefully,
@ -23,11 +24,29 @@ import (
type stepShutdown struct{} type stepShutdown struct{}
func (s *stepShutdown) Run(state multistep.StateBag) multistep.StepAction { func (s *stepShutdown) Run(state multistep.StateBag) multistep.StepAction {
comm := state.Get("communicator").(packer.Communicator)
config := state.Get("config").(*Config) config := state.Get("config").(*Config)
driver := state.Get("driver").(Driver) driver := state.Get("driver").(Driver)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
if state.Get("communicator") == nil {
cancelCh := make(chan struct{}, 1)
go func() {
defer close(cancelCh)
<-time.After(config.shutdownTimeout)
}()
ui.Say("Waiting for shutdown...")
if ok := driver.WaitForShutdown(cancelCh); ok {
log.Println("VM shut down.")
return multistep.ActionContinue
} else {
err := fmt.Errorf("Failed to shutdown")
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
}
comm := state.Get("communicator").(packer.Communicator)
if config.ShutdownCommand != "" { if config.ShutdownCommand != "" {
ui.Say("Gracefully halting virtual machine...") ui.Say("Gracefully halting virtual machine...")
log.Printf("Executing shutdown command: %s", config.ShutdownCommand) log.Printf("Executing shutdown command: %s", config.ShutdownCommand)