github.com/marksheahan/packer@v0.10.2-0.20160613200515-1acb2d6645a0/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  	deploy  func(resourceGroupName string, deploymentName string, cancelCh <-chan struct{}) error
    18  	say     func(message string)
    19  	error   func(e error)
    20  	config  *Config
    21  	factory templateFactoryFunc
    22  }
    23  
    24  func NewStepDeployTemplate(client *AzureClient, ui packer.Ui, config *Config, factory templateFactoryFunc) *StepDeployTemplate {
    25  	var step = &StepDeployTemplate{
    26  		client:  client,
    27  		say:     func(message string) { ui.Say(message) },
    28  		error:   func(e error) { ui.Error(e.Error()) },
    29  		config:  config,
    30  		factory: factory,
    31  	}
    32  
    33  	step.deploy = step.deployTemplate
    34  	return step
    35  }
    36  
    37  func (s *StepDeployTemplate) deployTemplate(resourceGroupName string, deploymentName string, cancelCh <-chan struct{}) error {
    38  	deployment, err := s.factory(s.config)
    39  	if err != nil {
    40  		return err
    41  	}
    42  
    43  	_, err = s.client.DeploymentsClient.CreateOrUpdate(resourceGroupName, deploymentName, *deployment, cancelCh)
    44  	if err != nil {
    45  		return err
    46  	}
    47  
    48  	poller := NewDeploymentPoller(func() (string, error) {
    49  		r, e := s.client.DeploymentsClient.Get(resourceGroupName, deploymentName)
    50  		if r.Properties != nil && r.Properties.ProvisioningState != nil {
    51  			return *r.Properties.ProvisioningState, e
    52  		}
    53  
    54  		return "UNKNOWN", e
    55  	})
    56  
    57  	pollStatus, err := poller.PollAsNeeded()
    58  	if err != nil {
    59  		return err
    60  	}
    61  
    62  	if pollStatus != DeploySucceeded {
    63  		return fmt.Errorf("Deployment failed with a status of '%s'.", pollStatus)
    64  	}
    65  
    66  	return nil
    67  }
    68  
    69  func (s *StepDeployTemplate) Run(state multistep.StateBag) multistep.StepAction {
    70  	s.say("Deploying deployment template ...")
    71  
    72  	var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string)
    73  	var deploymentName = state.Get(constants.ArmDeploymentName).(string)
    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, cancelCh)
    82  		},
    83  	)
    84  
    85  	return processInterruptibleResult(result, s.error, state)
    86  }
    87  
    88  func (*StepDeployTemplate) Cleanup(multistep.StateBag) {
    89  }