Support managed disk for platform images

pull/4943/head
Christopher Boumenot 9 years ago
parent c6ff4aae59
commit 9f4fb56041

@ -63,6 +63,7 @@ func (s *StepCaptureImage) Run(state multistep.StateBag) multistep.StepAction {
s.say("Capturing image ...")
var computeName = state.Get(constants.ArmComputeName).(string)
var location = state.Get(constants.ArmLocation).(string)
var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string)
var vmCaptureParameters = state.Get(constants.ArmVirtualMachineCaptureParameters).(*compute.VirtualMachineCaptureParameters)
var imageParameters = state.Get(constants.ArmImageParameters).(*compute.Image)
@ -72,8 +73,9 @@ func (s *StepCaptureImage) Run(state multistep.StateBag) multistep.StepAction {
var targetManagedImageName = state.Get(constants.ArmTargetManagedImageName).(string)
var targetManagedImageLocation = state.Get(constants.ArmTargetManagedImageLocation).(string)
s.say(fmt.Sprintf(" -> ResourceGroupName : '%s'", resourceGroupName))
s.say(fmt.Sprintf(" -> ComputeName : '%s'", computeName))
s.say(fmt.Sprintf(" -> Compute ResourceGroupName : '%s'", resourceGroupName))
s.say(fmt.Sprintf(" -> Compute Name : '%s'", computeName))
s.say(fmt.Sprintf(" -> Compute Location : '%s'", location))
result := common.StartInterruptibleTask(
func() bool {
@ -86,9 +88,9 @@ func (s *StepCaptureImage) Run(state multistep.StateBag) multistep.StepAction {
}
if isManagedImage {
s.say(fmt.Sprintf(" -> ImageResourceGroupName : '%s'", targetManagedImageResourceGroupName))
s.say(fmt.Sprintf(" -> ImageName : '%s'", targetManagedImageName))
s.say(fmt.Sprintf(" -> Image Location : '%s'", targetManagedImageLocation))
s.say(fmt.Sprintf(" -> Image ResourceGroupName : '%s'", targetManagedImageResourceGroupName))
s.say(fmt.Sprintf(" -> Image Name : '%s'", targetManagedImageName))
s.say(fmt.Sprintf(" -> Image Location : '%s'", targetManagedImageLocation))
return s.captureManagedImage(targetManagedImageResourceGroupName, targetManagedImageName, imageParameters, cancelCh)
} else {
return s.captureVhd(resourceGroupName, computeName, vmCaptureParameters, cancelCh)

@ -125,6 +125,7 @@ func TestStepCaptureImageShouldTakeStepArgumentsFromStateBag(t *testing.T) {
func createTestStateBagStepCaptureImage() multistep.StateBag {
stateBag := new(multistep.BasicStateBag)
stateBag.Put(constants.ArmLocation, "localhost")
stateBag.Put(constants.ArmComputeName, "Unit Test: ComputeName")
stateBag.Put(constants.ArmResourceGroupName, "Unit Test: ResourceGroupName")
stateBag.Put(constants.ArmVirtualMachineCaptureParameters, &compute.VirtualMachineCaptureParameters{})

@ -55,11 +55,11 @@ func (s *StepGetOSDisk) Run(state multistep.StateBag) multistep.StepAction {
var vhdUri string
if vm.StorageProfile.OsDisk.Vhd != nil {
s.say(fmt.Sprintf(" -> OS Disk : '%s'", vhdUri))
vhdUri = *vm.StorageProfile.OsDisk.Vhd.URI
s.say(fmt.Sprintf(" -> OS Disk : '%s'", vhdUri))
} else {
s.say(fmt.Sprintf(" -> Managed OS Disk : '%s'", vhdUri))
vhdUri = *vm.StorageProfile.OsDisk.ManagedDisk.ID
s.say(fmt.Sprintf(" -> Managed OS Disk : '%s'", vhdUri))
}
state.Put(constants.ArmOSDiskVhd, vhdUri)

@ -6,6 +6,7 @@ import (
"github.com/Azure/azure-sdk-for-go/arm/compute"
"github.com/Azure/azure-sdk-for-go/arm/resources/resources"
"fmt"
"github.com/hashicorp/packer/builder/azure/common/constants"
"github.com/hashicorp/packer/builder/azure/common/template"
)
@ -54,7 +55,15 @@ func GetVirtualMachineDeployment(config *Config) (*resources.Deployment, error)
} else if config.ManagedImageName != "" {
builder.SetManagedDiskUrl(config.ManagedImageName, config.managedImageLocation, config.managedImageBlobUri, config.managedImageOSState)
} else if config.TargetManagedImageName != "" && config.ImagePublisher != "" {
builder.SetManagedMarketplaceImage(config.Location, config.ImagePublisher, config.ImageOffer, config.ImageSku, config.ImageVersion)
imageID := fmt.Sprintf("/subscriptions/%s/providers/Microsoft.Compute/locations/%s/publishers/%s/ArtifactTypes/vmimage/offers/%s/skus/%s/versions/%s",
config.SubscriptionID,
config.Location,
config.ImagePublisher,
config.ImageOffer,
config.ImageSku,
config.ImageVersion)
builder.SetManagedMarketplaceImage(config.Location, config.ImagePublisher, config.ImageOffer, config.ImageSku, config.ImageVersion, imageID)
} else {
builder.SetMarketPlaceImage(config.ImagePublisher, config.ImageOffer, config.ImageSku, config.ImageVersion)
}

@ -87,8 +87,7 @@
{
"apiVersion": "[variables('apiVersion')]",
"dependsOn": [
"[concat('Microsoft.Network/networkInterfaces/', variables('nicName'))]",
"[concat('Microsoft.Compute/images/', 'packerManagedDisk')]"
"[concat('Microsoft.Network/networkInterfaces/', variables('nicName'))]"
],
"location": "[variables('location')]",
"name": "[parameters('vmName')]",
@ -123,34 +122,22 @@
}
}
},
"storageProfile": {
"imageReference": {
"id": "[resourceId(resourceGroup().name, 'Microsoft.Compute/images', 'packerManagedDisk')]"
},
"osDisk": {
"caching": "ReadWrite",
"createOption": "FromImage",
"name": "osdisk"
}
}
},
"type": "Microsoft.Compute/virtualMachines"
},
{
"apiVersion": "[variables('managedDiskApiVersion')]",
"location": "ignore",
"name": "packerManagedDisk",
"properties": {
"storageProfile": {
"imageReference": {
"offer": "UbuntuServer",
"publisher": "Canonical",
"sku": "16.04-LTS",
"version": "--version--"
},
"osDisk": {
"caching": "ReadWrite",
"createOption": "fromImage",
"name": "osdisk",
"osType": "Linux"
}
}
},
"type": "Microsoft.Compute/images"
"type": "Microsoft.Compute/virtualMachines"
}
],
"variables": {

@ -136,40 +136,25 @@ func (s *TemplateBuilder) SetManagedDiskUrl(managedDiskImageName, location, blob
return nil
}
func (s *TemplateBuilder) SetManagedMarketplaceImage(location, publisher, offer, sku, version string) error {
func (s *TemplateBuilder) SetManagedMarketplaceImage(location, publisher, offer, sku, version, imageID string) error {
resource, err := s.getResourceByType(resourceVirtualMachine)
if err != nil {
return err
}
managedDiskImageName := "packerManagedDisk"
resourceId := s.toResourceID(resourceManagedDisk, managedDiskImageName)
profile := resource.Properties.StorageProfile
profile.ImageReference = &compute.ImageReference{
ID: to.StringPtr(resourceId),
Publisher: &publisher,
Offer: &offer,
Sku: &sku,
Version: &version,
//ID: &imageID,
}
profile.OsDisk.Name = to.StringPtr("osdisk")
profile.OsDisk.OsType = s.osType
profile.OsDisk.CreateOption = compute.FromImage
profile.OsDisk.Vhd = nil
*resource.DependsOn = append(*resource.DependsOn, fmt.Sprintf("[concat('%s/', '%s')]", resourceManagedDisk, managedDiskImageName))
managedDiskResource := &Resource{
Type: to.StringPtr(resourceManagedDisk),
Name: &managedDiskImageName,
ApiVersion: to.StringPtr(s.toVariable("managedDiskApiVersion")),
Location: to.StringPtr(location),
Properties: &Properties{
StorageProfile: &compute.StorageProfile{
ImageReference: &compute.ImageReference{
Publisher: &publisher,
Offer: &offer,
Sku: &sku,
Version: &version,
},
},
},
}
*s.template.Resources = append(*s.template.Resources, *managedDiskResource)
return nil
}

Loading…
Cancel
Save