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
|
package main
import (
"bytes"
"flag"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
)
const (
workbench = `build`
)
var (
key = ""
repo = ""
commit = ""
task = ""
image = ""
port = ""
)
func init() {
flag.StringVar(&key, "key", "", "Path to SSH private key")
flag.StringVar(&repo, "repo", "", "Source code as valid Git URL")
flag.StringVar(&commit, "commit", "", "Commit or commit-ish reference for checkout")
flag.StringVar(&task, "task", "", "Shell code to execute for the build")
flag.StringVar(&image, "image", "", "QEMU qcow2 image which contains the VM")
flag.StringVar(&port, "port", "", "localhost port which is connected to VM SSH server")
}
func main() {
flag.Parse()
log.SetFlags(0)
log.Println("Starting craft")
defer log.Println("Exiting craft")
vm := qemu()
err := vm.Start()
if err != nil {
log.Fatal(err)
}
defer func() {
err := vm.Process.Kill()
if err != nil {
log.Fatal(err)
}
}()
waitBoot()
checkoutSources(repo, commit)
craft()
}
func runCommand(name string, args ...string) {
log.Printf("Remote execution: %s %s\n", name, strings.Join(args, " "))
command := sshCommand(name, args...)
command.Stderr = os.Stderr
command.Stdout = os.Stdout
err := command.Run()
if err != nil {
log.Fatal(err)
}
}
func qemu() *exec.Cmd {
return exec.Command(
"qemu-system-x86_64",
"-enable-kvm",
"-m",
"4G",
"-net",
"nic,model=virtio",
"-net",
fmt.Sprintf(
"user,hostfwd=tcp:127.0.0.1:%s-:22",
port,
),
"-drive",
fmt.Sprintf(
"file=%s,media=disk,snapshot=on,if=virtio",
image,
),
"-smp",
"cpus=4",
"-nographic",
)
}
func sshCommand(name string, arg ...string) *exec.Cmd {
return exec.Command(
"ssh",
append([]string{
"-p",
port,
"-o",
"UserKnownHostsFile=/dev/null",
"-o",
"StrictHostKeyChecking=no",
"-i",
key,
"root@localhost",
name,
}, arg...)...,
)
}
func waitBoot() {
retries := 5
initialDelay := 1 * time.Second
log.Println("Waiting for VM to boot")
time.Sleep(initialDelay) // without that `ssh` returns too quickly
for range retries {
cmd := sshCommand("true")
err := cmd.Run()
if err == nil {
log.Println("VM booted")
return
}
}
log.Fatalf("Could not reach VM %d times - giving up", retries)
}
func craft() {
script := fmt.Sprintf(`#!/bin/sh
cd %s
%s
`, workbench, task)
cmd := sshCommand("sh")
cmd.Stdin = bytes.NewBufferString(script)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
}
func checkoutSources(repo string, commit string) string {
runCommand(
"git",
"clone",
repo,
workbench,
)
runCommand(
"git",
"--git-dir",
filepath.Join(workbench, ".git"),
"--work-tree",
workbench,
"checkout",
commit,
)
runCommand(
"git",
"-C",
workbench,
"submodule",
"update",
"--depth=1",
"--init",
"--recursive",
)
return workbench
}
|