Merge pull request #4141 from mitchellh/ebsvoluemartifact
add ebsvolume artifact
This commit is contained in:
commit
aedf4ca465
|
@ -0,0 +1,87 @@
|
||||||
|
package ebsvolume
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/aws/aws-sdk-go/service/ec2"
|
||||||
|
"github.com/mitchellh/packer/packer"
|
||||||
|
)
|
||||||
|
|
||||||
|
// map of region to list of volume IDs
|
||||||
|
type EbsVolumes map[string][]string
|
||||||
|
|
||||||
|
// Artifact is an artifact implementation that contains built AMIs.
|
||||||
|
type Artifact struct {
|
||||||
|
// A map of regions to EBS Volume IDs.
|
||||||
|
Volumes EbsVolumes
|
||||||
|
|
||||||
|
// BuilderId is the unique ID for the builder that created this AMI
|
||||||
|
BuilderIdValue string
|
||||||
|
|
||||||
|
// EC2 connection for performing API stuff.
|
||||||
|
Conn *ec2.EC2
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *Artifact) BuilderId() string {
|
||||||
|
return a.BuilderIdValue
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*Artifact) Files() []string {
|
||||||
|
// We have no files
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// returns a sorted list of region:ID pairs
|
||||||
|
func (a *Artifact) idList() []string {
|
||||||
|
parts := make([]string, 0, len(a.Volumes))
|
||||||
|
for region, volumeIDs := range a.Volumes {
|
||||||
|
for _, volumeID := range volumeIDs {
|
||||||
|
parts = append(parts, fmt.Sprintf("%s:%s", region, volumeID))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Strings(parts)
|
||||||
|
return parts
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *Artifact) Id() string {
|
||||||
|
return strings.Join(a.idList(), ",")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *Artifact) String() string {
|
||||||
|
return fmt.Sprintf("EBS Volumes were created:\n\n%s", strings.Join(a.idList(), "\n"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *Artifact) State(name string) interface{} {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *Artifact) Destroy() error {
|
||||||
|
errors := make([]error, 0)
|
||||||
|
|
||||||
|
for region, volumeIDs := range a.Volumes {
|
||||||
|
for _, volumeID := range volumeIDs {
|
||||||
|
log.Printf("Deregistering Volume ID (%s) from region (%s)", volumeID, region)
|
||||||
|
|
||||||
|
input := &ec2.DeleteVolumeInput{
|
||||||
|
VolumeId: &volumeID,
|
||||||
|
}
|
||||||
|
if _, err := a.Conn.DeleteVolume(input); err != nil {
|
||||||
|
errors = append(errors, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(errors) > 0 {
|
||||||
|
if len(errors) == 1 {
|
||||||
|
return errors[0]
|
||||||
|
} else {
|
||||||
|
return &packer.MultiError{Errors: errors}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
|
@ -5,7 +5,6 @@ package ebsvolume
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/aws/aws-sdk-go/aws/session"
|
"github.com/aws/aws-sdk-go/aws/session"
|
||||||
"github.com/aws/aws-sdk-go/service/ec2"
|
"github.com/aws/aws-sdk-go/service/ec2"
|
||||||
|
@ -19,6 +18,8 @@ import (
|
||||||
"github.com/mitchellh/packer/template/interpolate"
|
"github.com/mitchellh/packer/template/interpolate"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const BuilderId = "mitchellh.amazon.ebsvolume"
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
common.PackerConfig `mapstructure:",squash"`
|
common.PackerConfig `mapstructure:",squash"`
|
||||||
awscommon.AccessConfig `mapstructure:",squash"`
|
awscommon.AccessConfig `mapstructure:",squash"`
|
||||||
|
@ -91,8 +92,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
|
||||||
|
|
||||||
launchBlockDevices := commonBlockDevices(b.config.VolumeMappings)
|
launchBlockDevices := commonBlockDevices(b.config.VolumeMappings)
|
||||||
|
|
||||||
var volumeIds *[]string
|
|
||||||
|
|
||||||
// Build the steps
|
// Build the steps
|
||||||
steps := []multistep.Step{
|
steps := []multistep.Step{
|
||||||
&awscommon.StepSourceAMIInfo{
|
&awscommon.StepSourceAMIInfo{
|
||||||
|
@ -132,7 +131,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
|
||||||
},
|
},
|
||||||
&stepTagEBSVolumes{
|
&stepTagEBSVolumes{
|
||||||
VolumeMapping: b.config.VolumeMappings,
|
VolumeMapping: b.config.VolumeMappings,
|
||||||
VolumeIDs: &volumeIds,
|
|
||||||
},
|
},
|
||||||
&awscommon.StepGetPassword{
|
&awscommon.StepGetPassword{
|
||||||
Debug: b.config.PackerDebug,
|
Debug: b.config.PackerDebug,
|
||||||
|
@ -168,8 +166,14 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
|
||||||
return nil, rawErr.(error)
|
return nil, rawErr.(error)
|
||||||
}
|
}
|
||||||
|
|
||||||
ui.Say(fmt.Sprintf("Created Volumes: %s", strings.Join(*volumeIds, ", ")))
|
// Build the artifact and return it
|
||||||
return nil, nil
|
artifact := &Artifact{
|
||||||
|
Volumes: state.Get("ebsvolumes").(EbsVolumes),
|
||||||
|
BuilderIdValue: BuilderId,
|
||||||
|
Conn: ec2conn,
|
||||||
|
}
|
||||||
|
ui.Say(fmt.Sprintf("Created Volumes: %s", artifact))
|
||||||
|
return artifact, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Builder) Cancel() {
|
func (b *Builder) Cancel() {
|
||||||
|
|
|
@ -11,7 +11,6 @@ import (
|
||||||
|
|
||||||
type stepTagEBSVolumes struct {
|
type stepTagEBSVolumes struct {
|
||||||
VolumeMapping []BlockDevice
|
VolumeMapping []BlockDevice
|
||||||
VolumeIDs **[]string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *stepTagEBSVolumes) Run(state multistep.StateBag) multistep.StepAction {
|
func (s *stepTagEBSVolumes) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
|
@ -19,15 +18,17 @@ func (s *stepTagEBSVolumes) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
instance := state.Get("instance").(*ec2.Instance)
|
instance := state.Get("instance").(*ec2.Instance)
|
||||||
ui := state.Get("ui").(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
|
|
||||||
volumeIds := make([]string, 0, len(s.VolumeMapping))
|
volumes := make(EbsVolumes)
|
||||||
for _, instanceBlockDevices := range instance.BlockDeviceMappings {
|
for _, instanceBlockDevices := range instance.BlockDeviceMappings {
|
||||||
for _, configVolumeMapping := range s.VolumeMapping {
|
for _, configVolumeMapping := range s.VolumeMapping {
|
||||||
if configVolumeMapping.DeviceName == *instanceBlockDevices.DeviceName {
|
if configVolumeMapping.DeviceName == *instanceBlockDevices.DeviceName {
|
||||||
volumeIds = append(volumeIds, *instanceBlockDevices.Ebs.VolumeId)
|
volumes[*ec2conn.Config.Region] = append(
|
||||||
|
volumes[*ec2conn.Config.Region],
|
||||||
|
*instanceBlockDevices.Ebs.VolumeId)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*s.VolumeIDs = &volumeIds
|
state.Put("ebsvolumes", volumes)
|
||||||
|
|
||||||
if len(s.VolumeMapping) > 0 {
|
if len(s.VolumeMapping) > 0 {
|
||||||
ui.Say("Tagging EBS volumes...")
|
ui.Say("Tagging EBS volumes...")
|
||||||
|
|
Loading…
Reference in New Issue