github.com/raghuse92/packer@v1.3.2/common/bootcommand/config.go (about)

     1  package bootcommand
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"time"
     7  
     8  	"github.com/hashicorp/packer/template/interpolate"
     9  )
    10  
    11  type BootConfig struct {
    12  	RawBootGroupInterval string        `mapstructure:"boot_keygroup_interval"`
    13  	RawBootWait          string        `mapstructure:"boot_wait"`
    14  	BootCommand          []string      `mapstructure:"boot_command"`
    15  	BootGroupInterval    time.Duration ``
    16  	BootWait             time.Duration ``
    17  }
    18  
    19  type VNCConfig struct {
    20  	BootConfig `mapstructure:",squash"`
    21  	DisableVNC bool `mapstructure:"disable_vnc"`
    22  	// time in ms to wait between each key press
    23  	RawBootKeyInterval string        `mapstructure:"boot_key_interval"`
    24  	BootKeyInterval    time.Duration ``
    25  }
    26  
    27  func (c *BootConfig) Prepare(ctx *interpolate.Context) (errs []error) {
    28  	if c.RawBootWait == "" {
    29  		c.RawBootWait = "10s"
    30  	}
    31  
    32  	if c.RawBootWait != "" {
    33  		bw, err := time.ParseDuration(c.RawBootWait)
    34  		if err != nil {
    35  			errs = append(
    36  				errs, fmt.Errorf("Failed parsing boot_wait: %s", err))
    37  		} else {
    38  			c.BootWait = bw
    39  		}
    40  	}
    41  
    42  	if c.RawBootGroupInterval == "" {
    43  		c.RawBootGroupInterval = "0ms"
    44  	}
    45  
    46  	if c.RawBootGroupInterval != "" {
    47  		bgi, err := time.ParseDuration(c.RawBootGroupInterval)
    48  		if err != nil {
    49  			errs = append(
    50  				errs, fmt.Errorf("Failed parsing boot_keygroup_interval: %s", err))
    51  		} else {
    52  			c.BootGroupInterval = bgi
    53  		}
    54  	}
    55  
    56  	if c.BootCommand != nil {
    57  		expSeq, err := GenerateExpressionSequence(c.FlatBootCommand())
    58  		if err != nil {
    59  			errs = append(errs, err)
    60  		} else if vErrs := expSeq.Validate(); vErrs != nil {
    61  			errs = append(errs, vErrs...)
    62  		}
    63  	}
    64  
    65  	return
    66  }
    67  
    68  func (c *BootConfig) FlatBootCommand() string {
    69  	return strings.Join(c.BootCommand, "")
    70  }
    71  
    72  func (c *VNCConfig) Prepare(ctx *interpolate.Context) (errs []error) {
    73  	if len(c.BootCommand) > 0 && c.DisableVNC {
    74  		errs = append(errs,
    75  			fmt.Errorf("A boot command cannot be used when vnc is disabled."))
    76  	}
    77  
    78  	if c.RawBootKeyInterval == "" {
    79  		c.RawBootKeyInterval = "0ms"
    80  	}
    81  
    82  	if c.RawBootKeyInterval != "" {
    83  		bki, err := time.ParseDuration(c.RawBootKeyInterval)
    84  		if err != nil {
    85  			errs = append(
    86  				errs, fmt.Errorf("Failed parsing boot_key_interval: %s", err))
    87  		} else {
    88  			c.BootKeyInterval = bki
    89  		}
    90  	}
    91  
    92  	errs = append(errs, c.BootConfig.Prepare(ctx)...)
    93  	return
    94  }