Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "include_data_disks" option to control whether data disks are included in the image #142

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions .web-docs/components/builder/cvm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,14 @@ a [communicator](/packer/docs/templates/legacy_json_templates/communicator) can
- LOCAL_BASIC: 50
- Other: 50 ~ 1000 (need whitelist if > 50)

- `data_disks` ([]tencentCloudDataDisk) - Add one or more data disks to the instance before creating the image.
- `data_disks` ([]TencentCloudDataDisk) - 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.

- `include_data_disks` (boolean) - Whether to include data disks in the resulting image. Defaults to true.

- `vpc_id` (string) - Specify vpc your cvm will be launched by.

Expand Down
6 changes: 4 additions & 2 deletions builder/tencentcloud/cvm/builder.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 15 additions & 9 deletions builder/tencentcloud/cvm/run_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: MPL-2.0

//go:generate packer-sdc struct-markdown
//go:generate packer-sdc mapstructure-to-hcl2 -type tencentCloudDataDisk
//go:generate packer-sdc mapstructure-to-hcl2 -type TencentCloudDataDisk

package cvm

Expand All @@ -18,9 +18,12 @@ import (
"github.com/pkg/errors"
)

type tencentCloudDataDisk struct {
DiskType string `mapstructure:"disk_type"`
DiskSize int64 `mapstructure:"disk_size"`
type TencentCloudDataDisk struct {
// The size of the data disk.
DiskSize int64 `mapstructure:"disk_size" required:"true"`
// The type of disk to use. See https://www.tencentcloud.com/document/api/213/15753#datadisk for valid values.
DiskType string `mapstructure:"disk_type"`
// The snapshot to use for the data disk.
SnapshotId string `mapstructure:"disk_snapshot_id"`
}

Expand Down Expand Up @@ -56,11 +59,9 @@ type TencentCloudRunConfig struct {
// 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"`
DataDisks []TencentCloudDataDisk `mapstructure:"data_disks"`
// Whether to include data disks in the resulting image. Defaults to true.
IncludeDataDisks config.Trilean `mapstructure:"include_data_disks" required:"false"`
// Specify vpc your cvm will be launched by.
VpcId string `mapstructure:"vpc_id" required:"false"`
// Specify vpc name you will create. if `vpc_id` is not set, Packer will
Expand Down Expand Up @@ -114,6 +115,11 @@ var ValidCBSType = []string{
}

func (cf *TencentCloudRunConfig) Prepare(ctx *interpolate.Context) []error {
// Include data disks by default
if cf.IncludeDataDisks != config.TriFalse {
cf.IncludeDataDisks = config.TriTrue
}

packerId := fmt.Sprintf("packer_%s", uuid.TimeOrderedUUID()[:8])
if cf.Comm.SSHKeyPairName == "" && cf.Comm.SSHTemporaryKeyPairName == "" &&
cf.Comm.SSHPrivateKeyFile == "" && cf.Comm.SSHPassword == "" && cf.Comm.WinRMPassword == "" {
Expand Down
24 changes: 12 additions & 12 deletions builder/tencentcloud/cvm/run_config.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions builder/tencentcloud/cvm/step_create_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package cvm
import (
"context"
"fmt"

"github.com/hashicorp/packer-plugin-sdk/multistep"
cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
)
Expand All @@ -28,12 +27,17 @@ func (s *stepCreateImage) Run(ctx context.Context, state multistep.StateBag) mul
req.ImageDescription = &config.ImageDescription
req.InstanceId = instance.InstanceId

// TODO: We should allow user to specify which data disk should be
// included into created image.
var dataDiskIds []*string
for _, disk := range instance.DataDisks {
dataDiskIds = append(dataDiskIds, disk.DiskId)
// There is no way to correlate instance disk IDs to our own data disk definitions,
// so the best we can do is to either include all disks or include none.
if config.IncludeDataDisks.True() {
for _, disk := range instance.DataDisks {
dataDiskIds = append(dataDiskIds, disk.DiskId)
}
} else {
Message(state, "Not including configured data disks in the resulting image", "")
}

if len(dataDiskIds) > 0 {
req.DataDiskIds = dataDiskIds
}
Expand Down
2 changes: 1 addition & 1 deletion builder/tencentcloud/cvm/step_run_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type stepRunInstance struct {
CamRoleName string
AssociatePublicIpAddress bool
Tags map[string]string
DataDisks []tencentCloudDataDisk
DataDisks []TencentCloudDataDisk
}

func (s *stepRunInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!-- Code generated from the comments of the TencentCloudDataDisk struct in builder/tencentcloud/cvm/run_config.go; DO NOT EDIT MANUALLY -->

- `disk_size` (int64) - The size of the data disk.

<!-- End of code generated from the comments of the TencentCloudDataDisk struct in builder/tencentcloud/cvm/run_config.go; -->
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,14 @@
- LOCAL_BASIC: 50
- Other: 50 ~ 1000 (need whitelist if > 50)

- `data_disks` ([]tencentCloudDataDisk) - Add one or more data disks to the instance before creating the image.
- `data_disks` ([]TencentCloudDataDisk) - 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.

- `include_data_disks` (boolean) - Whether to include data disks in the resulting image. Defaults to true.

- `vpc_id` (string) - Specify vpc your cvm will be launched by.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<!-- Code generated from the comments of the tencentCloudDataDisk struct in builder/tencentcloud/cvm/run_config.go; DO NOT EDIT MANUALLY -->
<!-- Code generated from the comments of the TencentCloudDataDisk struct in builder/tencentcloud/cvm/run_config.go; DO NOT EDIT MANUALLY -->

- `disk_type` (string) - Disk Type
- `disk_type` (string) - The type of disk to use. See https://www.tencentcloud.com/document/api/213/15753#datadisk for valid values.

- `disk_size` (int64) - Disk Size
- `disk_snapshot_id` (string) - The snapshot to use for the data disk.

- `disk_snapshot_id` (string) - Snapshot Id

<!-- End of code generated from the comments of the tencentCloudDataDisk struct in builder/tencentcloud/cvm/run_config.go; -->
<!-- End of code generated from the comments of the TencentCloudDataDisk struct in builder/tencentcloud/cvm/run_config.go; -->