forked from dmacvicar/terraform-provider-libvirt
/
cloudinit_def.go
346 lines (291 loc) · 9.54 KB
/
cloudinit_def.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package libvirt
import (
"bufio"
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
libvirt "github.com/digitalocean/go-libvirt"
"github.com/google/uuid"
"github.com/hooklift/iso9660"
)
const userDataFileName string = "user-data"
const metaDataFileName string = "meta-data"
const networkConfigFileName string = "network-config"
type defCloudInit struct {
Name string
PoolName string
MetaData string `yaml:"meta_data"`
UserData string `yaml:"user_data"`
NetworkConfig string `yaml:"network_config"`
}
func newCloudInitDef() defCloudInit {
return defCloudInit{}
}
// Create a ISO file based on the contents of the CloudInit instance and
// uploads it to the libVirt pool
// Returns a string holding terraform's internal ID of this resource
func (ci *defCloudInit) CreateIso() (string, error) {
iso, err := ci.createISO()
if err != nil {
return "", err
}
return iso, err
}
func removeTmpIsoDirectory(iso string) {
err := os.RemoveAll(filepath.Dir(iso))
if err != nil {
log.Printf("error while removing tmp directory holding the ISO file: %s", err)
}
}
func (ci *defCloudInit) UploadIso(client *Client, iso string) (string, error) {
virConn := client.libvirt
if virConn == nil {
return "", fmt.Errorf(LibVirtConIsNil)
}
pool, err := virConn.StoragePoolLookupByName(ci.PoolName)
if err != nil {
return "", fmt.Errorf("can't find storage pool '%s'", ci.PoolName)
}
client.poolMutexKV.Lock(ci.PoolName)
defer client.poolMutexKV.Unlock(ci.PoolName)
// Refresh the pool of the volume so that libvirt knows it is
// not longer in use.
err = waitForSuccess("error refreshing pool for volume", func() error {
return virConn.StoragePoolRefresh(pool, 0)
})
if err != nil {
return "", err
}
volumeDef := newDefVolume()
volumeDef.Name = ci.Name
// an existing image was given, this mean we can't choose size
img, err := newImage(iso)
if err != nil {
return "", err
}
defer removeTmpIsoDirectory(iso)
size, err := img.Size()
if err != nil {
return "", err
}
volumeDef.Capacity.Unit = "B"
volumeDef.Capacity.Value = size
volumeDef.Target.Format.Type = "raw"
volumeDefXML, err := xml.Marshal(volumeDef)
if err != nil {
return "", fmt.Errorf("error serializing libvirt volume: %s", err)
}
// create the volume
volume, err := virConn.StorageVolCreateXML(pool, string(volumeDefXML), 0)
if err != nil {
return "", fmt.Errorf("error creating libvirt volume for cloudinit device %s: %s", ci.Name, err)
}
// upload ISO file
err = img.Import(newCopier(virConn, &volume, uint64(size)), volumeDef)
if err != nil {
return "", fmt.Errorf("error while uploading cloudinit %s: %s", img.String(), err)
}
if volume.Key == "" {
return "", fmt.Errorf("error retrieving volume key")
}
return ci.buildTerraformKey(volume.Key), nil
}
// create a unique ID for terraform use
// The ID is made by the volume ID (the internal one used by libvirt)
// joined by the ";" with a UUID
func (ci *defCloudInit) buildTerraformKey(volumeKey string) string {
return fmt.Sprintf("%s;%s", volumeKey, uuid.New())
}
func getCloudInitVolumeKeyFromTerraformID(id string) (string, error) {
s := strings.SplitN(id, ";", 2)
if len(s) != 2 {
return "", fmt.Errorf("%s is not a valid key", id)
}
return s[0], nil
}
// Create the ISO holding all the cloud-init data
// Returns a string with the full path to the ISO file
func (ci *defCloudInit) createISO() (string, error) {
log.Print("Creating new ISO")
tmpDir, err := ci.createFiles()
if err != nil {
return "", err
}
isoDestination := filepath.Join(tmpDir, ci.Name)
cmd := exec.Command(
"mkisofs",
"-output",
isoDestination,
"-volid",
"cidata",
"-joliet",
"-rock",
filepath.Join(tmpDir, userDataFileName),
filepath.Join(tmpDir, metaDataFileName),
filepath.Join(tmpDir, networkConfigFileName))
log.Printf("About to execute cmd: %+v", cmd)
if err = cmd.Run(); err != nil {
return "", fmt.Errorf("error while starting the creation of CloudInit's ISO image: %s", err)
}
log.Printf("ISO created at %s", isoDestination)
return isoDestination, nil
}
// write user-data, meta-data network-config in tmp files and dedicated directory
// Returns a string containing the name of the temporary directory and an error
// object
func (ci *defCloudInit) createFiles() (string, error) {
log.Print("Creating ISO contents")
tmpDir, err := ioutil.TempDir("", "cloudinit")
if err != nil {
return "", fmt.Errorf("cannot create tmp directory for cloudinit ISO generation: %s",
err)
}
// user-data
if err = ioutil.WriteFile(filepath.Join(tmpDir, userDataFileName), []byte(ci.UserData), os.ModePerm); err != nil {
return "", fmt.Errorf("error while writing user-data to file: %s", err)
}
// meta-data
if err = ioutil.WriteFile(filepath.Join(tmpDir, metaDataFileName), []byte(ci.MetaData), os.ModePerm); err != nil {
return "", fmt.Errorf("error while writing meta-data to file: %s", err)
}
// network-config
if err = ioutil.WriteFile(filepath.Join(tmpDir, networkConfigFileName), []byte(ci.NetworkConfig), os.ModePerm); err != nil {
return "", fmt.Errorf("error while writing network-config to file: %s", err)
}
log.Print("ISO contents created")
return tmpDir, nil
}
// Creates a new defCloudInit object starting from a ISO volume handled by
// libvirt
func newCloudInitDefFromRemoteISO(virConn *libvirt.Libvirt, id string) (defCloudInit, error) {
ci := defCloudInit{}
key, err := getCloudInitVolumeKeyFromTerraformID(id)
if err != nil {
return ci, err
}
volume, err := virConn.StorageVolLookupByKey(key)
if err != nil {
return ci, fmt.Errorf("can't retrieve volume %s: %v", key, err)
}
err = ci.setCloudInitDiskNameFromExistingVol(virConn, volume)
if err != nil {
return ci, err
}
err = ci.setCloudInitPoolNameFromExistingVol(virConn, volume)
if err != nil {
return ci, err
}
isoFile, err := downloadISO(virConn, volume)
if isoFile != nil {
defer os.Remove(isoFile.Name())
defer isoFile.Close()
}
if err != nil {
return ci, err
}
err = ci.setCloudInitDataFromExistingCloudInitDisk(virConn, isoFile)
if err != nil {
return ci, err
}
return ci, nil
}
// setCloudInitDataFromExistingCloudInitDisk read and set UserData, MetaData, and NetworkConfig from existing CloudInitDisk
func (ci *defCloudInit) setCloudInitDataFromExistingCloudInitDisk(virConn *libvirt.Libvirt, isoFile *os.File) error {
isoReader, err := iso9660.NewReader(isoFile)
if err != nil {
return fmt.Errorf("error initializing ISO reader: %s", err)
}
for {
file, err := isoReader.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
dataBytes, err := readIso9660File(file)
if err != nil {
return err
}
// the following filenames need to be like this because in the ios9660 reader
// joliet is not supported. https://github.com/hooklift/iso9660/blob/master/README.md#not-supported
if file.Name() == "/user_dat." {
ci.UserData = string(dataBytes)
}
if file.Name() == "/meta_dat." {
ci.MetaData = string(dataBytes)
}
if file.Name() == "/network_." {
ci.NetworkConfig = string(dataBytes)
}
}
log.Printf("[DEBUG]: Read cloud-init from file: %+v", ci)
return nil
}
// FIXME Consider doing this inline.
// setCloudInitPoolNameFromExistingVol retrieve poolname from an existing CloudInitDisk
func (ci *defCloudInit) setCloudInitPoolNameFromExistingVol(virConn *libvirt.Libvirt, volume libvirt.StorageVol) error {
volPool, err := virConn.StoragePoolLookupByVolume(volume)
if err != nil {
return fmt.Errorf("error retrieving pool for cloudinit volume: %s", err)
}
if volPool.Name == "" {
return fmt.Errorf("error retrieving pool name for cloudinit volume: %s", volume.Name)
}
ci.PoolName = volPool.Name
return nil
}
// FIXME Consider doing this inline.
// setCloudInitDisklNameFromVol retrieve CloudInitname from an existing CloudInitDisk
func (ci *defCloudInit) setCloudInitDiskNameFromExistingVol(virConn *libvirt.Libvirt, volume libvirt.StorageVol) error {
if volume.Name == "" {
return fmt.Errorf("error retrieving cloudinit volume name for volume key: %s", volume.Key)
}
ci.Name = volume.Name
return nil
}
func readIso9660File(file os.FileInfo) ([]byte, error) {
log.Printf("ISO reader: processing file %s", file.Name())
dataBytes, err := ioutil.ReadAll(file.Sys().(io.Reader))
if err != nil {
return nil, fmt.Errorf("error while reading %s: %s", file.Name(), err)
}
return dataBytes, nil
}
// Downloads the ISO identified by `key` to a local tmp file.
// Returns a pointer to the ISO file. Note well: you have to close this file
// pointer when you are done.
func downloadISO(virConn *libvirt.Libvirt, volume libvirt.StorageVol) (*os.File, error) {
// get Volume info (required to get size later)
_, size, _, err := virConn.StorageVolGetInfo(volume)
if err != nil {
return nil, fmt.Errorf("error retrieving info for volume: %s", err)
}
// create tmp file for the ISO
tmpFile, err := ioutil.TempFile("", "cloudinit")
if err != nil {
return nil, fmt.Errorf("cannot create tmp file: %s", err)
}
w := bufio.NewWriterSize(tmpFile, int(size))
// download ISO file
if err := virConn.StorageVolDownload(volume, w, 0, size, 0); err != nil {
return tmpFile, fmt.Errorf("error while downloading volume: %s", err)
}
bytesCopied := w.Buffered()
err = w.Flush()
if err != nil {
return tmpFile, fmt.Errorf("error while copying remote volume to local disk: %s", err)
}
log.Printf("%d bytes downloaded", bytesCopied)
if uint64(bytesCopied) != size {
return tmpFile, fmt.Errorf("error while copying remote volume to local disk, bytesCopied %d != %d volume.size", bytesCopied, size)
}
tmpFile.Seek(0, 0)
return tmpFile, nil
}