github.phpd.cn/hashicorp/packer@v1.3.2/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  	DisableStopInstance      bool   `mapstructure:"disable_stop_instance"`
    23  	SecurityGroupId          string `mapstructure:"security_group_id"`
    24  	SecurityGroupName        string `mapstructure:"security_group_name"`
    25  	UserData                 string `mapstructure:"user_data"`
    26  	UserDataFile             string `mapstructure:"user_data_file"`
    27  	VpcId                    string `mapstructure:"vpc_id"`
    28  	VpcName                  string `mapstructure:"vpc_name"`
    29  	CidrBlock                string `mapstructure:"vpc_cidr_block"`
    30  	VSwitchId                string `mapstructure:"vswitch_id"`
    31  	VSwitchName              string `mapstructure:"vswitch_id"`
    32  	InstanceName             string `mapstructure:"instance_name"`
    33  	InternetChargeType       string `mapstructure:"internet_charge_type"`
    34  	InternetMaxBandwidthOut  int    `mapstructure:"internet_max_bandwidth_out"`
    35  
    36  	// Communicator settings
    37  	Comm         communicator.Config `mapstructure:",squash"`
    38  	SSHPrivateIp bool                `mapstructure:"ssh_private_ip"`
    39  }
    40  
    41  func (c *RunConfig) Prepare(ctx *interpolate.Context) []error {
    42  	if c.Comm.SSHKeyPairName == "" && c.Comm.SSHTemporaryKeyPairName == "" &&
    43  		c.Comm.SSHPrivateKeyFile == "" && c.Comm.SSHPassword == "" && c.Comm.WinRMPassword == "" {
    44  
    45  		c.Comm.SSHTemporaryKeyPairName = fmt.Sprintf("packer_%s", uuid.TimeOrderedUUID())
    46  	}
    47  
    48  	// Validation
    49  	errs := c.Comm.Prepare(ctx)
    50  	if c.AlicloudSourceImage == "" {
    51  		errs = append(errs, errors.New("A source_image must be specified"))
    52  	}
    53  
    54  	if strings.TrimSpace(c.AlicloudSourceImage) != c.AlicloudSourceImage {
    55  		errs = append(errs, errors.New("The source_image can't include spaces"))
    56  	}
    57  
    58  	if c.InstanceType == "" {
    59  		errs = append(errs, errors.New("An alicloud_instance_type must be specified"))
    60  	}
    61  
    62  	if c.UserData != "" && c.UserDataFile != "" {
    63  		errs = append(errs, fmt.Errorf("Only one of user_data or user_data_file can be specified."))
    64  	} else if c.UserDataFile != "" {
    65  		if _, err := os.Stat(c.UserDataFile); err != nil {
    66  			errs = append(errs, fmt.Errorf("user_data_file not found: %s", c.UserDataFile))
    67  		}
    68  	}
    69  
    70  	return errs
    71  }