File: ignition.go

package info (click to toggle)
golang-github-containers-gvisor-tap-vsocks 0.8.1-3
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 800 kB
  • sloc: sh: 95; makefile: 59
file content (162 lines) | stat: -rw-r--r-- 3,293 bytes parent folder | download
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
package e2e

import (
	"encoding/json"
	"net/url"
	"os"
)

var (
	mode    = 0644
	dirMode = 0744
	root    = "root"
	test    = "test"
	yes     = true
	no      = false
)

func CreateIgnition(ignitionFile string, publicKey string, user string, password string) error {

	linger := `[Unit]
Description=Activate podman socket
Wants=podman.socket
[Service]
ExecStart=/usr/bin/sleep infinity
`

	systemd := Systemd{
		Units: []Unit{
			{
				Name:    "systemd-resolved.service",
				Enabled: &no,
				Mask:    &yes,
			},
			{
				Name:    "podman.socket",
				Enabled: &yes,
			},
		},
	}

	passwd := Passwd{
		Users: []PasswdUser{
			{
				Name:         user,
				PasswordHash: &password,
				SSHAuthorizedKeys: []SSHAuthorizedKey{
					SSHAuthorizedKey(publicKey),
				},
				Groups: []Group{
					"wheel",
					"sudo",
				},
			},
			{
				Name:         "root",
				PasswordHash: &password,
				SSHAuthorizedKeys: []SSHAuthorizedKey{
					SSHAuthorizedKey(publicKey),
				},
			},
		},
	}

	storage := Storage{
		// Replaces resolv.conf with an empty file that will be overwritten by NetworkManager
		Files: []File{
			{
				Node: Node{
					Group:     NodeGroup{Name: &root},
					Path:      "/etc/resolv.conf",
					User:      NodeUser{Name: &root},
					Overwrite: &yes,
				},
				FileEmbedded1: FileEmbedded1{
					Contents: Resource{
						Source: encodeData(""),
					},
					Mode: &mode,
				},
			},
			{
				Node: Node{
					Group:     NodeGroup{Name: &test},
					Path:      "/home/" + test + "/.config/systemd/user/linger-podman.service",
					User:      NodeUser{Name: &test},
					Overwrite: &yes,
				},
				FileEmbedded1: FileEmbedded1{
					Contents: Resource{
						Source: encodeData(linger),
					},
					Mode: &mode,
				},
			},
			{
				Node: Node{
					Group:     NodeGroup{Name: &test},
					Path:      "/var/lib/systemd/linger/" + test,
					User:      NodeUser{Name: &test},
					Overwrite: &yes,
				},
				FileEmbedded1: FileEmbedded1{
					Contents: Resource{
						Source: encodeData(""),
					},
					Mode: &mode,
				},
			},
		},
		Directories: []Directory{
			dir("/home/" + test + "/.config"),
			dir("/home/" + test + "/.config/containers"),
			dir("/home/" + test + "/.config/systemd"),
			dir("/home/" + test + "/.config/systemd/user"),
			dir("/home/" + test + "/.config/systemd/user/default.target.wants"),
		},
		Links: []Link{
			{
				Node: Node{
					Group: NodeGroup{Name: &test},
					Path:  "/home/" + test + "/.config/systemd/user/default.target.wants/linger-podman.service",
					User:  NodeUser{Name: &test},
				},
				LinkEmbedded1: LinkEmbedded1{
					Hard:   &no,
					Target: "/home/" + test + "/.config/systemd/user/linger-podman.service",
				},
			},
		},
	}

	config := Config{
		Ignition: Ignition{Version: "3.2.0"},
		Systemd:  systemd,
		Passwd:   passwd,
		Storage:  storage,
	}

	contents, err := json.Marshal(config)
	if err != nil {
		return err
	}

	// #nosec
	return os.WriteFile(ignitionFile, contents, 0644)
}

func dir(path string) Directory {
	return Directory{
		Node: Node{
			Group: NodeGroup{Name: &test},
			Path:  path,
			User:  NodeUser{Name: &test},
		},
		DirectoryEmbedded1: DirectoryEmbedded1{Mode: &dirMode},
	}
}

func encodeData(data string) *string {
	str := "data:," + url.PathEscape(data)
	return &str
}