Skip to content

Commit

Permalink
Stream Tart stdout/stderr output to Packer UI (cirruslabs#124)
Browse files Browse the repository at this point in the history
Creating a Tart VM may involve long running operations such as
downloading an IPSW, or installing the OS. Tart gives us progress
during these operations, so let's forward it to the Packer UI,
the same way the shell-local provisioner does.

If a packer.Ui is not provided, we keep the previous code path
that returns the output to the caller, for example used by the
TartMachineIP helper.
  • Loading branch information
torarnv authored Mar 11, 2024
1 parent 6d4f2bf commit 2a23549
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 23 deletions.
29 changes: 19 additions & 10 deletions builder/tart/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
"os/exec"
"path"
"strings"

"github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/hashicorp/packer-plugin-sdk/shell-local/localexec"
)

const tartCommand = "tart"
Expand All @@ -21,20 +24,26 @@ func PathInTartHome(elem ...string) string {
return path.Join(userHome, ".tart", path.Join(elem...))
}

func TartExec(ctx context.Context, args ...string) (string, error) {
var out bytes.Buffer
func TartExec(ctx context.Context, ui packer.Ui, args ...string) (string, error) {

log.Printf("Executing tart: %#v", args)

cmd := exec.CommandContext(ctx, tartCommand, args...)
cmd.Stdout = &out
cmd.Stderr = &out
err := cmd.Run()

outString := strings.TrimSpace(out.String())
if ui != nil {
return "", localexec.RunAndStream(cmd, ui, []string{})
} else {
var out bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &out
err := cmd.Run()

if _, ok := err.(*exec.ExitError); ok {
err = fmt.Errorf("tart error: %s", outString)
}
outString := strings.TrimSpace(out.String())

return outString, err
if _, ok := err.(*exec.ExitError); ok {
err = fmt.Errorf("tart error: %s", outString)
}

return outString, err
}
}
2 changes: 1 addition & 1 deletion builder/tart/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ func TartMachineIP(ctx context.Context, vmName string, ipExtraArgs []string) (st
if len(ipExtraArgs) > 0 {
ipArgs = append(ipArgs, ipExtraArgs...)
}
return TartExec(ctx, ipArgs...)
return TartExec(ctx, nil, ipArgs...)
}
2 changes: 1 addition & 1 deletion builder/tart/step_cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ func (s *stepCleanVM) Cleanup(state multistep.StateBag) {
if cancelled || halted {
ui.Say("Cleaning up virtual machine...")
cmdArgs := []string{"delete", config.VMName}
_, _ = TartExec(context.Background(), cmdArgs...)
_, _ = TartExec(context.Background(), ui, cmdArgs...)
}
}
4 changes: 1 addition & 3 deletions builder/tart/step_clone_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ func (s *stepCloneVM) Run(ctx context.Context, state multistep.StateBag) multist
cmdArgs = append(cmdArgs, "--concurrency", fmt.Sprintf("%d", config.PullConcurrency))
}

if _, err := TartExec(ctx, cmdArgs...); err != nil {
if _, err := TartExec(ctx, ui, cmdArgs...); err != nil {
err := fmt.Errorf("Error cloning VM: %s", err)
state.Put("error", err)
ui.Error(err.Error())

return multistep.ActionHalt
}

Expand Down
4 changes: 1 addition & 3 deletions builder/tart/step_create_linux_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ func (s *stepCreateLinuxVM) Run(ctx context.Context, state multistep.StateBag) m

createArguments = append(createArguments, config.VMName)

if _, err := TartExec(ctx, createArguments...); err != nil {
if _, err := TartExec(ctx, ui, createArguments...); err != nil {
err := fmt.Errorf("Failed to create a VM: %s", err)
state.Put("error", err)
ui.Error(err.Error())

return multistep.ActionHalt
}

Expand Down
4 changes: 1 addition & 3 deletions builder/tart/step_create_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ func (s *stepCreateVM) Run(ctx context.Context, state multistep.StateBag) multis

createArguments = append(createArguments, config.VMName)

if _, err := TartExec(ctx, createArguments...); err != nil {
if _, err := TartExec(ctx, ui, createArguments...); err != nil {
err := fmt.Errorf("Failed to create a VM: %s", err)
state.Put("error", err)
ui.Error(err.Error())

return multistep.ActionHalt
}

Expand Down
3 changes: 1 addition & 2 deletions builder/tart/step_set_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ func (s *stepSetVM) Run(ctx context.Context, state multistep.StateBag) multistep
setArguments = append(setArguments, "--display", config.Display)
}

if _, err := TartExec(ctx, setArguments...); err != nil {
if _, err := TartExec(ctx, ui, setArguments...); err != nil {
err := fmt.Errorf("Error updating VM: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}

Expand Down

0 comments on commit 2a23549

Please sign in to comment.