github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/alicloud/ecs/run_config.go (about)

     1  package ecs
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"os"
     7  	"strings"
     8  
     9  	"github.com/hashicorp/packer/common/uuid"
    10  	"github.com/hashicorp/packer/helper/communicator"
    11  	"github.com/hashicorp/packer/template/interpolate"
    12  )
    13  
    14  type RunConfig struct {
    15  	AssociatePublicIpAddress bool   `mapstructure:"associate_public_ip_address"`
    16  	ZoneId                   string `mapstructure:"zone_id"`
    17  	IOOptimized              bool   `mapstructure:"io_optimized"`
    18  	InstanceType             string `mapstructure:"instance_type"`
    19  	Description              string `mapstructure:"description"`
    20  	AlicloudSourceImage      string `mapstructure:"source_image"`
    21  	ForceStopInstance        bool   `mapstructure:"force_stop_instance"`
    22  	SecurityGroupId          string `mapstructure:"security_group_id"`
    23  	SecurityGroupName        string `mapstructure:"security_group_name"`
    24  	UserData                 string `mapstructure:"user_data"`
    25  	UserDataFile             string `mapstructure:"user_data_file"`
    26  	VpcId                    string `mapstructure:"vpc_id"`
    27  	VpcName                  string `mapstructure:"vpc_name"`
    28  	CidrBlock                string `mapstructure:"vpc_cidr_block"`
    29  	VSwitchId                string `mapstructure:"vswitch_id"`
    30  	VSwitchName              string `mapstructure:"vswitch_id"`
    31  	InstanceName             string `mapstructure:"instance_name"`
    32  	InternetChargeType       string `mapstructure:"internet_charge_type"`
    33  	InternetMaxBandwidthOut  int    `mapstructure:"internet_max_bandwidth_out"`
    34  	TemporaryKeyPairName     string `mapstructure:"temporary_key_pair_name"`
    35  
    36  	// Communicator settings
    37  	Comm           communicator.Config `mapstructure:",squash"`
    38  	SSHKeyPairName string              `mapstructure:"ssh_keypair_name"`
    39  	SSHPrivateIp   bool                `mapstructure:"ssh_private_ip"`
    40  }
    41  
    42  func (c *RunConfig) Prepare(ctx *interpolate.Context) []error {
    43  	if c.SSHKeyPairName == "" && c.TemporaryKeyPairName == "" &&
    44  		c.Comm.SSHPrivateKey == "" && c.Comm.SSHPassword == "" && c.Comm.WinRMPassword == "" {
    45  
    46  		c.TemporaryKeyPairName = fmt.Sprintf("packer_%s", uuid.TimeOrderedUUID())
    47  	}
    48  
    49  	// Validation
    50  	errs := c.Comm.Prepare(ctx)
    51  	if c.AlicloudSourceImage == "" {
    52  		errs = append(errs, errors.New("A source_image must be specified"))
    53  	}
    54  
    55  	if strings.TrimSpace(c.AlicloudSourceImage) != c.AlicloudSourceImage {
    56  		errs = append(errs, errors.New("The source_image can't include spaces"))
    57  	}
    58  
    59  	if c.InstanceType == "" {
    60  		errs = append(errs, errors.New("An aliclod_instance_type must be specified"))
    61  	}
    62  
    63  	if c.UserData != "" && c.UserDataFile != "" {
    64  		errs = append(errs, fmt.Errorf("Only one of user_data or user_data_file can be specified."))
    65  	} else if c.UserDataFile != "" {
    66  		if _, err := os.Stat(c.UserDataFile); err != nil {
    67  			errs = append(errs, fmt.Errorf("user_data_file not found: %s", c.UserDataFile))
    68  		}
    69  	}
    70  
    71  	return errs
    72  }