Abstract out GetSR

This commit is contained in:
Cheng Sun 2014-12-15 14:17:20 +00:00
parent b73cbc1230
commit 2a4ce52eaa
2 changed files with 33 additions and 35 deletions

View File

@ -431,6 +431,14 @@ func (self *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (pa
return artifact, nil return artifact, nil
} }
func (self *Builder) Cancel() {
if self.runner != nil {
log.Println("Cancelling the step runner...")
self.runner.Cancel()
}
fmt.Println("Cancelling the builder")
}
// all steps should check config.ShouldKeepInstance first before cleaning up // all steps should check config.ShouldKeepInstance first before cleaning up
func (cfg config) ShouldKeepInstance(state multistep.StateBag) bool { func (cfg config) ShouldKeepInstance(state multistep.StateBag) bool {
switch cfg.KeepInstance { switch cfg.KeepInstance {
@ -448,10 +456,26 @@ func (cfg config) ShouldKeepInstance(state multistep.StateBag) bool {
} }
} }
func (self *Builder) Cancel() { func (config config) GetSR(client XenAPIClient) (*SR, error) {
if self.runner != nil { if config.SrName == "" {
log.Println("Cancelling the step runner...") // Find the default SR
self.runner.Cancel() return client.GetDefaultSR()
} else {
// Use the provided name label to find the SR to use
srs, err := client.GetSRByNameLabel(config.SrName)
if err != nil {
return nil, err
}
switch {
case len(srs) == 0:
return nil, fmt.Errorf("Couldn't find a SR with the specified name-label '%s'", config.SrName)
case len(srs) > 1:
return nil, fmt.Errorf("Found more than one SR with the name '%s'. The name must be unique", config.SrName)
}
return srs[0], nil
} }
fmt.Println("Cancelling the builder")
} }

View File

@ -61,37 +61,11 @@ func (self *stepCreateInstance) Run(state multistep.StateBag) multistep.StepActi
} }
// Create VDI for the instance // Create VDI for the instance
var sr *SR
if config.SrName == "" { sr, err := config.GetSR(client)
// Find the default SR if err != nil {
default_sr, err := client.GetDefaultSR() ui.Error(fmt.Sprintf("Unable to get SR: %s", err.Error()))
sr = default_sr return multistep.ActionHalt
if err != nil {
ui.Error(fmt.Sprintf("Error getting default SR: %s", err.Error()))
return multistep.ActionHalt
}
} else {
// Use the provided name label to find the SR to use
srs, err := client.GetSRByNameLabel(config.SrName)
if err != nil {
ui.Error(fmt.Sprintf("Error getting default SR: %s", err.Error()))
return multistep.ActionHalt
}
switch {
case len(srs) == 0:
ui.Error(fmt.Sprintf("Couldn't find a SR with the specified name-label '%s'. Aborting.", config.SrName))
return multistep.ActionHalt
case len(srs) > 1:
ui.Error(fmt.Sprintf("Found more than one SR with the name '%s'. The name must be unique. Aborting.", config.SrName))
return multistep.ActionHalt
}
sr = srs[0]
} }
vdi, err := sr.CreateVdi("Packer-disk", config.RootDiskSize) vdi, err := sr.CreateVdi("Packer-disk", config.RootDiskSize)