File: config.go

package info (click to toggle)
packer 1.3.4%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 8,324 kB
  • sloc: python: 619; sh: 557; makefile: 111
file content (94 lines) | stat: -rw-r--r-- 2,251 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package bootcommand

import (
	"fmt"
	"strings"
	"time"

	"github.com/hashicorp/packer/template/interpolate"
)

type BootConfig struct {
	RawBootGroupInterval string        `mapstructure:"boot_keygroup_interval"`
	RawBootWait          string        `mapstructure:"boot_wait"`
	BootCommand          []string      `mapstructure:"boot_command"`
	BootGroupInterval    time.Duration ``
	BootWait             time.Duration ``
}

type VNCConfig struct {
	BootConfig `mapstructure:",squash"`
	DisableVNC bool `mapstructure:"disable_vnc"`
	// time in ms to wait between each key press
	RawBootKeyInterval string        `mapstructure:"boot_key_interval"`
	BootKeyInterval    time.Duration ``
}

func (c *BootConfig) Prepare(ctx *interpolate.Context) (errs []error) {
	if c.RawBootWait == "" {
		c.RawBootWait = "10s"
	}

	if c.RawBootWait != "" {
		bw, err := time.ParseDuration(c.RawBootWait)
		if err != nil {
			errs = append(
				errs, fmt.Errorf("Failed parsing boot_wait: %s", err))
		} else {
			c.BootWait = bw
		}
	}

	if c.RawBootGroupInterval == "" {
		c.RawBootGroupInterval = "0ms"
	}

	if c.RawBootGroupInterval != "" {
		bgi, err := time.ParseDuration(c.RawBootGroupInterval)
		if err != nil {
			errs = append(
				errs, fmt.Errorf("Failed parsing boot_keygroup_interval: %s", err))
		} else {
			c.BootGroupInterval = bgi
		}
	}

	if c.BootCommand != nil {
		expSeq, err := GenerateExpressionSequence(c.FlatBootCommand())
		if err != nil {
			errs = append(errs, err)
		} else if vErrs := expSeq.Validate(); vErrs != nil {
			errs = append(errs, vErrs...)
		}
	}

	return
}

func (c *BootConfig) FlatBootCommand() string {
	return strings.Join(c.BootCommand, "")
}

func (c *VNCConfig) Prepare(ctx *interpolate.Context) (errs []error) {
	if len(c.BootCommand) > 0 && c.DisableVNC {
		errs = append(errs,
			fmt.Errorf("A boot command cannot be used when vnc is disabled."))
	}

	if c.RawBootKeyInterval == "" {
		c.RawBootKeyInterval = "0ms"
	}

	if c.RawBootKeyInterval != "" {
		bki, err := time.ParseDuration(c.RawBootKeyInterval)
		if err != nil {
			errs = append(
				errs, fmt.Errorf("Failed parsing boot_key_interval: %s", err))
		} else {
			c.BootKeyInterval = bki
		}
	}

	errs = append(errs, c.BootConfig.Prepare(ctx)...)
	return
}