This repository has been archived by the owner on Feb 13, 2021. It is now read-only.
/
step_poll_status.go
138 lines (110 loc) · 3.88 KB
/
step_poll_status.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// 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"
"log"
"time"
"github.com/Azure/packer-azure/packer/builder/azure/common/constants"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"github.com/Azure/azure-sdk-for-go/management"
vm "github.com/Azure/azure-sdk-for-go/management/virtualmachine"
)
type StepPollStatus struct {
TmpServiceName string
TmpVmName string
OSType string
}
func (s *StepPollStatus) Run(state multistep.StateBag) multistep.StepAction {
client := state.Get(constants.RequestManager).(management.Client)
vmc := vm.NewClient(client)
ui := state.Get(constants.Ui).(packer.Ui)
errorMsg := "Error polling temporary Azure VM is ready: %s"
ui.Say("Polling till temporary Azure VM is ready...")
if len(s.OSType) == 0 {
err := fmt.Errorf(errorMsg, "'OSType' param is empty")
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
var count uint = 60
var duration time.Duration = 40
sleepTime := time.Second * duration
total := count * uint(duration)
var deployment vm.DeploymentResponse
for count > 0 {
var err error // deployment needs to be accessed outside of this loop, can't use :=
deployment, err = vmc.GetDeployment(s.TmpServiceName, s.TmpVmName)
if err != nil {
err := fmt.Errorf(errorMsg, err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
if len(deployment.RoleInstanceList) > 0 {
powerState := deployment.RoleInstanceList[0].PowerState
instanceStatus := deployment.RoleInstanceList[0].InstanceStatus
if powerState == vm.PowerStateStarted && instanceStatus == vm.InstanceStatusReadyRole {
break
}
if instanceStatus == vm.InstanceStatusFailedStartingRole ||
instanceStatus == vm.InstanceStatusFailedStartingVM ||
instanceStatus == vm.InstanceStatusUnresponsiveRole {
err := fmt.Errorf(errorMsg, "deployment.RoleInstanceList[0].instanceStatus is "+instanceStatus)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
if powerState == vm.PowerStateStopping ||
powerState == vm.PowerStateStopped ||
powerState == vm.PowerStateUnknown {
err := fmt.Errorf(errorMsg, "deployment.RoleInstanceList[0].PowerState is "+powerState)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
}
// powerState_Starting or deployment.RoleInstanceList[0] == 0
log.Println(fmt.Sprintf("Waiting for another %v seconds...", uint(duration)))
time.Sleep(sleepTime)
count--
}
if count == 0 {
err := fmt.Errorf(errorMsg, fmt.Sprintf("time is up (%d seconds)", total))
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
state.Put(constants.VmRunning, 1)
log.Println("s.OSType = " + s.OSType)
if s.OSType == constants.Target_Linux {
endpoints := deployment.RoleInstanceList[0].InstanceEndpoints
if len(endpoints) == 0 {
err := fmt.Errorf(errorMsg, "deployment.RoleInstanceList[0].InstanceEndpoints list is empty")
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
vip := endpoints[0].Vip
state.Put(constants.SSHHost, vip)
ui.Message("VM Endpoint: " + vip)
}
roleList := deployment.RoleList
if len(roleList) == 0 {
err := fmt.Errorf(errorMsg, "deployment.RoleList is empty")
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
diskName := roleList[0].OSVirtualHardDisk.DiskName
ui.Message("VM DiskName: " + diskName)
state.Put(constants.HardDiskName, diskName)
mediaLink := roleList[0].OSVirtualHardDisk.MediaLink
ui.Message("VM MediaLink: " + mediaLink)
state.Put(constants.MediaLink, mediaLink)
return multistep.ActionContinue
}
func (s *StepPollStatus) Cleanup(state multistep.StateBag) {
// nothing to do
}