-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer.go
277 lines (242 loc) · 6.31 KB
/
container.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
package main
import (
"context"
"dagger/fedora/internal/dagger"
"fmt"
"strings"
)
// ContainerAddress returns the string representation of the source
// container address
func (f *Fedora) ContainerAddress(
registry string,
org *string,
variant string,
suffix *string,
tag string,
) string {
s := ""
if suffix != nil {
s = fmt.Sprintf("-%s", *suffix)
}
const sourceStrWithOrg = "%s/%s/%s%s:%s" // registry/org/variant+suffix:tag
const sourceStrWithoutOrg = "%s/%s%s:%s" // registry/variant+suffix:tag
if org != nil {
return fmt.Sprintf(sourceStrWithOrg,
registry,
*org,
variant,
s,
tag,
)
}
return fmt.Sprintf(sourceStrWithoutOrg,
registry,
variant,
s,
tag,
)
}
// Container returns a Fedora container as a dagger.Container object
func (f *Fedora) Container(ctx context.Context) (*dagger.Container, error) {
ctr, err := f.
ContainerFrom(
ctx,
f.ContainerAddress(
f.Registry,
f.Org,
f.Variant,
f.Suffix,
f.Tag,
),
)
if err != nil {
return nil, err
}
return ctr, nil
}
// ContainerFrom returns a Fedora container as a dagger.Container object
func (f *Fedora) ContainerFrom(
ctx context.Context,
// base container image to pull FROM
from string,
) (*dagger.Container, error) {
ctr := dag.
Container().
From(from)
ctr = f.ctrWithDirectoriesInstalled(ctr)
ctr = f.ctrWithFilesInstalled(ctr)
if f.Repos != nil {
var err error
ctr, err = f.ctrWithReposInstalled(ctr)
if err != nil {
return nil, err
}
}
if f.ExecScriptPre != nil {
var err error
ctr, err = f.ctrExecScripts(ctx, ctr, f.ExecScriptPre)
if err != nil {
return nil, err
}
}
for _, cmd := range f.ExecPre {
ctr = ctr.WithExec(cmd)
}
if f.PackagesInstalled != nil || f.PackagesRemoved != nil || f.PackagesSwapped != nil {
var err error
ctr, err = f.ctrWithPackagesInstalledAndRemoved(ctx, ctr)
if err != nil {
return nil, err
}
}
ctr = f.ctrWithReposRemoved(ctr)
if f.ExecScriptPost != nil {
var err error
ctr, err = f.ctrExecScripts(ctx, ctr, f.ExecScriptPost)
if err != nil {
return nil, err
}
}
if f.ExecScriptPre != nil || f.ExecScriptPost != nil {
scripts := append(f.ExecScriptPre, f.ExecScriptPost...)
var err error
ctr, err = f.ctrScriptsCleanup(ctx, ctr, scripts)
if err != nil {
return nil, err
}
}
for _, cmd := range f.ExecPost {
ctr = ctr.WithExec(cmd)
}
ctr = f.ctrWithLabels(ctr)
return ctr, nil
}
// ContainerVersionFromLabel returns the label value for the image version,
// defined as 'version' OR 'org.opencontainers.image.version'
// and a possible error
func (f *Fedora) ContainerVersionFromLabel(
ctx context.Context,
// Container to use to determine the release version from
// +optional
ctr *dagger.Container,
) (string, error) {
if ctr == nil {
var err error
ctr, err = f.Container(ctx)
if err != nil {
return "", err
}
}
version, err := ctr.Label(ctx, "version")
if err != nil || len(version) <= 0 {
version, err = ctr.Label(ctx, "org.opencontainers.image.version")
if err != nil || len(version) <= 0 {
return "", fmt.Errorf(
"unable to determine version from container labels",
)
}
}
return version, nil
}
// ContainerReleaseVersionFromLabel returns the label value for the image
// version, defined as 'version' OR 'org.opencontainers.image.version'
// - if the version contains sub-versions & dates delimited by '.' they will
// parsed out
func (f *Fedora) ContainerReleaseVersionFromLabel(
ctx context.Context,
// Container to use to determine the release version from
// +optional
ctr *dagger.Container,
) (string, error) {
version, err := f.ContainerVersionFromLabel(ctx, ctr)
if err != nil {
return "", err
}
releaseVersion := strings.Split(version, ".")
if len(releaseVersion) <= 0 {
return "", fmt.Errorf(
"unable to determine release version from base image version: %s",
version,
)
}
return releaseVersion[0], nil
}
// ctrWithDirectoriesInstalled returns a container type with the Fedora object
// with Directories installed
func (f *Fedora) ctrWithDirectoriesInstalled(ctr *dagger.Container) *dagger.Container {
for _, d := range f.Directories {
ctr = ctr.WithDirectory(d.Destination, d.Source)
}
return ctr
}
// ctrWithFilesInstalled returns a container type with the Fedora object
// with Files installed
func (f *Fedora) ctrWithFilesInstalled(ctr *dagger.Container) *dagger.Container {
for _, d := range f.Files {
ctr = ctr.WithFile(d.Destination, d.Source)
}
return ctr
}
// ctrWithLabels returns a container type with the Fedora object
// with Labels added
func (f *Fedora) ctrWithLabels(ctr *dagger.Container) *dagger.Container {
for _, l := range f.Labels {
ctr = ctr.WithLabel(l.Name, l.Value)
}
return ctr
}
// ctrWithExec wraps Container.WithExec allowing the command and args to be
// separated
func (f *Fedora) ctrWithExec(
ctr *dagger.Container,
// command to be executed
command []string,
// arguments to be passed to the given command
args ...string,
) *dagger.Container {
if args != nil {
command = append(command, args...)
}
return ctr.WithExec(command)
}
// ctrExecScripts adds the given scripts (files) to the Fedora container
// and executes them. They will be removed from the final image as part
// or the ctrScriptsCleanup step
func (f *Fedora) ctrExecScripts(
ctx context.Context,
// Container image to run scripts against
ctr *dagger.Container,
// scripts (files) to be run
scripts []*dagger.File,
) (*dagger.Container, error) {
for _, script := range scripts {
scriptName, err := script.Name(ctx)
if err != nil {
return nil, err
}
scriptTmp := fmt.Sprintf("/tmp/%s", scriptName)
ctr = ctr.WithFile(scriptTmp, script).WithExec([]string{scriptTmp})
}
return ctr, nil
}
// ctrScriptsCleanup removes the Fedora container scripts (files) added as part
// of the ctrExecScripts step
func (f *Fedora) ctrScriptsCleanup(
ctx context.Context,
// Container image to run scripts against
ctr *dagger.Container,
// scripts (files) to be removed
scripts []*dagger.File,
) (*dagger.Container, error) {
filesToDelete := []string{}
for _, script := range scripts {
scriptName, err := script.Name(ctx)
if err != nil {
return nil, err
}
scriptTmp := fmt.Sprintf("/tmp/%s", scriptName)
filesToDelete = append(filesToDelete, scriptTmp)
}
cmd := append([]string{"rm", "-f"}, filesToDelete...)
return ctr.WithExec(cmd), nil
}