2017-03-03 03:56:17 -05:00
|
|
|
package ecs
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2017-03-03 03:56:17 -05:00
|
|
|
"fmt"
|
2017-05-25 21:49:35 -04:00
|
|
|
|
2017-03-03 03:56:17 -05:00
|
|
|
"github.com/denverdino/aliyungo/common"
|
|
|
|
"github.com/denverdino/aliyungo/ecs"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-17 09:04:52 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2017-03-03 03:56:17 -05:00
|
|
|
)
|
|
|
|
|
2018-06-15 00:32:53 -04:00
|
|
|
type stepShareAlicloudImage struct {
|
2017-03-03 03:56:17 -05:00
|
|
|
AlicloudImageShareAccounts []string
|
|
|
|
AlicloudImageUNShareAccounts []string
|
|
|
|
RegionId string
|
|
|
|
}
|
|
|
|
|
2018-06-15 00:32:53 -04:00
|
|
|
func (s *stepShareAlicloudImage) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2017-03-03 03:56:17 -05:00
|
|
|
client := state.Get("client").(*ecs.Client)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
alicloudImages := state.Get("alicloudimages").(map[string]string)
|
2017-10-26 08:42:49 -04:00
|
|
|
for copiedRegion, copiedImageId := range alicloudImages {
|
2017-03-03 03:56:17 -05:00
|
|
|
err := client.ModifyImageSharePermission(
|
|
|
|
&ecs.ModifyImageSharePermissionArgs{
|
2017-10-26 08:42:49 -04:00
|
|
|
RegionId: common.Region(copiedRegion),
|
|
|
|
ImageId: copiedImageId,
|
2017-03-03 03:56:17 -05:00
|
|
|
AddAccount: s.AlicloudImageShareAccounts,
|
|
|
|
RemoveAccount: s.AlicloudImageUNShareAccounts,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
2017-05-25 21:49:35 -04:00
|
|
|
ui.Say(fmt.Sprintf("Failed modifying image share permissions: %s", err))
|
2017-03-03 03:56:17 -05:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2018-06-15 00:32:53 -04:00
|
|
|
func (s *stepShareAlicloudImage) Cleanup(state multistep.StateBag) {
|
2017-03-03 03:56:17 -05:00
|
|
|
_, cancelled := state.GetOk(multistep.StateCancelled)
|
|
|
|
_, halted := state.GetOk(multistep.StateHalted)
|
|
|
|
if cancelled || halted {
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
client := state.Get("client").(*ecs.Client)
|
|
|
|
alicloudImages := state.Get("alicloudimages").(map[string]string)
|
2017-05-25 21:49:35 -04:00
|
|
|
ui.Say("Restoring image share permission because cancellations or error...")
|
2017-10-26 08:42:49 -04:00
|
|
|
for copiedRegion, copiedImageId := range alicloudImages {
|
2017-03-03 03:56:17 -05:00
|
|
|
err := client.ModifyImageSharePermission(
|
|
|
|
&ecs.ModifyImageSharePermissionArgs{
|
2017-10-26 08:42:49 -04:00
|
|
|
RegionId: common.Region(copiedRegion),
|
|
|
|
ImageId: copiedImageId,
|
2017-03-03 03:56:17 -05:00
|
|
|
AddAccount: s.AlicloudImageUNShareAccounts,
|
|
|
|
RemoveAccount: s.AlicloudImageShareAccounts,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2017-05-25 21:49:35 -04:00
|
|
|
ui.Say(fmt.Sprintf("Restoring image share permission failed: %s", err))
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|