/
writefiles.go
173 lines (147 loc) · 4.59 KB
/
writefiles.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
/*
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 cloudinit
import (
"bytes"
"compress/gzip"
"encoding/base64"
"fmt"
"io"
"path/filepath"
"strings"
"github.com/pkg/errors"
"sigs.k8s.io/yaml"
)
// writeFilesAction defines a list of files that should be written to a node.
type writeFilesAction struct {
Files []files `json:"write_files,"`
}
type files struct {
Path string `json:"path,"`
Encoding string `json:"encoding,omitempty"`
Owner string `json:"owner,omitempty"`
Permissions string `json:"permissions,omitempty"`
Content string `json:"content,"`
Append bool `json:"append,"`
}
func newWriteFilesAction() action {
return &writeFilesAction{}
}
func (a *writeFilesAction) Unmarshal(userData []byte) error {
if err := yaml.Unmarshal(userData, a); err != nil {
return errors.Wrapf(err, "error parsing write_files action: %s", userData)
}
return nil
}
// Commands return a list of commands to run on the node.
// Each command defines the parameters of a shell command necessary to generate a file replicating the cloud-init write_files module.
func (a *writeFilesAction) Commands() ([]Cmd, error) {
commands := make([]Cmd, 0)
for _, f := range a.Files {
// Fix attributes and apply defaults
path := fixPath(f.Path) // NB. the real cloud init module for writes files converts path into absolute paths; this is not possible here...
encodings := fixEncoding(f.Encoding)
owner := fixOwner(f.Owner)
permissions := fixPermissions(f.Permissions)
content, err := fixContent(f.Content, encodings)
if err != nil {
return commands, errors.Wrapf(err, "error decoding content for %s", path)
}
// Make the directory so cat + redirection will work
directory := filepath.Dir(path)
commands = append(commands, Cmd{Cmd: "mkdir", Args: []string{"-p", directory}})
redirects := ">"
if f.Append {
redirects = ">>"
}
// generate a command that will create a file with the epxected contents.
commands = append(commands, Cmd{Cmd: "/bin/sh", Args: []string{"-c", fmt.Sprintf("cat %s %s /dev/stdin", redirects, path)}, Stdin: content})
// if permissions are different than default ownership, add a command to modify the permissions.
if permissions != "0644" {
commands = append(commands, Cmd{Cmd: "chmod", Args: []string{permissions, path}})
}
// if ownership is different than default ownership, add a command to modify file ownerhsip.
if owner != "root:root" {
commands = append(commands, Cmd{Cmd: "chown", Args: []string{owner, path}})
}
}
return commands, nil
}
func fixPath(p string) string {
return strings.TrimSpace(p)
}
func fixOwner(o string) string {
o = strings.TrimSpace(o)
if o != "" {
return o
}
return "root:root"
}
func fixPermissions(p string) string {
p = strings.TrimSpace(p)
if p != "" {
return p
}
return "0644"
}
func fixEncoding(e string) []string {
e = strings.ToLower(e)
e = strings.TrimSpace(e)
switch e {
case "gz", "gzip":
return []string{"application/x-gzip"}
case "gz+base64", "gzip+base64", "gz+b64", "gzip+b64":
return []string{"application/base64", "application/x-gzip"}
case "base64", "b64":
return []string{"application/base64"}
}
return []string{"text/plain"}
}
func fixContent(content string, encodings []string) (string, error) {
for _, e := range encodings {
switch e {
case "application/base64":
rByte, err := base64.StdEncoding.DecodeString(content)
if err != nil {
return content, errors.WithStack(err)
}
return string(rByte), nil
case "application/x-gzip":
rByte, err := gUnzipData([]byte(content))
if err != nil {
return content, err
}
return string(rByte), nil
case "text/plain":
return content, nil
default:
return content, errors.Errorf("Unknown bootstrap data encoding: %q", content)
}
}
return content, nil
}
func gUnzipData(data []byte) ([]byte, error) {
var r io.Reader
var err error
b := bytes.NewBuffer(data)
r, err = gzip.NewReader(b)
if err != nil {
return nil, errors.WithStack(err)
}
var resB bytes.Buffer
_, err = resB.ReadFrom(r)
if err != nil {
return nil, errors.WithStack(err)
}
return resB.Bytes(), nil
}