-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from chhaj5236/feature/image_without_datadisks
support creating image without data disks
- Loading branch information
Showing
7 changed files
with
179 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package ecs | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/denverdino/aliyungo/common" | ||
"github.com/denverdino/aliyungo/ecs" | ||
"github.com/hashicorp/packer/helper/multistep" | ||
"github.com/hashicorp/packer/packer" | ||
) | ||
|
||
type stepCreateAlicloudSnapshot struct { | ||
snapshot *ecs.SnapshotType | ||
} | ||
|
||
func (s *stepCreateAlicloudSnapshot) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { | ||
config := state.Get("config").(Config) | ||
client := state.Get("client").(*ecs.Client) | ||
ui := state.Get("ui").(packer.Ui) | ||
|
||
instance := state.Get("instance").(*ecs.InstanceAttributesType) | ||
disks, _, err := client.DescribeDisks(&ecs.DescribeDisksArgs{ | ||
RegionId: common.Region(config.AlicloudRegion), | ||
InstanceId: instance.InstanceId, | ||
DiskType: ecs.DiskTypeAllSystem, | ||
}) | ||
|
||
if err != nil { | ||
return halt(state, err, "Error describe disks") | ||
} | ||
if len(disks) == 0 { | ||
return halt(state, err, "Unable to find system disk of instance") | ||
} | ||
|
||
// Create the alicloud snapshot | ||
ui.Say(fmt.Sprintf("Creating snapshot from system disk: %s", disks[0].DiskId)) | ||
|
||
snapshotId, err := client.CreateSnapshot(&ecs.CreateSnapshotArgs{ | ||
DiskId: disks[0].DiskId, | ||
}) | ||
|
||
if err != nil { | ||
return halt(state, err, "Error creating snapshot") | ||
} | ||
|
||
err = client.WaitForSnapShotReady(common.Region(config.AlicloudRegion), | ||
snapshotId, ALICLOUD_DEFAULT_LONG_TIMEOUT) | ||
if err != nil { | ||
return halt(state, err, "Timeout waiting for snapshot to be created") | ||
} | ||
|
||
snapshots, _, err := client.DescribeSnapshots(&ecs.DescribeSnapshotsArgs{ | ||
RegionId: common.Region(config.AlicloudRegion), | ||
SnapshotIds: []string{snapshotId}, | ||
}) | ||
|
||
if err != nil { | ||
return halt(state, err, "Error querying created snapshot") | ||
} | ||
if len(snapshots) == 0 { | ||
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 | ||
} | ||
|
||
func (s *stepCreateAlicloudSnapshot) Cleanup(state multistep.StateBag) { | ||
if s.snapshot == nil { | ||
return | ||
} | ||
_, cancelled := state.GetOk(multistep.StateCancelled) | ||
_, halted := state.GetOk(multistep.StateHalted) | ||
if !cancelled && !halted { | ||
return | ||
} | ||
|
||
client := state.Get("client").(*ecs.Client) | ||
ui := state.Get("ui").(packer.Ui) | ||
|
||
ui.Say("Deleting the snapshot because of cancellation or error...") | ||
if err := client.DeleteSnapshot(s.snapshot.SnapshotId); err != nil { | ||
ui.Error(fmt.Sprintf("Error deleting snapshot, it may still be around: %s", err)) | ||
return | ||
} | ||
} |