-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpackages.go
269 lines (230 loc) · 6.55 KB
/
packages.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
package main
import (
"context"
"dagger/fedora/internal/dagger"
"fmt"
"path/filepath"
)
const etcYumReposD = "/etc/yum.repos.d/"
// Repo represents a yum repository object
type Repo struct {
Url string
FileName string
Keep bool
}
// WithReposFromUrls will add the content at each given url and install them
// on the Container image prior to package installation via
// WithPackagesInstalled. Optionally removing the repository afterward, prior
// to exporting the container.
func (f *Fedora) WithReposFromUrls(
ctx context.Context,
// urls of yum repository files to install (i.e. GitHub raw file)
urls []string,
// If true, the repository will not be removed on the generated Container
// image
keep bool,
) *Fedora {
for _, r := range urls {
repo := Repo{
Url: r,
Keep: keep,
FileName: filepath.Base(r),
}
f.Repos = append(f.Repos, &repo)
}
return f
}
// WithPackageGroupsInstalled will install the given package groups
//
// note: not supported on ostree-based images, will be skipped
func (f *Fedora) WithPackageGroupsInstalled(
ctx context.Context,
// list of package groups to be installed
groups []string,
) *Fedora {
f.PackageGroupsInstalled = append(f.PackageGroupsInstalled, groups...)
return f
}
// WithPackageGroupsRemoved will remove the given package groups
//
// note: not supported on ostree-based images, will be skipped
func (f *Fedora) WithPackageGroupsRemoved(
ctx context.Context,
// list of package groups to be installed
groups []string,
) *Fedora {
f.PackageGroupsRemoved = append(f.PackageGroupsRemoved, groups...)
return f
}
// WithPackagesInstalled will install the given packages on the generated
// Container image.
func (f *Fedora) WithPackagesInstalled(
ctx context.Context,
// list of packages to be installed
packages []string,
) *Fedora {
f.PackagesInstalled = packages
return f
}
// WithPackagesRemoved will remove the given packages on the generated Container
// image.
func (f *Fedora) WithPackagesRemoved(
ctx context.Context,
// list of packages to be removed on the generated Container image
packages []string,
) *Fedora {
f.PackagesRemoved = packages
return f
}
type Swap struct {
Remove string
Install string
}
// Remove spec and install spec in one transaction
// equivalent to:
// `dnf swap <remove> <install>`
// ostree-based:
// `rpm-ostree override remove <remove> --install <install>`
func (f *Fedora) WithPackagesSwapped(
ctx context.Context,
// package to remove
remove string,
// package to install
install string,
) *Fedora {
swap := Swap{Remove: remove, Install: install}
f.PackagesSwapped = append(f.PackagesSwapped, swap)
return f
}
// ctrWithReposInstalled will get the f.Repos urls and install them in the
// returned Container image.
func (f *Fedora) ctrWithReposInstalled(ctr *dagger.Container) (*dagger.Container, error) {
if f.Repos == nil {
return ctr, nil
}
for _, r := range f.Repos {
contents, err := httpGet(r.Url)
if err != nil {
return nil, fmt.Errorf("error getting repo (%s) url: %w", r.FileName, err)
}
fileOpts := dagger.ContainerWithNewFileOpts{
Permissions: 0644,
Owner: "root:root",
}
ctr = ctr.WithNewFile(
fmt.Sprintf("%s/%s", etcYumReposD, r.FileName),
string(contents),
fileOpts,
)
}
return ctr, nil
}
// ctrWithReposRemoved will remove f.Repos which are not marked to be kept
// in the generated Container image.
func (f *Fedora) ctrWithReposRemoved(ctr *dagger.Container) *dagger.Container {
cmd := []string{"rm", "-f"}
run := false
for _, r := range f.Repos {
if !r.Keep {
run = true
cmd = append(cmd, fmt.Sprintf("%s/%s", etcYumReposD, r.FileName))
}
}
if run {
return ctr.WithExec(cmd)
}
return ctr
}
// ctrWithPackagesInstalledAndRemoved executes installing and removing packages
// as specified by the Fedora object
func (f *Fedora) ctrWithPackagesInstalledAndRemoved(
ctx context.Context,
ctr *dagger.Container,
) (*dagger.Container, error) {
ostreeBootable, _ := ctr.Label(ctx, "ostree.bootable")
if ostreeBootable == "true" {
return f.ctrWithPackagesInstalledAndRemovedRpmOstree(ctx, ctr), nil
}
return f.ctrWithPackagesInstalledAndRemovedDnf(ctx, ctr), nil
}
// ctrWithPackagesInstalledAndRemovedDnf executes installing and removing
// packages using `dnf` as specified by the Fedora object
func (f *Fedora) ctrWithPackagesInstalledAndRemovedDnf(
ctx context.Context,
ctr *dagger.Container,
) *dagger.Container {
const dnf = "dnf"
if len(f.PackageGroupsRemoved) > 0 {
ctr = f.ctrWithExec(ctr,
[]string{dnf, "-y", "group", "remove"},
f.PackageGroupsRemoved...,
)
}
// dnf swap only supports two packages so remove & install must be done in
// separate transactions. https://bugzilla.redhat.com/show_bug.cgi?id=1934883
if len(f.PackagesRemoved) > 0 {
ctr = f.ctrWithExec(ctr,
[]string{dnf, "-y", "remove"},
f.PackagesRemoved...,
)
}
packageGroupsInstalled := len(f.PackageGroupsInstalled) > 0
packagesInstalled := len(f.PackagesInstalled) > 0
if packageGroupsInstalled || packagesInstalled {
ctr = f.ctrWithExec(ctr,
[]string{dnf, "-y", "upgrade"},
)
}
if packageGroupsInstalled {
ctr = f.ctrWithExec(ctr,
[]string{dnf, "-y", "group", "install"},
f.PackageGroupsInstalled...,
)
}
if packagesInstalled {
ctr = f.ctrWithExec(ctr,
[]string{dnf, "-y", "install"},
f.PackagesInstalled...,
)
}
for _, swap := range f.PackagesSwapped {
ctr = f.ctrWithExec(ctr,
[]string{dnf, "-y", "swap"},
swap.Remove, swap.Install,
)
}
return ctr.WithExec([]string{dnf, "clean", "all"})
}
// ctrWithPackagesInstalledAndRemoved will return the given Container with
// packages installed and removed as defined by the Fedora object
func (f *Fedora) ctrWithPackagesInstalledAndRemovedRpmOstree(
ctx context.Context,
ctr *dagger.Container,
) *dagger.Container {
removePackages := len(f.PackagesRemoved) > 0
installPackages := len(f.PackagesInstalled) > 0
cmd := []string{"rpm-ostree", "override", "remove"}
// Doing both actions in one command allows for replacing required
// packages with alternatives
if installPackages && removePackages {
cmd = append(cmd, f.PackagesRemoved...)
for _, p := range f.PackagesInstalled {
cmd = append(cmd, fmt.Sprintf("--install=%s", p))
}
return f.ctrWithExec(ctr, cmd)
} else if removePackages {
return f.ctrWithExec(ctr,
cmd,
f.PackagesRemoved...,
)
} else if installPackages {
return f.ctrWithExec(ctr,
[]string{"rpm-ostree", "install"},
f.PackagesInstalled...,
)
}
for _, swap := range f.PackagesSwapped {
ctr = f.ctrWithExec(ctr, cmd, swap.Remove, "--install", swap.Install)
}
return ctr
}