-
Notifications
You must be signed in to change notification settings - Fork 11
/
util.go
222 lines (181 loc) · 5.37 KB
/
util.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
package main
import (
"archive/tar"
"compress/flate"
"compress/gzip"
"encoding/json"
"fmt"
"github.com/mitchellh/packer/packer"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)
// Copies a file by copying the contents of the file to another place.
func CopyContents(dst, src string) error {
srcF, err := os.Open(src)
if err != nil {
return err
}
defer srcF.Close()
dstF, err := os.Create(dst)
if err != nil {
return err
}
defer dstF.Close()
if _, err := io.Copy(dstF, srcF); err != nil {
return err
}
return nil
}
// DirToBox takes the directory and compresses it into a Vagrant-compatible
// box. This function does not perform checks to verify that dir is
// actually a proper box. This is an expected precondition.
func DirToBox(dst, dir string, ui packer.Ui, level int) error {
log.Printf("Turning dir into box: %s => %s", dir, dst)
dstF, err := os.Create(dst)
if err != nil {
return err
}
defer dstF.Close()
var dstWriter io.Writer = dstF
if level != flate.NoCompression {
log.Printf("Compressing with gzip compression level: %d", level)
gzipWriter, err := gzip.NewWriterLevel(dstWriter, level)
if err != nil {
return err
}
defer gzipWriter.Close()
dstWriter = gzipWriter
}
tarWriter := tar.NewWriter(dstWriter)
defer tarWriter.Close()
// This is the walk func that tars each of the files in the dir
tarWalk := func(path string, info os.FileInfo, prevErr error) error {
// If there was a prior error, return it
if prevErr != nil {
return prevErr
}
// Skip directories
if info.IsDir() {
log.Printf("Skipping directory '%s' for box '%s'", path, dst)
return nil
}
log.Printf("Box add: '%s' to '%s'", path, dst)
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
header, err := tar.FileInfoHeader(info, "")
if err != nil {
return err
}
// We have to set the Name explicitly because it is supposed to
// be a relative path to the root. Otherwise, the tar ends up
// being a bunch of files in the root, even if they're actually
// nested in a dir in the original "dir" param.
header.Name, err = filepath.Rel(dir, path)
if err != nil {
return err
}
if ui != nil {
ui.Message(fmt.Sprintf("Compressing: %s", header.Name))
}
if err := tarWriter.WriteHeader(header); err != nil {
return err
}
if _, err := io.Copy(tarWriter, f); err != nil {
return err
}
return nil
}
// Tar.gz everything up
return filepath.Walk(dir, tarWalk)
}
// WriteMetadata writes the "metadata.json" file for a Vagrant box.
func WriteMetadata(dir string, contents interface{}) error {
f, err := os.Create(filepath.Join(dir, "metadata.json"))
if err != nil {
return err
}
defer f.Close()
enc := json.NewEncoder(f)
return enc.Encode(contents)
}
// func RunAndLog(cmd *exec.Cmd) (string, string, error) {
// var stdout, stderr bytes.Buffer
// log.Printf("Executing: %s %v", cmd.Path, cmd.Args[1:])
// cmd.Stdout = &stdout
// cmd.Stderr = &stderr
// err := cmd.Run()
// stdoutString := strings.TrimSpace(stdout.String())
// stderrString := strings.TrimSpace(stderr.String())
// if _, ok := err.(*exec.ExitError); ok {
// err = fmt.Errorf("VMware error: %s", stderrString)
// }
// log.Printf("stdout: %s", stdoutString)
// log.Printf("stderr: %s", stderrString)
// // Replace these for Windows, we only want to deal with Unix
// // style line endings.
// returnStdout := strings.Replace(stdout.String(), "\r\n", "\n", -1)
// returnStderr := strings.Replace(stderr.String(), "\r\n", "\n", -1)
// return returnStdout, returnStderr, err
// }
func FindOvfTool() (ovftool string, err error) {
// Accumulate any errors
errs := new(packer.MultiError)
// use ovftool in PATH, so use can decide which one to use
ovftool = "ovftool"
if _, err := exec.LookPath(ovftool); err != nil {
errs = packer.MultiErrorAppend(
errs, fmt.Errorf("ovftool not found in path: %s", err))
files := make([]string, 0, 6)
// search ovftool at some specific places
files = append(files, "/Applications/VMware Fusion.app/Contents/Library/VMware OVF Tool/ovftool")
if os.Getenv("ProgramFiles(x86)") != "" {
files = append(files,
filepath.Join(os.Getenv("ProgramFiles(x86)"), "/VMware/Client Integration Plug-in 5.5/ovftool/ovftool.exe"))
}
if os.Getenv("ProgramFiles") != "" {
files = append(files,
filepath.Join(os.Getenv("ProgramFiles"), "/VMware/Client Integration Plug-in 5.5/ovftool/ovftool.exe"))
}
if os.Getenv("ProgramFiles(x86)") != "" {
files = append(files,
filepath.Join(os.Getenv("ProgramFiles(x86)"), "/VMware/VMware Workstation/ovftool/ovftool.exe"))
}
if os.Getenv("ProgramFiles") != "" {
files = append(files,
filepath.Join(os.Getenv("ProgramFiles"), "/VMware/VMware Workstation/ovftool/ovftool.exe"))
}
file := findFile(files)
if file == "" {
errs = packer.MultiErrorAppend(
errs, fmt.Errorf("ovftool not found: %s", err))
} else {
ovftool = file
}
}
return
}
func findFile(files []string) string {
for _, file := range files {
file = normalizePath(file)
log.Printf("Searching for file '%s'", file)
if _, err := os.Stat(file); err == nil {
log.Printf("Found file '%s'", file)
return file
}
}
log.Printf("File not found")
return ""
}
func normalizePath(path string) string {
path = strings.Replace(path, "\\", "/", -1)
path = strings.Replace(path, "//", "/", -1)
path = strings.TrimRight(path, "/")
return path
}