forked from tinkerbell/smee
/
ignition.go
54 lines (47 loc) · 1.45 KB
/
ignition.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
package coreos
import (
"fmt"
"net/http"
"github.com/packethost/tinkerbell/files/ignition"
"github.com/packethost/tinkerbell/files/unit"
"github.com/packethost/tinkerbell/installers"
"github.com/packethost/tinkerbell/job"
)
func init() {
http.HandleFunc("/coreos/ignition.json", serveIgnitionConfig("coreos"))
http.HandleFunc("/flatcar/ignition.json", serveIgnitionConfig("flatcar"))
}
func buildNetworkUnits(j job.Job) (nu ignition.NetworkUnits) {
configureBondDevUnit(j, nu.Add("00-bond.netdev"))
configureNetworkUnit(j, nu.Add("00-bond.network"))
for i, port := range j.Interfaces() {
filename := fmt.Sprintf("%02d-nic%d.network", i+1, i)
u := unit.New(filename)
if ok := configureBondSlaveUnit(j, u, port); ok {
nu.Append(u)
}
}
return
}
func buildSystemdUnits(j job.Job) (su ignition.SystemdUnits) {
configureInstaller(j, su.Add("install.service"))
return
}
func serveIgnitionConfig(distro string) func(w http.ResponseWriter, req *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
j, err := job.CreateFromRemoteAddr(req.RemoteAddr)
if err != nil {
installers.Logger(distro).With("client", req.RemoteAddr).Error(err)
w.WriteHeader(http.StatusNotFound)
return
}
c := ignition.Config{
Network: buildNetworkUnits(j),
Systemd: buildSystemdUnits(j),
}
if err := c.Render(w); err != nil {
w.WriteHeader(http.StatusInternalServerError)
j.Error(err, "unable to render ignition config")
}
}
}