packer-cn/builder/hyperone/step_prepare_device.go

58 lines
1.4 KiB
Go
Raw Normal View History

2019-01-28 06:30:15 -05:00
package hyperone
import (
"context"
"fmt"
"log"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
)
2019-02-05 10:20:42 -05:00
const (
vmBusPath = "/sys/bus/vmbus/devices"
2019-02-05 10:20:42 -05:00
)
2019-01-28 06:30:15 -05:00
type stepPrepareDevice struct{}
func (s *stepPrepareDevice) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
2019-01-28 06:30:15 -05:00
ui := state.Get("ui").(packer.Ui)
config := state.Get("config").(*Config)
if config.ChrootDevice != "" {
state.Put("device", config.ChrootDevice)
return multistep.ActionContinue
}
controllerNumber := state.Get("chroot_controller_number").(string)
controllerLocation := state.Get("chroot_controller_location").(int)
2019-01-28 06:30:15 -05:00
log.Println("Searching for available device...")
2019-02-05 10:20:42 -05:00
cmd := fmt.Sprintf("find %s/%s/ -path *:%d/block -exec ls {} \\;",
vmBusPath, controllerNumber, controllerLocation)
2019-02-05 10:20:42 -05:00
block, err := captureOutput(cmd, state)
2019-01-28 06:30:15 -05:00
if err != nil {
err := fmt.Errorf("error finding available device: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
if block == "" {
err := fmt.Errorf("device not found")
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
device := fmt.Sprintf("/dev/%s", block)
ui.Say(fmt.Sprintf("Found device: %s", device))
2019-01-28 06:30:15 -05:00
state.Put("device", device)
return multistep.ActionContinue
}
func (s *stepPrepareDevice) Cleanup(state multistep.StateBag) {}