packer-cn/builder/azure/arm/step_deploy_template.go

70 lines
2.0 KiB
Go
Raw Normal View History

package arm
import (
"fmt"
2017-04-04 16:39:01 -04:00
"github.com/hashicorp/packer/builder/azure/common"
"github.com/hashicorp/packer/builder/azure/common/constants"
"github.com/hashicorp/packer/packer"
"github.com/mitchellh/multistep"
)
type StepDeployTemplate struct {
2016-05-21 02:01:16 -04:00
client *AzureClient
deploy func(resourceGroupName string, deploymentName string, cancelCh <-chan struct{}) error
say func(message string)
error func(e error)
config *Config
factory templateFactoryFunc
}
2016-05-21 02:01:16 -04:00
func NewStepDeployTemplate(client *AzureClient, ui packer.Ui, config *Config, factory templateFactoryFunc) *StepDeployTemplate {
var step = &StepDeployTemplate{
2016-05-21 02:01:16 -04:00
client: client,
say: func(message string) { ui.Say(message) },
error: func(e error) { ui.Error(e.Error()) },
config: config,
factory: factory,
}
step.deploy = step.deployTemplate
return step
}
2016-05-21 02:01:16 -04:00
func (s *StepDeployTemplate) deployTemplate(resourceGroupName string, deploymentName string, cancelCh <-chan struct{}) error {
deployment, err := s.factory(s.config)
if err != nil {
return err
}
_, errChan := s.client.DeploymentsClient.CreateOrUpdate(resourceGroupName, deploymentName, *deployment, cancelCh)
err = <-errChan
2017-06-08 20:57:59 -04:00
if err != nil {
s.say(s.client.LastError.Error())
}
return err
}
func (s *StepDeployTemplate) Run(state multistep.StateBag) multistep.StepAction {
s.say("Deploying deployment template ...")
var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string)
var deploymentName = state.Get(constants.ArmDeploymentName).(string)
s.say(fmt.Sprintf(" -> ResourceGroupName : '%s'", resourceGroupName))
s.say(fmt.Sprintf(" -> DeploymentName : '%s'", deploymentName))
result := common.StartInterruptibleTask(
func() bool { return common.IsStateCancelled(state) },
func(cancelCh <-chan struct{}) error {
2016-05-21 02:01:16 -04:00
return s.deploy(resourceGroupName, deploymentName, cancelCh)
},
)
return processInterruptibleResult(result, s.error, state)
}
func (*StepDeployTemplate) Cleanup(multistep.StateBag) {
}