This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
package-manager-install.go
124 lines (111 loc) · 3.52 KB
/
package-manager-install.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
package dep
import (
"bytes"
"fmt"
"os"
"os/exec"
"strings"
"github.com/pkg/errors"
)
func (p *PackageManager) rembasedInstall(installVersion *Software, more ...*Software) error {
var errBuf bytes.Buffer
pkgs := make([]string, len(more)+1)
for idx, sw := range append([]*Software{installVersion}, more...) {
pkgs[idx] = sw.Package
if sw.Version == "" {
continue
}
pkgs[idx] = fmt.Sprintf("%s-%s", sw.Package, sw.Version)
cmd := exec.Command("yum", "versionlock", "delete", sw.Package)
cmd.Stderr = &errBuf
if p.monitor.IsVerbose() {
fmt.Println(strings.Join(cmd.Args, " "))
cmd.Stdout = os.Stdout
}
err := cmd.Run()
stderr := errBuf.String()
if err != nil && !strings.Contains(stderr, "versionlock delete: no matches") {
return errors.Wrapf(err, "unlocking package %s failed with stderr %s", sw.Package, stderr)
}
errBuf.Reset()
cmd = exec.Command("yum", "versionlock", "add", "-y", pkgs[idx])
cmd.Stderr = &errBuf
if p.monitor.IsVerbose() {
fmt.Println(strings.Join(cmd.Args, " "))
cmd.Stdout = os.Stdout
}
if err := cmd.Run(); err != nil {
return errors.Wrapf(err, "locking package %s at version %s failed with stderr %s", sw.Package, sw.Version, errBuf.String())
}
errBuf.Reset()
}
cmd := exec.Command("yum", append([]string{"install", "-y"}, pkgs...)...)
cmd.Stderr = &errBuf
if p.monitor.IsVerbose() {
fmt.Println(strings.Join(cmd.Args, " "))
cmd.Stdout = os.Stdout
}
return errors.Wrapf(cmd.Run(), "installing yum packages %s failed with stderr %s", strings.Join(pkgs, " and "), errBuf.String())
}
// TODO: Use lower level apt instead of apt-get?
func (p *PackageManager) debbasedInstall(installVersion *Software, more ...*Software) error {
var errBuf bytes.Buffer
pkgs := make([]string, len(more)+1)
hold := make([]string, 0)
for idx, sw := range append([]*Software{installVersion}, more...) {
pkgs[idx] = sw.Package
if sw.Version == "" {
continue
}
pkgs[idx] = fmt.Sprintf("%s=%s", sw.Package, sw.Version)
hold = append(hold, sw.Package)
cmd := exec.Command("apt-mark", "unhold", sw.Package)
cmd.Stderr = &errBuf
if p.monitor.IsVerbose() {
fmt.Println(strings.Join(cmd.Args, " "))
cmd.Stdout = os.Stdout
}
if err := cmd.Run(); err != nil {
return errors.Wrapf(err, "unholding installed package failed with stderr %s", errBuf.String())
}
errBuf.Reset()
}
cmd := exec.Command("dpkg", "--configure", "-a")
cmd.Stderr = &errBuf
if p.monitor.IsVerbose() {
fmt.Println(strings.Join(cmd.Args, " "))
cmd.Stdout = os.Stdout
}
if err := cmd.Run(); err != nil {
return errors.Wrapf(err, "cleaning up dpkg failed with stderr %s", errBuf.String())
}
errBuf.Reset()
cmd = exec.Command("apt-get", append(strings.Fields(
"--assume-yes --allow-downgrades install -y"), pkgs...)...)
cmd.Stderr = &errBuf
if p.monitor.IsVerbose() {
fmt.Println(strings.Join(cmd.Args, " "))
cmd.Stdout = os.Stdout
}
if err := cmd.Run(); err != nil {
return errors.Wrapf(err, "installing package failed with stderr %s", errBuf.String())
}
errBuf.Reset()
for _, pkg := range hold {
cmd = exec.Command("apt-mark", "hold", pkg)
cmd.Stderr = &errBuf
if p.monitor.IsVerbose() {
fmt.Println(strings.Join(cmd.Args, " "))
cmd.Stdout = os.Stdout
}
if err := cmd.Run(); err != nil {
return errors.Wrapf(err, "holding package failed with stderr %s", errBuf.String())
}
errBuf.Reset()
p.monitor.WithFields(map[string]interface{}{
"package": installVersion.Package,
"version": installVersion.Version,
}).Debug("Installed package")
}
return nil
}