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