/
main.go
278 lines (230 loc) · 6.57 KB
/
main.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
package main
import (
//"archive/tar"
//"compress/gzip"
"flag"
"fmt"
"github.com/op/go-logging"
"io"
"io/ioutil"
"math"
"net/http"
"os"
//"path/filepath"
"encoding/json"
"os/exec"
)
var log = logging.MustGetLogger("main")
var archiveFilename = flag.String("archive", "", "The archive to use. Setting this option will surpress the download.")
var boxName = flag.String("name", "", "The name of the box.")
func main() {
flag.Parse()
uid := os.Getuid()
euid := os.Geteuid()
if uid != 0 || euid != 0 {
println("grab-box not started with root privileges. Please run grab-box as root.")
return
}
fmt.Println("Welcome to grab-box\n")
if archiveFilename == nil || *archiveFilename == "" {
fmt.Println("What is the username/boxname of the box?")
var slug string
if _, err := fmt.Scanln(&slug); err != nil {
log.Fatal(err)
}
fmt.Println("")
metaUrl := fmt.Sprintf("https://app.wercker.com/api/v2/boxes/%v/*", slug)
response, err := http.Get(metaUrl)
if err != nil {
print("Unable to create response for url: %v", err)
}
defer response.Body.Close()
var metaDoc map[string]interface{}
decoder := json.NewDecoder(response.Body)
if err := decoder.Decode(&metaDoc); err != nil {
print("Unable to decode meta data response: %v", err)
}
boxurl := metaDoc["tarballUrl"].(string)
filename, err := downloadBox(boxurl)
if err != nil {
log.Fatal(err)
}
archiveFilename = &filename
}
if boxName == nil || *boxName == "" {
fmt.Println("What is the name of the box?\n")
if _, err := fmt.Scanln(boxName); err != nil {
log.Fatal(err)
}
}
unpackArchive(*archiveFilename, *boxName)
fmt.Println("\nFixing lxc config...")
fixConfig(fmt.Sprintf("/var/lib/lxc/%v/config", *boxName), *boxName)
fmt.Printf("\n\nFinished! You can execute the following command to start the container:\n\n")
fmt.Printf("\tsudo lxc-start -n '%v'", *boxName)
}
func unpackArchive(filename string, boxname string) {
containerDir := fmt.Sprintf("/var/lib/lxc/%v", boxname)
if err := os.MkdirAll(containerDir, 0777); err != nil {
log.Fatal(err)
}
fmt.Println("\nUnarchiving container...")
untar(filename, containerDir)
}
func fixConfig(filename string, boxName string) {
config := `# Template used to create this container: ubuntu
# Template script checksum (SHA-1): 6f468a9a658112f6420fb39d2ab90a80fd43cd22
lxc.network.type = veth
lxc.network.hwaddr = 00:16:3e:dd:01:4e
lxc.network.link = lxcbr0
lxc.network.flags = up
lxc.rootfs = /var/lib/lxc/` + boxName + `/rootfs
lxc.mount = /var/lib/lxc/` + boxName + `/fstab
lxc.pivotdir = lxc_putold
lxc.devttydir = lxc
lxc.tty = 4
lxc.pts = 1024
lxc.utsname = ` + boxName + `
lxc.arch = amd64
lxc.cap.drop = sys_module mac_admin mac_override
# When using LXC with apparmor, uncomment the next line to run unconfined:
#lxc.aa_profile = unconfined
lxc.cgroup.devices.deny = a
# Allow any mknod (but not using the node)
lxc.cgroup.devices.allow = c *:* m
lxc.cgroup.devices.allow = b *:* m
# /dev/null and zero
lxc.cgroup.devices.allow = c 1:3 rwm
lxc.cgroup.devices.allow = c 1:5 rwm
# consoles
lxc.cgroup.devices.allow = c 5:1 rwm
lxc.cgroup.devices.allow = c 5:0 rwm
#lxc.cgroup.devices.allow = c 4:0 rwm
#lxc.cgroup.devices.allow = c 4:1 rwm
# /dev/{,u}random
lxc.cgroup.devices.allow = c 1:9 rwm
lxc.cgroup.devices.allow = c 1:8 rwm
lxc.cgroup.devices.allow = c 136:* rwm
lxc.cgroup.devices.allow = c 5:2 rwm
# rtc
lxc.cgroup.devices.allow = c 254:0 rwm
#fuse
lxc.cgroup.devices.allow = c 10:229 rwm
#tun
lxc.cgroup.devices.allow = c 10:200 rwm
#full
lxc.cgroup.devices.allow = c 1:7 rwm
#hpet
lxc.cgroup.devices.allow = c 10:228 rwm
#kvm
lxc.cgroup.devices.allow = c 10:232 rwm
`
f, err := os.Create(filename)
if err != nil {
log.Fatal(err)
}
_, err = f.WriteString(config)
if err != nil {
log.Fatal(err)
}
}
// Downloads the box archive to a temporary file. It returns the filename
// to the temporary file, or an error.
func downloadBox(url string) (string, error) {
fmt.Printf("Downloading box: %v\n", url)
response, err := http.Get(url)
if err != nil {
return "", fmt.Errorf("Unable to create response for url: %v", err)
}
defer response.Body.Close()
archiveFile, err := ioutil.TempFile(os.TempDir(), "box.tar.gz")
if err != nil {
return "", fmt.Errorf("Unable to create temporary file: %v", err)
}
defer archiveFile.Close()
percentageComplete := float64(0)
bytesWritten := int64(0)
buffer := make([]byte, 32*1024)
for {
nRead, errRead := response.Body.Read(buffer)
if nRead > 0 {
nWritten, errWrite := archiveFile.Write(buffer[0:nRead])
if nWritten > 0 {
bytesWritten += int64(nWritten)
}
if errWrite != nil {
log.Fatalf("Error writing file: %v", err)
}
if nRead != nWritten {
log.Fatal(io.ErrShortWrite)
}
}
if errRead == io.EOF {
break
}
if errRead != nil {
log.Fatal("Error ")
}
newPercentageComplete := (float64(100) / float64(response.ContentLength)) * float64(bytesWritten)
if math.Floor(newPercentageComplete) != math.Floor(percentageComplete) {
fmt.Print(".")
percentageComplete = newPercentageComplete
}
}
fmt.Print("Complete!\n\n")
return archiveFile.Name(), nil
}
func untar(filename string, directory string) {
tarPath, err := exec.LookPath("tar")
if err != nil {
log.Fatal(err)
}
//cmd := exec.Command(tarPath, fmt.Sprintf("xfz \"%v\" -C \"%v\"", filename, directory))
cmd := exec.Command(tarPath, "xfz", filename, "-C", directory)
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
out, _ := cmd.Output()
log.Debug(string(out))
// file, err := os.Open(filename)
// if err != nil {
// log.Fatal(err)
// }
// defer file.Close()
// gzipReader, err := gzip.NewReader(file)
// if err != nil {
// log.Fatal(err)
// }
// reader := tar.NewReader(gzipReader)
// for {
// header, err := reader.Next()
// if err == io.EOF {
// break
// }
// if err != nil {
// log.Fatal(err)
// }
// if header.FileInfo().IsDir() {
// dir := filepath.Join(directory, header.Name)
// os.Mkdir(dir, header.FileInfo().Mode())
// } else {
// path := filepath.Join(directory, header.Name)
// fileinfo := header.FileInfo()
// file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_EXCL, fileinfo.Mode()|os.ModeSetuid|os.ModeSetgid)
// if err != nil {
// log.Fatal(err)
// }
// if _, err := io.Copy(file, reader); err != nil {
// log.Fatal(err)
// }
// if err := file.Chown(header.Uid, header.Gid); err != nil {
// log.Fatal(err)
// }
// file.Close()
// if err := os.Chown(path, header.Uid, header.Gid); err != nil {
// log.Fatal(err)
// }
// }
// }
fmt.Printf("Container created in: %v", directory)
}