github.com/ttysteale/packer@v0.8.2-0.20150708160520-e5f8ea386ed8/builder/googlecompute/config.go (about)

     1  package googlecompute
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"time"
     7  
     8  	"github.com/mitchellh/packer/common"
     9  	"github.com/mitchellh/packer/common/uuid"
    10  	"github.com/mitchellh/packer/helper/communicator"
    11  	"github.com/mitchellh/packer/helper/config"
    12  	"github.com/mitchellh/packer/packer"
    13  	"github.com/mitchellh/packer/template/interpolate"
    14  )
    15  
    16  // Config is the configuration structure for the GCE builder. It stores
    17  // both the publicly settable state as well as the privately generated
    18  // state of the config object.
    19  type Config struct {
    20  	common.PackerConfig `mapstructure:",squash"`
    21  	Comm                communicator.Config `mapstructure:",squash"`
    22  
    23  	AccountFile string `mapstructure:"account_file"`
    24  	ProjectId   string `mapstructure:"project_id"`
    25  
    26  	DiskName             string            `mapstructure:"disk_name"`
    27  	DiskSizeGb           int64             `mapstructure:"disk_size"`
    28  	ImageName            string            `mapstructure:"image_name"`
    29  	ImageDescription     string            `mapstructure:"image_description"`
    30  	InstanceName         string            `mapstructure:"instance_name"`
    31  	MachineType          string            `mapstructure:"machine_type"`
    32  	Metadata             map[string]string `mapstructure:"metadata"`
    33  	Network              string            `mapstructure:"network"`
    34  	SourceImage          string            `mapstructure:"source_image"`
    35  	SourceImageProjectId string            `mapstructure:"source_image_project_id"`
    36  	RawStateTimeout      string            `mapstructure:"state_timeout"`
    37  	Tags                 []string          `mapstructure:"tags"`
    38  	UseInternalIP        bool              `mapstructure:"use_internal_ip"`
    39  	Zone                 string            `mapstructure:"zone"`
    40  
    41  	account         accountFile
    42  	privateKeyBytes []byte
    43  	stateTimeout    time.Duration
    44  	ctx             interpolate.Context
    45  }
    46  
    47  func NewConfig(raws ...interface{}) (*Config, []string, error) {
    48  	c := new(Config)
    49  	err := config.Decode(c, &config.DecodeOpts{
    50  		Interpolate:        true,
    51  		InterpolateContext: &c.ctx,
    52  		InterpolateFilter: &interpolate.RenderFilter{
    53  			Exclude: []string{
    54  				"run_command",
    55  			},
    56  		},
    57  	}, raws...)
    58  	if err != nil {
    59  		return nil, nil, err
    60  	}
    61  
    62  	// Set defaults.
    63  	if c.Network == "" {
    64  		c.Network = "default"
    65  	}
    66  
    67  	if c.DiskSizeGb == 0 {
    68  		c.DiskSizeGb = 10
    69  	}
    70  
    71  	if c.ImageDescription == "" {
    72  		c.ImageDescription = "Created by Packer"
    73  	}
    74  
    75  	if c.ImageName == "" {
    76  		c.ImageName = "packer-{{timestamp}}"
    77  	}
    78  
    79  	if c.InstanceName == "" {
    80  		c.InstanceName = fmt.Sprintf("packer-%s", uuid.TimeOrderedUUID())
    81  	}
    82  
    83  	if c.DiskName == "" {
    84  		c.DiskName = c.InstanceName
    85  	}
    86  
    87  	if c.MachineType == "" {
    88  		c.MachineType = "n1-standard-1"
    89  	}
    90  
    91  	if c.RawStateTimeout == "" {
    92  		c.RawStateTimeout = "5m"
    93  	}
    94  
    95  	if c.Comm.SSHUsername == "" {
    96  		c.Comm.SSHUsername = "root"
    97  	}
    98  
    99  	var errs *packer.MultiError
   100  	if es := c.Comm.Prepare(&c.ctx); len(es) > 0 {
   101  		errs = packer.MultiErrorAppend(errs, es...)
   102  	}
   103  
   104  	// Process required parameters.
   105  	if c.ProjectId == "" {
   106  		errs = packer.MultiErrorAppend(
   107  			errs, errors.New("a project_id must be specified"))
   108  	}
   109  
   110  	if c.SourceImage == "" {
   111  		errs = packer.MultiErrorAppend(
   112  			errs, errors.New("a source_image must be specified"))
   113  	}
   114  
   115  	if c.Zone == "" {
   116  		errs = packer.MultiErrorAppend(
   117  			errs, errors.New("a zone must be specified"))
   118  	}
   119  
   120  	stateTimeout, err := time.ParseDuration(c.RawStateTimeout)
   121  	if err != nil {
   122  		errs = packer.MultiErrorAppend(
   123  			errs, fmt.Errorf("Failed parsing state_timeout: %s", err))
   124  	}
   125  	c.stateTimeout = stateTimeout
   126  
   127  	if c.AccountFile != "" {
   128  		if err := loadJSON(&c.account, c.AccountFile); err != nil {
   129  			errs = packer.MultiErrorAppend(
   130  				errs, fmt.Errorf("Failed parsing account file: %s", err))
   131  		}
   132  	}
   133  
   134  	// Check for any errors.
   135  	if errs != nil && len(errs.Errors) > 0 {
   136  		return nil, nil, errs
   137  	}
   138  
   139  	return c, nil, nil
   140  }