-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
archive.go
232 lines (186 loc) · 6.55 KB
/
archive.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
/*
Copyright 2017 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 (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"reflect"
"strconv"
"strings"
"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/hashing"
)
// Archive task downloads and extracts a tar file
type Archive struct {
Name string
// Source is the location for the archive
Source string `json:"source,omitempty"`
// Hash is the source tar
Hash string `json:"hash,omitempty"`
// TargetDir is the directory for extraction
TargetDir string `json:"target,omitempty"`
// StripComponents is the number of components to remove when expanding the archive
StripComponents int `json:"stripComponents,omitempty"`
// MapFiles is the list of files to extract with corresponding directories to extract
MapFiles map[string]string `json:"mapFiles,omitempty"`
}
const (
localArchiveDir = "/var/cache/nodeup/archives/"
localArchiveStateDir = "/var/cache/nodeup/archives/state/"
)
var _ fi.HasDependencies = &Archive{}
// GetDependencies implements HasDependencies::GetDependencies
func (e *Archive) GetDependencies(tasks map[string]fi.Task) []fi.Task {
var deps []fi.Task
// Requires parent directories to be created
deps = append(deps, findCreatesDirParents(e.TargetDir, tasks)...)
return deps
}
var _ fi.HasName = &Archive{}
func (e *Archive) GetName() *string {
return &e.Name
}
// String returns a string representation, implementing the Stringer interface
func (e *Archive) String() string {
return fmt.Sprintf("Archive: %s %s->%s", e.Name, e.Source, e.TargetDir)
}
var _ CreatesDir = &Archive{}
// Dir implements CreatesDir::Dir
func (e *Archive) Dir() string {
return e.TargetDir
}
// Find implements fi.Task::Find
func (e *Archive) Find(c *fi.Context) (*Archive, error) {
// We write a marker file to prevent re-execution
localStateFile := path.Join(localArchiveStateDir, e.Name)
stateBytes, err := ioutil.ReadFile(localStateFile)
if err != nil {
if os.IsNotExist(err) {
stateBytes = nil
} else {
klog.Warningf("error reading archive state %s: %v", localStateFile, err)
// We can just reinstall
return nil, nil
}
}
if stateBytes == nil {
// No marker file found, assume archive not installed
return nil, nil
}
state := &Archive{}
if err := json.Unmarshal(stateBytes, state); err != nil {
klog.Warningf("error unmarshaling archive state %s: %v", localStateFile, err)
// We can just reinstall
return nil, nil
}
if state.Hash == e.Hash && state.TargetDir == e.TargetDir {
return state, nil
}
// Existing version is different, force a reinstall
return nil, nil
}
// Run implements fi.Task::Run
func (e *Archive) Run(c *fi.Context) error {
return fi.DefaultDeltaRunMethod(e, c)
}
// CheckChanges implements fi.Task::CheckChanges
func (_ *Archive) CheckChanges(a, e, changes *Archive) error {
return nil
}
// RenderLocal implements the fi.Task::Render functionality for a local target
func (_ *Archive) RenderLocal(t *local.LocalTarget, a, e, changes *Archive) error {
if a == nil {
klog.Infof("Installing archive %q", e.Name)
localFile := path.Join(localArchiveDir, e.Name)
if err := os.MkdirAll(localArchiveDir, 0755); err != nil {
return fmt.Errorf("error creating directories %q: %v", localArchiveDir, err)
}
var hash *hashing.Hash
if e.Hash != "" {
parsed, err := hashing.FromString(e.Hash)
if err != nil {
return fmt.Errorf("error parsing hash: %v", err)
}
hash = parsed
}
if _, err := fi.DownloadURL(e.Source, localFile, hash); err != nil {
return err
}
if len(e.MapFiles) == 0 {
targetDir := e.TargetDir
if err := os.MkdirAll(targetDir, 0755); err != nil {
return fmt.Errorf("error creating directories %q: %v", targetDir, err)
}
args := []string{"tar", "xf", localFile, "-C", targetDir}
if e.StripComponents != 0 {
args = append(args, "--strip-components="+strconv.Itoa(e.StripComponents))
}
klog.Infof("running command %s", args)
cmd := exec.Command(args[0], args[1:]...)
if output, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("error installing archive %q: %v: %s", e.Name, err, string(output))
}
} else {
for src, dest := range e.MapFiles {
stripCount := strings.Count(src, "/")
targetDir := filepath.Join(e.TargetDir, dest)
if err := os.MkdirAll(targetDir, 0755); err != nil {
return fmt.Errorf("error creating directories %q: %v", targetDir, err)
}
args := []string{"tar", "xf", localFile, "-C", targetDir, "--wildcards", "--strip-components=" + strconv.Itoa(stripCount), src}
klog.Infof("running command %s", args)
cmd := exec.Command(args[0], args[1:]...)
if output, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("error installing archive %q: %v: %s", e.Name, err, string(output))
}
}
}
// We write a marker file to prevent re-execution
localStateFile := path.Join(localArchiveStateDir, e.Name)
if err := os.MkdirAll(localArchiveStateDir, 0755); err != nil {
return fmt.Errorf("error creating directories %q: %v", localArchiveStateDir, err)
}
state, err := json.MarshalIndent(e, "", " ")
if err != nil {
return fmt.Errorf("error marshaling archive state: %v", err)
}
if err := ioutil.WriteFile(localStateFile, state, 0644); err != nil {
return fmt.Errorf("error writing archive state: %v", err)
}
} else {
if !reflect.DeepEqual(changes, &Archive{}) {
klog.Warningf("cannot apply archive changes for %q: %v", e.Name, changes)
}
}
return nil
}
// RenderCloudInit implements fi.Task::Render functionality for a CloudInit target
func (_ *Archive) RenderCloudInit(t *cloudinit.CloudInitTarget, a, e, changes *Archive) error {
archiveName := e.Name
localFile := path.Join(localArchiveDir, archiveName)
t.AddMkdirpCommand(localArchiveDir, 0755)
targetDir := e.TargetDir
t.AddMkdirpCommand(targetDir, 0755)
url := e.Source
t.AddDownloadCommand(cloudinit.Always, url, localFile)
t.AddCommand(cloudinit.Always, "tar", "xf", localFile, "-C", targetDir)
return nil
}