-
Notifications
You must be signed in to change notification settings - Fork 74
/
install_binaries.go
91 lines (76 loc) · 2.58 KB
/
install_binaries.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
package phase
import (
"fmt"
"github.com/k0sproject/k0sctl/pkg/apis/k0sctl.k0sproject.io/v1beta1"
"github.com/k0sproject/k0sctl/pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster"
"github.com/k0sproject/rig/exec"
"github.com/sirupsen/logrus"
)
// InstallBinaries installs the k0s binaries from the temp location of UploadBinaries or InstallBinaries
type InstallBinaries struct {
GenericPhase
hosts cluster.Hosts
}
// Title for the phase
func (p *InstallBinaries) Title() string {
return "Install k0s binaries on hosts"
}
// Prepare the phase
func (p *InstallBinaries) Prepare(config *v1beta1.Cluster) error {
p.Config = config
p.hosts = p.Config.Spec.Hosts.Filter(func(h *cluster.Host) bool {
if h.Reset && h.Metadata.K0sBinaryVersion != nil {
return false
}
// Upgrade is handled in UpgradeControllers/UpgradeWorkers phases
if h.Metadata.NeedsUpgrade {
return false
}
return h.Metadata.K0sBinaryTempFile != ""
})
return nil
}
// ShouldRun is true when the phase should be run
func (p *InstallBinaries) ShouldRun() bool {
return len(p.hosts) > 0 || !p.IsWet()
}
// DryRun reports what would happen if Run is called.
func (p *InstallBinaries) DryRun() error {
return p.parallelDo(
p.Config.Spec.Hosts.Filter(func(h *cluster.Host) bool { return h.Metadata.K0sBinaryTempFile != "" }),
func(h *cluster.Host) error {
p.DryMsgf(h, "install k0s %s binary from %s to %s", p.Config.Spec.K0s.Version, h.Metadata.K0sBinaryTempFile, h.Configurer.K0sBinaryPath())
if err := h.Execf(`chmod +x "%s"`, h.Metadata.K0sBinaryTempFile, exec.Sudo(h)); err != nil {
logrus.Warnf("%s: failed to chmod k0s temp binary for dry-run: %s", h, err.Error())
}
h.Configurer.SetPath("K0sBinaryPath", h.Metadata.K0sBinaryTempFile)
h.Metadata.K0sBinaryVersion = p.Config.Spec.K0s.Version
return nil
},
)
}
// Run the phase
func (p *InstallBinaries) Run() error {
return p.parallelDo(p.hosts, p.installBinary)
}
func (p *InstallBinaries) installBinary(h *cluster.Host) error {
if err := h.UpdateK0sBinary(h.Metadata.K0sBinaryTempFile, p.Config.Spec.K0s.Version); err != nil {
return fmt.Errorf("failed to install k0s binary: %w", err)
}
return nil
}
func (p *InstallBinaries) CleanUp() {
err := p.parallelDo(p.hosts, func(h *cluster.Host) error {
if h.Metadata.K0sBinaryTempFile == "" {
return nil
}
logrus.Infof("%s: cleaning up k0s binary tempfile", h)
if err := h.Configurer.DeleteFile(h, h.Metadata.K0sBinaryTempFile); err != nil {
return fmt.Errorf("clean up tempfile: %w", err)
}
return nil
})
if err != nil {
logrus.Debugf("failed to clean up tempfiles: %v", err)
}
}