Skip to content

Commit 5f6648d

Browse files
author
dwc0011
committed
Create the manual mount command step
1 parent c430f94 commit 5f6648d

1 file changed

Lines changed: 117 additions & 0 deletions

File tree

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package chroot
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"fmt"
7+
"log"
8+
"path/filepath"
9+
10+
"github.com/hashicorp/packer-plugin-sdk/common"
11+
"github.com/hashicorp/packer-plugin-sdk/multistep"
12+
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
13+
"github.com/hashicorp/packer-plugin-sdk/packerbuilderdata"
14+
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
15+
)
16+
17+
type manualMountCommandData struct {
18+
Device string
19+
}
20+
21+
// StepManualMountCommand sets up the a new block device when building from scratch
22+
type StepManualMountCommand struct {
23+
Command string
24+
mountPath string
25+
26+
GeneratedData *packerbuilderdata.GeneratedData
27+
}
28+
29+
func (s *StepManualMountCommand) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
30+
config := state.Get("config").(*Config)
31+
device := state.Get("device").(string)
32+
ui := state.Get("ui").(packersdk.Ui)
33+
34+
ui.Say("Running manual mount commands...")
35+
36+
if config.NVMEDevicePath != "" {
37+
// customizable device path for mounting NVME block devices on c5 and m5 HVM
38+
device = config.NVMEDevicePath
39+
}
40+
ui.Say(fmt.Sprintf("Command is: %s", s.Command))
41+
if len(s.Command) == 0 {
42+
return multistep.ActionContinue
43+
}
44+
45+
ictx := config.GetContext()
46+
ictx.Data = &manualMountCommandData{Device: filepath.Base(device)}
47+
mountPath, err := interpolate.Render(config.MountPath, &ictx)
48+
49+
if err != nil {
50+
err := fmt.Errorf("Error preparing mount directory: %s", err)
51+
state.Put("error", err)
52+
ui.Error(err.Error())
53+
return multistep.ActionHalt
54+
}
55+
56+
mountPath, err = filepath.Abs(mountPath)
57+
if err != nil {
58+
err := fmt.Errorf("Error preparing mount directory: %s", err)
59+
state.Put("error", err)
60+
ui.Error(err.Error())
61+
return multistep.ActionHalt
62+
}
63+
64+
ui.Say(fmt.Sprintf("Mount Path After ABS is: %s", mountPath))
65+
66+
log.Printf("Mount path: %s", mountPath)
67+
stderr := new(bytes.Buffer)
68+
69+
wrappedCommand := state.Get("wrappedCommand").(common.CommandWrapper)
70+
71+
ui.Say("Running manual mount commands...")
72+
mountCommand, err := wrappedCommand(fmt.Sprintf("%s %s", s.Command, mountPath))
73+
if err != nil {
74+
err := fmt.Errorf("Error creating mount command: %s", err)
75+
state.Put("error", err)
76+
ui.Error(err.Error())
77+
return multistep.ActionHalt
78+
}
79+
80+
cmd := common.ShellCommand(mountCommand)
81+
cmd.Stderr = stderr
82+
if err := cmd.Run(); err != nil {
83+
err := fmt.Errorf(
84+
"Error mounting root volume: %s\nStderr: %s", err, stderr.String())
85+
state.Put("error", err)
86+
ui.Error(err.Error())
87+
return multistep.ActionHalt
88+
}
89+
90+
// Set the mount path so we remember to unmount it later
91+
s.mountPath = mountPath
92+
state.Put("mount_path", s.mountPath)
93+
s.GeneratedData.Put("MountPath", s.mountPath)
94+
state.Put("mount_device_cleanup", s)
95+
96+
return multistep.ActionContinue
97+
}
98+
99+
func (s *StepManualMountCommand) Cleanup(state multistep.StateBag) {
100+
ui := state.Get("ui").(packersdk.Ui)
101+
if err := s.CleanupFunc(state); err != nil {
102+
ui.Error(err.Error())
103+
}
104+
}
105+
106+
func (s *StepManualMountCommand) CleanupFunc(state multistep.StateBag) error {
107+
if s.mountPath == "" {
108+
return nil
109+
}
110+
111+
ui := state.Get("ui").(packersdk.Ui)
112+
113+
ui.Say("Skipping UnMount Root Mount, it must be manually unmounted...")
114+
115+
s.mountPath = ""
116+
return nil
117+
}

0 commit comments

Comments
 (0)