feature: add run_tags to instance in tencentcloud builder

Instance tags are useful, our customer asks us to support it in packer as
well, to enable them to identify the purpose of the instance, even the
instance runs in a very short time.
This commit is contained in:
ZhiQiang Fan 2019-06-28 23:18:41 +08:00
parent 44d2198a1b
commit 310a40f8fe
5 changed files with 54 additions and 22 deletions

View File

@ -107,6 +107,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
HostName: b.config.HostName, HostName: b.config.HostName,
InternetMaxBandwidthOut: b.config.InternetMaxBandwidthOut, InternetMaxBandwidthOut: b.config.InternetMaxBandwidthOut,
AssociatePublicIpAddress: b.config.AssociatePublicIpAddress, AssociatePublicIpAddress: b.config.AssociatePublicIpAddress,
Tags: b.config.RunTags,
}, },
&communicator.StepConnect{ &communicator.StepConnect{
Config: &b.config.TencentCloudRunConfig.Comm, Config: &b.config.TencentCloudRunConfig.Comm,

View File

@ -11,26 +11,27 @@ import (
) )
type TencentCloudRunConfig struct { type TencentCloudRunConfig struct {
AssociatePublicIpAddress bool `mapstructure:"associate_public_ip_address"` AssociatePublicIpAddress bool `mapstructure:"associate_public_ip_address"`
SourceImageId string `mapstructure:"source_image_id"` SourceImageId string `mapstructure:"source_image_id"`
InstanceType string `mapstructure:"instance_type"` InstanceType string `mapstructure:"instance_type"`
InstanceName string `mapstructure:"instance_name"` InstanceName string `mapstructure:"instance_name"`
DiskType string `mapstructure:"disk_type"` DiskType string `mapstructure:"disk_type"`
DiskSize int64 `mapstructure:"disk_size"` DiskSize int64 `mapstructure:"disk_size"`
VpcId string `mapstructure:"vpc_id"` VpcId string `mapstructure:"vpc_id"`
VpcName string `mapstructure:"vpc_name"` VpcName string `mapstructure:"vpc_name"`
VpcIp string `mapstructure:"vpc_ip"` VpcIp string `mapstructure:"vpc_ip"`
SubnetId string `mapstructure:"subnet_id"` SubnetId string `mapstructure:"subnet_id"`
SubnetName string `mapstructure:"subnet_name"` SubnetName string `mapstructure:"subnet_name"`
CidrBlock string `mapstructure:"cidr_block"` // 10.0.0.0/16(default), 172.16.0.0/12, 192.168.0.0/16 CidrBlock string `mapstructure:"cidr_block"` // 10.0.0.0/16(default), 172.16.0.0/12, 192.168.0.0/16
SubnectCidrBlock string `mapstructure:"subnect_cidr_block"` SubnectCidrBlock string `mapstructure:"subnect_cidr_block"`
InternetChargeType string `mapstructure:"internet_charge_type"` InternetChargeType string `mapstructure:"internet_charge_type"`
InternetMaxBandwidthOut int64 `mapstructure:"internet_max_bandwidth_out"` InternetMaxBandwidthOut int64 `mapstructure:"internet_max_bandwidth_out"`
SecurityGroupId string `mapstructure:"security_group_id"` SecurityGroupId string `mapstructure:"security_group_id"`
SecurityGroupName string `mapstructure:"security_group_name"` SecurityGroupName string `mapstructure:"security_group_name"`
UserData string `mapstructure:"user_data"` UserData string `mapstructure:"user_data"`
UserDataFile string `mapstructure:"user_data_file"` UserDataFile string `mapstructure:"user_data_file"`
HostName string `mapstructure:"host_name"` HostName string `mapstructure:"host_name"`
RunTags map[string]string `mapstructure:"run_tags"`
// Communicator settings // Communicator settings
Comm communicator.Config `mapstructure:",squash"` Comm communicator.Config `mapstructure:",squash"`
@ -120,6 +121,10 @@ func (cf *TencentCloudRunConfig) Prepare(ctx *interpolate.Context) []error {
cf.HostName = cf.InstanceName[:15] cf.HostName = cf.InstanceName[:15]
} }
if cf.RunTags == nil {
cf.RunTags = make(map[string]string)
}
return errs return errs
} }

View File

@ -26,6 +26,7 @@ type stepRunInstance struct {
HostName string HostName string
InternetMaxBandwidthOut int64 InternetMaxBandwidthOut int64
AssociatePublicIpAddress bool AssociatePublicIpAddress bool
Tags map[string]string
} }
func (s *stepRunInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { func (s *stepRunInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
@ -89,6 +90,22 @@ func (s *stepRunInstance) Run(ctx context.Context, state multistep.StateBag) mul
req.ClientToken = &s.InstanceName req.ClientToken = &s.InstanceName
req.HostName = &s.HostName req.HostName = &s.HostName
req.UserData = &userData req.UserData = &userData
var tags []*cvm.Tag
for k, v := range s.Tags {
tags = append(tags, &cvm.Tag{
Key: &k,
Value: &v,
})
}
resourceType := "instance"
if len(tags) > 0 {
req.TagSpecification = []*cvm.TagSpecification{
&cvm.TagSpecification{
ResourceType: &resourceType,
Tags: tags,
},
}
}
resp, err := client.RunInstances(req) resp, err := client.RunInstances(req)
if err != nil { if err != nil {

View File

@ -15,7 +15,10 @@
"image_name": "PackerTest", "image_name": "PackerTest",
"disk_type": "CLOUD_PREMIUM", "disk_type": "CLOUD_PREMIUM",
"packer_debug": true, "packer_debug": true,
"associate_public_ip_address": true "associate_public_ip_address": true,
"run_tags": {
"good": "luck"
}
}], }],
"provisioners": [{ "provisioners": [{
"type": "shell", "type": "shell",

View File

@ -116,6 +116,9 @@ builder.
- `host_name` (string) - host name. - `host_name` (string) - host name.
- `run_tags` (map of strings) - Tags to apply to the instance that is *launched* to create the image.
These tags are *not* applied to the resulting image.
## Basic Example ## Basic Example
Here is a basic example for Tencentcloud. Here is a basic example for Tencentcloud.
@ -137,7 +140,10 @@ Here is a basic example for Tencentcloud.
"ssh_username" : "root", "ssh_username" : "root",
"image_name": "packerTest2", "image_name": "packerTest2",
"packer_debug": true, "packer_debug": true,
"associate_public_ip_address": true "associate_public_ip_address": true,
"run_tags": {
"good": "luck"
}
}], }],
"provisioners": [{ "provisioners": [{
"type": "shell", "type": "shell",