Skip to content

Commit 9957253

Browse files
committed
add the before_start_hook
this will executed after the vm is configured, but before its started this receives the vm configuration as a json in stdin the hook is expected to return a json with the modified configuration
1 parent 31386f5 commit 9957253

File tree

9 files changed

+85
-0
lines changed

9 files changed

+85
-0
lines changed

.web-docs/components/builder/clone/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ boot time.
276276
For example `-no-reboot -smbios type=0,vendor=FOO`.
277277
Note: this option is for experts only.
278278

279+
- `before_start_hook` ([]string) - Before Start Hook
280+
279281
<!-- End of code generated from the comments of the Config struct in builder/proxmox/common/config.go; -->
280282

281283

.web-docs/components/builder/iso/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ in the image's Cloud-Init settings for provisioning.
207207
For example `-no-reboot -smbios type=0,vendor=FOO`.
208208
Note: this option is for experts only.
209209

210+
- `before_start_hook` ([]string) - Before Start Hook
211+
210212
<!-- End of code generated from the comments of the Config struct in builder/proxmox/common/config.go; -->
211213

212214

builder/proxmox/clone/config.hcl2spec.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

builder/proxmox/common/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ type Config struct {
196196
// Note: this option is for experts only.
197197
AdditionalArgs string `mapstructure:"qemu_additional_args"`
198198

199+
BeforeStartHook []string `mapstructure:"before_start_hook"`
200+
199201
Ctx interpolate.Context `mapstructure-to-hcl2:",skip"`
200202
}
201203

builder/proxmox/common/config.hcl2spec.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

builder/proxmox/common/step_start_vm.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
package proxmox
55

66
import (
7+
"bytes"
78
"context"
9+
"encoding/json"
810
"fmt"
911
"log"
12+
"os/exec"
1013
"strconv"
1114
"strings"
1215

@@ -243,6 +246,69 @@ func (s *stepStartVM) Run(ctx context.Context, state multistep.StateBag) multist
243246
// info available in the vmref type.
244247
state.Put("instance_id", vmRef.VmId())
245248

249+
// Execute the before_start_hook.
250+
if len(c.BeforeStartHook) > 0 {
251+
ui.Say("Executing the before_start_hook")
252+
253+
cfg, err := client.GetVmConfig(vmRef)
254+
if err != nil {
255+
err := fmt.Errorf("failed to get the VM configuration: %w", err)
256+
state.Put("error", err)
257+
ui.Error(err.Error())
258+
return multistep.ActionHalt
259+
}
260+
261+
cfgDoc, err := json.Marshal(cfg)
262+
if err != nil {
263+
err := fmt.Errorf("failed to marshal the VM configuration: %w", err)
264+
state.Put("error", err)
265+
ui.Error(err.Error())
266+
return multistep.ActionHalt
267+
}
268+
269+
log.Printf("executing the before_start_hook in VM with ID %d and config %s", vmRef.VmId(), cfgDoc)
270+
271+
var stdout, stderr bytes.Buffer
272+
var cmdArgs []string
273+
if len(c.BeforeStartHook) > 1 {
274+
cmdArgs = c.BeforeStartHook[1:]
275+
}
276+
cmd := exec.Command(c.BeforeStartHook[0], cmdArgs...)
277+
cmd.Stdin = bytes.NewReader(cfgDoc)
278+
cmd.Stdout = &stdout
279+
cmd.Stderr = &stderr
280+
err = cmd.Run()
281+
if err != nil {
282+
log.Printf("failed to execute the before_start_hook in VM with ID %d. stderr=%s stdout=%s", vmRef.VmId(), stderr.Bytes(), stdout.Bytes())
283+
err := fmt.Errorf("failed to execute the before_start_hook: %w", err)
284+
state.Put("error", err)
285+
ui.Error(err.Error())
286+
return multistep.ActionHalt
287+
}
288+
newCfgDoc := stdout.Bytes()
289+
290+
log.Printf("executed the before_start_hook in VM with ID %d and returned the config %s", vmRef.VmId(), newCfgDoc)
291+
292+
if bytes.HasPrefix(newCfgDoc, []byte{'{'}) {
293+
var vmConfig map[string]interface{}
294+
err = json.Unmarshal(newCfgDoc, &vmConfig)
295+
if err != nil {
296+
err := fmt.Errorf("failed to unmarshal the before_start_hook output: %w", err)
297+
state.Put("error", err)
298+
ui.Error(err.Error())
299+
return multistep.ActionHalt
300+
}
301+
302+
_, err = client.SetVmConfig(vmRef, vmConfig)
303+
if err != nil {
304+
err := fmt.Errorf("failed to set the VM configuration: %w", err)
305+
state.Put("error", err)
306+
ui.Error(err.Error())
307+
return multistep.ActionHalt
308+
}
309+
}
310+
}
311+
246312
ui.Say("Starting VM")
247313
_, err := client.StartVm(vmRef)
248314
if err != nil {

builder/proxmox/iso/config.hcl2spec.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs-partials/builder/proxmox/common/Config-not-required.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,6 @@
139139
For example `-no-reboot -smbios type=0,vendor=FOO`.
140140
Note: this option is for experts only.
141141

142+
- `before_start_hook` ([]string) - Before Start Hook
143+
142144
<!-- End of code generated from the comments of the Config struct in builder/proxmox/common/config.go; -->
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<!-- Code generated from the comments of the hooks struct in builder/proxmox/common/config.go; DO NOT EDIT MANUALLY -->
2+
3+
- `before_start` ([]string) - Before Start
4+
5+
<!-- End of code generated from the comments of the hooks struct in builder/proxmox/common/config.go; -->

0 commit comments

Comments
 (0)