Add StepAttachDisk

This commit is contained in:
Paul Meyer 2019-05-29 18:00:04 +00:00
parent 4d750ddefa
commit d1f8b8545d
3 changed files with 71 additions and 2 deletions

View File

@ -131,7 +131,11 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
}
steps = append(steps,
//&StepAttachDisk{},
&StepAttachDisk{ // sets 'device' in stateBag
SubscriptionID: info.SubscriptionID,
ResourceGroup: info.ResourceGroupName,
DiskName: osDiskName,
},
&amznchroot.StepPreMountCommands{
Commands: b.config.PreMountCommands,
},

View File

@ -6,7 +6,7 @@ import (
"testing"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-03-01/compute"
"github.com/hashicorp/packer/builder/azure/chroot/client"
"github.com/hashicorp/packer/builder/azure/common/client"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

View File

@ -0,0 +1,65 @@
package chroot
import (
"context"
"fmt"
"github.com/hashicorp/packer/builder/azure/common/client"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"log"
"time"
)
var _ multistep.Step = &StepAttachDisk{}
type StepAttachDisk struct {
SubscriptionID, ResourceGroup, DiskName string
}
func (s StepAttachDisk) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
azcli := state.Get("azureclient").(client.AzureClientSet)
ui := state.Get("ui").(packer.Ui)
diskResourceID := fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/disks/%s",
s.SubscriptionID,
s.ResourceGroup,
s.DiskName)
ui.Say(fmt.Sprintf("Attaching disk '%s'", diskResourceID))
da := NewDiskAttacher(azcli)
lun, err := da.AttachDisk(ctx, diskResourceID)
if err != nil {
log.Printf("StepAttachDisk.Run: error: %+v", err)
err := fmt.Errorf(
"error attaching disk '%s': %v", diskResourceID, err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
ui.Say("Disk attached, waiting for device to show up")
ctx, cancel := context.WithTimeout(ctx, time.Minute*3) // in case is not configured correctly
defer cancel()
device, err := da.WaitForDevice(ctx, lun)
ui.Say(fmt.Sprintf("Disk available at %q", device))
state.Put("device", device)
return multistep.ActionContinue
}
func (s StepAttachDisk) Cleanup(state multistep.StateBag) {
azcli := state.Get("azureclient").(client.AzureClientSet)
ui := state.Get("ui").(packer.Ui)
diskResourceID := fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/disks/%s",
s.SubscriptionID,
s.ResourceGroup,
s.DiskName)
ui.Say(fmt.Sprintf("Detaching disk '%s'", diskResourceID))
da := NewDiskAttacher(azcli)
err := da.DetachDisk(context.Background(), diskResourceID)
if err != nil {
ui.Error(fmt.Sprintf("error detaching %q: %v", diskResourceID, err))
}
}