github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/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  	return err
    45  }
    46  
    47  func (s *StepDeployTemplate) Run(state multistep.StateBag) multistep.StepAction {
    48  	s.say("Deploying deployment template ...")
    49  
    50  	var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string)
    51  	var deploymentName = state.Get(constants.ArmDeploymentName).(string)
    52  
    53  	s.say(fmt.Sprintf(" -> ResourceGroupName : '%s'", resourceGroupName))
    54  	s.say(fmt.Sprintf(" -> DeploymentName    : '%s'", deploymentName))
    55  
    56  	result := common.StartInterruptibleTask(
    57  		func() bool { return common.IsStateCancelled(state) },
    58  		func(cancelCh <-chan struct{}) error {
    59  			return s.deploy(resourceGroupName, deploymentName, cancelCh)
    60  		},
    61  	)
    62  
    63  	return processInterruptibleResult(result, s.error, state)
    64  }
    65  
    66  func (*StepDeployTemplate) Cleanup(multistep.StateBag) {
    67  }