/
templates.go
142 lines (125 loc) · 3.68 KB
/
templates.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
/*
Copyright 2022 The Tinkerbell 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 templates provides methods for rendering templates used for
// creating Tinkerbell machines for ClusterAPI.
package templates
import (
"bytes"
"fmt"
"text/template"
"github.com/pkg/errors"
)
var (
// ErrMissingName is the error returned when the WorfklowTemplate Name is not specified.
ErrMissingName = fmt.Errorf("name can't be empty")
// ErrMissingImageURL is the error returned when the WorfklowTemplate ImageURL is not specified.
ErrMissingImageURL = fmt.Errorf("imageURL can't be empty")
)
// WorkflowTemplate is a helper struct for rendering CAPT Template data.
type WorkflowTemplate struct {
Name string
MetadataURL string
ImageURL string
DestDisk string
DestPartition string
DeviceTemplateName string
}
// Render renders workflow template for a given machine including user-data.
func (wt *WorkflowTemplate) Render() (string, error) {
if wt.Name == "" {
return "", ErrMissingName
}
if wt.ImageURL == "" {
return "", ErrMissingImageURL
}
if wt.DeviceTemplateName == "" {
wt.DeviceTemplateName = "{{.device_1}}"
}
tpl, err := template.New("template").Parse(workflowTemplate)
if err != nil {
return "", errors.Wrap(err, "unable to parse template")
}
buf := &bytes.Buffer{}
err = tpl.Execute(buf, wt)
if err != nil {
return "", errors.Wrap(err, "unable to execute template")
}
return buf.String(), nil
}
const (
workflowTemplate = `
version: "0.1"
name: {{.Name}}
global_timeout: 6000
tasks:
- name: "{{.Name}}"
worker: "{{.DeviceTemplateName}}"
volumes:
- /dev:/dev
- /dev/console:/dev/console
- /lib/firmware:/lib/firmware:ro
actions:
- name: "stream-image"
image: oci2disk:v1.0.0
timeout: 600
environment:
IMG_URL: {{.ImageURL}}
DEST_DISK: {{.DestDisk}}
COMPRESSED: true
- name: "add-tink-cloud-init-config"
image: writefile:v1.0.0
timeout: 90
environment:
DEST_DISK: {{.DestPartition}}
FS_TYPE: ext4
DEST_PATH: /etc/cloud/cloud.cfg.d/10_tinkerbell.cfg
UID: 0
GID: 0
MODE: 0600
DIRMODE: 0700
CONTENTS: |
datasource:
Ec2:
metadata_urls: ["{{.MetadataURL}}"]
strict_id: false
system_info:
default_user:
name: tink
groups: [wheel, adm]
sudo: ["ALL=(ALL) NOPASSWD:ALL"]
shell: /bin/bash
manage_etc_hosts: localhost
warnings:
dsid_missing_source: off
- name: "add-tink-cloud-init-ds-config"
image: writefile:v1.0.0
timeout: 90
environment:
DEST_DISK: {{.DestPartition}}
FS_TYPE: ext4
DEST_PATH: /etc/cloud/ds-identify.cfg
UID: 0
GID: 0
MODE: 0600
DIRMODE: 0700
CONTENTS: |
datasource: Ec2
- name: "kexec-image"
image: kexec:v1.0.0
timeout: 90
pid: host
environment:
BLOCK_DEVICE: {{.DestPartition}}
FS_TYPE: ext4
`
)