2018-04-18 17:10:28 -04:00
|
|
|
package bootcommand
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
|
|
|
)
|
|
|
|
|
2018-04-18 17:20:34 -04:00
|
|
|
type BootConfig struct {
|
2018-04-18 17:10:28 -04:00
|
|
|
RawBootWait string `mapstructure:"boot_wait"`
|
|
|
|
BootCommand []string `mapstructure:"boot_command"`
|
|
|
|
|
|
|
|
BootWait time.Duration ``
|
|
|
|
}
|
|
|
|
|
|
|
|
type VNCConfig struct {
|
2018-04-18 17:20:34 -04:00
|
|
|
BootConfig
|
2018-04-18 17:10:28 -04:00
|
|
|
DisableVNC bool `mapstructure:"disable_vnc"`
|
|
|
|
}
|
|
|
|
|
2018-04-18 17:20:34 -04:00
|
|
|
func (c *BootConfig) Prepare(ctx *interpolate.Context) (errs []error) {
|
2018-04-18 17:10:28 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2018-04-18 20:48:35 -04:00
|
|
|
|
|
|
|
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...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-18 17:10:28 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-18 17:20:34 -04:00
|
|
|
func (c *BootConfig) FlatBootCommand() string {
|
2018-04-18 17:10:28 -04:00
|
|
|
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."))
|
|
|
|
}
|
2018-04-18 17:20:34 -04:00
|
|
|
errs = append(errs, c.BootConfig.Prepare(ctx)...)
|
2018-04-18 17:10:28 -04:00
|
|
|
return
|
|
|
|
}
|