| 1 | package e2eutils |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "net/url" |
| 6 | "os" |
| 7 | ) |
| 8 | |
| 9 | var ( |
| 10 | mode = 0644 |
| 11 | dirMode = 0744 |
| 12 | root = "root" |
| 13 | test = "test" |
| 14 | yes = true |
| 15 | no = false |
| 16 | ) |
| 17 | |
| 18 | func CreateIgnition(ignitionFile string, publicKey string, user string, password string) error { |
| 19 | |
| 20 | linger := `[Unit] |
| 21 | Description=Activate podman socket |
| 22 | Wants=podman.socket |
| 23 | [Service] |
| 24 | ExecStart=/usr/bin/sleep infinity |
| 25 | ` |
| 26 | |
| 27 | systemd := Systemd{ |
| 28 | Units: []Unit{ |
| 29 | { |
| 30 | Name: "systemd-resolved.service", |
| 31 | Enabled: &no, |
| 32 | Mask: &yes, |
| 33 | }, |
| 34 | { |
| 35 | Name: "podman.socket", |
| 36 | Enabled: &yes, |
| 37 | }, |
| 38 | }, |
| 39 | } |
| 40 | |
| 41 | passwd := Passwd{ |
| 42 | Users: []PasswdUser{ |
| 43 | { |
| 44 | Name: user, |
| 45 | PasswordHash: &password, |
| 46 | SSHAuthorizedKeys: []SSHAuthorizedKey{ |
| 47 | SSHAuthorizedKey(publicKey), |
| 48 | }, |
| 49 | Groups: []Group{ |
| 50 | "wheel", |
| 51 | "sudo", |
| 52 | }, |
| 53 | }, |
| 54 | { |
| 55 | Name: "root", |
| 56 | PasswordHash: &password, |
| 57 | SSHAuthorizedKeys: []SSHAuthorizedKey{ |
| 58 | SSHAuthorizedKey(publicKey), |
| 59 | }, |
| 60 | }, |
| 61 | }, |
| 62 | } |
| 63 | |
| 64 | storage := Storage{ |
| 65 | |
| 66 | Files: []File{ |
| 67 | { |
| 68 | Node: Node{ |
| 69 | Group: NodeGroup{Name: &root}, |
| 70 | Path: "/etc/resolv.conf", |
| 71 | User: NodeUser{Name: &root}, |
| 72 | Overwrite: &yes, |
| 73 | }, |
| 74 | FileEmbedded1: FileEmbedded1{ |
| 75 | Contents: Resource{ |
| 76 | Source: encodeData(""), |
| 77 | }, |
| 78 | Mode: &mode, |
| 79 | }, |
| 80 | }, |
| 81 | { |
| 82 | Node: Node{ |
| 83 | Group: NodeGroup{Name: &test}, |
| 84 | Path: "/home/" + test + "/.config/systemd/user/linger-podman.service", |
| 85 | User: NodeUser{Name: &test}, |
| 86 | Overwrite: &yes, |
| 87 | }, |
| 88 | FileEmbedded1: FileEmbedded1{ |
| 89 | Contents: Resource{ |
| 90 | Source: encodeData(linger), |
| 91 | }, |
| 92 | Mode: &mode, |
| 93 | }, |
| 94 | }, |
| 95 | { |
| 96 | Node: Node{ |
| 97 | Group: NodeGroup{Name: &test}, |
| 98 | Path: "/var/lib/systemd/linger/" + test, |
| 99 | User: NodeUser{Name: &test}, |
| 100 | Overwrite: &yes, |
| 101 | }, |
| 102 | FileEmbedded1: FileEmbedded1{ |
| 103 | Contents: Resource{ |
| 104 | Source: encodeData(""), |
| 105 | }, |
| 106 | Mode: &mode, |
| 107 | }, |
| 108 | }, |
| 109 | }, |
| 110 | Directories: []Directory{ |
| 111 | dir("/home/" + test + "/.config"), |
| 112 | dir("/home/" + test + "/.config/containers"), |
| 113 | dir("/home/" + test + "/.config/systemd"), |
| 114 | dir("/home/" + test + "/.config/systemd/user"), |
| 115 | dir("/home/" + test + "/.config/systemd/user/default.target.wants"), |
| 116 | }, |
| 117 | Links: []Link{ |
| 118 | { |
| 119 | Node: Node{ |
| 120 | Group: NodeGroup{Name: &test}, |
| 121 | Path: "/home/" + test + "/.config/systemd/user/default.target.wants/linger-podman.service", |
| 122 | User: NodeUser{Name: &test}, |
| 123 | }, |
| 124 | LinkEmbedded1: LinkEmbedded1{ |
| 125 | Hard: &no, |
| 126 | Target: "/home/" + test + "/.config/systemd/user/linger-podman.service", |
| 127 | }, |
| 128 | }, |
| 129 | }, |
| 130 | } |
| 131 | |
| 132 | config := Config{ |
| 133 | Ignition: Ignition{Version: "3.2.0"}, |
| 134 | Systemd: systemd, |
| 135 | Passwd: passwd, |
| 136 | Storage: storage, |
| 137 | } |
| 138 | |
| 139 | contents, err := json.Marshal(config) |
| 140 | if err != nil { |
| 141 | return err |
| 142 | } |
| 143 | |
| 144 | |
| 145 | return os.WriteFile(ignitionFile, contents, 0644) |
| 146 | } |
| 147 | |
| 148 | func dir(path string) Directory { |
| 149 | return Directory{ |
| 150 | Node: Node{ |
| 151 | Group: NodeGroup{Name: &test}, |
| 152 | Path: path, |
| 153 | User: NodeUser{Name: &test}, |
| 154 | }, |
| 155 | DirectoryEmbedded1: DirectoryEmbedded1{Mode: &dirMode}, |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | func encodeData(data string) *string { |
| 160 | str := "data:," + url.PathEscape(data) |
| 161 | return &str |
| 162 | } |