This repository has been archived by the owner on Jul 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
156 lines (129 loc) · 5.33 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
package main
import (
"archive/tar"
"context"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/archive"
)
const (
SETUP_SCRIPT_FILE_DEFAULT = "setup.sh"
PACKAGE_LIST_FILE_DEFAULT = "packages.txt"
REPOSITORY_LIST_FILE_DEFAULT = "repositories.txt"
OUTPUT_IMAGE_FILE_DEFAULT = "alpine.qcow2"
MAXIMUM_DISK_SIZE_DEFAULT = "20G"
DOCKER_IMAGE_URL = "docker.io/library/alpine:edge"
WORKDIR = "/tmp"
)
func main() {
setupScriptFile := flag.String("script", SETUP_SCRIPT_FILE_DEFAULT, "Setup script file")
packageListFile := flag.String("packages", PACKAGE_LIST_FILE_DEFAULT, "Package list file")
repositoryListFile := flag.String("repositories", REPOSITORY_LIST_FILE_DEFAULT, "Repository list file")
maximumDiskSize := flag.String("maximumDiskSize", MAXIMUM_DISK_SIZE_DEFAULT, "Maximum disk size")
outputImageFile := flag.String("output", OUTPUT_IMAGE_FILE_DEFAULT, "Output image file")
verbose := flag.Bool("verbose", false, "Enable verbose logging")
flag.Parse()
ctx := context.Background()
filePaths := [][2]string{{*setupScriptFile, SETUP_SCRIPT_FILE_DEFAULT}, {*packageListFile, PACKAGE_LIST_FILE_DEFAULT}, {*repositoryListFile, REPOSITORY_LIST_FILE_DEFAULT}}
for _, filePath := range filePaths {
info, err := os.Stat(filePath[0])
if err != nil {
log.Fatal("could not check if file exists", filePath[0], err)
}
if info == nil {
log.Fatal("file doesn't exist", filePath[0], err)
}
}
log.Println("connecting to Docker daemon")
cli, err := client.NewEnvClient()
if err != nil {
log.Fatal("could not connect to Docker daemon", err)
}
log.Println("pulling Alpine Linux image")
out, err := cli.ImagePull(ctx, DOCKER_IMAGE_URL, types.ImagePullOptions{})
if err != nil {
log.Fatal("could not pull Alpine Linux image", err)
}
defer out.Close()
if _, err := ioutil.ReadAll(out); err != nil {
log.Fatal("could not write pulled Alpine Linux image", err)
}
log.Println("creating Alpine Linux container")
resp, err := cli.ContainerCreate(ctx, &container.Config{Image: DOCKER_IMAGE_URL, Cmd: []string{"tail", "-f", "/dev/null"}}, &container.HostConfig{Privileged: true, DNS: []string{"8.8.8.8"}}, nil, "")
if err != nil {
log.Fatal("could not create Alpine Linux container", err)
}
if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
log.Fatal("could not start Alpine Linux container", err)
}
defer func() {
log.Println("stopping and removing Alpine Linux container")
if err := cli.ContainerRemove(ctx, resp.ID, types.ContainerRemoveOptions{Force: true}); err != nil {
log.Fatal("could not remove Alpine Linux container", err)
}
}()
log.Println("copying files to Alpine Linux container")
cmds := [][]string{}
for _, filePath := range filePaths {
archive, err := archive.Tar(filePath[0], archive.Gzip)
if err != nil {
log.Fatal("could not create tar archive for file to copy into Alpine Linux container", filePath, err)
}
if err := cli.CopyToContainer(ctx, resp.ID, WORKDIR, archive, types.CopyToContainerOptions{}); err != nil {
log.Fatal("could not copy tar archive for file to copy into Alpine Linux container", filePath, err)
}
cmds = append(cmds, []string{"mv", path.Join(WORKDIR, filepath.Base(filePath[0])), path.Join(WORKDIR, filePath[1])})
}
log.Println("building image in Alpine Linux container")
cmds = append(cmds, []string{"chmod", "+x", path.Join(WORKDIR, SETUP_SCRIPT_FILE_DEFAULT)}, []string{"apk", "add", "alpine-make-vm-image"}, []string{"sh", "-c", fmt.Sprintf("alpine-make-vm-image --image-format qcow2 -s %v --repositories-file %v --packages \"$(cat %v)\" --script-chroot %v %v", *maximumDiskSize, REPOSITORY_LIST_FILE_DEFAULT, PACKAGE_LIST_FILE_DEFAULT, OUTPUT_IMAGE_FILE_DEFAULT, SETUP_SCRIPT_FILE_DEFAULT)})
for _, cmd := range cmds {
exec, err := cli.ContainerExecCreate(ctx, resp.ID, types.ExecConfig{Cmd: cmd, WorkingDir: WORKDIR, AttachStdout: true, AttachStderr: true})
if err != nil {
log.Fatal("could not create exec", exec.ID, err)
}
attach, err := cli.ContainerExecAttach(ctx, exec.ID, types.ExecStartCheck{})
if err != nil {
log.Fatal("could not attach to exec", exec.ID, err)
}
defer attach.Close()
if *verbose {
go io.Copy(os.Stdout, attach.Reader)
}
running := true
for running {
info, err := cli.ContainerExecInspect(ctx, exec.ID)
if err != nil {
log.Fatal("could not inspect exec", exec.ID, err)
}
if info.ExitCode != 0 {
log.Fatal("could not run command in Alpine Linux container, exited with non-zero exit code", info.ExitCode, cmd)
}
running = info.Running
}
}
log.Println("copying image from Alpine Linux container to host")
localFile, err := os.Create(*outputImageFile)
if err != nil {
log.Fatal("could not create output file", *outputImageFile, err)
}
tarStream, _, err := cli.CopyFromContainer(ctx, resp.ID, path.Join(WORKDIR, OUTPUT_IMAGE_FILE_DEFAULT))
if err != nil {
log.Fatal("could not request tar stream from Docker daemon", err)
}
remoteFile := tar.NewReader(tarStream)
if _, err := remoteFile.Next(); err != nil {
log.Fatal("could not read tar archive", err)
}
if _, err := io.Copy(localFile, remoteFile); err != nil {
log.Fatal("could not write to output file", *outputImageFile, err)
}
}