Skip to content

Commit

Permalink
Merge pull request #4 from JenGoldstrich/dtl
Browse files Browse the repository at this point in the history
DTL Builder
  • Loading branch information
JenGoldstrich authored Jul 14, 2023
2 parents 4db57fc + dff3683 commit 911138f
Show file tree
Hide file tree
Showing 22 changed files with 446 additions and 781 deletions.
10 changes: 4 additions & 6 deletions builder/azure/arm/azure_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,19 @@ func NewAzureClient(ctx context.Context, isVHDBuild bool, cloud *environments.En
azureClient.ImagesClient.Client.UserAgent = fmt.Sprintf("%s %s", useragent.String(version.AzurePluginVersion.FormattedVersion()), azureClient.ImagesClient.Client.UserAgent)
azureClient.ImagesClient.Client.PollingDuration = pollingDuration

// Clients that are using the existing SDK/auth logic
azureClient.StorageAccountsClient = hashiStorageAccountsSDK.NewStorageAccountsClientWithBaseURI(*resourceManagerEndpoint)
azureClient.StorageAccountsClient.Client.Authorizer = authWrapper.AutorestAuthorizer(resourceManagerAuthorizer)
azureClient.StorageAccountsClient.Client.RequestInspector = withInspection(maxlen)
azureClient.StorageAccountsClient.Client.ResponseInspector = byConcatDecorators(byInspecting(maxlen), errorCapture(azureClient))
azureClient.StorageAccountsClient.Client.UserAgent = fmt.Sprintf("%s %s", useragent.String(version.AzurePluginVersion.FormattedVersion()), azureClient.StorageAccountsClient.Client.UserAgent)
azureClient.StorageAccountsClient.Client.PollingDuration = pollingDuration

api := environments.AzurePublic().ResourceManager
networkMetaClient, err := hashiNetworkMetaSDK.NewClientWithBaseURI(api, func(c *resourcemanager.Client) {
// TODO Request/Response inpectors for Track 2
networkMetaClient, err := hashiNetworkMetaSDK.NewClientWithBaseURI(cloud.ResourceManager, func(c *resourcemanager.Client) {
c.Client.Authorizer = resourceManagerAuthorizer
c.Client.UserAgent = "some-user-agent"
})

if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -281,7 +281,7 @@ func buildAuthorizer(ctx context.Context, authOpts NewSDKAuthOptions, env enviro
var authConfig auth.Credentials
switch authOpts.AuthType {
case AuthTypeDeviceLogin:
return nil, fmt.Errorf("DeviceLogin is not supported, however you can use the Azure CLI `az login --use-device-code` to use a device code, and then use CLI authentication")
return nil, fmt.Errorf("DeviceLogin is not supported in v2 of the Azure Packer Plugin, however you can use the Azure CLI `az login --use-device-code` to use a device code, and then use CLI authentication")
case AuthTypeAzureCLI:
authConfig = auth.Credentials{
Environment: env,
Expand Down Expand Up @@ -322,7 +322,6 @@ func buildAuthorizer(ctx context.Context, authOpts NewSDKAuthOptions, env enviro
authorizer, err := auth.NewAuthorizerFromCredentials(ctx, authConfig, api)
if err != nil {
return nil, err
//fmt.Errorf("building Resource Manager authorizer from credentials: %+v", err)
}
return authorizer, nil
}
Expand All @@ -339,5 +338,4 @@ func getObjectIdFromToken(token string) (string, error) {
return "", err
}
return claims["oid"].(string), nil

}
87 changes: 0 additions & 87 deletions builder/azure/arm/capture_template.go

This file was deleted.

1 change: 0 additions & 1 deletion builder/azure/arm/step_deploy_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ func deleteResource(ctx context.Context, client *AzureClient, subscriptionId str
switch resourceType {
case "Microsoft.Compute/virtualMachines":
vmID := hashiVMSDK.NewVirtualMachineID(subscriptionId, resourceGroupName, resourceName)
// TODO don't rely on default operations, set hard delete to false
if err := client.VirtualMachinesClient.DeleteThenPoll(ctx, vmID, hashiVMSDK.DefaultDeleteOperationOptions()); err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion builder/azure/common/constants/stateBag.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const (
)

const (
ArmCaptureTemplate string = "arm.CaptureTemplate"
ArmComputeName string = "arm.ComputeName"
ArmImageParameters string = "arm.ImageParameters"
ArmCertificateUrl string = "arm.CertificateUrl"
Expand Down Expand Up @@ -70,4 +69,5 @@ const (
ArmBuildDiskEncryptionSetId string = "arm.ArmBuildDiskEncryptionSetId"
ArmSubscription string = "arm.Subscription"
ArmBuildVMInternalId string = "arm.BuildVMInternalId"
DtlLabName string = "dtl.LabName"
)
150 changes: 22 additions & 128 deletions builder/azure/dtl/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ package dtl
import (
"bytes"
"fmt"
"net/url"
"path"
"strings"
)

const (
Expand All @@ -24,20 +21,16 @@ type Artifact struct {
// OS type: Linux, Windows
OSType string

// VHD
StorageAccountLocation string
OSDiskUri string
TemplateUri string
OSDiskUriReadOnlySas string
TemplateUriReadOnlySas string

// Managed Image
ManagedImageResourceGroupName string
ManagedImageName string
ManagedImageLocation string
ManagedImageId string
ManagedImageOSDiskSnapshotName string
ManagedImageDataDiskSnapshotPrefix string
ManagedImageResourceGroupName string
ManagedImageName string
ManagedImageLocation string
ManagedImageId string

// Shared Image Gallery
// ARM resource id for Shared Image Gallery
ManagedImageSharedImageGalleryId string
SharedImageGalleryLocation string

// Additional Disks
AdditionalDisks *[]AdditionalDiskArtifact
Expand All @@ -53,82 +46,17 @@ func NewManagedImageArtifact(osType, resourceGroup, name, location, id string) (
}, nil
}

func NewArtifact(template *CaptureTemplate, getSasUrl func(name string) string, osType string) (*Artifact, error) {
if template == nil {
return nil, fmt.Errorf("nil capture template")
}

if len(template.Resources) != 1 {
return nil, fmt.Errorf("malformed capture template, expected one resource")
}

vhdUri, err := url.Parse(template.Resources[0].Properties.StorageProfile.OSDisk.Image.Uri)
if err != nil {
return nil, err
}

templateUri, err := storageUriToTemplateUri(vhdUri)
if err != nil {
return nil, err
}

var additional_disks *[]AdditionalDiskArtifact
if template.Resources[0].Properties.StorageProfile.DataDisks != nil {
data_disks := make([]AdditionalDiskArtifact, len(template.Resources[0].Properties.StorageProfile.DataDisks))
for i, additionaldisk := range template.Resources[0].Properties.StorageProfile.DataDisks {
additionalVhdUri, err := url.Parse(additionaldisk.Image.Uri)
if err != nil {
return nil, err
}
data_disks[i].AdditionalDiskUri = additionalVhdUri.String()
data_disks[i].AdditionalDiskUriReadOnlySas = getSasUrl(getStorageUrlPath(additionalVhdUri))
}
additional_disks = &data_disks
}

func NewManagedImageArtifactWithSIGAsDestination(osType, resourceGroup, name, location, id, destinationSharedImageGalleryId string) (*Artifact, error) {
return &Artifact{
OSType: osType,
OSDiskUri: vhdUri.String(),
OSDiskUriReadOnlySas: getSasUrl(getStorageUrlPath(vhdUri)),
TemplateUri: templateUri.String(),
TemplateUriReadOnlySas: getSasUrl(getStorageUrlPath(templateUri)),

AdditionalDisks: additional_disks,

StorageAccountLocation: template.Resources[0].Location,
ManagedImageResourceGroupName: resourceGroup,
ManagedImageName: name,
ManagedImageLocation: location,
ManagedImageId: id,
OSType: osType,
ManagedImageSharedImageGalleryId: destinationSharedImageGalleryId,
}, nil
}

func getStorageUrlPath(u *url.URL) string {
parts := strings.Split(u.Path, "/")
return strings.Join(parts[3:], "/")
}

func storageUriToTemplateUri(su *url.URL) (*url.URL, error) {
// packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd -> 4085bb15-3644-4641-b9cd-f575918640b4
filename := path.Base(su.Path)
parts := strings.Split(filename, ".")

if len(parts) < 3 {
return nil, fmt.Errorf("malformed URL")
}

// packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd -> packer
prefixParts := strings.Split(parts[0], "-")
prefix := strings.Join(prefixParts[:len(prefixParts)-1], "-")

templateFilename := fmt.Sprintf("%s-vmTemplate.%s.json", prefix, parts[1])

// https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd"
// ->
// https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json"
return url.Parse(strings.Replace(su.String(), filename, templateFilename, 1))
}

func (a *Artifact) isManagedImage() bool {
return a.ManagedImageResourceGroupName != ""
}

func (*Artifact) BuilderId() string {
return BuilderId
}
Expand All @@ -138,16 +66,11 @@ func (*Artifact) Files() []string {
}

func (a *Artifact) Id() string {
if a.OSDiskUri != "" {
return a.OSDiskUri
}
return a.ManagedImageId
}

func (a *Artifact) State(name string) interface{} {
switch name {
case "atlas.artifact.metadata":
return a.stateAtlasMetadata()
default:
return nil
}
Expand All @@ -158,45 +81,16 @@ func (a *Artifact) String() string {

buf.WriteString(fmt.Sprintf("%s:\n\n", a.BuilderId()))
buf.WriteString(fmt.Sprintf("OSType: %s\n", a.OSType))
if a.isManagedImage() {
buf.WriteString(fmt.Sprintf("ManagedImageResourceGroupName: %s\n", a.ManagedImageResourceGroupName))
buf.WriteString(fmt.Sprintf("ManagedImageName: %s\n", a.ManagedImageName))
buf.WriteString(fmt.Sprintf("ManagedImageId: %s\n", a.ManagedImageId))
buf.WriteString(fmt.Sprintf("ManagedImageLocation: %s\n", a.ManagedImageLocation))
if a.ManagedImageOSDiskSnapshotName != "" {
buf.WriteString(fmt.Sprintf("ManagedImageOSDiskSnapshotName: %s\n", a.ManagedImageOSDiskSnapshotName))
}
if a.ManagedImageDataDiskSnapshotPrefix != "" {
buf.WriteString(fmt.Sprintf("ManagedImageDataDiskSnapshotPrefix: %s\n", a.ManagedImageDataDiskSnapshotPrefix))
}
} else {
buf.WriteString(fmt.Sprintf("StorageAccountLocation: %s\n", a.StorageAccountLocation))
buf.WriteString(fmt.Sprintf("OSDiskUri: %s\n", a.OSDiskUri))
buf.WriteString(fmt.Sprintf("OSDiskUriReadOnlySas: %s\n", a.OSDiskUriReadOnlySas))
buf.WriteString(fmt.Sprintf("TemplateUri: %s\n", a.TemplateUri))
buf.WriteString(fmt.Sprintf("TemplateUriReadOnlySas: %s\n", a.TemplateUriReadOnlySas))
if a.AdditionalDisks != nil {
for i, additionaldisk := range *a.AdditionalDisks {
buf.WriteString(fmt.Sprintf("AdditionalDiskUri (datadisk-%d): %s\n", i+1, additionaldisk.AdditionalDiskUri))
buf.WriteString(fmt.Sprintf("AdditionalDiskUriReadOnlySas (datadisk-%d): %s\n", i+1, additionaldisk.AdditionalDiskUriReadOnlySas))
}
}
buf.WriteString(fmt.Sprintf("ManagedImageResourceGroupName: %s\n", a.ManagedImageResourceGroupName))
buf.WriteString(fmt.Sprintf("ManagedImageName: %s\n", a.ManagedImageName))
buf.WriteString(fmt.Sprintf("ManagedImageId: %s\n", a.ManagedImageId))
buf.WriteString(fmt.Sprintf("ManagedImageLocation: %s\n", a.ManagedImageLocation))
if a.ManagedImageSharedImageGalleryId != "" {
buf.WriteString(fmt.Sprintf("ManagedImageSharedImageGalleryId: %s\n", a.ManagedImageSharedImageGalleryId))
}

return buf.String()
}

func (*Artifact) Destroy() error {
return nil
}

func (a *Artifact) stateAtlasMetadata() interface{} {
metadata := make(map[string]string)
metadata["StorageAccountLocation"] = a.StorageAccountLocation
metadata["OSDiskUri"] = a.OSDiskUri
metadata["OSDiskUriReadOnlySas"] = a.OSDiskUriReadOnlySas
metadata["TemplateUri"] = a.TemplateUri
metadata["TemplateUriReadOnlySas"] = a.TemplateUriReadOnlySas

return metadata
}
Loading

0 comments on commit 911138f

Please sign in to comment.