2019-05-31 08:27:41 -04:00
|
|
|
//go:generate struct-markdown
|
2019-10-14 10:43:59 -04:00
|
|
|
//go:generate mapstructure-to-hcl2 -type tencentCloudDataDisk
|
2019-05-31 08:27:41 -04:00
|
|
|
|
2018-12-20 11:09:44 -05:00
|
|
|
package cvm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2019-10-21 01:21:21 -04:00
|
|
|
"strings"
|
2018-12-20 11:09:44 -05:00
|
|
|
|
|
|
|
"github.com/hashicorp/packer/common/uuid"
|
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2019-06-19 05:17:19 -04:00
|
|
|
type tencentCloudDataDisk struct {
|
|
|
|
DiskType string `mapstructure:"disk_type"`
|
|
|
|
DiskSize int64 `mapstructure:"disk_size"`
|
|
|
|
SnapshotId string `mapstructure:"disk_snapshot_id"`
|
|
|
|
}
|
|
|
|
|
2018-12-20 11:09:44 -05:00
|
|
|
type TencentCloudRunConfig struct {
|
2019-05-28 11:50:58 -04:00
|
|
|
// Whether allocate public ip to your cvm.
|
2019-06-06 10:29:25 -04:00
|
|
|
// Default value is false.
|
|
|
|
AssociatePublicIpAddress bool `mapstructure:"associate_public_ip_address" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The base image id of Image you want to create
|
2019-06-06 10:29:25 -04:00
|
|
|
// your customized image from.
|
|
|
|
SourceImageId string `mapstructure:"source_image_id" required:"true"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The instance type your cvm will be launched by.
|
2019-06-06 10:29:25 -04:00
|
|
|
// You should reference Instace Type
|
|
|
|
// for parameter taking.
|
|
|
|
InstanceType string `mapstructure:"instance_type" required:"true"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Instance name.
|
2019-06-06 10:29:25 -04:00
|
|
|
InstanceName string `mapstructure:"instance_name" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Root disk type your cvm will be launched by. you could
|
2019-06-06 10:29:25 -04:00
|
|
|
// reference Disk Type
|
|
|
|
// for parameter taking.
|
|
|
|
DiskType string `mapstructure:"disk_type" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Root disk size your cvm will be launched by. values range(in GB):
|
2019-06-06 10:29:25 -04:00
|
|
|
DiskSize int64 `mapstructure:"disk_size" required:"false"`
|
2019-08-21 06:28:34 -04:00
|
|
|
// Add one or more data disks to the instance before creating the image.
|
|
|
|
// Note that if the source image has data disk snapshots, this argument
|
|
|
|
// will be ignored, and the running instance will use source image data
|
|
|
|
// disk settings, in such case, `disk_type` argument will be used as disk
|
|
|
|
// type for all data disks, and each data disk size will use the origin
|
|
|
|
// value in source image.
|
|
|
|
// The data disks allow for the following argument:
|
|
|
|
// - `disk_type` - Type of the data disk. Valid choices: `CLOUD_BASIC`, `CLOUD_PREMIUM` and `CLOUD_SSD`.
|
|
|
|
// - `disk_size` - Size of the data disk.
|
|
|
|
// - `disk_snapshot_id` - Id of the snapshot for a data disk.
|
|
|
|
DataDisks []tencentCloudDataDisk `mapstructure:"data_disks"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Specify vpc your cvm will be launched by.
|
2019-06-06 10:29:25 -04:00
|
|
|
VpcId string `mapstructure:"vpc_id" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Specify vpc name you will create. if vpc_id is not set, packer will
|
2019-06-06 10:29:25 -04:00
|
|
|
// create a vpc for you named this parameter.
|
|
|
|
VpcName string `mapstructure:"vpc_name" required:"false"`
|
|
|
|
VpcIp string `mapstructure:"vpc_ip"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Specify subnet your cvm will be launched by.
|
2019-06-06 10:29:25 -04:00
|
|
|
SubnetId string `mapstructure:"subnet_id" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Specify subnet name you will create. if subnet_id is not set, packer will
|
2019-06-06 10:29:25 -04:00
|
|
|
// create a subnet for you named this parameter.
|
|
|
|
SubnetName string `mapstructure:"subnet_name" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Specify cider block of the vpc you will create if vpc_id not set
|
2019-06-06 10:29:25 -04:00
|
|
|
CidrBlock string `mapstructure:"cidr_block" required:"false"` // 10.0.0.0/16(default), 172.16.0.0/12, 192.168.0.0/16
|
2019-05-28 11:50:58 -04:00
|
|
|
// Specify cider block of the subnet you will create if
|
2019-06-06 10:29:25 -04:00
|
|
|
// subnet_id not set
|
|
|
|
SubnectCidrBlock string `mapstructure:"subnect_cidr_block" required:"false"`
|
|
|
|
InternetChargeType string `mapstructure:"internet_charge_type"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Max bandwidth out your cvm will be launched by(in MB).
|
2019-06-06 10:29:25 -04:00
|
|
|
// values can be set between 1 ~ 100.
|
|
|
|
InternetMaxBandwidthOut int64 `mapstructure:"internet_max_bandwidth_out" required:"false"`
|
2019-10-21 01:21:21 -04:00
|
|
|
// Specify securitygroup your cvm will be launched by.
|
2019-06-06 10:29:25 -04:00
|
|
|
SecurityGroupId string `mapstructure:"security_group_id" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Specify security name you will create if security_group_id not set.
|
2019-06-06 10:29:25 -04:00
|
|
|
SecurityGroupName string `mapstructure:"security_group_name" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// userdata.
|
2019-06-06 10:29:25 -04:00
|
|
|
UserData string `mapstructure:"user_data" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// userdata file.
|
2019-06-06 10:29:25 -04:00
|
|
|
UserDataFile string `mapstructure:"user_data_file" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// host name.
|
2019-06-06 10:29:25 -04:00
|
|
|
HostName string `mapstructure:"host_name" required:"false"`
|
2019-08-21 06:28:34 -04:00
|
|
|
// Tags to apply to the instance that is *launched* to create the image.
|
|
|
|
// These tags are *not* applied to the resulting image.
|
|
|
|
RunTags map[string]string `mapstructure:"run_tags" required:"false"`
|
2018-12-20 11:09:44 -05:00
|
|
|
|
|
|
|
// Communicator settings
|
|
|
|
Comm communicator.Config `mapstructure:",squash"`
|
|
|
|
SSHPrivateIp bool `mapstructure:"ssh_private_ip"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var ValidCBSType = []string{
|
|
|
|
"LOCAL_BASIC", "LOCAL_SSD", "CLOUD_BASIC", "CLOUD_SSD", "CLOUD_PREMIUM",
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cf *TencentCloudRunConfig) Prepare(ctx *interpolate.Context) []error {
|
2019-10-21 01:21:21 -04:00
|
|
|
packerId := fmt.Sprintf("packer_%s", uuid.TimeOrderedUUID()[:8])
|
2018-12-20 11:09:44 -05:00
|
|
|
if cf.Comm.SSHKeyPairName == "" && cf.Comm.SSHTemporaryKeyPairName == "" &&
|
|
|
|
cf.Comm.SSHPrivateKeyFile == "" && cf.Comm.SSHPassword == "" && cf.Comm.WinRMPassword == "" {
|
|
|
|
//tencentcloud support key pair name length max to 25
|
2019-10-21 01:21:21 -04:00
|
|
|
cf.Comm.SSHTemporaryKeyPairName = packerId
|
2018-12-20 11:09:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
errs := cf.Comm.Prepare(ctx)
|
|
|
|
if cf.SourceImageId == "" {
|
|
|
|
errs = append(errs, errors.New("source_image_id must be specified"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if !CheckResourceIdFormat("img", cf.SourceImageId) {
|
|
|
|
errs = append(errs, errors.New("source_image_id wrong format"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if cf.InstanceType == "" {
|
|
|
|
errs = append(errs, errors.New("instance_type must be specified"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if cf.UserData != "" && cf.UserDataFile != "" {
|
|
|
|
errs = append(errs, errors.New("only one of user_data or user_data_file can be specified"))
|
|
|
|
} else if cf.UserDataFile != "" {
|
|
|
|
if _, err := os.Stat(cf.UserDataFile); err != nil {
|
|
|
|
errs = append(errs, errors.New("user_data_file not exist"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cf.VpcId != "" || cf.CidrBlock != "") && cf.SubnetId == "" && cf.SubnectCidrBlock == "" {
|
|
|
|
errs = append(errs, errors.New("if vpc cidr_block is specified, then "+
|
|
|
|
"subnet_cidr_block must also be specified."))
|
|
|
|
}
|
|
|
|
|
|
|
|
if cf.VpcId == "" {
|
|
|
|
if cf.VpcName == "" {
|
2019-10-21 01:21:21 -04:00
|
|
|
cf.VpcName = packerId
|
2018-12-20 11:09:44 -05:00
|
|
|
}
|
|
|
|
if cf.CidrBlock == "" {
|
|
|
|
cf.CidrBlock = "10.0.0.0/16"
|
|
|
|
}
|
|
|
|
if cf.SubnetId != "" {
|
|
|
|
errs = append(errs, errors.New("can't set subnet_id without set vpc_id"))
|
|
|
|
}
|
|
|
|
}
|
2019-10-21 01:21:21 -04:00
|
|
|
|
2018-12-20 11:09:44 -05:00
|
|
|
if cf.SubnetId == "" {
|
|
|
|
if cf.SubnetName == "" {
|
2019-10-21 01:21:21 -04:00
|
|
|
cf.SubnetName = packerId
|
2018-12-20 11:09:44 -05:00
|
|
|
}
|
|
|
|
if cf.SubnectCidrBlock == "" {
|
|
|
|
cf.SubnectCidrBlock = "10.0.8.0/24"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if cf.SecurityGroupId == "" && cf.SecurityGroupName == "" {
|
2019-10-21 01:21:21 -04:00
|
|
|
cf.SecurityGroupName = packerId
|
2018-12-20 11:09:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if cf.DiskType != "" && !checkDiskType(cf.DiskType) {
|
|
|
|
errs = append(errs, errors.New(fmt.Sprintf("specified disk_type(%s) is invalid", cf.DiskType)))
|
|
|
|
} else if cf.DiskType == "" {
|
|
|
|
cf.DiskType = "CLOUD_BASIC"
|
|
|
|
}
|
|
|
|
|
|
|
|
if cf.DiskSize <= 0 {
|
|
|
|
cf.DiskSize = 50
|
|
|
|
}
|
|
|
|
|
|
|
|
if cf.AssociatePublicIpAddress && cf.InternetMaxBandwidthOut <= 0 {
|
|
|
|
cf.InternetMaxBandwidthOut = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if cf.InstanceName == "" {
|
2019-10-21 01:21:21 -04:00
|
|
|
cf.InstanceName = packerId
|
2018-12-20 11:09:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if cf.HostName == "" {
|
2019-10-21 01:21:21 -04:00
|
|
|
cf.HostName = cf.InstanceName
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(cf.HostName) > 15 {
|
|
|
|
cf.HostName = cf.HostName[:15]
|
2018-12-20 11:09:44 -05:00
|
|
|
}
|
2019-10-21 01:21:21 -04:00
|
|
|
cf.HostName = strings.Replace(cf.HostName, "_", "-", -1)
|
2018-12-20 11:09:44 -05:00
|
|
|
|
2019-06-28 11:18:41 -04:00
|
|
|
if cf.RunTags == nil {
|
|
|
|
cf.RunTags = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
2018-12-20 11:09:44 -05:00
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkDiskType(diskType string) bool {
|
|
|
|
for _, valid := range ValidCBSType {
|
|
|
|
if valid == diskType {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2019-10-21 01:21:21 -04:00
|
|
|
|
2018-12-20 11:09:44 -05:00
|
|
|
return false
|
|
|
|
}
|