github.com/kimor79/packer@v0.8.7-0.20151221212622-d507b18eb4cf/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  	Preemptible          bool              `mapstructure:"preemptible"`
    35  	SourceImage          string            `mapstructure:"source_image"`
    36  	SourceImageProjectId string            `mapstructure:"source_image_project_id"`
    37  	RawStateTimeout      string            `mapstructure:"state_timeout"`
    38  	Tags                 []string          `mapstructure:"tags"`
    39  	UseInternalIP        bool              `mapstructure:"use_internal_ip"`
    40  	Zone                 string            `mapstructure:"zone"`
    41  
    42  	account         accountFile
    43  	privateKeyBytes []byte
    44  	stateTimeout    time.Duration
    45  	ctx             interpolate.Context
    46  }
    47  
    48  func NewConfig(raws ...interface{}) (*Config, []string, error) {
    49  	c := new(Config)
    50  	err := config.Decode(c, &config.DecodeOpts{
    51  		Interpolate:        true,
    52  		InterpolateContext: &c.ctx,
    53  		InterpolateFilter: &interpolate.RenderFilter{
    54  			Exclude: []string{
    55  				"run_command",
    56  			},
    57  		},
    58  	}, raws...)
    59  	if err != nil {
    60  		return nil, nil, err
    61  	}
    62  
    63  	var errs *packer.MultiError
    64  
    65  	// Set defaults.
    66  	if c.Network == "" {
    67  		c.Network = "default"
    68  	}
    69  
    70  	if c.DiskSizeGb == 0 {
    71  		c.DiskSizeGb = 10
    72  	}
    73  
    74  	if c.ImageDescription == "" {
    75  		c.ImageDescription = "Created by Packer"
    76  	}
    77  
    78  	if c.ImageName == "" {
    79  		img, err := interpolate.Render("packer-{{timestamp}}", nil)
    80  		if err != nil {
    81  			errs = packer.MultiErrorAppend(errs,
    82  				fmt.Errorf("Unable to parse image name: %s ", err))
    83  			c.ImageName = img
    84  		}
    85  	}
    86  
    87  	if c.InstanceName == "" {
    88  		c.InstanceName = fmt.Sprintf("packer-%s", uuid.TimeOrderedUUID())
    89  	}
    90  
    91  	if c.DiskName == "" {
    92  		c.DiskName = c.InstanceName
    93  	}
    94  
    95  	if c.MachineType == "" {
    96  		c.MachineType = "n1-standard-1"
    97  	}
    98  
    99  	if c.RawStateTimeout == "" {
   100  		c.RawStateTimeout = "5m"
   101  	}
   102  
   103  	if c.Comm.SSHUsername == "" {
   104  		c.Comm.SSHUsername = "root"
   105  	}
   106  
   107  	if es := c.Comm.Prepare(&c.ctx); len(es) > 0 {
   108  		errs = packer.MultiErrorAppend(errs, es...)
   109  	}
   110  
   111  	// Process required parameters.
   112  	if c.ProjectId == "" {
   113  		errs = packer.MultiErrorAppend(
   114  			errs, errors.New("a project_id must be specified"))
   115  	}
   116  
   117  	if c.SourceImage == "" {
   118  		errs = packer.MultiErrorAppend(
   119  			errs, errors.New("a source_image must be specified"))
   120  	}
   121  
   122  	if c.Zone == "" {
   123  		errs = packer.MultiErrorAppend(
   124  			errs, errors.New("a zone must be specified"))
   125  	}
   126  
   127  	stateTimeout, err := time.ParseDuration(c.RawStateTimeout)
   128  	if err != nil {
   129  		errs = packer.MultiErrorAppend(
   130  			errs, fmt.Errorf("Failed parsing state_timeout: %s", err))
   131  	}
   132  	c.stateTimeout = stateTimeout
   133  
   134  	if c.AccountFile != "" {
   135  		if err := processAccountFile(&c.account, c.AccountFile); err != nil {
   136  			errs = packer.MultiErrorAppend(errs, err)
   137  		}
   138  	}
   139  
   140  	// Check for any errors.
   141  	if errs != nil && len(errs.Errors) > 0 {
   142  		return nil, nil, errs
   143  	}
   144  
   145  	return c, nil, nil
   146  }