/
build.go
244 lines (196 loc) · 4.96 KB
/
build.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
package util
import (
"archive/tar"
"bytes"
"context"
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/archimoebius/fishler/asset"
config "github.com/archimoebius/fishler/cli/config/root"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
)
func buildImage(client *client.Client, tags []string, dockerBasepath string) error {
ctx := context.Background()
reader, err := client.ImagePull(ctx, "docker.io/library/alpine", types.ImagePullOptions{})
if err != nil {
return err
}
defer reader.Close()
_, err = io.Copy(os.Stdout, reader)
if err != nil {
return err
}
var dockerFileContent []byte
// Create a buffer
buf := new(bytes.Buffer)
tw := tar.NewWriter(buf)
defer tw.Close()
dockerfile := fmt.Sprintf("%s/Dockerfile", dockerBasepath)
docker_rootfs := fmt.Sprintf("%s/rootfs/", dockerBasepath)
// Create a filereader
dockerFileReader, err := os.Open(dockerfile) // #nosec
if errors.Is(err, fs.ErrNotExist) {
dockerFileContent, err = asset.DockerFolder.ReadFile(dockerfile)
Logger.Infof("Building %s with embedFS %s", dockerfile, docker_rootfs)
}
if err != nil {
return err
}
if len(dockerFileContent) == 0 {
Logger.Infof("Building %s with rootFS %s", dockerfile, docker_rootfs)
// Read the actual Dockerfile
dockerFileContent, err = io.ReadAll(dockerFileReader)
if err != nil {
return err
}
}
// Make a TAR header for the file
tarHeader := &tar.Header{
Name: dockerfile,
Size: int64(len(dockerFileContent)), // #nosec
}
// Writes the header described for the TAR file
err = tw.WriteHeader(tarHeader)
if err != nil {
return err
}
// Writes the dockerfile data to the TAR file
_, err = tw.Write(dockerFileContent)
if err != nil {
return err
}
cwd, err := os.Getwd()
if err != nil {
return err
}
var has_rootfs = true
err = os.Chdir(docker_rootfs)
if errors.Is(err, fs.ErrNotExist) {
has_rootfs = false
err = fs.WalkDir(asset.DockerFolder, ".", func(path string, info fs.DirEntry, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if strings.HasSuffix(path, "Dockerfile") {
return nil // no need to embed the Dockerfile in the docker image :P
}
filedata, err := asset.DockerFolder.ReadFile(path)
if err != nil {
return err
}
if !strings.HasPrefix(path, "docker/rootfs/") {
return nil // no need to embed anything in the docker image that shouldn't be in the rootfs
}
var mode = int64(0o0644)
// this script is invoked by root at container creation to make any last second changes to the environment before the user is provided a shell
if strings.HasSuffix(path, "fixme") {
mode = int64(0o0700)
}
// not really bash :P
if strings.HasSuffix(path, "bash") {
mode = int64(0o0755)
}
hdr := &tar.Header{
Name: fmt.Sprintf("/%s", strings.ReplaceAll(path, "docker/rootfs/", "")),
Mode: mode,
Size: int64(len(filedata)),
}
if err := tw.WriteHeader(hdr); err != nil {
return err
}
if _, err := tw.Write(filedata); err != nil {
return err
}
return nil
})
if err != nil {
return err
}
} else {
return err
}
if has_rootfs {
err = filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
filedata, err := os.ReadFile(path) // #nosec
if err != nil {
return err
}
hdr := &tar.Header{
Name: fmt.Sprintf("/%s", path),
Mode: int64(info.Mode()),
Size: info.Size(),
}
if err := tw.WriteHeader(hdr); err != nil {
return err
}
if _, err := tw.Write(filedata); err != nil {
return err
}
return nil
})
if err != nil {
return err
}
err = os.Chdir(cwd)
if err != nil {
return err
}
return nil
}
dockerFileTarReader := bytes.NewReader(buf.Bytes())
labels := make(map[string]string)
labels[config.Setting.DockerImagename] = "latest"
// Define the build options to use for the file
// https://godoc.org/github.com/docker/docker/api/types#ImageBuildOptions
buildOptions := types.ImageBuildOptions{
Context: dockerFileTarReader,
Dockerfile: dockerfile,
Remove: true,
Tags: tags,
Version: types.BuilderBuildKit,
SuppressOutput: false,
NoCache: true,
Labels: labels,
}
// Build the actual image
imageBuildResponse, err := client.ImageBuild(
ctx,
dockerFileTarReader,
buildOptions,
)
if err != nil {
return err
}
// Read the STDOUT from the build process
defer imageBuildResponse.Body.Close()
_, err = io.Copy(os.Stdout, imageBuildResponse.Body)
if err != nil {
return err
}
return nil
}
func BuildFishler(client *client.Client, ctx context.Context, forceBuild bool) error {
images, err := GetDockerImage(client, ctx)
if err != nil {
return err
}
if len(images) == 0 || forceBuild {
return buildImage(client, []string{config.Setting.DockerImagename}, config.Setting.DockerBasepath)
}
return nil
}