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
|
|
|
"errors"
|
|
|
|
"fmt"
|
2017-05-25 21:49:35 -04:00
|
|
|
"time"
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
type stepConfigAlicloudVPC struct {
|
|
|
|
VpcId string
|
|
|
|
CidrBlock string //192.168.0.0/16 or 172.16.0.0/16 (default)
|
|
|
|
VpcName string
|
|
|
|
isCreate bool
|
|
|
|
}
|
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *stepConfigAlicloudVPC) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2018-09-18 09:40:57 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2017-03-03 03:56:17 -05:00
|
|
|
client := state.Get("client").(*ecs.Client)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
if len(s.VpcId) != 0 {
|
|
|
|
vpcs, _, err := client.DescribeVpcs(&ecs.DescribeVpcsArgs{
|
|
|
|
VpcId: s.VpcId,
|
|
|
|
RegionId: common.Region(config.AlicloudRegion),
|
|
|
|
})
|
|
|
|
if err != nil {
|
2017-05-25 21:49:35 -04:00
|
|
|
ui.Say(fmt.Sprintf("Failed querying vpcs: %s", err))
|
2017-03-03 03:56:17 -05:00
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
if len(vpcs) > 0 {
|
|
|
|
vpc := vpcs[0]
|
|
|
|
state.Put("vpcid", vpc.VpcId)
|
|
|
|
s.isCreate = false
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
2017-05-25 21:49:35 -04:00
|
|
|
message := fmt.Sprintf("The specified vpc {%s} doesn't exist.", s.VpcId)
|
2017-03-03 03:56:17 -05:00
|
|
|
state.Put("error", errors.New(message))
|
|
|
|
ui.Say(message)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
|
|
|
|
}
|
2017-06-01 22:33:12 -04:00
|
|
|
ui.Say("Creating vpc")
|
2017-03-03 03:56:17 -05:00
|
|
|
vpc, err := client.CreateVpc(&ecs.CreateVpcArgs{
|
|
|
|
RegionId: common.Region(config.AlicloudRegion),
|
|
|
|
CidrBlock: s.CidrBlock,
|
|
|
|
VpcName: s.VpcName,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
2017-06-01 22:33:12 -04:00
|
|
|
ui.Say(fmt.Sprintf("Failed creating vpc: %s", err))
|
2017-03-03 03:56:17 -05:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
err = client.WaitForVpcAvailable(common.Region(config.AlicloudRegion), vpc.VpcId, ALICLOUD_DEFAULT_SHORT_TIMEOUT)
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
2017-06-01 22:33:12 -04:00
|
|
|
ui.Say(fmt.Sprintf("Failed waiting for vpc to become available: %s", err))
|
2017-03-03 03:56:17 -05:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
state.Put("vpcid", vpc.VpcId)
|
|
|
|
s.isCreate = true
|
|
|
|
s.VpcId = vpc.VpcId
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepConfigAlicloudVPC) Cleanup(state multistep.StateBag) {
|
|
|
|
if !s.isCreate {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
client := state.Get("client").(*ecs.Client)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
message(state, "VPC")
|
2017-07-20 19:44:32 -04:00
|
|
|
timeoutPoint := time.Now().Add(60 * time.Second)
|
2017-03-03 03:56:17 -05:00
|
|
|
for {
|
|
|
|
if err := client.DeleteVpc(s.VpcId); err != nil {
|
|
|
|
e, _ := err.(*common.Error)
|
2017-05-25 22:59:53 -04:00
|
|
|
if (e.Code == "DependencyViolation.Instance" || e.Code == "DependencyViolation.RouteEntry" ||
|
|
|
|
e.Code == "DependencyViolation.VSwitch" ||
|
2018-06-24 00:23:35 -04:00
|
|
|
e.Code == "DependencyViolation.SecurityGroup" ||
|
|
|
|
e.Code == "Forbbiden") && time.Now().Before(timeoutPoint) {
|
2017-03-03 03:56:17 -05:00
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
continue
|
|
|
|
}
|
2017-05-25 21:49:35 -04:00
|
|
|
ui.Error(fmt.Sprintf("Error deleting vpc, it may still be around: %s", err))
|
2017-03-03 03:56:17 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|