forked from xenserver/packer-plugin-xenserver
/
step_upload_vdi.go
128 lines (108 loc) · 3.04 KB
/
step_upload_vdi.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
package common
import (
"fmt"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
xsclient "github.com/xenserver/go-xenserver-client"
"log"
"os"
"time"
)
type StepUploadVdi struct {
VdiNameFunc func() string
ImagePathFunc func() string
VdiUuidKey string
}
func (self *StepUploadVdi) Run(state multistep.StateBag) multistep.StepAction {
config := state.Get("commonconfig").(CommonConfig)
ui := state.Get("ui").(packer.Ui)
client := state.Get("client").(xsclient.XenAPIClient)
imagePath := self.ImagePathFunc()
vdiName := self.VdiNameFunc()
if imagePath == "" {
// skip if no disk image to attach
return multistep.ActionContinue
}
ui.Say(fmt.Sprintf("Step: Upload VDI '%s'", vdiName))
// Create VDI for the image
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(imagePath)
if err != nil {
ui.Error(fmt.Sprintf("Unable to open disk image '%s': %s", imagePath, err.Error()))
return multistep.ActionHalt
}
// Get file length
fstat, err := fh.Stat()
if err != nil {
ui.Error(fmt.Sprintf("Unable to stat disk image '%s': %s", imagePath, err.Error()))
return multistep.ActionHalt
}
fileLength := fstat.Size()
// Create the VDI
vdi, err := sr.CreateVdi(vdiName, fileLength)
if err != nil {
ui.Error(fmt.Sprintf("Unable to create VDI '%s': %s", vdiName, err.Error()))
return multistep.ActionHalt
}
vdiUuid, err := vdi.GetUuid()
if err != nil {
ui.Error(fmt.Sprintf("Unable to get UUID of VDI '%s': %s", vdiName, err.Error()))
return multistep.ActionHalt
}
state.Put(self.VdiUuidKey, vdiUuid)
_, err = HTTPUpload(fmt.Sprintf("https://%s/import_raw_vdi?vdi=%s&session_id=%s",
client.Host,
vdi.Ref,
client.Session.(string),
), fh, state)
if err != nil {
ui.Error(fmt.Sprintf("Unable to upload VDI: %s", err.Error()))
return multistep.ActionHalt
}
return multistep.ActionContinue
}
func (self *StepUploadVdi) Cleanup(state multistep.StateBag) {
config := state.Get("commonconfig").(CommonConfig)
ui := state.Get("ui").(packer.Ui)
client := state.Get("client").(xsclient.XenAPIClient)
vdiName := self.VdiNameFunc()
if config.ShouldKeepVM(state) {
return
}
vdiUuidRaw, ok := state.GetOk(self.VdiUuidKey)
if !ok {
// VDI doesn't exist
return
}
vdiUuid := vdiUuidRaw.(string)
if vdiUuid == "" {
// VDI already cleaned up
return
}
vdi, err := client.GetVdiByUuid(vdiUuid)
if err != nil {
ui.Error(fmt.Sprintf("Can't get VDI '%s': %s", vdiUuid, err.Error()))
return
}
// an interrupted import_raw_vdi takes a while to release the VDI
// so try several times
for i := 0; i < 3; i++ {
log.Printf("Trying to destroy VDI...")
err = vdi.Destroy()
if err == nil {
break
}
time.Sleep(1 * time.Second)
}
if err != nil {
ui.Error(fmt.Sprintf("Can't destroy VDI '%s': %s", vdiUuid, err.Error()))
return
}
ui.Say(fmt.Sprintf("Destroyed VDI '%s'", vdiName))
state.Put(self.VdiUuidKey, "")
}