2016-03-04 05:14:55 -05:00
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
2016-04-21 19:50:03 -04:00
|
|
|
// Licensed under the MIT License. See the LICENSE file in the project root for license information.
|
2016-03-04 05:14:55 -05:00
|
|
|
|
|
|
|
package arm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2016-04-21 19:50:03 -04:00
|
|
|
"github.com/mitchellh/packer/builder/azure/common"
|
2016-03-10 20:46:22 -05:00
|
|
|
"github.com/mitchellh/packer/builder/azure/common/constants"
|
2016-04-21 19:50:03 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
2016-03-04 05:14:55 -05:00
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
type StepDeployTemplate struct {
|
2016-04-21 19:50:03 -04:00
|
|
|
client *AzureClient
|
|
|
|
template string
|
|
|
|
deploy func(resourceGroupName string, deploymentName string, templateParameters *TemplateParameters, cancelCh <-chan struct{}) error
|
|
|
|
say func(message string)
|
|
|
|
error func(e error)
|
2016-03-04 05:14:55 -05:00
|
|
|
}
|
|
|
|
|
2016-04-21 19:50:03 -04:00
|
|
|
func NewStepDeployTemplate(client *AzureClient, ui packer.Ui, template string) *StepDeployTemplate {
|
2016-03-04 05:14:55 -05:00
|
|
|
var step = &StepDeployTemplate{
|
2016-04-21 19:50:03 -04:00
|
|
|
client: client,
|
|
|
|
template: template,
|
|
|
|
say: func(message string) { ui.Say(message) },
|
|
|
|
error: func(e error) { ui.Error(e.Error()) },
|
2016-03-04 05:14:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
step.deploy = step.deployTemplate
|
|
|
|
return step
|
|
|
|
}
|
|
|
|
|
2016-04-21 19:50:03 -04:00
|
|
|
func (s *StepDeployTemplate) deployTemplate(resourceGroupName string, deploymentName string, templateParameters *TemplateParameters, cancelCh <-chan struct{}) error {
|
|
|
|
factory := newDeploymentFactory(s.template)
|
2016-03-04 05:14:55 -05:00
|
|
|
deployment, err := factory.create(*templateParameters)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-04-21 19:50:03 -04:00
|
|
|
_, err = s.client.DeploymentsClient.CreateOrUpdate(resourceGroupName, deploymentName, *deployment, cancelCh)
|
2016-03-04 05:14:55 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
poller := NewDeploymentPoller(func() (string, error) {
|
|
|
|
r, e := s.client.DeploymentsClient.Get(resourceGroupName, deploymentName)
|
|
|
|
if r.Properties != nil && r.Properties.ProvisioningState != nil {
|
|
|
|
return *r.Properties.ProvisioningState, e
|
|
|
|
}
|
|
|
|
|
|
|
|
return "UNKNOWN", e
|
|
|
|
})
|
|
|
|
|
|
|
|
pollStatus, err := poller.PollAsNeeded()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if pollStatus != DeploySucceeded {
|
|
|
|
return fmt.Errorf("Deployment failed with a status of '%s'.", pollStatus)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepDeployTemplate) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
s.say("Deploying deployment template ...")
|
|
|
|
|
|
|
|
var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string)
|
|
|
|
var deploymentName = state.Get(constants.ArmDeploymentName).(string)
|
|
|
|
var templateParameters = state.Get(constants.ArmTemplateParameters).(*TemplateParameters)
|
|
|
|
|
|
|
|
s.say(fmt.Sprintf(" -> ResourceGroupName : '%s'", resourceGroupName))
|
|
|
|
s.say(fmt.Sprintf(" -> DeploymentName : '%s'", deploymentName))
|
|
|
|
|
2016-04-21 19:50:03 -04:00
|
|
|
result := common.StartInterruptibleTask(
|
|
|
|
func() bool { return common.IsStateCancelled(state) },
|
|
|
|
func(cancelCh <-chan struct{}) error {
|
|
|
|
return s.deploy(resourceGroupName, deploymentName, templateParameters, cancelCh)
|
|
|
|
},
|
|
|
|
)
|
2016-03-04 05:14:55 -05:00
|
|
|
|
2016-04-21 19:50:03 -04:00
|
|
|
return processInterruptibleResult(result, s.error, state)
|
2016-03-04 05:14:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (*StepDeployTemplate) Cleanup(multistep.StateBag) {
|
|
|
|
}
|