Skip to content

Commit 13e4a71

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 13e4a71

File tree

9 files changed

+81
-0
lines changed

9 files changed

+81
-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: 62 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,65 @@ 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+
cmd := exec.Command(c.BeforeStartHook[0], c.BeforeStartHook[1:]...)
273+
cmd.Stdin = bytes.NewReader(cfgDoc)
274+
cmd.Stdout = &stdout
275+
cmd.Stderr = &stderr
276+
err = cmd.Run()
277+
if err != nil {
278+
log.Printf("failed to execute the before_start_hook in VM with ID %d. stderr=%s stdout=%s", vmRef.VmId(), stderr.Bytes(), stdout.Bytes())
279+
err := fmt.Errorf("failed to execute the before_start_hook: %w", err)
280+
state.Put("error", err)
281+
ui.Error(err.Error())
282+
return multistep.ActionHalt
283+
}
284+
newCfgDoc := stdout.Bytes()
285+
286+
log.Printf("executed the before_start_hook in VM with ID %d and returned the config %s", vmRef.VmId(), newCfgDoc)
287+
288+
if bytes.HasPrefix(newCfgDoc, []byte{'{'}) {
289+
var vmConfig map[string]interface{}
290+
err = json.Unmarshal(newCfgDoc, &vmConfig)
291+
if err != nil {
292+
err := fmt.Errorf("failed to unmarshal the before_start_hook output: %w", err)
293+
state.Put("error", err)
294+
ui.Error(err.Error())
295+
return multistep.ActionHalt
296+
}
297+
298+
_, err = client.SetVmConfig(vmRef, vmConfig)
299+
if err != nil {
300+
err := fmt.Errorf("failed to set the VM configuration: %w", err)
301+
state.Put("error", err)
302+
ui.Error(err.Error())
303+
return multistep.ActionHalt
304+
}
305+
}
306+
}
307+
246308
ui.Say("Starting VM")
247309
_, err := client.StartVm(vmRef)
248310
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)