github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/azure/arm/step_deploy_template.go (about)

     1  package arm
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/packer/builder/azure/common"
     7  	"github.com/hashicorp/packer/builder/azure/common/constants"
     8  	"github.com/hashicorp/packer/packer"
     9  	"github.com/mitchellh/multistep"
    10  )
    11  
    12  type StepDeployTemplate struct {
    13  	client  *AzureClient
    14  	deploy  func(resourceGroupName string, deploymentName string, cancelCh <-chan struct{}) error
    15  	say     func(message string)
    16  	error   func(e error)
    17  	config  *Config
    18  	factory templateFactoryFunc
    19  }
    20  
    21  func NewStepDeployTemplate(client *AzureClient, ui packer.Ui, config *Config, factory templateFactoryFunc) *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  		config:  config,
    27  		factory: factory,
    28  	}
    29  
    30  	step.deploy = step.deployTemplate
    31  	return step
    32  }
    33  
    34  func (s *StepDeployTemplate) deployTemplate(resourceGroupName string, deploymentName string, cancelCh <-chan struct{}) error {
    35  	deployment, err := s.factory(s.config)
    36  	if err != nil {
    37  		return err
    38  	}
    39  
    40  	_, errChan := s.client.DeploymentsClient.CreateOrUpdate(resourceGroupName, deploymentName, *deployment, cancelCh)
    41  
    42  	err = <-errChan
    43  	if err != nil {
    44  		s.say(s.client.LastError.Error())
    45  	}
    46  	return err
    47  }
    48  
    49  func (s *StepDeployTemplate) Run(state multistep.StateBag) multistep.StepAction {
    50  	s.say("Deploying deployment template ...")
    51  
    52  	var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string)
    53  	var deploymentName = state.Get(constants.ArmDeploymentName).(string)
    54  
    55  	s.say(fmt.Sprintf(" -> ResourceGroupName : '%s'", resourceGroupName))
    56  	s.say(fmt.Sprintf(" -> DeploymentName    : '%s'", deploymentName))
    57  
    58  	result := common.StartInterruptibleTask(
    59  		func() bool { return common.IsStateCancelled(state) },
    60  		func(cancelCh <-chan struct{}) error {
    61  			return s.deploy(resourceGroupName, deploymentName, cancelCh)
    62  		},
    63  	)
    64  
    65  	return processInterruptibleResult(result, s.error, state)
    66  }
    67  
    68  func (*StepDeployTemplate) Cleanup(multistep.StateBag) {
    69  }