forked from oracle/smith
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock.go
152 lines (131 loc) · 3.66 KB
/
mock.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
package main
import (
"io/ioutil"
"path/filepath"
"regexp"
"strings"
"github.com/oracle/smith/execute"
"github.com/Sirupsen/logrus"
)
const (
MOCK = "/usr/bin/mock"
SITE = "/etc/mock/site-defaults.cfg"
BASEDIR = "/var/lib/mock"
)
func MockBuildDebuginfo(pkgMfst *RPMManifest, mock *MockDef) (string, error) {
config := mock.Config
args := []string{}
args = append(args, "-r")
args = append(args, config)
args = append(args, "--install")
wantedList := pkgMfst.DebugCandidates(mock.DebugDeps)
args = append(args, wantedList...)
outpath := getOutPath(config)
_, _, err := execute.Execute(MOCK, args...)
if err != nil {
logrus.Errorf("Failed to install debuginfo: %v", err)
return "", err
}
outpath = filepath.Join(outpath, "root")
if err = pkgMfst.FindDebugInstalled(outpath, wantedList, mock.Config); err != nil {
return "", err
}
return outpath, nil
}
func MockExecuteQuiet(config, name string, arg ...string) (string, string, error) {
args := []string{}
args = append(args, "-r")
args = append(args, config)
args = append(args, "--chroot")
args = append(args, "--")
// combine command into single arg so shell expansion in the chroot works
command := []string{name}
command = append(command, arg...)
args = append(args, strings.Join(command, " "))
return execute.ExecuteQuiet(MOCK, args...)
}
func MockCopy(config, src, dst string) error {
args := []string{}
args = append(args, "-r")
args = append(args, config)
args = append(args, "--copyin")
args = append(args, "--")
args = append(args, src)
args = append(args, dst)
_, stderr, err := execute.ExecuteQuiet(MOCK, args...)
if err != nil {
logrus.Warnf("Copyin failed with stderr: %s", stderr)
}
return err
}
func MockBuild(name string, fast bool, mock *MockDef) (string, error) {
config := mock.Config
args := []string{}
args = append(args, "-r")
args = append(args, config)
outpath := getOutPath(config)
if fast {
inst := strings.TrimSuffix(name, ".rpm")
instArgs := append(args, "--yum-cmd", "--", "-C", "list", "installed", inst)
_, _, err := execute.Execute(MOCK, instArgs...)
if err == nil {
return filepath.Join(outpath, "root"), nil
}
} else {
_, _, err := execute.Execute(MOCK, append(args, "--clean")...)
if err != nil {
logrus.Errorf("Failed to reset build environment: %v", err)
return "", err
}
}
if mock.PreBuild != "" {
_, _, err := execute.Execute(mock.PreBuild)
if err != nil {
logrus.Errorf("Failed to run pre-build: %v", err)
return "", err
}
}
if mock.PostBuild != "" {
defer func() {
_, _, err := execute.Execute(mock.PostBuild)
if err != nil {
logrus.Errorf("Failed to run post-build: %v", err)
}
}()
}
installArgs := append(args, "--install")
installArgs = append(installArgs, mock.Deps...)
installArgs = append(installArgs, name)
_, _, err := execute.Execute(MOCK, installArgs...)
if err != nil {
logrus.Errorf("Failed to install %v: %v", name, err)
return "", err
}
return filepath.Join(outpath, "root"), nil
}
func getOutPath(config string) string {
data, err := ioutil.ReadFile(config)
if err != nil {
return ""
}
re := regexp.MustCompile(`(?m)^config_opts\['root'\]\s*=\s*'([^']+)'`)
rootMatch := re.FindSubmatch(data)
if len(rootMatch) < 2 {
return ""
}
basedir := BASEDIR
re = regexp.MustCompile(`(?m)^config_opts\['basedir'\]\s*=\s*'([^']+)'`)
basedirMatch := re.FindSubmatch(data)
if len(basedirMatch) >= 2 {
basedir = string(basedirMatch[1])
} else {
siteData, err := ioutil.ReadFile(SITE)
if err == nil {
basedirMatch = re.FindSubmatch(siteData)
if len(basedirMatch) >= 2 {
basedir = string(basedirMatch[1])
}
}
}
return filepath.Join(basedir, string(rootMatch[1]))
}