forked from xenserver/packer-plugin-xenserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
step_export.go
214 lines (168 loc) · 5.04 KB
/
step_export.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package common
import (
"crypto/tls"
"fmt"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
xsclient "github.com/xenserver/go-xenserver-client"
"io"
"net/http"
"os"
)
type StepExport struct{}
func downloadFile(url, filename string, ui packer.Ui) (err error) {
// Create the file
fh, err := os.Create(filename)
if err != nil {
return err
}
// Define a new transport which allows self-signed certs
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
// Create a client
client := &http.Client{Transport: tr}
// Create request and download file
resp, err := client.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
var progress uint
var total uint
var percentage uint
var marker_len uint
progress = uint(0)
total = uint(resp.ContentLength)
percentage = uint(0)
marker_len = uint(5)
var buffer [4096]byte
for {
n, err := resp.Body.Read(buffer[:])
if err != nil && err != io.EOF {
return err
}
progress += uint(n)
if _, write_err := fh.Write(buffer[:n]); write_err != nil {
return write_err
}
if err == io.EOF {
break
}
// Increment percentage in multiples of marker_len
cur_percentage := ((progress * 100 / total) / marker_len) * marker_len
if cur_percentage > percentage {
percentage = cur_percentage
ui.Message(fmt.Sprintf("Downloading... %d%%", percentage))
}
}
return nil
}
func (StepExport) Run(state multistep.StateBag) multistep.StepAction {
config := state.Get("commonconfig").(CommonConfig)
ui := state.Get("ui").(packer.Ui)
client := state.Get("client").(xsclient.XenAPIClient)
instance_uuid := state.Get("instance_uuid").(string)
suffix := ".vhd"
extrauri := "&format=vhd"
instance, err := client.GetVMByUuid(instance_uuid)
if err != nil {
ui.Error(fmt.Sprintf("Could not get VM with UUID '%s': %s", instance_uuid, err.Error()))
return multistep.ActionHalt
}
ui.Say("Step: export artifact")
switch config.Format {
case "none":
ui.Say("Skipping export")
return multistep.ActionContinue
case "template":
// convert VM to template
ui.Say("Converting VM to template")
instance.SetIsATemplate(true)
if err != nil {
ui.Error(fmt.Sprintf("Could not convert VM to template", err.Error()))
return multistep.ActionHalt
}
case "xva":
// export the VM
export_url := fmt.Sprintf("https://%s/export?uuid=%s&session_id=%s",
client.Host,
instance_uuid,
client.Session.(string),
)
export_filename := fmt.Sprintf("%s/%s.xva", config.OutputDir, config.VMName)
ui.Say("Getting XVA " + export_url)
err = downloadFile(export_url, export_filename, ui)
if err != nil {
ui.Error(fmt.Sprintf("Could not download XVA: %s", err.Error()))
return multistep.ActionHalt
}
case "vdi_raw":
suffix = ".raw"
extrauri = ""
fallthrough
case "vdi_vhd":
// export the disks
disks, err := instance.GetDisks()
if err != nil {
ui.Error(fmt.Sprintf("Could not get VM disks: %s", err.Error()))
return multistep.ActionHalt
}
for _, disk := range disks {
disk_uuid, err := disk.GetUuid()
if err != nil {
ui.Error(fmt.Sprintf("Could not get disk with UUID '%s': %s", disk_uuid, err.Error()))
return multistep.ActionHalt
}
// Work out XenServer version
hosts, err := client.GetHosts()
if err != nil {
ui.Error(fmt.Sprintf("Could not retrieve hosts in the pool: %s", err.Error()))
return multistep.ActionHalt
}
host := hosts[0]
host_software_versions, err := host.GetSoftwareVersion()
xs_version := host_software_versions["product_version"].(string)
if err != nil {
ui.Error(fmt.Sprintf("Could not get the software version: %s", err.Error()))
return multistep.ActionHalt
}
var disk_export_url string
// @todo: check for 6.5 SP1
if xs_version <= "6.5.0" && config.Format == "vdi_vhd" {
// Export the VHD using a Transfer VM
disk_export_url, err = disk.Expose("vhd")
if err != nil {
ui.Error(fmt.Sprintf("Failed to expose disk %s: %s", disk_uuid, err.Error()))
return multistep.ActionHalt
}
} else {
// Use the preferred direct export from XAPI
// Basic auth in URL request is required as session token is not
// accepted for some reason.
// @todo: raise with XAPI team.
disk_export_url = fmt.Sprintf("https://%s:%s@%s/export_raw_vdi?vdi=%s%s",
client.Username,
client.Password,
client.Host,
disk_uuid,
extrauri)
}
disk_export_filename := fmt.Sprintf("%s/%s%s", config.OutputDir, disk_uuid, suffix)
ui.Say("Getting VDI " + disk_export_url)
err = downloadFile(disk_export_url, disk_export_filename, ui)
if err != nil {
ui.Error(fmt.Sprintf("Could not download VDI: %s", err.Error()))
return multistep.ActionHalt
}
// Call unexpose in case a TVM was used. The call is harmless
// if that is not the case.
disk.Unexpose()
}
default:
panic(fmt.Sprintf("Unknown export format '%s'", config.Format))
}
ui.Say("Download completed: " + config.OutputDir)
return multistep.ActionContinue
}
func (StepExport) Cleanup(state multistep.StateBag) {}