-
Notifications
You must be signed in to change notification settings - Fork 80
/
script.go
121 lines (96 loc) · 2.46 KB
/
script.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
package ipxe
import "fmt"
type Script struct {
buf []byte
}
func NewScript() *Script {
return new(Script).Reset()
}
func (s *Script) Args(args ...string) *Script {
s.buf = s.buf[:len(s.buf)-1]
for _, arg := range args {
s.buf = append(append(s.buf, ' '), arg...)
}
s.buf = append(s.buf, '\n')
return s
}
// AppendString takes a string and appends it to the current Script
func (s *Script) AppendString(s_script string) *Script {
s.buf = append(s.buf, s_script...)
s.buf = append(s.buf, '\n')
return s
}
// PhoneHome takes a type and will post boots device connected to dhcp event
func (s *Script) PhoneHome(typ string) *Script {
s.buf = append(s.buf, `
params
param body Device connected to DHCP system
param type `+typ+`
imgfetch ${tinkerbell}/phone-home##params
imgfree
`...)
return s
}
// Chain - Chainload another iPXE script
func (s *Script) Chain(uri string) *Script {
s.buf = append(append(s.buf, "chain --autofree "...), uri...)
s.buf = append(s.buf, '\n')
return s
}
func (s *Script) DHCP() *Script {
s.buf = append(s.buf, "dhcp\n"...)
return s
}
func (s *Script) Boot() *Script {
s.buf = append(s.buf, "boot\n"...)
return s
}
func (s *Script) Bytes() []byte {
return s.buf
}
func (s *Script) Initrd(uri string, args ...string) *Script {
s.buf = append(append(s.buf, "initrd "...), uri...)
for _, arg := range args {
s.buf = append(append(s.buf, ' '), arg...)
}
s.buf = append(s.buf, '\n')
return s
}
func (s *Script) Kernel(uri string, args ...string) *Script {
s.buf = append(append(s.buf, "kernel "...), uri...)
for _, arg := range args {
s.buf = append(append(s.buf, ' '), arg...)
}
s.buf = append(s.buf, '\n')
return s
}
func (s *Script) Or(line string) *Script {
s.buf = append(s.buf[:len(s.buf)-1], " || "...)
s.buf = append(s.buf, line...)
s.buf = append(s.buf, '\n')
return s
}
func (s *Script) Reset() *Script {
s.buf = append(s.buf[:0], "#!ipxe\n\n"...)
return s
}
// Echo outputs a string to console
func (s *Script) Echo(message string) *Script {
s.buf = append(append(s.buf, "echo "...), message...)
s.buf = append(s.buf, '\n')
return s
}
func (s *Script) Set(name, value string) *Script {
s.buf = append(append(s.buf, "set "...), name...)
s.buf = append(append(s.buf, ' '), value...)
s.buf = append(s.buf, '\n')
return s
}
func (s *Script) Shell() *Script {
s.buf = append(s.buf, "shell\n"...)
return s
}
func (s *Script) Sleep(value int) *Script {
s.buf = append(s.buf, fmt.Sprintf("sleep %d\n", value)...)
return s
}