This repository has been archived by the owner on Feb 13, 2021. It is now read-only.
/
step_set_provision_infrastructure.go
87 lines (67 loc) · 2.7 KB
/
step_set_provision_infrastructure.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See the LICENSE file in the project root for license information.
package azure
import (
"fmt"
"github.com/Azure/packer-azure/packer/builder/azure/common/constants"
"github.com/Azure/packer-azure/packer/communicator/azureVmCustomScriptExtension"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"github.com/Azure/azure-sdk-for-go/management"
"github.com/Azure/azure-sdk-for-go/storage"
)
type StepSetProvisionInfrastructure struct {
VmName string
ServiceName string
StorageAccountName string
TempContainerName string
ProvisionTimeoutInMinutes uint
flagTempContainerCreated bool
}
func (s *StepSetProvisionInfrastructure) Run(state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
client := state.Get(constants.RequestManager).(management.Client)
config := state.Get(constants.Config).(*Config)
errorMsg := "Error StepSetProvisionInfrastructure: %s"
ui.Say("Preparing infrastructure for provision...")
// get key for storage account
ui.Message("Looking up storage account...")
//create temporary container
s.flagTempContainerCreated = false
ui.Message("Creating Azure temporary container...")
err := config.storageClient.GetBlobService().CreateContainer(s.TempContainerName, storage.ContainerAccessTypePrivate)
if err != nil {
err := fmt.Errorf(errorMsg, err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
s.flagTempContainerCreated = true
comm := azureVmCustomScriptExtension.New(
azureVmCustomScriptExtension.Config{
ServiceName: s.ServiceName,
VmName: s.VmName,
StorageAccountName: s.StorageAccountName,
StorageAccountKey: config.storageAccountKey,
BlobClient: config.storageClient.GetBlobService(),
ContainerName: s.TempContainerName,
Ui: ui,
ManagementClient: client,
ProvisionTimeoutInMinutes: s.ProvisionTimeoutInMinutes,
})
packerCommunicator := packer.Communicator(comm)
state.Put("communicator", packerCommunicator)
return multistep.ActionContinue
}
func (s *StepSetProvisionInfrastructure) Cleanup(state multistep.StateBag) {
ui := state.Get("ui").(packer.Ui)
config := state.Get(constants.Config).(*Config)
ui.Say("Cleaning Up Infrastructure for provision...")
if s.flagTempContainerCreated {
ui.Message("Removing Azure temporary container...")
err := config.storageClient.GetBlobService().DeleteContainer(s.TempContainerName)
if err != nil {
ui.Message("Error removing temporary container: " + err.Error())
}
}
}