-
Notifications
You must be signed in to change notification settings - Fork 0
/
execute.go
88 lines (73 loc) · 1.66 KB
/
execute.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
// +build !windows
package rkeworker
import (
"bytes"
"context"
"encoding/base64"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/rancher/norman/types"
"github.com/rancher/rancher/pkg/rkecerts"
)
func ExecutePlan(ctx context.Context, nodeConfig *NodeConfig, writeCertOnly bool) error {
if nodeConfig.Certs != "" {
bundle, err := rkecerts.Unmarshal(nodeConfig.Certs)
if err != nil {
return err
}
if err := bundle.Explode(); err != nil {
return err
}
}
f := fileWriter{}
for _, file := range nodeConfig.Files {
f.write(file.Name, file.Contents)
}
if writeCertOnly {
return nil
}
for name, process := range nodeConfig.Processes {
if strings.Contains(name, "sidekick") || strings.Contains(name, "share-mnt") {
if err := runProcess(ctx, name, process, false); err != nil {
return err
}
}
}
for name, process := range nodeConfig.Processes {
if !strings.Contains(name, "sidekick") {
if err := runProcess(ctx, name, process, true); err != nil {
return err
}
}
}
return nil
}
type fileWriter struct {
errs []error
}
func (f *fileWriter) write(path string, base64Content string) {
if path == "" {
return
}
content, err := base64.StdEncoding.DecodeString(base64Content)
if err != nil {
f.errs = append(f.errs, err)
return
}
existing, err := ioutil.ReadFile(path)
if err == nil && bytes.Equal(existing, content) {
return
}
dir := filepath.Dir(path)
if err := os.MkdirAll(dir, 0700); err != nil {
f.errs = append(f.errs, err)
}
if err := ioutil.WriteFile(path, content, 0600); err != nil {
f.errs = append(f.errs, err)
}
}
func (f *fileWriter) err() error {
return types.NewErrors(f.errs...)
}