-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
update_packages.go
96 lines (78 loc) · 2.6 KB
/
update_packages.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
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package nodetasks
import (
"fmt"
"os"
"os/exec"
"syscall"
"k8s.io/klog/v2"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/nodeup/cloudinit"
"k8s.io/kops/upup/pkg/fi/nodeup/local"
"k8s.io/kops/util/pkg/distributions"
)
type UpdatePackages struct {
// We can't be completely empty or we don't run
Updated bool
}
var _ fi.HasDependencies = &UpdatePackages{}
func NewUpdatePackages() *UpdatePackages {
return &UpdatePackages{Updated: true}
}
func (p *UpdatePackages) GetDependencies(tasks map[string]fi.Task) []fi.Task {
return []fi.Task{}
}
func (p *UpdatePackages) String() string {
return "UpdatePackages"
}
func (e *UpdatePackages) Find(c *fi.Context) (*UpdatePackages, error) {
return nil, nil
}
func (e *UpdatePackages) Run(c *fi.Context) error {
return fi.DefaultDeltaRunMethod(e, c)
}
func (s *UpdatePackages) CheckChanges(a, e, changes *UpdatePackages) error {
return nil
}
func (_ *UpdatePackages) RenderLocal(t *local.LocalTarget, a, e, changes *UpdatePackages) error {
if os.Getenv("SKIP_PACKAGE_UPDATE") != "" {
klog.Infof("SKIP_PACKAGE_UPDATE was set; skipping package update")
return nil
}
d, err := distributions.FindDistribution("/")
if err != nil {
return fmt.Errorf("unknown or unsupported distro: %v", err)
}
var args []string
if d.IsDebianFamily() {
args = []string{"apt-get", "update"}
} else if d.IsRHELFamily() {
// Probably not technically needed
args = []string{"/usr/bin/yum", "check-update"}
} else {
return fmt.Errorf("unsupported package system")
}
klog.Infof("running command %s", args)
cmd := exec.Command(args[0], args[1:]...)
output, err := cmd.CombinedOutput()
// 'yum check-update' exits with 100 if it finds updates; treat it like a success
if exitCode := cmd.ProcessState.Sys().(syscall.WaitStatus).ExitStatus(); err != nil && exitCode != 100 {
return fmt.Errorf("error update packages: %v: %s", err, string(output))
}
return nil
}
func (_ *UpdatePackages) RenderCloudInit(t *cloudinit.CloudInitTarget, a, e, changes *UpdatePackages) error {
t.Config.PackageUpdate = true
return nil
}