github.com/kikitux/packer@v0.10.1-0.20160322154024-6237df566f9f/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/constants"
    11  	"github.com/mitchellh/packer/packer"
    12  )
    13  
    14  type StepDeployTemplate struct {
    15  	client *AzureClient
    16  	deploy func(resourceGroupName string, deploymentName string, templateParameters *TemplateParameters) error
    17  	say    func(message string)
    18  	error  func(e error)
    19  }
    20  
    21  func NewStepDeployTemplate(client *AzureClient, ui packer.Ui) *StepDeployTemplate {
    22  	var step = &StepDeployTemplate{
    23  		client: client,
    24  		say:    func(message string) { ui.Say(message) },
    25  		error:  func(e error) { ui.Error(e.Error()) },
    26  	}
    27  
    28  	step.deploy = step.deployTemplate
    29  	return step
    30  }
    31  
    32  func (s *StepDeployTemplate) deployTemplate(resourceGroupName string, deploymentName string, templateParameters *TemplateParameters) error {
    33  	factory := newDeploymentFactory(Linux)
    34  	deployment, err := factory.create(*templateParameters)
    35  	if err != nil {
    36  		return err
    37  	}
    38  
    39  	res, err := s.client.DeploymentsClient.CreateOrUpdate(resourceGroupName, deploymentName, *deployment)
    40  	if err != nil {
    41  		return err
    42  	}
    43  
    44  	s.client.DeploymentsClient.PollAsNeeded(res.Response.Response)
    45  	poller := NewDeploymentPoller(func() (string, error) {
    46  		r, e := s.client.DeploymentsClient.Get(resourceGroupName, deploymentName)
    47  		if r.Properties != nil && r.Properties.ProvisioningState != nil {
    48  			return *r.Properties.ProvisioningState, e
    49  		}
    50  
    51  		return "UNKNOWN", e
    52  	})
    53  
    54  	pollStatus, err := poller.PollAsNeeded()
    55  	if err != nil {
    56  		return err
    57  	}
    58  
    59  	if pollStatus != DeploySucceeded {
    60  		return fmt.Errorf("Deployment failed with a status of '%s'.", pollStatus)
    61  	}
    62  
    63  	return nil
    64  }
    65  
    66  func (s *StepDeployTemplate) Run(state multistep.StateBag) multistep.StepAction {
    67  	s.say("Deploying deployment template ...")
    68  
    69  	var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string)
    70  	var deploymentName = state.Get(constants.ArmDeploymentName).(string)
    71  	var templateParameters = state.Get(constants.ArmTemplateParameters).(*TemplateParameters)
    72  
    73  	s.say(fmt.Sprintf(" -> ResourceGroupName : '%s'", resourceGroupName))
    74  	s.say(fmt.Sprintf(" -> DeploymentName    : '%s'", deploymentName))
    75  
    76  	err := s.deploy(resourceGroupName, deploymentName, templateParameters)
    77  	if err != nil {
    78  		state.Put(constants.Error, err)
    79  		s.error(err)
    80  
    81  		return multistep.ActionHalt
    82  	}
    83  
    84  	return multistep.ActionContinue
    85  }
    86  
    87  func (*StepDeployTemplate) Cleanup(multistep.StateBag) {
    88  }