Skip to content

Commit 5332df3

Browse files
committed
support source_image_family
1 parent e1bd239 commit 5332df3

File tree

5 files changed

+82
-4
lines changed

5 files changed

+82
-4
lines changed

builder/tencentcloud/cvm/builder.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,15 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
101101
&stepPreValidate{
102102
b.config.SkipCreateImage,
103103
},
104+
&stepCheckSourceImageFamily{
105+
b.config.SourceImageId,
106+
b.config.SourceImageName,
107+
b.config.SourceImageFamily,
108+
},
104109
&stepCheckSourceImage{
105110
b.config.SourceImageId,
111+
b.config.SourceImageName,
112+
b.config.SourceImageFamily,
106113
},
107114
&stepConfigKeyPair{
108115
Debug: b.config.PackerDebug,

builder/tencentcloud/cvm/run_config.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,17 @@ type TencentCloudRunConfig struct {
2929
// Default value is `false`.
3030
AssociatePublicIpAddress bool `mapstructure:"associate_public_ip_address" required:"false"`
3131
// The base image id of Image you want to create
32+
// You can also specify `source_image_family`. If both `source_image` and `source_image_family` are specified, `source_image` takes precedence.
3233
// your customized image from.
3334
SourceImageId string `mapstructure:"source_image_id" required:"false"`
3435
// The base image name of Image you want to create your
35-
// customized image from.Conflict with SourceImageId.
36+
// customized image from.Conflict with SourceImageId and SourceImageName.
3637
SourceImageName string `mapstructure:"source_image_name" required:"false"`
38+
// The source image family to use to create the new image from.
39+
// The image family always returns its latest image that is not deprecated.
40+
// Conflict with SourceImageId and SourceImageName. It takes effect when SourceImageId and SourceImageName are empty.
41+
// Example value: business-daily-update.
42+
SourceImageFamily string `mapstructure:"image_family" required:"false"`
3743
// Charge type of cvm, values can be `POSTPAID_BY_HOUR` (default) `SPOTPAID`
3844
InstanceChargeType string `mapstructure:"instance_charge_type" required:"false"`
3945
// The instance type your cvm will be launched by.
@@ -133,8 +139,8 @@ func (cf *TencentCloudRunConfig) Prepare(ctx *interpolate.Context) []error {
133139
errs = append(errs, errors.New("zone must be specified"))
134140
}
135141

136-
if cf.SourceImageId == "" && cf.SourceImageName == "" {
137-
errs = append(errs, errors.New("source_image_id or source_image_name must be specified"))
142+
if cf.SourceImageId == "" && cf.SourceImageName == "" && cf.SourceImageFamily == "" {
143+
errs = append(errs, errors.New("source_image_id or source_image_name or source_image_family must be specified"))
138144
}
139145

140146
if cf.SourceImageId != "" && !CheckResourceIdFormat("img", cf.SourceImageId) {

builder/tencentcloud/cvm/step_check_source_image.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,15 @@ import (
1313
)
1414

1515
type stepCheckSourceImage struct {
16-
sourceImageId string
16+
sourceImageId string
17+
sourceImageName string
18+
sourceImageFamily string
1719
}
1820

1921
func (s *stepCheckSourceImage) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
22+
if s.sourceImageId == "" && s.sourceImageName == "" && s.sourceImageFamily != "" {
23+
return multistep.ActionContinue
24+
}
2025
var (
2126
imageNameRegex *regexp.Regexp
2227
err error
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package cvm
5+
6+
import (
7+
"context"
8+
"fmt"
9+
10+
"github.com/hashicorp/packer-plugin-sdk/multistep"
11+
cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
12+
)
13+
14+
type stepCheckSourceImageFamily struct {
15+
sourceImageId string
16+
sourceImageName string
17+
sourceImageFamily string
18+
}
19+
20+
func (s *stepCheckSourceImageFamily) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
21+
if s.sourceImageId != "" || s.sourceImageName != "" {
22+
return multistep.ActionContinue
23+
}
24+
config := state.Get("config").(*Config)
25+
client := state.Get("cvm_client").(*cvm.Client)
26+
27+
Say(state, config.SourceImageFamily, "Try to check the source image and get the latest valid image of the image family")
28+
29+
req := cvm.NewDescribeImageFromFamilyRequest()
30+
req.ImageFamily = &config.InstanceType
31+
32+
var resp *cvm.DescribeImageFromFamilyResponse
33+
err := Retry(ctx, func(ctx context.Context) error {
34+
var err error
35+
resp, err = client.DescribeImageFromFamily(req)
36+
return err
37+
})
38+
if err != nil {
39+
return Halt(state, err, "Failed to get source image info from the image family")
40+
}
41+
42+
image := resp.Response.Image
43+
if image != nil {
44+
if image.ImageId != nil && !*image.ImageDeprecated {
45+
state.Put("source_image", image.ImageId)
46+
Message(state, fmt.Sprintf("Get the latest image from the image family, id: %v", *image.ImageId), "Image found")
47+
return multistep.ActionContinue
48+
}
49+
} else {
50+
return Halt(state, err, "Failed to get source image info from the image family")
51+
}
52+
53+
return Halt(state, fmt.Errorf("No image found under current instance_type(%s) restriction", config.InstanceType), "")
54+
}
55+
56+
func (s *stepCheckSourceImageFamily) Cleanup(bag multistep.StateBag) {}

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,12 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.1175 h1:w0z
314314
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.1175/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
315315
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.1200 h1:n6elge0PuoOtHt67BhlQka5Y7ChPbCtp23zYDFw56V0=
316316
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.1200/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
317+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.1.31 h1:PKa4c2BLYbW5LUOWGNXt20+rV9L8JnLqBXZjnOXsHKQ=
318+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.1.31/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
317319
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.1072 h1:BO6eEqw2CxeP73AnOfkq99mJAPccP+w3exmVuBDMvAc=
318320
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.1072/go.mod h1:Ot4h9BuqIbyOPotq8cIT3vCCMDdn6LXkc37jU2teHnQ=
321+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.1.31 h1:M6v6WE88puzkxap8QgVSHM3u2Pe80E8uwps8U08FOOk=
322+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.1.31/go.mod h1:oMQNF1IsVtrOHdBONFRCWz0T5zqmxzM6JDBPl8ub6EM=
319323
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/organization v1.0.1200 h1:hYOvCfgPKpH2OS6+6ZOT+h21CfduIbGfz7RE+Ey1H14=
320324
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/organization v1.0.1200/go.mod h1:qYzdOsPWtOJ18uQ/4QupBTF98ariELEH4dx93GiBeuY=
321325
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sts v1.0.797 h1:Z9rTZBoR4arEXA9gYLu8AQnMInG1scb+WnlIWczLH2A=

0 commit comments

Comments
 (0)