forked from apptainer/singularity
/
conveyorPacker_zypper.go
246 lines (197 loc) · 6.38 KB
/
conveyorPacker_zypper.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
// Copyright (c) 2018-2019, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
package sources
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
"github.com/sylabs/singularity/internal/pkg/sylog"
"github.com/sylabs/singularity/pkg/build/types"
)
const (
zypperConf = "/etc/zypp/zypp.conf"
)
// ZypperConveyorPacker only needs to hold the bundle for the container
type ZypperConveyorPacker struct {
b *types.Bundle
}
// Get downloads container information from the specified source
func (cp *ZypperConveyorPacker) Get(b *types.Bundle) (err error) {
cp.b = b
// check for zypper on system
zypperPath, err := exec.LookPath("zypper")
if err != nil {
return fmt.Errorf("zypper is not in PATH: %v", err)
}
// check for rpm on system
err = rpmPathCheck()
if err != nil {
return
}
// get mirrorURL, OSVerison, and Includes components to definition
mirrorurl, ok := cp.b.Recipe.Header["mirrorurl"]
if !ok {
return fmt.Errorf("Invalid zypper header, no MirrorURL specified")
}
// look for an OS version if the mirror specifies it
osversion := ""
regex := regexp.MustCompile(`(?i)%{OSVERSION}`)
if regex.MatchString(mirrorurl) {
osversion, ok = cp.b.Recipe.Header["osversion"]
if !ok {
return fmt.Errorf("Invalid zypper header, OSVersion referenced in mirror but no OSVersion specified")
}
mirrorurl = regex.ReplaceAllString(mirrorurl, osversion)
}
include, _ := cp.b.Recipe.Header["include"]
// check for include environment variable and add it to requires string
include += ` ` + os.Getenv("INCLUDE")
// trim leading and trailing whitespace
include = strings.TrimSpace(include)
// add aaa_base to start of include list by default
include = `aaa_base ` + include
// Create the main portion of zypper config
err = cp.genZypperConfig()
if err != nil {
return fmt.Errorf("While generating Zypper config: %v", err)
}
err = cp.copyPseudoDevices()
if err != nil {
return fmt.Errorf("While copying pseudo devices: %v", err)
}
// Add mirrorURL as repo
cmd := exec.Command(zypperPath, `--root`, cp.b.Rootfs(), `ar`, mirrorurl, `repo-oss`)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err = cmd.Run(); err != nil {
return fmt.Errorf("While adding zypper mirror: %v", err)
}
// Refreshing gpg keys
cmd = exec.Command(zypperPath, `--root`, cp.b.Rootfs(), `--gpg-auto-import-keys`, `refresh`)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err = cmd.Run(); err != nil {
return fmt.Errorf("While refreshing gpg keys: %v", err)
}
args := []string{`--non-interactive`, `-c`, filepath.Join(cp.b.Rootfs(), zypperConf), `--root`, cp.b.Rootfs(), `--releasever=` + osversion, `-n`, `install`, `--auto-agree-with-licenses`, `--download-in-advance`}
args = append(args, strings.Fields(include)...)
// Zypper install command
cmd = exec.Command(zypperPath, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
sylog.Debugf("\n\tZypper Path: %s\n\tDetected Arch: %s\n\tOSVersion: %s\n\tMirrorURL: %s\n\tIncludes: %s\n", zypperPath, runtime.GOARCH, osversion, mirrorurl, include)
// run zypper
if err = cmd.Run(); err != nil {
return fmt.Errorf("While bootstrapping from zypper: %v", err)
}
return nil
}
// Pack puts relevant objects in a Bundle!
func (cp *ZypperConveyorPacker) Pack() (b *types.Bundle, err error) {
err = cp.insertBaseEnv()
if err != nil {
return nil, fmt.Errorf("While inserting base environment: %v", err)
}
err = cp.insertRunScript()
if err != nil {
return nil, fmt.Errorf("While inserting runscript: %v", err)
}
return cp.b, nil
}
func (cp *ZypperConveyorPacker) insertBaseEnv() (err error) {
if err = makeBaseEnv(cp.b.Rootfs()); err != nil {
return
}
return nil
}
func (cp *ZypperConveyorPacker) insertRunScript() (err error) {
f, err := os.Create(cp.b.Rootfs() + "/.singularity.d/runscript")
if err != nil {
return
}
defer f.Close()
_, err = f.WriteString("#!/bin/sh\n")
if err != nil {
return
}
if err != nil {
return
}
f.Sync()
err = os.Chmod(cp.b.Rootfs()+"/.singularity.d/runscript", 0755)
if err != nil {
return
}
return nil
}
func (cp *ZypperConveyorPacker) genZypperConfig() (err error) {
err = os.MkdirAll(filepath.Join(cp.b.Rootfs(), "/etc/zypp"), 0775)
if err != nil {
return fmt.Errorf("While creating %v: %v", filepath.Join(cp.b.Rootfs(), "/etc/zypp"), err)
}
err = ioutil.WriteFile(filepath.Join(cp.b.Rootfs(), zypperConf), []byte("[main]\ncachedir=/val/cache/zypp-bootstrap\n\n"), 0664)
if err != nil {
return
}
return nil
}
func (cp *ZypperConveyorPacker) copyPseudoDevices() (err error) {
err = os.Mkdir(filepath.Join(cp.b.Rootfs(), "/dev"), 0775)
if err != nil {
return fmt.Errorf("While creating %v: %v", filepath.Join(cp.b.Rootfs(), "/dev"), err)
}
devs := []string{"/dev/null", "/dev/zero", "/dev/random", "/dev/urandom"}
for _, dev := range devs {
cmd := exec.Command("cp", "-a", dev, filepath.Join(cp.b.Rootfs(), "/dev"))
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err = cmd.Run(); err != nil {
f, err := os.Create(cp.b.Rootfs() + "/.singularity.d/runscript")
if err != nil {
return fmt.Errorf("While creating %v: %v", filepath.Join(cp.b.Rootfs(), dev), err)
}
defer f.Close()
}
}
return nil
}
func rpmPathCheck() (err error) {
var output, stderr bytes.Buffer
cmd := exec.Command("rpm", "--showrc")
cmd.Stdout = &output
cmd.Stderr = &stderr
if err = cmd.Run(); err != nil {
return fmt.Errorf("%v: %v", err, stderr.String())
}
rpmDBPath := ""
scanner := bufio.NewScanner(&output)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
// search for dbpath from showrc output
if strings.Contains(scanner.Text(), "_dbpath\t") {
// second field in the string is the path
rpmDBPath = strings.Fields(scanner.Text())[2]
}
}
if rpmDBPath != `%{_var}/lib/rpm` {
return fmt.Errorf("RPM database is using a non-standard path: %s\n"+
"There is a way to work around this problem:\n"+
"Create a file at path %s/.rpmmacros.\n"+
"Place the following lines into the '.rpmmacros' file:\n"+
"%s\n"+
"%s\n"+
"After creating the file, re-run the bootstrap.\n"+
"More info: https://github.com/sylabs/singularity/issues/241\n",
rpmDBPath, os.Getenv("HOME"), `%_var /var`, `%_dbpath %{_var}/lib/rpm`)
}
return nil
}