2016-03-04 05:14:55 -05:00
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
2016-05-06 23:32:18 -04:00
|
|
|
// Licensed under the MIT License. See the LICENSE file in builder/azure for license information.
|
2016-03-04 05:14:55 -05:00
|
|
|
|
|
|
|
package arm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2016-06-30 19:51:52 -04:00
|
|
|
"strings"
|
2016-04-21 19:50:03 -04:00
|
|
|
"time"
|
2016-03-04 05:14:55 -05:00
|
|
|
|
2016-04-21 19:50:03 -04:00
|
|
|
packerAzureCommon "github.com/mitchellh/packer/builder/azure/common"
|
2016-03-04 05:14:55 -05:00
|
|
|
|
|
|
|
"github.com/Azure/go-autorest/autorest/azure"
|
|
|
|
|
2016-04-21 19:50:03 -04:00
|
|
|
"github.com/mitchellh/packer/builder/azure/common/constants"
|
|
|
|
"github.com/mitchellh/packer/builder/azure/common/lin"
|
|
|
|
|
2016-03-04 05:14:55 -05:00
|
|
|
"github.com/mitchellh/multistep"
|
2016-04-29 18:02:28 -04:00
|
|
|
packerCommon "github.com/mitchellh/packer/common"
|
2016-03-04 05:14:55 -05:00
|
|
|
"github.com/mitchellh/packer/helper/communicator"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Builder struct {
|
|
|
|
config *Config
|
|
|
|
stateBag multistep.StateBag
|
|
|
|
runner multistep.Runner
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2016-06-30 19:51:52 -04:00
|
|
|
DefaultNicName = "packerNic"
|
2016-03-04 05:14:55 -05:00
|
|
|
DefaultPublicIPAddressName = "packerPublicIP"
|
2016-04-21 19:50:03 -04:00
|
|
|
DefaultSasBlobContainer = "system/Microsoft.Compute"
|
|
|
|
DefaultSasBlobPermission = "r"
|
|
|
|
DefaultSecretName = "packerKeyVaultSecret"
|
2016-03-04 05:14:55 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
|
|
|
|
c, warnings, errs := newConfig(raws...)
|
|
|
|
if errs != nil {
|
|
|
|
return warnings, errs
|
|
|
|
}
|
|
|
|
|
|
|
|
b.config = c
|
|
|
|
|
|
|
|
b.stateBag = new(multistep.BasicStateBag)
|
2016-04-21 19:50:03 -04:00
|
|
|
b.configureStateBag(b.stateBag)
|
|
|
|
b.setTemplateParameters(b.stateBag)
|
2016-03-04 05:14:55 -05:00
|
|
|
|
|
|
|
return warnings, errs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
|
2016-06-22 19:04:13 -04:00
|
|
|
ui.Say("Running builder ...")
|
|
|
|
|
|
|
|
if err := newConfigRetriever().FillParameters(b.config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-04 05:14:55 -05:00
|
|
|
|
2016-04-29 18:02:28 -04:00
|
|
|
log.Print(":: Configuration")
|
|
|
|
packerAzureCommon.DumpConfig(b.config, func(s string) { log.Print(s) })
|
|
|
|
|
2016-03-04 05:14:55 -05:00
|
|
|
b.stateBag.Put("hook", hook)
|
|
|
|
b.stateBag.Put(constants.Ui, ui)
|
|
|
|
|
2016-04-21 19:50:03 -04:00
|
|
|
spnCloud, spnKeyVault, err := b.getServicePrincipalTokens(ui.Say)
|
2016-03-04 05:14:55 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Message("Creating Azure Resource Manager (ARM) client ...")
|
2016-04-21 19:50:03 -04:00
|
|
|
azureClient, err := NewAzureClient(
|
|
|
|
b.config.SubscriptionID,
|
|
|
|
b.config.ResourceGroupName,
|
|
|
|
b.config.StorageAccount,
|
2016-07-07 20:28:47 -04:00
|
|
|
b.config.cloudEnvironment,
|
2016-04-21 19:50:03 -04:00
|
|
|
spnCloud,
|
|
|
|
spnKeyVault)
|
|
|
|
|
2016-03-04 05:14:55 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-06-30 19:51:52 -04:00
|
|
|
resolver := newResourceResolver(azureClient)
|
|
|
|
if err := resolver.Resolve(b.config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-04-21 19:50:03 -04:00
|
|
|
b.config.storageAccountBlobEndpoint, err = b.getBlobEndpoint(azureClient, b.config.ResourceGroupName, b.config.StorageAccount)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-06-30 19:51:52 -04:00
|
|
|
endpointConnectType := PublicEndpoint
|
|
|
|
if b.isPrivateNetworkCommunication() {
|
|
|
|
endpointConnectType = PrivateEndpoint
|
|
|
|
}
|
|
|
|
|
2016-04-21 19:50:03 -04:00
|
|
|
b.setTemplateParameters(b.stateBag)
|
|
|
|
var steps []multistep.Step
|
|
|
|
|
2016-10-12 20:54:59 -04:00
|
|
|
if b.config.OSType == constants.Target_Linux {
|
2016-04-21 19:50:03 -04:00
|
|
|
steps = []multistep.Step{
|
|
|
|
NewStepCreateResourceGroup(azureClient, ui),
|
2016-05-21 02:01:16 -04:00
|
|
|
NewStepValidateTemplate(azureClient, ui, b.config, GetVirtualMachineDeployment),
|
|
|
|
NewStepDeployTemplate(azureClient, ui, b.config, GetVirtualMachineDeployment),
|
2016-06-30 19:51:52 -04:00
|
|
|
NewStepGetIPAddress(azureClient, ui, endpointConnectType),
|
2016-04-21 19:50:03 -04:00
|
|
|
&communicator.StepConnectSSH{
|
|
|
|
Config: &b.config.Comm,
|
|
|
|
Host: lin.SSHHost,
|
|
|
|
SSHConfig: lin.SSHConfig(b.config.UserName),
|
|
|
|
},
|
2016-04-29 18:02:28 -04:00
|
|
|
&packerCommon.StepProvision{},
|
2016-04-21 19:50:03 -04:00
|
|
|
NewStepGetOSDisk(azureClient, ui),
|
|
|
|
NewStepPowerOffCompute(azureClient, ui),
|
|
|
|
NewStepCaptureImage(azureClient, ui),
|
|
|
|
NewStepDeleteResourceGroup(azureClient, ui),
|
|
|
|
NewStepDeleteOSDisk(azureClient, ui),
|
|
|
|
}
|
2016-10-12 20:54:59 -04:00
|
|
|
} else if b.config.OSType == constants.Target_Windows {
|
2016-04-21 19:50:03 -04:00
|
|
|
steps = []multistep.Step{
|
|
|
|
NewStepCreateResourceGroup(azureClient, ui),
|
2016-05-21 02:01:16 -04:00
|
|
|
NewStepValidateTemplate(azureClient, ui, b.config, GetKeyVaultDeployment),
|
|
|
|
NewStepDeployTemplate(azureClient, ui, b.config, GetKeyVaultDeployment),
|
2016-04-21 19:50:03 -04:00
|
|
|
NewStepGetCertificate(azureClient, ui),
|
|
|
|
NewStepSetCertificate(b.config, ui),
|
2016-05-21 02:01:16 -04:00
|
|
|
NewStepValidateTemplate(azureClient, ui, b.config, GetVirtualMachineDeployment),
|
|
|
|
NewStepDeployTemplate(azureClient, ui, b.config, GetVirtualMachineDeployment),
|
2016-06-30 19:51:52 -04:00
|
|
|
NewStepGetIPAddress(azureClient, ui, endpointConnectType),
|
2016-04-21 19:50:03 -04:00
|
|
|
&communicator.StepConnectWinRM{
|
|
|
|
Config: &b.config.Comm,
|
|
|
|
Host: func(stateBag multistep.StateBag) (string, error) {
|
|
|
|
return stateBag.Get(constants.SSHHost).(string), nil
|
|
|
|
},
|
|
|
|
WinRMConfig: func(multistep.StateBag) (*communicator.WinRMConfig, error) {
|
|
|
|
return &communicator.WinRMConfig{
|
|
|
|
Username: b.config.UserName,
|
|
|
|
Password: b.config.tmpAdminPassword,
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
2016-04-29 18:02:28 -04:00
|
|
|
&packerCommon.StepProvision{},
|
2016-04-21 19:50:03 -04:00
|
|
|
NewStepGetOSDisk(azureClient, ui),
|
|
|
|
NewStepPowerOffCompute(azureClient, ui),
|
|
|
|
NewStepCaptureImage(azureClient, ui),
|
|
|
|
NewStepDeleteResourceGroup(azureClient, ui),
|
|
|
|
NewStepDeleteOSDisk(azureClient, ui),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("Builder does not support the os_type '%s'", b.config.OSType)
|
2016-03-04 05:14:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.PackerDebug {
|
|
|
|
ui.Message(fmt.Sprintf("temp admin user: '%s'", b.config.UserName))
|
|
|
|
ui.Message(fmt.Sprintf("temp admin password: '%s'", b.config.Password))
|
|
|
|
}
|
|
|
|
|
2016-09-13 20:04:18 -04:00
|
|
|
b.runner = packerCommon.NewRunner(steps, b.config.PackerConfig, ui)
|
2016-03-04 05:14:55 -05:00
|
|
|
b.runner.Run(b.stateBag)
|
|
|
|
|
|
|
|
// Report any errors.
|
|
|
|
if rawErr, ok := b.stateBag.GetOk(constants.Error); ok {
|
|
|
|
return nil, rawErr.(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we were interrupted or cancelled, then just exit.
|
|
|
|
if _, ok := b.stateBag.GetOk(multistep.StateCancelled); ok {
|
|
|
|
return nil, errors.New("Build was cancelled.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := b.stateBag.GetOk(multistep.StateHalted); ok {
|
|
|
|
return nil, errors.New("Build was halted.")
|
|
|
|
}
|
|
|
|
|
2016-04-21 19:50:03 -04:00
|
|
|
if template, ok := b.stateBag.GetOk(constants.ArmCaptureTemplate); ok {
|
|
|
|
return NewArtifact(
|
|
|
|
template.(*CaptureTemplate),
|
|
|
|
func(name string) string {
|
|
|
|
month := time.Now().AddDate(0, 1, 0).UTC()
|
|
|
|
sasUrl, _ := azureClient.BlobStorageClient.GetBlobSASURI(DefaultSasBlobContainer, name, month, DefaultSasBlobPermission)
|
|
|
|
return sasUrl
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Artifact{}, nil
|
2016-03-04 05:14:55 -05:00
|
|
|
}
|
|
|
|
|
2016-06-30 19:51:52 -04:00
|
|
|
func (b *Builder) isPrivateNetworkCommunication() bool {
|
|
|
|
return b.config.VirtualNetworkName != ""
|
|
|
|
}
|
|
|
|
|
2016-03-04 05:14:55 -05:00
|
|
|
func (b *Builder) Cancel() {
|
|
|
|
if b.runner != nil {
|
|
|
|
log.Println("Cancelling the step runner...")
|
|
|
|
b.runner.Cancel()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-21 19:50:03 -04:00
|
|
|
func (b *Builder) getBlobEndpoint(client *AzureClient, resourceGroupName string, storageAccountName string) (string, error) {
|
|
|
|
account, err := client.AccountsClient.GetProperties(resourceGroupName, storageAccountName)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return *account.Properties.PrimaryEndpoints.Blob, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builder) configureStateBag(stateBag multistep.StateBag) {
|
2016-03-04 05:14:55 -05:00
|
|
|
stateBag.Put(constants.AuthorizedKey, b.config.sshAuthorizedKey)
|
|
|
|
stateBag.Put(constants.PrivateKey, b.config.sshPrivateKey)
|
|
|
|
|
2016-07-29 17:17:33 -04:00
|
|
|
stateBag.Put(constants.ArmTags, &b.config.AzureTags)
|
2016-03-04 05:14:55 -05:00
|
|
|
stateBag.Put(constants.ArmComputeName, b.config.tmpComputeName)
|
|
|
|
stateBag.Put(constants.ArmDeploymentName, b.config.tmpDeploymentName)
|
2016-04-21 19:50:03 -04:00
|
|
|
stateBag.Put(constants.ArmKeyVaultName, b.config.tmpKeyVaultName)
|
2016-03-04 05:14:55 -05:00
|
|
|
stateBag.Put(constants.ArmLocation, b.config.Location)
|
2016-06-30 19:51:52 -04:00
|
|
|
stateBag.Put(constants.ArmNicName, DefaultNicName)
|
2016-04-21 19:50:03 -04:00
|
|
|
stateBag.Put(constants.ArmPublicIPAddressName, DefaultPublicIPAddressName)
|
2016-03-04 05:14:55 -05:00
|
|
|
stateBag.Put(constants.ArmResourceGroupName, b.config.tmpResourceGroupName)
|
2016-04-21 19:50:03 -04:00
|
|
|
stateBag.Put(constants.ArmStorageAccountName, b.config.StorageAccount)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builder) setTemplateParameters(stateBag multistep.StateBag) {
|
2016-03-04 05:14:55 -05:00
|
|
|
stateBag.Put(constants.ArmVirtualMachineCaptureParameters, b.config.toVirtualMachineCaptureParameters())
|
2016-04-21 19:50:03 -04:00
|
|
|
}
|
2016-03-04 05:14:55 -05:00
|
|
|
|
2016-04-21 19:50:03 -04:00
|
|
|
func (b *Builder) getServicePrincipalTokens(say func(string)) (*azure.ServicePrincipalToken, *azure.ServicePrincipalToken, error) {
|
|
|
|
var servicePrincipalToken *azure.ServicePrincipalToken
|
|
|
|
var servicePrincipalTokenVault *azure.ServicePrincipalToken
|
2016-03-04 05:14:55 -05:00
|
|
|
|
2016-04-21 19:50:03 -04:00
|
|
|
var err error
|
2016-03-04 05:14:55 -05:00
|
|
|
|
2016-04-21 19:50:03 -04:00
|
|
|
if b.config.useDeviceLogin {
|
2016-07-16 01:23:53 -04:00
|
|
|
servicePrincipalToken, err = packerAzureCommon.Authenticate(*b.config.cloudEnvironment, b.config.TenantID, say)
|
2016-04-21 19:50:03 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
auth := NewAuthenticate(*b.config.cloudEnvironment, b.config.ClientID, b.config.ClientSecret, b.config.TenantID)
|
2016-03-04 05:14:55 -05:00
|
|
|
|
2016-04-21 19:50:03 -04:00
|
|
|
servicePrincipalToken, err = auth.getServicePrincipalToken()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2016-07-07 20:28:47 -04:00
|
|
|
servicePrincipalTokenVault, err = auth.getServicePrincipalTokenWithResource(
|
|
|
|
strings.TrimRight(b.config.cloudEnvironment.KeyVaultEndpoint, "/"))
|
|
|
|
|
2016-04-21 19:50:03 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
2016-03-04 05:14:55 -05:00
|
|
|
|
2016-04-21 19:50:03 -04:00
|
|
|
return servicePrincipalToken, servicePrincipalTokenVault, nil
|
2016-03-04 05:14:55 -05:00
|
|
|
}
|