summaryrefslogtreecommitdiff
path: root/main.go
blob: da2284db2a0be70fe8cf242f99a4f75b0923e4c8 (plain)
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
package main

import (
	"bytes"
	"flag"
	"fmt"
	"log"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
)

var (
	repo = ""
	commit = ""
	task = ""
	image = ""
	port = ""
)

func init() {
	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)
		}
	}()

	workbench := prepareWorkbench(repo, commit)
	defer os.RemoveAll(workbench)
	craft(workbench)
}

func runCommand(dir string, name string, args ...string) {
	log.Printf("%s %s\n", name, strings.Join(args, " "))
	command := exec.Command(name, args...)
	command.Dir = dir
	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 craft(workbench string) {
	script := fmt.Sprintf(`#!/bin/sh

%s
`, task)

	cmd := exec.Command("sh")
	cmd.Dir = workbench
	cmd.Stdin = bytes.NewBufferString(script)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr

	err := cmd.Run()
	if err != nil {
		log.Fatal(err)
	}
}

func prepareWorkbench(repo string, commit string) string {
	workbench, err := os.MkdirTemp("", "*-craft")
	if err != nil {
		log.Fatal(err)
	}

	runCommand(
		workbench,
		"git",
		"clone",
		repo,
		workbench,
	)

	runCommand(
		workbench,
		"git",
		"--git-dir",
		filepath.Join(workbench, ".git"),
		"--work-tree",
		workbench,
		"checkout",
		commit,
	)

	runCommand(
		workbench,
		"git",
		"--git-dir",
		filepath.Join(workbench, ".git"),
		"--work-tree",
		workbench,
		"submodule",
		"update",
		"--depth=1",
		"--init",
		"--recursive",
	)

	return workbench
}