apply tags to relevant snapshots

This commit is contained in:
bozhi.ch 2018-11-26 16:13:06 +08:00
parent 53bfe7179f
commit 7704ff2f24
4 changed files with 30 additions and 25 deletions

View File

@ -45,38 +45,34 @@ func (s *stepCreateAlicloudImage) Run(_ context.Context, state multistep.StateBa
} }
if err != nil { if err != nil {
err := fmt.Errorf("Error creating image: %s", err) return halt(state, err, "Error creating image")
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
} }
err = client.WaitForImageReady(common.Region(config.AlicloudRegion), imageId, s.WaitSnapshotReadyTimeout) err = client.WaitForImageReady(common.Region(config.AlicloudRegion), imageId, s.WaitSnapshotReadyTimeout)
if err != nil { if err != nil {
err := fmt.Errorf("Timeout waiting for image to be created: %s", err) return halt(state, err, "Timeout waiting for image to be created")
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
} }
images, _, err := client.DescribeImages(&ecs.DescribeImagesArgs{ images, _, err := client.DescribeImages(&ecs.DescribeImagesArgs{
RegionId: common.Region(config.AlicloudRegion), RegionId: common.Region(config.AlicloudRegion),
ImageId: imageId}) ImageId: imageId})
if err != nil { if err != nil {
err := fmt.Errorf("Error querying created image: %s", err) return halt(state, err, "Error querying created imaged")
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
} }
if len(images) == 0 { if len(images) == 0 {
err := fmt.Errorf("Unable to find created image: %s", err) return halt(state, err, "Unable to find created image")
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
} }
s.image = &images[0] s.image = &images[0]
var snapshotIds = []string{}
for _, device := range images[0].DiskDeviceMappings.DiskDeviceMapping {
snapshotIds = append(snapshotIds, device.SnapshotId)
}
state.Put("alicloudimage", imageId) state.Put("alicloudimage", imageId)
state.Put("alicloudsnapshots", snapshotIds)
alicloudImages := make(map[string]string) alicloudImages := make(map[string]string)
alicloudImages[config.AlicloudRegion] = images[0].ImageId alicloudImages[config.AlicloudRegion] = images[0].ImageId
state.Put("alicloudimages", alicloudImages) state.Put("alicloudimages", alicloudImages)

View File

@ -62,11 +62,7 @@ func (s *stepCreateAlicloudSnapshot) Run(_ context.Context, state multistep.Stat
return halt(state, err, "Unable to find created snapshot") return halt(state, err, "Unable to find created snapshot")
} }
s.snapshot = &snapshots[0] s.snapshot = &snapshots[0]
state.Put("alicloudsnapshot", snapshotId) state.Put("alicloudsnapshot", snapshotId)
alicloudSnapshots := make(map[string]string)
alicloudSnapshots[config.AlicloudRegion] = snapshotId
state.Put("alicloudsnapshots", alicloudSnapshots)
return multistep.ActionContinue return multistep.ActionContinue
} }

View File

@ -18,6 +18,8 @@ func (s *stepCreateTags) Run(_ context.Context, state multistep.StateBag) multis
client := state.Get("client").(*ecs.Client) client := state.Get("client").(*ecs.Client)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
imageId := state.Get("alicloudimage").(string) imageId := state.Get("alicloudimage").(string)
snapshotIds := state.Get("alicloudsnapshots").([]string)
if len(s.Tags) == 0 { if len(s.Tags) == 0 {
return multistep.ActionContinue return multistep.ActionContinue
} }
@ -29,11 +31,22 @@ func (s *stepCreateTags) Run(_ context.Context, state multistep.StateBag) multis
Tag: s.Tags, Tag: s.Tags,
}) })
if err != nil { if err != nil {
err := fmt.Errorf("Error Adding tags to image: %s", err) return halt(state, err, "Error Adding tags to image")
state.Put("error", err)
ui.Say(err.Error())
return multistep.ActionHalt
} }
for _, snapshotId := range snapshotIds {
ui.Say(fmt.Sprintf("Adding tags(%s) to snapshot: %s", s.Tags, snapshotId))
err = client.AddTags(&ecs.AddTagsArgs{
ResourceId: snapshotId,
ResourceType: ecs.TagResourceSnapshot,
RegionId: common.Region(config.AlicloudRegion),
Tag: s.Tags,
})
if err != nil {
return halt(state, err, "Error Adding tags to snapshot")
}
}
return multistep.ActionContinue return multistep.ActionContinue
} }
func (s *stepCreateTags) Cleanup(state multistep.StateBag) { func (s *stepCreateTags) Cleanup(state multistep.StateBag) {

View File

@ -258,7 +258,7 @@ builder.
EIP. The default value is false. EIP. The default value is false.
- `tags` (object of key/value strings) - Tags applied to the destination - `tags` (object of key/value strings) - Tags applied to the destination
image. image and relevant snapshots.
## Basic Example ## Basic Example