File: util.go

package info (click to toggle)
golang-github-gophercloud-utils 0.0~git20200508.b0167b9-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 784 kB
  • sloc: sh: 20; makefile: 7
file content (59 lines) | stat: -rw-r--r-- 1,028 bytes parent folder | download | duplicates (3)
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
package nodes

import (
	"bytes"
	"compress/gzip"
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
)

// Gzips a file
func GzipFile(path string) ([]byte, error) {
	var buf bytes.Buffer

	w := gzip.NewWriter(&buf)
	contents, err := ioutil.ReadFile(path)
	if err != nil {
		return nil, err
	}
	_, err = w.Write(contents)
	if err != nil {
		return nil, err
	}

	err = w.Close()
	if err != nil {
		return nil, err
	}

	result := buf.Bytes()
	return result, nil
}

// Packs a directory into a gzipped ISO image
func PackDirectoryAsISO(path string) ([]byte, error) {
	iso, err := ioutil.TempFile("", "gophercloud-iso")
	if err != nil {
		return nil, err
	}
	iso.Close()
	defer os.Remove(iso.Name())
	cmd := exec.Command(
		"mkisofs",
		"-o", iso.Name(),
		"-ldots",
		"-allow-lowercase",
		"-allow-multidot", "-l",
		"-publisher", "gophercloud",
		"-quiet", "-J",
		"-r", "-V", "config-2",
		path,
	)
	if err = cmd.Run(); err != nil {
		return nil, fmt.Errorf("error creating configdrive iso: %s", err.Error())
	}

	return GzipFile(iso.Name())
}