blob: eb4840fb45f9c5019d9bc0677510d99abf660969 [file] [log] [blame]
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +04001package tasks
2
3import (
4 "fmt"
Giorgi Lekveishvilid542b732024-03-25 18:17:39 +04005 "strings"
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +04006
7 "github.com/miekg/dns"
8
9 "github.com/giolekva/pcloud/core/installer"
gioe72b54f2024-04-22 10:44:41 +040010 "github.com/giolekva/pcloud/core/installer/soft"
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +040011)
12
Giorgi Lekveishvilid542b732024-03-25 18:17:39 +040013var initGroups = []string{"admin"}
14
gioe72b54f2024-04-22 10:44:41 +040015func CreateRepoClient(env installer.EnvConfig, st *state) Task {
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +040016 t := newLeafTask("Create repo client", func() error {
gioe72b54f2024-04-22 10:44:41 +040017 r, err := st.ssClient.GetRepo("config")
gio3af43942024-04-16 08:13:50 +040018 if err != nil {
19 return err
20 }
giof8843412024-05-22 16:38:05 +040021 appManager, err := installer.NewAppManager(r, st.nsCreator, st.jc, st.hf, "/apps")
Giorgi Lekveishvili77ee2dc2023-12-11 16:51:10 +040022 if err != nil {
23 return err
24 }
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +040025 st.appManager = appManager
26 st.appsRepo = installer.NewInMemoryAppRepository(installer.CreateAllApps())
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +040027 return nil
28 })
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +040029 t.beforeStart = func() {
30 st.infoListener("Setting up core infrastructure services.")
31 }
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +040032 return &t
33}
34
gioe72b54f2024-04-22 10:44:41 +040035func SetupInfra(env installer.EnvConfig, st *state) Task {
gio7841f4f2024-07-26 19:53:49 +040036 tasks := []Task{
gioe72b54f2024-04-22 10:44:41 +040037 SetupNetwork(env, st),
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +040038 SetupCertificateIssuers(env, st),
39 SetupAuth(env, st),
40 SetupGroupMemberships(env, st),
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +040041 SetupWelcome(env, st),
42 SetupAppStore(env, st),
Davit Tabidze56f86a42024-04-09 19:15:25 +040043 SetupLauncher(env, st),
gio7841f4f2024-07-26 19:53:49 +040044 }
45 if env.PrivateDomain != "" {
46 tasks = append(tasks, SetupHeadscale(env, st))
47 }
48 return newConcurrentParentTask(
49 "Setup core services",
50 true,
51 tasks...,
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +040052 )
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +040053}
54
gioe72b54f2024-04-22 10:44:41 +040055func CommitEnvironmentConfiguration(env installer.EnvConfig, st *state) Task {
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +040056 t := newLeafTask("commit config", func() error {
gioe72b54f2024-04-22 10:44:41 +040057 r, err := st.ssClient.GetRepo("config")
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +040058 if err != nil {
Giorgi Lekveishvili77ee2dc2023-12-11 16:51:10 +040059 return err
60 }
gioe72b54f2024-04-22 10:44:41 +040061 r.Do(func(r soft.RepoFS) (string, error) {
62 if err := soft.WriteYaml(r, "config.yaml", env); err != nil {
63 return "", err
gio3af43942024-04-16 08:13:50 +040064 }
gioe72b54f2024-04-22 10:44:41 +040065 rootKust, err := soft.ReadKustomization(r, "kustomization.yaml")
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +040066 if err != nil {
gio3af43942024-04-16 08:13:50 +040067 return "", err
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +040068 }
gioe72b54f2024-04-22 10:44:41 +040069 if err := soft.WriteYaml(r, "kustomization.yaml", rootKust); err != nil {
gio3af43942024-04-16 08:13:50 +040070 return "", err
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +040071 }
gio3af43942024-04-16 08:13:50 +040072 return "configure charts repo", nil
73 })
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +040074 return nil
75 })
76 return &t
77}
78
Giorgi Lekveishvilid542b732024-03-25 18:17:39 +040079type firstAccount struct {
80 Created bool `json:"created"`
gio2728e402024-08-01 18:14:21 +040081 Domain string `json:"domain"`
Giorgi Lekveishvilid542b732024-03-25 18:17:39 +040082 Groups []string `json:"groups"`
83}
84
gioe72b54f2024-04-22 10:44:41 +040085func ConfigureFirstAccount(env installer.EnvConfig, st *state) Task {
Giorgi Lekveishvilid542b732024-03-25 18:17:39 +040086 t := newLeafTask("Configure first account settings", func() error {
gioe72b54f2024-04-22 10:44:41 +040087 r, err := st.ssClient.GetRepo("config")
Giorgi Lekveishvilid542b732024-03-25 18:17:39 +040088 if err != nil {
89 return err
90 }
gioe72b54f2024-04-22 10:44:41 +040091 return r.Do(func(r soft.RepoFS) (string, error) {
gio2728e402024-08-01 18:14:21 +040092 fa := firstAccount{false, env.Domain, initGroups}
gioe72b54f2024-04-22 10:44:41 +040093 if err := soft.WriteYaml(r, "first-account.yaml", fa); err != nil {
gio3af43942024-04-16 08:13:50 +040094 return "", err
95 }
96 return "first account membership configuration", nil
97 })
Giorgi Lekveishvilid542b732024-03-25 18:17:39 +040098 })
99 return &t
100}
101
gioe72b54f2024-04-22 10:44:41 +0400102func SetupNetwork(env installer.EnvConfig, st *state) Task {
gio7841f4f2024-07-26 19:53:49 +0400103 t := newLeafTask("Setup networks", func() error {
Giorgi Lekveishvili77ee2dc2023-12-11 16:51:10 +0400104 {
gio3cdee592024-04-17 10:15:56 +0400105 app, err := installer.FindEnvApp(st.appsRepo, "metallb-ipaddresspool")
Giorgi Lekveishvili77ee2dc2023-12-11 16:51:10 +0400106 if err != nil {
107 return err
108 }
gio3af43942024-04-16 08:13:50 +0400109 {
gio44f621b2024-04-29 09:44:38 +0400110 instanceId := fmt.Sprintf("%s-ingress-private", app.Slug())
gio3cdee592024-04-17 10:15:56 +0400111 appDir := fmt.Sprintf("/apps/%s", instanceId)
gio3af43942024-04-16 08:13:50 +0400112 namespace := fmt.Sprintf("%s%s-ingress-private", env.NamespacePrefix, app.Namespace())
gio778577f2024-04-29 09:44:38 +0400113 if _, err := st.appManager.Install(app, instanceId, appDir, namespace, map[string]any{
gioe72b54f2024-04-22 10:44:41 +0400114 "name": fmt.Sprintf("%s-ingress-private", env.Id),
115 "from": env.Network.Ingress.String(),
116 "to": env.Network.Ingress.String(),
gio3af43942024-04-16 08:13:50 +0400117 "autoAssign": false,
118 "namespace": "metallb-system",
119 }); err != nil {
120 return err
121 }
Giorgi Lekveishvili77ee2dc2023-12-11 16:51:10 +0400122 }
gio3af43942024-04-16 08:13:50 +0400123 {
gio44f621b2024-04-29 09:44:38 +0400124 instanceId := fmt.Sprintf("%s-headscale", app.Slug())
gio3cdee592024-04-17 10:15:56 +0400125 appDir := fmt.Sprintf("/apps/%s", instanceId)
gio3af43942024-04-16 08:13:50 +0400126 namespace := fmt.Sprintf("%s%s-ingress-private", env.NamespacePrefix, app.Namespace())
gio778577f2024-04-29 09:44:38 +0400127 if _, err := st.appManager.Install(app, instanceId, appDir, namespace, map[string]any{
gioe72b54f2024-04-22 10:44:41 +0400128 "name": fmt.Sprintf("%s-headscale", env.Id),
129 "from": env.Network.Headscale.String(),
130 "to": env.Network.Headscale.String(),
gio3af43942024-04-16 08:13:50 +0400131 "autoAssign": false,
132 "namespace": "metallb-system",
133 }); err != nil {
134 return err
135 }
Giorgi Lekveishvili77ee2dc2023-12-11 16:51:10 +0400136 }
gio3af43942024-04-16 08:13:50 +0400137 {
gio44f621b2024-04-29 09:44:38 +0400138 instanceId := app.Slug()
gio3cdee592024-04-17 10:15:56 +0400139 appDir := fmt.Sprintf("/apps/%s", instanceId)
gio3af43942024-04-16 08:13:50 +0400140 namespace := fmt.Sprintf("%s%s", env.NamespacePrefix, app.Namespace())
gio778577f2024-04-29 09:44:38 +0400141 if _, err := st.appManager.Install(app, instanceId, appDir, namespace, map[string]any{
gioe72b54f2024-04-22 10:44:41 +0400142 "name": env.Id,
143 "from": env.Network.ServicesFrom.String(),
144 "to": env.Network.ServicesTo.String(),
gio3af43942024-04-16 08:13:50 +0400145 "autoAssign": false,
146 "namespace": "metallb-system",
147 }); err != nil {
148 return err
149 }
Giorgi Lekveishvili77ee2dc2023-12-11 16:51:10 +0400150 }
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +0400151 }
gio7841f4f2024-07-26 19:53:49 +0400152 if env.PrivateDomain != "" {
Giorgi Lekveishvilib59b7c22024-04-03 22:17:50 +0400153 keys, err := installer.NewSSHKeyPair("port-allocator")
154 if err != nil {
155 return err
156 }
gioe72b54f2024-04-22 10:44:41 +0400157 user := fmt.Sprintf("%s-port-allocator", env.Id)
Giorgi Lekveishvilib59b7c22024-04-03 22:17:50 +0400158 if err := st.ssClient.AddUser(user, keys.AuthorizedKey()); err != nil {
159 return err
160 }
161 if err := st.ssClient.AddReadWriteCollaborator("config", user); err != nil {
162 return err
163 }
gio3cdee592024-04-17 10:15:56 +0400164 app, err := installer.FindEnvApp(st.appsRepo, "private-network")
Giorgi Lekveishvili77ee2dc2023-12-11 16:51:10 +0400165 if err != nil {
166 return err
167 }
gio44f621b2024-04-29 09:44:38 +0400168 instanceId := app.Slug()
gio3cdee592024-04-17 10:15:56 +0400169 appDir := fmt.Sprintf("/apps/%s", instanceId)
gio3af43942024-04-16 08:13:50 +0400170 namespace := fmt.Sprintf("%s%s", env.NamespacePrefix, app.Namespace())
gio778577f2024-04-29 09:44:38 +0400171 if _, err := st.appManager.Install(app, instanceId, appDir, namespace, map[string]any{
Giorgi Lekveishvilie009a5d2024-01-05 14:10:11 +0400172 "privateNetwork": map[string]any{
173 "hostname": "private-network-proxy",
174 "username": "private-network-proxy",
gioe72b54f2024-04-22 10:44:41 +0400175 "ipSubnet": fmt.Sprintf("%s.0/24", strings.Join(strings.Split(env.Network.DNS.String(), ".")[:3], ".")),
Giorgi Lekveishvili77ee2dc2023-12-11 16:51:10 +0400176 },
Giorgi Lekveishvilib59b7c22024-04-03 22:17:50 +0400177 "sshPrivateKey": string(keys.RawPrivateKey()),
Giorgi Lekveishvili77ee2dc2023-12-11 16:51:10 +0400178 }); err != nil {
179 return err
180 }
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +0400181 }
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +0400182 return nil
183 })
184 return &t
185}
186
gioe72b54f2024-04-22 10:44:41 +0400187func SetupCertificateIssuers(env installer.EnvConfig, st *state) Task {
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +0400188 pub := newLeafTask(fmt.Sprintf("Public %s", env.Domain), func() error {
gio3cdee592024-04-17 10:15:56 +0400189 app, err := installer.FindEnvApp(st.appsRepo, "certificate-issuer-public")
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +0400190 if err != nil {
191 return err
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +0400192 }
gio44f621b2024-04-29 09:44:38 +0400193 instanceId := app.Slug()
gio3cdee592024-04-17 10:15:56 +0400194 appDir := fmt.Sprintf("/apps/%s", instanceId)
gio3af43942024-04-16 08:13:50 +0400195 namespace := fmt.Sprintf("%s%s", env.NamespacePrefix, app.Namespace())
gio7841f4f2024-07-26 19:53:49 +0400196 if _, err := st.appManager.Install(app, instanceId, appDir, namespace, map[string]any{
197 "network": "Public",
198 }); err != nil {
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +0400199 return err
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +0400200 }
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +0400201 return nil
202 })
gio7841f4f2024-07-26 19:53:49 +0400203 tasks := []Task{&pub}
204 if env.PrivateDomain != "" {
205 priv := newLeafTask(fmt.Sprintf("Private p.%s", env.Domain), func() error {
206 app, err := installer.FindEnvApp(st.appsRepo, "certificate-issuer-private")
207 if err != nil {
208 return err
209 }
210 instanceId := app.Slug()
211 appDir := fmt.Sprintf("/apps/%s", instanceId)
212 namespace := fmt.Sprintf("%s%s", env.NamespacePrefix, app.Namespace())
213 if _, err := st.appManager.Install(app, instanceId, appDir, namespace, map[string]any{}); err != nil {
214 return err
215 }
216 return nil
217 })
218 tasks = append(tasks, &priv)
219 }
220 return newSequentialParentTask("Configure TLS certificate issuers", false, tasks...)
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +0400221}
222
gioe72b54f2024-04-22 10:44:41 +0400223func SetupAuth(env installer.EnvConfig, st *state) Task {
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +0400224 t := newLeafTask("Setup", func() error {
gio3cdee592024-04-17 10:15:56 +0400225 app, err := installer.FindEnvApp(st.appsRepo, "core-auth")
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +0400226 if err != nil {
227 return err
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +0400228 }
gio44f621b2024-04-29 09:44:38 +0400229 instanceId := app.Slug()
gio3cdee592024-04-17 10:15:56 +0400230 appDir := fmt.Sprintf("/apps/%s", instanceId)
gio3af43942024-04-16 08:13:50 +0400231 namespace := fmt.Sprintf("%s%s", env.NamespacePrefix, app.Namespace())
gio778577f2024-04-29 09:44:38 +0400232 if _, err := st.appManager.Install(app, instanceId, appDir, namespace, map[string]any{
gio7841f4f2024-07-26 19:53:49 +0400233 "network": "Public",
Giorgi Lekveishvilie009a5d2024-01-05 14:10:11 +0400234 "subdomain": "test", // TODO(giolekva): make core-auth chart actually use this
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +0400235 }); err != nil {
236 return err
237 }
238 return nil
239 })
240 return newSequentialParentTask(
241 "Authentication services",
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +0400242 false,
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +0400243 &t,
gioe72b54f2024-04-22 10:44:41 +0400244 waitForAddr(st.httpClient, fmt.Sprintf("https://accounts-ui.%s", env.Domain)),
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +0400245 )
246}
247
gioe72b54f2024-04-22 10:44:41 +0400248func SetupGroupMemberships(env installer.EnvConfig, st *state) Task {
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +0400249 t := newLeafTask("Setup", func() error {
gio3cdee592024-04-17 10:15:56 +0400250 app, err := installer.FindEnvApp(st.appsRepo, "memberships")
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +0400251 if err != nil {
252 return err
253 }
gio44f621b2024-04-29 09:44:38 +0400254 instanceId := app.Slug()
gio3cdee592024-04-17 10:15:56 +0400255 appDir := fmt.Sprintf("/apps/%s", instanceId)
gio3af43942024-04-16 08:13:50 +0400256 namespace := fmt.Sprintf("%s%s", env.NamespacePrefix, app.Namespace())
gio7841f4f2024-07-26 19:53:49 +0400257 network := "Public"
258 if env.PrivateDomain != "" {
259 network = "Private"
260 }
gio778577f2024-04-29 09:44:38 +0400261 if _, err := st.appManager.Install(app, instanceId, appDir, namespace, map[string]any{
gio7841f4f2024-07-26 19:53:49 +0400262 "network": network,
Giorgi Lekveishvilid542b732024-03-25 18:17:39 +0400263 "authGroups": strings.Join(initGroups, ","),
264 }); err != nil {
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +0400265 return err
266 }
267 return nil
268 })
giob79db3a2024-08-01 14:20:42 +0400269 var addr string
270 if env.PrivateDomain != "" {
271 addr = fmt.Sprintf("https://memberships.%s", env.PrivateDomain)
272 } else {
273 addr = fmt.Sprintf("https://memberships.%s", env.Domain)
274 }
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +0400275 return newSequentialParentTask(
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +0400276 "Group membership",
277 false,
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +0400278 &t,
giob79db3a2024-08-01 14:20:42 +0400279 waitForAddr(st.httpClient, addr),
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +0400280 )
281}
282
gio09a3e5b2024-04-26 14:11:06 +0400283func SetupLauncher(env installer.EnvConfig, st *state) Task {
284 t := newLeafTask("Setup", func() error {
285 user := fmt.Sprintf("%s-launcher", env.Id)
286 keys, err := installer.NewSSHKeyPair(user)
287 if err != nil {
288 return err
289 }
290 if err := st.ssClient.AddUser(user, keys.AuthorizedKey()); err != nil {
291 return err
292 }
293 if err := st.ssClient.AddReadWriteCollaborator("config", user); err != nil {
294 return err
295 }
296 app, err := installer.FindEnvApp(st.appsRepo, "launcher")
297 if err != nil {
298 return err
299 }
300 instanceId := app.Slug()
301 appDir := fmt.Sprintf("/apps/%s", instanceId)
302 namespace := fmt.Sprintf("%s%s", env.NamespacePrefix, app.Namespace())
303 if _, err := st.appManager.Install(app, instanceId, appDir, namespace, map[string]any{
gio7841f4f2024-07-26 19:53:49 +0400304 "network": "Public",
gio09a3e5b2024-04-26 14:11:06 +0400305 "repoAddr": st.ssClient.GetRepoAddress("config"),
306 "sshPrivateKey": string(keys.RawPrivateKey()),
307 }); err != nil {
308 return err
309 }
310 return nil
311 })
312 return newSequentialParentTask(
313 "Launcher",
314 false,
315 &t,
316 waitForAddr(st.httpClient, fmt.Sprintf("https://launcher.%s", env.Domain)),
317 )
318}
319
gioe72b54f2024-04-22 10:44:41 +0400320func SetupHeadscale(env installer.EnvConfig, st *state) Task {
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +0400321 t := newLeafTask("Setup", func() error {
gio3cdee592024-04-17 10:15:56 +0400322 app, err := installer.FindEnvApp(st.appsRepo, "headscale")
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +0400323 if err != nil {
324 return err
325 }
gio44f621b2024-04-29 09:44:38 +0400326 instanceId := app.Slug()
gio3cdee592024-04-17 10:15:56 +0400327 appDir := fmt.Sprintf("/apps/%s", instanceId)
gio3af43942024-04-16 08:13:50 +0400328 namespace := fmt.Sprintf("%s%s", env.NamespacePrefix, app.Namespace())
gio778577f2024-04-29 09:44:38 +0400329 if _, err := st.appManager.Install(app, instanceId, appDir, namespace, map[string]any{
gio7841f4f2024-07-26 19:53:49 +0400330 "network": "Public",
Giorgi Lekveishvilie009a5d2024-01-05 14:10:11 +0400331 "subdomain": "headscale",
gioe72b54f2024-04-22 10:44:41 +0400332 "ipSubnet": fmt.Sprintf("%s/24", env.Network.DNS.String()),
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +0400333 }); err != nil {
334 return err
335 }
336 return nil
337 })
338 return newSequentialParentTask(
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +0400339 "Setup mesh VPN",
340 false,
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +0400341 &t,
gioe72b54f2024-04-22 10:44:41 +0400342 waitForAddr(st.httpClient, fmt.Sprintf("https://headscale.%s/apple", env.Domain)),
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +0400343 )
344}
345
gioe72b54f2024-04-22 10:44:41 +0400346func SetupWelcome(env installer.EnvConfig, st *state) Task {
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +0400347 t := newLeafTask("Setup", func() error {
348 keys, err := installer.NewSSHKeyPair("welcome")
349 if err != nil {
350 return err
351 }
gioe72b54f2024-04-22 10:44:41 +0400352 user := fmt.Sprintf("%s-welcome", env.Id)
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +0400353 if err := st.ssClient.AddUser(user, keys.AuthorizedKey()); err != nil {
354 return err
355 }
356 if err := st.ssClient.AddReadWriteCollaborator("config", user); err != nil {
357 return err
358 }
gio3cdee592024-04-17 10:15:56 +0400359 app, err := installer.FindEnvApp(st.appsRepo, "welcome")
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +0400360 if err != nil {
361 return err
362 }
gio44f621b2024-04-29 09:44:38 +0400363 instanceId := app.Slug()
gio3cdee592024-04-17 10:15:56 +0400364 appDir := fmt.Sprintf("/apps/%s", instanceId)
gio3af43942024-04-16 08:13:50 +0400365 namespace := fmt.Sprintf("%s%s", env.NamespacePrefix, app.Namespace())
gio778577f2024-04-29 09:44:38 +0400366 if _, err := st.appManager.Install(app, instanceId, appDir, namespace, map[string]any{
gio7841f4f2024-07-26 19:53:49 +0400367 "network": "Public",
Giorgi Lekveishvilie009a5d2024-01-05 14:10:11 +0400368 "repoAddr": st.ssClient.GetRepoAddress("config"),
369 "sshPrivateKey": string(keys.RawPrivateKey()),
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +0400370 }); err != nil {
371 return err
372 }
373 return nil
374 })
375 return newSequentialParentTask(
376 "Welcome service",
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +0400377 false,
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +0400378 &t,
gioe72b54f2024-04-22 10:44:41 +0400379 waitForAddr(st.httpClient, fmt.Sprintf("https://welcome.%s", env.Domain)),
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +0400380 )
381}
382
gioe72b54f2024-04-22 10:44:41 +0400383func SetupAppStore(env installer.EnvConfig, st *state) Task {
giob79db3a2024-08-01 14:20:42 +0400384 t := newLeafTask("Setup", func() error {
gioe72b54f2024-04-22 10:44:41 +0400385 user := fmt.Sprintf("%s-appmanager", env.Id)
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +0400386 keys, err := installer.NewSSHKeyPair(user)
387 if err != nil {
388 return err
389 }
390 if err := st.ssClient.AddUser(user, keys.AuthorizedKey()); err != nil {
391 return err
392 }
393 if err := st.ssClient.AddReadWriteCollaborator("config", user); err != nil {
394 return err
395 }
gio3cdee592024-04-17 10:15:56 +0400396 app, err := installer.FindEnvApp(st.appsRepo, "app-manager") // TODO(giolekva): configure
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +0400397 if err != nil {
398 return err
399 }
gio44f621b2024-04-29 09:44:38 +0400400 instanceId := app.Slug()
gio3cdee592024-04-17 10:15:56 +0400401 appDir := fmt.Sprintf("/apps/%s", instanceId)
gio3af43942024-04-16 08:13:50 +0400402 namespace := fmt.Sprintf("%s%s", env.NamespacePrefix, app.Namespace())
gio7841f4f2024-07-26 19:53:49 +0400403 network := "Public"
404 if env.PrivateDomain != "" {
405 network = "Private"
406 }
gio778577f2024-04-29 09:44:38 +0400407 if _, err := st.appManager.Install(app, instanceId, appDir, namespace, map[string]any{
gio7841f4f2024-07-26 19:53:49 +0400408 "network": network,
Giorgi Lekveishvilie009a5d2024-01-05 14:10:11 +0400409 "repoAddr": st.ssClient.GetRepoAddress("config"),
410 "sshPrivateKey": string(keys.RawPrivateKey()),
Giorgi Lekveishvili3c91e8b2024-03-25 20:20:14 +0400411 "authGroups": strings.Join(initGroups, ","),
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +0400412 }); err != nil {
413 return err
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +0400414 }
Giorgi Lekveishvili77ee2dc2023-12-11 16:51:10 +0400415 return nil
416 })
giob79db3a2024-08-01 14:20:42 +0400417 var addr string
418 if env.PrivateDomain != "" {
419 addr = fmt.Sprintf("https://apps.%s", env.PrivateDomain)
420 } else {
421 addr = fmt.Sprintf("https://apps.%s", env.Domain)
422 }
423 return newSequentialParentTask(
424 "Application marketplace",
425 false,
426 &t,
427 waitForAddr(st.httpClient, addr),
428 )
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +0400429}
430
gioe72b54f2024-04-22 10:44:41 +0400431// TODO(gio-dns): remove
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +0400432type DNSSecKey struct {
433 Basename string `json:"basename,omitempty"`
434 Key []byte `json:"key,omitempty"`
435 Private []byte `json:"private,omitempty"`
436 DS []byte `json:"ds,omitempty"`
437}
438
439func newDNSSecKey(zone string) (DNSSecKey, error) {
440 key := &dns.DNSKEY{
441 Hdr: dns.RR_Header{Name: dns.Fqdn(zone), Class: dns.ClassINET, Ttl: 3600, Rrtype: dns.TypeDNSKEY},
442 Algorithm: dns.ECDSAP256SHA256, Flags: 257, Protocol: 3,
443 }
444 priv, err := key.Generate(256)
445 if err != nil {
446 return DNSSecKey{}, err
447 }
448 return DNSSecKey{
449 Basename: fmt.Sprintf("K%s+%03d+%05d", key.Header().Name, key.Algorithm, key.KeyTag()),
450 Key: []byte(key.String()),
451 Private: []byte(key.PrivateKeyString(priv)),
452 DS: []byte(key.ToDS(dns.SHA256).String()),
453 }, nil
454}