This repository has been archived by the owner on Nov 20, 2021. It is now read-only.
/
cloudinit.go
146 lines (135 loc) · 3.28 KB
/
cloudinit.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
package cloudinit
import (
"bytes"
"strings"
"text/template"
"github.com/pkg/errors"
machinev1 "github.com/criticalstack/machine-api/api/v1alpha1"
)
const (
nodeCloudInit = `## template: jinja
#cloud-config
{{template "files" .Files}}
runcmd:
{{- template "commands" .PreCritCommands }}
- 'crit up --config /var/lib/crit/config.yaml {{.Verbosity}}'
{{- template "commands" .PostCritCommands }}
{{- template "ntp" .NTP }}
{{- template "users" .Users }}
`
commandsTemplate = `{{- define "commands" -}}
{{ range . }}
- {{printf "%q" .}}
{{- end -}}
{{- end -}}
`
filesTemplate = `{{ define "files" -}}
write_files:{{ range . }}
- path: {{.Path}}
{{ if ne .Encoding "" -}}
encoding: "{{.Encoding}}"
{{ end -}}
{{ if ne .Owner "" -}}
owner: {{.Owner}}
{{ end -}}
{{ if ne .Permissions "" -}}
permissions: '{{.Permissions}}'
{{ end -}}
content: |
{{.Content | Indent 6}}
{{- end -}}
{{- end -}}
`
ntpTemplate = `{{ define "ntp" -}}
{{- if . }}
ntp:
{{ if .Enabled -}}
enabled: true
{{ end -}}
servers:{{ range .Servers }}
- {{ . }}
{{- end -}}
{{- end -}}
{{- end -}}
`
usersTemplate = `{{ define "users" -}}
{{- if . }}
users:{{ range . }}
- name: {{ .Name }}
{{- if .Passwd }}
passwd: {{ .Passwd }}
{{- end -}}
{{- if .Gecos }}
gecos: {{ .Gecos }}
{{- end -}}
{{- if .Groups }}
groups: {{ .Groups }}
{{- end -}}
{{- if .HomeDir }}
homedir: {{ .HomeDir }}
{{- end -}}
{{- if .Inactive }}
inactive: true
{{- end -}}
{{- if .LockPassword }}
lock_passwd: {{ .LockPassword }}
{{- end -}}
{{- if .Shell }}
shell: {{ .Shell }}
{{- end -}}
{{- if .PrimaryGroup }}
primary_group: {{ .PrimaryGroup }}
{{- end -}}
{{- if .Sudo }}
sudo: {{ .Sudo }}
{{- end -}}
{{- if .SSHAuthorizedKeys }}
ssh_authorized_keys:{{ range .SSHAuthorizedKeys }}
- {{ . }}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
`
)
type Config struct {
Files []machinev1.File
PreCritCommands []string
PostCritCommands []string
Users []machinev1.User
NTP *machinev1.NTP
Format machinev1.Format
Verbosity bool
}
func Write(input *Config) ([]byte, error) {
tm := template.New("Node").Funcs(template.FuncMap{
"trim": strings.TrimSpace,
"Indent": func(i int, input string) string {
split := strings.Split(input, "\n")
ident := "\n" + strings.Repeat(" ", i)
return strings.Repeat(" ", i) + strings.Join(split, ident)
},
})
if _, err := tm.Parse(filesTemplate); err != nil {
return nil, errors.Wrap(err, "failed to parse files template")
}
if _, err := tm.Parse(commandsTemplate); err != nil {
return nil, errors.Wrap(err, "failed to parse commands template")
}
if _, err := tm.Parse(ntpTemplate); err != nil {
return nil, errors.Wrap(err, "failed to parse ntp template")
}
if _, err := tm.Parse(usersTemplate); err != nil {
return nil, errors.Wrap(err, "failed to parse users template")
}
t, err := tm.Parse(nodeCloudInit)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse %s template", "Node")
}
var out bytes.Buffer
if err := t.Execute(&out, input); err != nil {
return nil, errors.Wrapf(err, "failed to generate %s template", "Node")
}
return out.Bytes(), nil
}