Skip to content

Commit

Permalink
Recovery mode support (#25)
Browse files Browse the repository at this point in the history
* Recovery mode support

* Communicator can be missing
  • Loading branch information
edigaryev committed Sep 27, 2022
1 parent 13cc207 commit 04688ea
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 23 deletions.
22 changes: 14 additions & 8 deletions builder/tart/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Config struct {
FromIPSW string `mapstructure:"from_ipsw" required:"true"`
VMName string `mapstructure:"vm_name" required:"true"`
VMBaseName string `mapstructure:"vm_base_name" required:"true"`
Recovery bool `mapstructure:"recovery" required:"false"`
CpuCount uint8 `mapstructure:"cpu_count" required:"false"`
MemoryGb uint16 `mapstructure:"memory_gb" required:"false"`
Display string `mapstructure:"display" required:"false"`
Expand Down Expand Up @@ -59,23 +60,28 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack

if b.config.FromIPSW != "" {
steps = append(steps, new(stepCreateVM))
} else {
} else if b.config.VMBaseName != "" {
steps = append(steps, new(stepCloneVM))
}

steps = append(steps,
new(stepSetVM),
new(stepDiskFilePrepare),
new(stepRun),
&communicator.StepConnect{
Config: &b.config.Comm,
Host: TartMachineIP(b.config.VMName),
SSHConfig: b.config.Comm.SSHConfigFunc(),
},
new(stepResize),
&commonsteps.StepProvision{},
)

if !b.config.Recovery {
steps = append(steps,
&communicator.StepConnect{
Config: &b.config.Comm,
Host: TartMachineIP(b.config.VMName),
SSHConfig: b.config.Comm.SSHConfigFunc(),
},
new(stepResize),
&commonsteps.StepProvision{},
)
}

// Setup the state bag and initial state for the steps
state := new(multistep.BasicStateBag)
state.Put("config", &b.config)
Expand Down
2 changes: 2 additions & 0 deletions builder/tart/builder.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion builder/tart/step_disk_resize.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ func (s *stepResize) Run(ctx context.Context, state multistep.StateBag) multiste
return multistep.ActionContinue
}

ui.Say("Let's SSH in and claim the new space for the disk...")
communicator := state.Get("communicator").(packersdk.Communicator)
if communicator == nil {
return multistep.ActionContinue
}

ui.Say("Let's SSH in and claim the new space for the disk...")

ui.Say("Freeing space...")
repairCmd := packersdk.RemoteCmd{
Expand Down
28 changes: 14 additions & 14 deletions builder/tart/step_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ func (s *stepRun) Run(ctx context.Context, state multistep.StateBag) multistep.S
if !config.DisableVNC {
runArgs = append(runArgs, "--vnc-experimental")
}
if config.Recovery {
runArgs = append(runArgs, "--recovery")
}
cmd := exec.Command("tart", runArgs...)
stdout := bytes.NewBufferString("")
cmd.Stdout = stdout
Expand Down Expand Up @@ -81,22 +84,19 @@ func (s *stepRun) Cleanup(state multistep.StateBag) {
config := state.Get("config").(*Config)
ui := state.Get("ui").(packersdk.Ui)

communicator := state.Get("communicator").(packersdk.Communicator)

if communicator == nil {
return
}

ui.Say("Gracefully shutting down the VM...")
shutdownCmd := packersdk.RemoteCmd{
Command: fmt.Sprintf("echo %s | sudo -S shutdown -h now", config.Comm.Password()),
}
communicator := state.Get("communicator")
if communicator != nil {
ui.Say("Gracefully shutting down the VM...")

err := shutdownCmd.RunWithUi(context.Background(), communicator, ui)
shutdownCmd := packersdk.RemoteCmd{
Command: fmt.Sprintf("echo %s | sudo -S shutdown -h now", config.Comm.Password()),
}

if err != nil {
ui.Say("Failed to gracefully shutdown VM...")
ui.Error(err.Error())
err := shutdownCmd.RunWithUi(context.Background(), communicator.(packersdk.Communicator), ui)
if err != nil {
ui.Say("Failed to gracefully shutdown VM...")
ui.Error(err.Error())
}
}

cmd := state.Get("tart-cmd").(*exec.Cmd)
Expand Down

0 comments on commit 04688ea

Please sign in to comment.