forked from xenserver/packer-plugin-xenserver
/
step_import_instance.go
182 lines (144 loc) · 4.39 KB
/
step_import_instance.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package xva
import (
"fmt"
"os"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
xscommon "github.com/rdobson/packer-builder-xenserver/builder/xenserver/common"
xsclient "github.com/xenserver/go-xenserver-client"
)
type stepImportInstance struct {
instance *xsclient.VM
vdi *xsclient.VDI
}
func (self *stepImportInstance) Run(state multistep.StateBag) multistep.StepAction {
client := state.Get("client").(xsclient.XenAPIClient)
config := state.Get("config").(config)
ui := state.Get("ui").(packer.Ui)
ui.Say("Step: Import Instance")
// find the SR
sr, err := config.GetSR(client)
if err != nil {
ui.Error(fmt.Sprintf("Unable to get SR: %s", err.Error()))
return multistep.ActionHalt
}
// Open the file for reading (NB: httpUpload closes the file for us)
fh, err := os.Open(config.SourcePath)
if err != nil {
ui.Error(fmt.Sprintf("Unable to open XVA '%s': %s", config.SourcePath, err.Error()))
return multistep.ActionHalt
}
result, err := xscommon.HTTPUpload(fmt.Sprintf("https://%s/import?session_id=%s&sr_id=%s",
client.Host,
client.Session.(string),
sr.Ref,
), fh, state)
if err != nil {
ui.Error(fmt.Sprintf("Unable to upload VDI: %s", err.Error()))
return multistep.ActionHalt
}
if result == nil {
ui.Error("XAPI did not reply with an instance reference")
return multistep.ActionHalt
}
instance := xsclient.VM(*result)
/*
err = instance.SetStaticMemoryRange(config.VMMemory*1024*1024, config.VMMemory*1024*1024)
if err != nil {
ui.Error(fmt.Sprintf("Error setting VM memory=%d: %s", config.VMMemory*1024*1024, err.Error()))
return multistep.ActionHalt
}
instance.SetPlatform(config.PlatformArgs)
if err != nil {
ui.Error(fmt.Sprintf("Error setting VM platform: %s", err.Error()))
return multistep.ActionHalt
}
// Connect Network
var network *xscommon.Network
if config.NetworkName == "" {
// No network has be specified. Use the management interface
network = new(xscommon.Network)
network.Ref = ""
network.Client = &client
pifs, err := client.GetPIFs()
if err != nil {
ui.Error(fmt.Sprintf("Error getting PIFs: %s", err.Error()))
return multistep.ActionHalt
}
for _, pif := range pifs {
pif_rec, err := pif.GetRecord()
if err != nil {
ui.Error(fmt.Sprintf("Error getting PIF record: %s", err.Error()))
return multistep.ActionHalt
}
if pif_rec["management"].(bool) {
network.Ref = pif_rec["network"].(string)
}
}
if network.Ref == "" {
ui.Error("Error: couldn't find management network. Aborting.")
return multistep.ActionHalt
}
} else {
// Look up the network by it's name label
networks, err := client.GetNetworkByNameLabel(config.NetworkName)
if err != nil {
ui.Error(fmt.Sprintf("Error occured getting Network by name-label: %s", err.Error()))
return multistep.ActionHalt
}
switch {
case len(networks) == 0:
ui.Error(fmt.Sprintf("Couldn't find a network with the specified name-label '%s'. Aborting.", config.NetworkName))
return multistep.ActionHalt
case len(networks) > 1:
ui.Error(fmt.Sprintf("Found more than one network with the name '%s'. The name must be unique. Aborting.", config.NetworkName))
return multistep.ActionHalt
}
network = networks[0]
}
if err != nil {
ui.Say(err.Error())
}
_, err = instance.ConnectNetwork(network, "0")
if err != nil {
ui.Say(err.Error())
}
*/
instanceId, err := instance.GetUuid()
if err != nil {
ui.Error(fmt.Sprintf("Unable to get VM UUID: %s", err.Error()))
return multistep.ActionHalt
}
state.Put("instance_uuid", instanceId)
instance.SetDescription(config.VMDescription)
if err != nil {
ui.Error(fmt.Sprintf("Error setting VM description: %s", err.Error()))
return multistep.ActionHalt
}
ui.Say(fmt.Sprintf("Imported instance '%s'", instanceId))
return multistep.ActionContinue
}
func (self *stepImportInstance) Cleanup(state multistep.StateBag) {
/*
config := state.Get("config").(config)
if config.ShouldKeepVM(state) {
return
}
ui := state.Get("ui").(packer.Ui)
if self.instance != nil {
ui.Say("Destroying VM")
_ = self.instance.HardShutdown() // redundant, just in case
err := self.instance.Destroy()
if err != nil {
ui.Error(err.Error())
}
}
if self.vdi != nil {
ui.Say("Destroying VDI")
err := self.vdi.Destroy()
if err != nil {
ui.Error(err.Error())
}
}
*/
}