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 {
err := fmt.Errorf("Error creating image: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
return halt(state, err, "Error creating image")
}
err = client.WaitForImageReady(common.Region(config.AlicloudRegion), imageId, s.WaitSnapshotReadyTimeout)
if err != nil {
err := fmt.Errorf("Timeout waiting for image to be created: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
return halt(state, err, "Timeout waiting for image to be created")
}
images, _, err := client.DescribeImages(&ecs.DescribeImagesArgs{
RegionId: common.Region(config.AlicloudRegion),
ImageId: imageId})
if err != nil {
err := fmt.Errorf("Error querying created image: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
return halt(state, err, "Error querying created imaged")
}
if len(images) == 0 {
err := fmt.Errorf("Unable to find created image: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
return halt(state, err, "Unable to find created image")
}
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("alicloudsnapshots", snapshotIds)
alicloudImages := make(map[string]string)
alicloudImages[config.AlicloudRegion] = images[0].ImageId
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")
}
s.snapshot = &snapshots[0]
state.Put("alicloudsnapshot", snapshotId)
alicloudSnapshots := make(map[string]string)
alicloudSnapshots[config.AlicloudRegion] = snapshotId
state.Put("alicloudsnapshots", alicloudSnapshots)
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)
ui := state.Get("ui").(packer.Ui)
imageId := state.Get("alicloudimage").(string)
snapshotIds := state.Get("alicloudsnapshots").([]string)
if len(s.Tags) == 0 {
return multistep.ActionContinue
}
@ -29,11 +31,22 @@ func (s *stepCreateTags) Run(_ context.Context, state multistep.StateBag) multis
Tag: s.Tags,
})
if err != nil {
err := fmt.Errorf("Error Adding tags to image: %s", err)
state.Put("error", err)
ui.Say(err.Error())
return multistep.ActionHalt
return halt(state, err, "Error Adding tags to image")
}
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
}
func (s *stepCreateTags) Cleanup(state multistep.StateBag) {

View File

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