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

common: Use new multistep API

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