-
Notifications
You must be signed in to change notification settings - Fork 27
/
dnf.go
260 lines (211 loc) · 6.31 KB
/
dnf.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
package dnf
/*
Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
*/
import (
"bytes"
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/facebookincubator/go2chef/util/temp"
"github.com/facebookincubator/go2chef/util"
"github.com/facebookincubator/go2chef"
"github.com/mitchellh/mapstructure"
)
// TypeNames for the three variants of this step plugin
const (
TypeName = "go2chef.step.install.linux.dnf"
YumTypeName = "go2chef.step.install.linux.yum"
RPMTypeName = "go2chef.step.install.linux.rpm"
)
var (
// DefaultPackageName is the default package name to use for Chef installation
DefaultPackageName = "chef"
)
/*
Step implements Chef installation via RHEL/Fedora DNF/YUM/RPM
If you provide a `source` config block, this plugin will download it and search for
an RPM based on `package_name` (and, if specified, `version`).
Example config for a Chef install
{
"type": "go2chef.step.install.linux.dnf",
"name": "install chef",
"package_name": "chef"
}
*/
type Step struct {
StepName string `mapstructure:"name"`
DNFBinary string `mapstructure:"dnf_binary"`
RPMBinary string `mapstructure:"rpm_binary"`
PackageName string `mapstructure:"package_name"`
Version string `mapstructure:"version"`
RPMCheckTimeoutSeconds int `mapstructure:"rpm_check_timeout_seconds"`
InstallTimeoutSeconds int `mapstructure:"install_timeout_seconds"`
installWithRPM bool
logger go2chef.Logger
source go2chef.Source
downloadPath string
packageRegex *regexp.Regexp
packageVersionRegex *regexp.Regexp
}
func (s *Step) String() string {
return "<" + TypeName + ":" + s.StepName + ">"
}
// SetName sets the name of this step instance
func (s *Step) SetName(str string) {
s.StepName = str
}
// Name gets the name of this step instance
func (s *Step) Name() string {
return s.StepName
}
// Type returns the type of this step instance
func (s *Step) Type() string {
return TypeName
}
// Download fetches resources required for this step's execution
func (s *Step) Download() error {
if s.source == nil {
return nil
}
if s.isInstalled() {
return nil
}
tmpdir, err := temp.Dir("", "go2chef-install")
if err != nil {
return err
}
if err := s.source.DownloadToPath(tmpdir); err != nil {
return err
}
s.downloadPath = tmpdir
return nil
}
// Execute performs the installation
func (s *Step) Execute() error {
installPackage := s.PackageName
if !s.isInstalled() {
if s.source != nil {
rpm, err := s.findRPM()
if err != nil {
s.logger.Errorf("step execution failed: could not find step RPM from %s", s.source.Name())
return err
}
installPackage = filepath.Join(s.downloadPath, rpm)
}
if s.installWithRPM {
return s.installChefRPM(installPackage)
}
return s.installChefDNF(installPackage)
}
s.logger.Infof("%s specified is already installed, not reinstalling", installPackage)
return nil
}
// LoaderForBinary provides an instantiation function for this step plugin specific to the passed binary
func LoaderForBinary(binary string) go2chef.StepLoader {
return func(config map[string]interface{}) (go2chef.Step, error) {
step := &Step{
StepName: "",
DNFBinary: "/usr/bin/" + binary,
RPMBinary: "/usr/bin/rpm",
PackageName: DefaultPackageName,
RPMCheckTimeoutSeconds: 60,
InstallTimeoutSeconds: 300,
installWithRPM: binary == "rpm",
logger: go2chef.GetGlobalLogger(),
source: nil,
downloadPath: "",
}
if err := mapstructure.Decode(config, step); err != nil {
return nil, err
}
source, err := go2chef.GetSourceFromStepConfig(config)
if err != nil {
return nil, err
}
step.source = source
reStr := fmt.Sprintf("^%s-.*.rpm$", step.PackageName)
regex, err := regexp.Compile(reStr)
if err != nil {
step.logger.Errorf("failed to compile package matching regex %s", reStr)
return nil, err
}
step.packageRegex = regex
vreStr := fmt.Sprintf("^%s-%s.*", step.PackageName, step.Version)
vRegex, err := regexp.Compile(vreStr)
if err != nil {
step.logger.Errorf("failed to compile package version match regex %s", vreStr)
}
step.packageVersionRegex = vRegex
return step, nil
}
}
var _ go2chef.Step = &Step{}
func init() {
go2chef.RegisterStep(TypeName, LoaderForBinary("dnf"))
go2chef.RegisterStep(YumTypeName, LoaderForBinary("yum"))
go2chef.RegisterStep(RPMTypeName, LoaderForBinary("rpm"))
}
func (s *Step) findRPM() (string, error) {
s.logger.Debugf(0, "searching for RPM in %s matching %s", s.downloadPath, s.packageRegex)
return util.MatchPath(s.downloadPath, s.packageRegex)
}
func (s *Step) isInstalled() bool {
installed := false
if s.Version != "" {
if err := s.checkInstalled(); err != nil {
switch err.(type) {
case *exec.ExitError:
installed = false
case *go2chef.ErrChefAlreadyInstalled:
s.logger.Infof("%s", err)
installed = true
}
}
}
return installed
}
func (s *Step) checkInstalled() error {
chkCtx, chkCtxCancel := context.WithTimeout(context.Background(), time.Duration(s.RPMCheckTimeoutSeconds)*time.Second)
defer chkCtxCancel()
// run rpm -q <package> to get current package
buf := &bytes.Buffer{}
cmd := exec.CommandContext(chkCtx, s.RPMBinary, "-q", s.PackageName)
cmd.Stdin = nil
cmd.Stderr = os.Stderr
cmd.Stdout = buf
if err := cmd.Run(); err != nil {
return err
}
inst := strings.TrimSpace(buf.String())
if s.packageVersionRegex.MatchString(inst) {
return &go2chef.ErrChefAlreadyInstalled{
Installed: inst,
Requested: s.packageVersionRegex.String(),
}
}
return nil
}
func (s *Step) installChefDNF(installPackage string) error {
instCtx, instCtxCancel := context.WithTimeout(context.Background(), time.Duration(s.InstallTimeoutSeconds)*time.Second)
defer instCtxCancel()
cmd := exec.CommandContext(instCtx, s.DNFBinary, "-y", "install", installPackage)
cmd.Stdin = nil
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
return cmd.Run()
}
func (s *Step) installChefRPM(installPackage string) error {
instCtx, instCtxCancel := context.WithTimeout(context.Background(), time.Duration(s.InstallTimeoutSeconds)*time.Second)
defer instCtxCancel()
cmd := exec.CommandContext(instCtx, s.RPMBinary, "-Uvh", "--oldpackage", installPackage)
cmd.Stdin = nil
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
return cmd.Run()
}