github.com/yoctocloud/packer@v0.6.2-0.20160520224004-e11a0a18423f/builder/azure/arm/step_deploy_template.go (about)

     1  // Copyright (c) Microsoft Corporation. All rights reserved.
     2  // Licensed under the MIT License. See the LICENSE file in builder/azure for license information.
     3  
     4  package arm
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/mitchellh/multistep"
    10  	"github.com/mitchellh/packer/builder/azure/common"
    11  	"github.com/mitchellh/packer/builder/azure/common/constants"
    12  	"github.com/mitchellh/packer/packer"
    13  )
    14  
    15  type StepDeployTemplate struct {
    16  	client   *AzureClient
    17  	template string
    18  	deploy   func(resourceGroupName string, deploymentName string, templateParameters *TemplateParameters, cancelCh <-chan struct{}) error
    19  	say      func(message string)
    20  	error    func(e error)
    21  }
    22  
    23  func NewStepDeployTemplate(client *AzureClient, ui packer.Ui, template string) *StepDeployTemplate {
    24  	var step = &StepDeployTemplate{
    25  		client:   client,
    26  		template: template,
    27  		say:      func(message string) { ui.Say(message) },
    28  		error:    func(e error) { ui.Error(e.Error()) },
    29  	}
    30  
    31  	step.deploy = step.deployTemplate
    32  	return step
    33  }
    34  
    35  func (s *StepDeployTemplate) deployTemplate(resourceGroupName string, deploymentName string, templateParameters *TemplateParameters, cancelCh <-chan struct{}) error {
    36  	factory := newDeploymentFactory(s.template)
    37  	deployment, err := factory.create(*templateParameters)
    38  	if err != nil {
    39  		return err
    40  	}
    41  
    42  	_, err = s.client.DeploymentsClient.CreateOrUpdate(resourceGroupName, deploymentName, *deployment, cancelCh)
    43  	if err != nil {
    44  		return err
    45  	}
    46  
    47  	poller := NewDeploymentPoller(func() (string, error) {
    48  		r, e := s.client.DeploymentsClient.Get(resourceGroupName, deploymentName)
    49  		if r.Properties != nil && r.Properties.ProvisioningState != nil {
    50  			return *r.Properties.ProvisioningState, e
    51  		}
    52  
    53  		return "UNKNOWN", e
    54  	})
    55  
    56  	pollStatus, err := poller.PollAsNeeded()
    57  	if err != nil {
    58  		return err
    59  	}
    60  
    61  	if pollStatus != DeploySucceeded {
    62  		return fmt.Errorf("Deployment failed with a status of '%s'.", pollStatus)
    63  	}
    64  
    65  	return nil
    66  }
    67  
    68  func (s *StepDeployTemplate) Run(state multistep.StateBag) multistep.StepAction {
    69  	s.say("Deploying deployment template ...")
    70  
    71  	var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string)
    72  	var deploymentName = state.Get(constants.ArmDeploymentName).(string)
    73  	var templateParameters = state.Get(constants.ArmTemplateParameters).(*TemplateParameters)
    74  
    75  	s.say(fmt.Sprintf(" -> ResourceGroupName : '%s'", resourceGroupName))
    76  	s.say(fmt.Sprintf(" -> DeploymentName    : '%s'", deploymentName))
    77  
    78  	result := common.StartInterruptibleTask(
    79  		func() bool { return common.IsStateCancelled(state) },
    80  		func(cancelCh <-chan struct{}) error {
    81  			return s.deploy(resourceGroupName, deploymentName, templateParameters, cancelCh)
    82  		},
    83  	)
    84  
    85  	return processInterruptibleResult(result, s.error, state)
    86  }
    87  
    88  func (*StepDeployTemplate) Cleanup(multistep.StateBag) {
    89  }