Commit 0b830c92 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

common: Use new multistep API

parent 1a2af970
......@@ -11,7 +11,7 @@ import (
// MultistepDebugFn will return a proper multistep.DebugPauseFn to
// use for debugging if you're using multistep in your builder.
func MultistepDebugFn(ui packer.Ui) multistep.DebugPauseFn {
return func(loc multistep.DebugLocation, name string, state map[string]interface{}) {
return func(loc multistep.DebugLocation, name string, state multistep.StateBag) {
var locationString string
switch loc {
case multistep.DebugLocationAfterRun:
......@@ -41,7 +41,7 @@ func MultistepDebugFn(ui packer.Ui) multistep.DebugPauseFn {
case <-result:
return
case <-time.After(100 * time.Millisecond):
if _, ok := state[multistep.StateCancelled]; ok {
if _, ok := state.GetOk(multistep.StateCancelled); ok {
return
}
}
......
......@@ -25,11 +25,11 @@ type StepConnectSSH struct {
// SSHAddress is a function that returns the TCP address to connect to
// for SSH. This is a function so that you can query information
// if necessary for this address.
SSHAddress func(map[string]interface{}) (string, error)
SSHAddress func(multistep.StateBag) (string, error)
// SSHConfig is a function that returns the proper client configuration
// for SSH access.
SSHConfig func(map[string]interface{}) (*gossh.ClientConfig, error)
SSHConfig func(multistep.StateBag) (*gossh.ClientConfig, error)
// SSHWaitTimeout is the total timeout to wait for SSH to become available.
SSHWaitTimeout time.Duration
......@@ -40,8 +40,8 @@ type StepConnectSSH struct {
comm packer.Communicator
}
func (s *StepConnectSSH) Run(state map[string]interface{}) multistep.StepAction {
ui := state["ui"].(packer.Ui)
func (s *StepConnectSSH) Run(state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
var comm packer.Communicator
var err error
......@@ -69,14 +69,14 @@ WaitLoop:
ui.Say("Connected to SSH!")
s.comm = comm
state["communicator"] = comm
state.Put("communicator", comm)
break WaitLoop
case <-timeout:
ui.Error("Timeout waiting for SSH.")
close(cancel)
return multistep.ActionHalt
case <-time.After(1 * time.Second):
if _, ok := state[multistep.StateCancelled]; ok {
if _, ok := state.GetOk(multistep.StateCancelled); ok {
// The step sequence was cancelled, so cancel waiting for SSH
// and just start the halting process.
close(cancel)
......@@ -89,10 +89,10 @@ WaitLoop:
return multistep.ActionContinue
}
func (s *StepConnectSSH) Cleanup(map[string]interface{}) {
func (s *StepConnectSSH) Cleanup(multistep.StateBag) {
}
func (s *StepConnectSSH) waitForSSH(state map[string]interface{}, cancel <-chan struct{}) (packer.Communicator, error) {
func (s *StepConnectSSH) waitForSSH(state multistep.StateBag, cancel <-chan struct{}) (packer.Communicator, error) {
handshakeAttempts := 0
var comm packer.Communicator
......
......@@ -22,19 +22,20 @@ type StepCreateFloppy struct {
floppyPath string
}
func (s *StepCreateFloppy) Run(state map[string]interface{}) multistep.StepAction {
func (s *StepCreateFloppy) Run(state multistep.StateBag) multistep.StepAction {
if len(s.Files) == 0 {
log.Println("No floppy files specified. Floppy disk will not be made.")
return multistep.ActionContinue
}
ui := state["ui"].(packer.Ui)
ui := state.Get("ui").(packer.Ui)
ui.Say("Creating floppy disk...")
// Create a temporary file to be our floppy drive
floppyF, err := ioutil.TempFile("", "packer")
if err != nil {
state["error"] = fmt.Errorf("Error creating temporary file for floppy: %s", err)
state.Put("error",
fmt.Errorf("Error creating temporary file for floppy: %s", err))
return multistep.ActionHalt
}
defer floppyF.Close()
......@@ -46,7 +47,7 @@ func (s *StepCreateFloppy) Run(state map[string]interface{}) multistep.StepActio
// Set the size of the file to be a floppy sized
if err := floppyF.Truncate(1440 * 1024); err != nil {
state["error"] = fmt.Errorf("Error creating floppy: %s", err)
state.Put("error", fmt.Errorf("Error creating floppy: %s", err))
return multistep.ActionHalt
}
......@@ -54,7 +55,7 @@ func (s *StepCreateFloppy) Run(state map[string]interface{}) multistep.StepActio
log.Println("Initializing block device backed by temporary file")
device, err := fs.NewFileDisk(floppyF)
if err != nil {
state["error"] = fmt.Errorf("Error creating floppy: %s", err)
state.Put("error", fmt.Errorf("Error creating floppy: %s", err))
return multistep.ActionHalt
}
......@@ -66,7 +67,7 @@ func (s *StepCreateFloppy) Run(state map[string]interface{}) multistep.StepActio
OEMName: "packer",
}
if fat.FormatSuperFloppy(device, formatConfig); err != nil {
state["error"] = fmt.Errorf("Error creating floppy: %s", err)
state.Put("error", fmt.Errorf("Error creating floppy: %s", err))
return multistep.ActionHalt
}
......@@ -74,7 +75,7 @@ func (s *StepCreateFloppy) Run(state map[string]interface{}) multistep.StepActio
log.Println("Initializing FAT filesystem on block device")
fatFs, err := fat.New(device)
if err != nil {
state["error"] = fmt.Errorf("Error creating floppy: %s", err)
state.Put("error", fmt.Errorf("Error creating floppy: %s", err))
return multistep.ActionHalt
}
......@@ -82,7 +83,7 @@ func (s *StepCreateFloppy) Run(state map[string]interface{}) multistep.StepActio
log.Println("Reading the root directory from the filesystem")
rootDir, err := fatFs.RootDir()
if err != nil {
state["error"] = fmt.Errorf("Error creating floppy: %s", err)
state.Put("error", fmt.Errorf("Error creating floppy: %s", err))
return multistep.ActionHalt
}
......@@ -90,18 +91,18 @@ func (s *StepCreateFloppy) Run(state map[string]interface{}) multistep.StepActio
for _, filename := range s.Files {
ui.Message(fmt.Sprintf("Copying: %s", filepath.Base(filename)))
if s.addSingleFile(rootDir, filename); err != nil {
state["error"] = fmt.Errorf("Error adding file to floppy: %s", err)
state.Put("error", fmt.Errorf("Error adding file to floppy: %s", err))
return multistep.ActionHalt
}
}
// Set the path to the floppy so it can be used later
state["floppy_path"] = s.floppyPath
state.Put("floppy_path", s.floppyPath)
return multistep.ActionContinue
}
func (s *StepCreateFloppy) Cleanup(map[string]interface{}) {
func (s *StepCreateFloppy) Cleanup(multistep.StateBag) {
if s.floppyPath != "" {
log.Printf("Deleting floppy disk: %s", s.floppyPath)
os.Remove(s.floppyPath)
......
......@@ -37,16 +37,16 @@ type StepDownload struct {
Url []string
}
func (s *StepDownload) Run(state map[string]interface{}) multistep.StepAction {
cache := state["cache"].(packer.Cache)
ui := state["ui"].(packer.Ui)
func (s *StepDownload) Run(state multistep.StateBag) multistep.StepAction {
cache := state.Get("cache").(packer.Cache)
ui := state.Get("ui").(packer.Ui)
var checksum []byte
if s.Checksum != "" {
var err error
checksum, err = hex.DecodeString(s.Checksum)
if err != nil {
state["error"] = fmt.Errorf("Error parsing checksum: %s", err)
state.Put("error", fmt.Errorf("Error parsing checksum: %s", err))
return multistep.ActionHalt
}
}
......@@ -89,20 +89,20 @@ func (s *StepDownload) Run(state map[string]interface{}) multistep.StepAction {
if finalPath == "" {
err := fmt.Errorf("%s download failed.", s.Description)
state["error"] = err
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
state[s.ResultKey] = finalPath
state.Put(s.ResultKey, finalPath)
return multistep.ActionContinue
}
func (s *StepDownload) Cleanup(map[string]interface{}) {}
func (s *StepDownload) Cleanup(multistep.StateBag) {}
func (s *StepDownload) download(config *DownloadConfig, state map[string]interface{}) (string, error, bool) {
func (s *StepDownload) download(config *DownloadConfig, state multistep.StateBag) (string, error, bool) {
var path string
ui := state["ui"].(packer.Ui)
ui := state.Get("ui").(packer.Ui)
download := NewDownloadClient(config)
downloadCompleteCh := make(chan error, 1)
......@@ -129,7 +129,7 @@ func (s *StepDownload) download(config *DownloadConfig, state map[string]interfa
ui.Message(fmt.Sprintf("Download progress: %d%%", progress))
}
case <-time.After(1 * time.Second):
if _, ok := state[multistep.StateCancelled]; ok {
if _, ok := state.GetOk(multistep.StateCancelled); ok {
ui.Say("Interrupt received. Cancelling download...")
return "", nil, false
}
......
......@@ -18,10 +18,10 @@ import (
// <nothing>
type StepProvision struct{}
func (*StepProvision) Run(state map[string]interface{}) multistep.StepAction {
comm := state["communicator"].(packer.Communicator)
hook := state["hook"].(packer.Hook)
ui := state["ui"].(packer.Ui)
func (*StepProvision) Run(state multistep.StateBag) multistep.StepAction {
comm := state.Get("communicator").(packer.Communicator)
hook := state.Get("hook").(packer.Hook)
ui := state.Get("ui").(packer.Ui)
// Run the provisioner in a goroutine so we can continually check
// for cancellations...
......@@ -35,13 +35,13 @@ func (*StepProvision) Run(state map[string]interface{}) multistep.StepAction {
select {
case err := <-errCh:
if err != nil {
state["error"] = err
state.Put("error", err)
return multistep.ActionHalt
}
return multistep.ActionContinue
case <-time.After(1 * time.Second):
if _, ok := state[multistep.StateCancelled]; ok {
if _, ok := state.GetOk(multistep.StateCancelled); ok {
log.Println("Cancelling provisioning due to interrupt...")
hook.Cancel()
return multistep.ActionHalt
......@@ -50,4 +50,4 @@ func (*StepProvision) Run(state map[string]interface{}) multistep.StepAction {
}
}
func (*StepProvision) Cleanup(map[string]interface{}) {}
func (*StepProvision) Cleanup(multistep.StateBag) {}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment