2016-03-04 05:14:55 -05:00
|
|
|
package arm
|
|
|
|
|
2016-04-21 19:50:03 -04:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2016-03-04 05:14:55 -05:00
|
|
|
const (
|
|
|
|
BuilderId = "Azure.ResourceManagement.VMImage"
|
|
|
|
)
|
|
|
|
|
2018-02-23 18:34:13 -05:00
|
|
|
type AdditionalDiskArtifact struct {
|
|
|
|
AdditionalDiskUri string
|
|
|
|
AdditionalDiskUriReadOnlySas string
|
|
|
|
}
|
|
|
|
|
2016-04-21 19:50:03 -04:00
|
|
|
type Artifact struct {
|
2017-06-07 18:01:10 -04:00
|
|
|
// VHD
|
2016-04-21 19:50:03 -04:00
|
|
|
StorageAccountLocation string
|
|
|
|
OSDiskUri string
|
|
|
|
TemplateUri string
|
|
|
|
OSDiskUriReadOnlySas string
|
|
|
|
TemplateUriReadOnlySas string
|
2017-06-07 18:01:10 -04:00
|
|
|
|
|
|
|
// Managed Image
|
|
|
|
ManagedImageResourceGroupName string
|
|
|
|
ManagedImageName string
|
|
|
|
ManagedImageLocation string
|
2018-02-23 18:34:13 -05:00
|
|
|
|
|
|
|
// Additional Disks
|
|
|
|
AdditionalDisks *[]AdditionalDiskArtifact
|
2017-06-07 18:01:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewManagedImageArtifact(resourceGroup, name, location string) (*Artifact, error) {
|
|
|
|
return &Artifact{
|
|
|
|
ManagedImageResourceGroupName: resourceGroup,
|
|
|
|
ManagedImageName: name,
|
|
|
|
ManagedImageLocation: location,
|
|
|
|
}, nil
|
2016-04-21 19:50:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewArtifact(template *CaptureTemplate, getSasUrl func(name string) 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
|
|
|
|
}
|
|
|
|
|
2018-02-23 18:34:13 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-04-21 19:50:03 -04:00
|
|
|
return &Artifact{
|
|
|
|
OSDiskUri: vhdUri.String(),
|
|
|
|
OSDiskUriReadOnlySas: getSasUrl(getStorageUrlPath(vhdUri)),
|
|
|
|
TemplateUri: templateUri.String(),
|
|
|
|
TemplateUriReadOnlySas: getSasUrl(getStorageUrlPath(templateUri)),
|
|
|
|
|
2018-02-23 18:34:13 -05:00
|
|
|
AdditionalDisks: additional_disks,
|
|
|
|
|
2016-04-21 19:50:03 -04:00
|
|
|
StorageAccountLocation: template.Resources[0].Location,
|
|
|
|
}, nil
|
2016-03-04 05:14:55 -05:00
|
|
|
}
|
|
|
|
|
2016-04-21 19:50:03 -04:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2018-03-03 03:53:10 -05:00
|
|
|
func (a *Artifact) isManagedImage() bool {
|
2017-06-07 18:01:10 -04:00
|
|
|
return a.ManagedImageResourceGroupName != ""
|
|
|
|
}
|
|
|
|
|
2016-04-21 19:50:03 -04:00
|
|
|
func (*Artifact) BuilderId() string {
|
2016-03-04 05:14:55 -05:00
|
|
|
return BuilderId
|
|
|
|
}
|
|
|
|
|
2016-04-21 19:50:03 -04:00
|
|
|
func (*Artifact) Files() []string {
|
2016-03-04 05:14:55 -05:00
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
|
2017-06-07 03:12:03 -04:00
|
|
|
func (a *Artifact) Id() string {
|
|
|
|
return a.OSDiskUri
|
2016-03-04 05:14:55 -05:00
|
|
|
}
|
|
|
|
|
2017-06-01 17:11:40 -04:00
|
|
|
func (a *Artifact) State(name string) interface{} {
|
|
|
|
switch name {
|
|
|
|
case "atlas.artifact.metadata":
|
|
|
|
return a.stateAtlasMetadata()
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
2016-03-04 05:14:55 -05:00
|
|
|
}
|
|
|
|
|
2016-04-21 19:50:03 -04:00
|
|
|
func (a *Artifact) String() string {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
buf.WriteString(fmt.Sprintf("%s:\n\n", a.BuilderId()))
|
2018-03-03 03:53:10 -05:00
|
|
|
if a.isManagedImage() {
|
2017-06-07 18:01:10 -04:00
|
|
|
buf.WriteString(fmt.Sprintf("ManagedImageResourceGroupName: %s\n", a.ManagedImageResourceGroupName))
|
|
|
|
buf.WriteString(fmt.Sprintf("ManagedImageName: %s\n", a.ManagedImageName))
|
|
|
|
buf.WriteString(fmt.Sprintf("ManagedImageLocation: %s\n", a.ManagedImageLocation))
|
|
|
|
} 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))
|
2018-02-23 18:34:13 -05:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
2017-06-07 18:01:10 -04:00
|
|
|
}
|
2016-04-21 19:50:03 -04:00
|
|
|
|
|
|
|
return buf.String()
|
2016-03-04 05:14:55 -05:00
|
|
|
}
|
|
|
|
|
2016-04-21 19:50:03 -04:00
|
|
|
func (*Artifact) Destroy() error {
|
2016-03-04 05:14:55 -05:00
|
|
|
return nil
|
|
|
|
}
|
2017-06-01 17:11:40 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|