blob: ee21ba0280935c5b0801cc9674c3df74a7c0116c [file] [log] [blame]
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +04001package main
2
3import (
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +04004 "log"
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +04005 "os"
6
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +04007 "golang.org/x/crypto/ssh"
Giorgi Lekveishvili8fe056b2023-06-23 12:01:43 +04008
9 "github.com/giolekva/pcloud/core/installer"
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040010 "github.com/giolekva/pcloud/core/installer/soft"
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +040011 "github.com/giolekva/pcloud/core/installer/tasks"
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +040012 "github.com/giolekva/pcloud/core/installer/welcome"
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +040013
14 "github.com/go-git/go-billy/v5/memfs"
15 "github.com/spf13/cobra"
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040016)
17
18var appManagerFlags struct {
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +040019 sshKey string
20 repoAddr string
21 port int
22 appRepoAddr string
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040023}
24
25func appManagerCmd() *cobra.Command {
26 cmd := &cobra.Command{
27 Use: "appmanager",
Giorgi Lekveishvili7efe22f2023-05-30 13:01:53 +040028 RunE: appManagerCmdRun,
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040029 }
gio308105e2024-04-19 13:12:13 +040030 cmd.Flags().IntVar(
31 &appManagerFlags.port,
32 "port",
33 8080,
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040034 "",
35 )
36 cmd.Flags().StringVar(
Giorgi Lekveishvili7efe22f2023-05-30 13:01:53 +040037 &appManagerFlags.repoAddr,
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040038 "repo-addr",
39 "",
40 "",
41 )
gio308105e2024-04-19 13:12:13 +040042 cmd.Flags().StringVar(
43 &appManagerFlags.sshKey,
44 "ssh-key",
45 "",
Giorgi Lekveishvili7efe22f2023-05-30 13:01:53 +040046 "",
47 )
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +040048 cmd.Flags().StringVar(
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +040049 &appManagerFlags.appRepoAddr,
50 "app-repo-addr",
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +040051 "",
52 "",
53 )
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040054 return cmd
55}
56
57func appManagerCmdRun(cmd *cobra.Command, args []string) error {
Giorgi Lekveishvili7efe22f2023-05-30 13:01:53 +040058 sshKey, err := os.ReadFile(appManagerFlags.sshKey)
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040059 if err != nil {
60 return err
61 }
62 signer, err := ssh.ParsePrivateKey(sshKey)
63 if err != nil {
64 return err
65 }
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040066 addr, err := soft.ParseRepositoryAddress(appManagerFlags.repoAddr)
67 if err != nil {
68 return err
69 }
Giorgi Lekveishvili57dffb32023-08-07 15:45:43 +040070 repo, err := soft.CloneRepository(addr, signer)
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040071 if err != nil {
72 return err
73 }
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +040074 log.Println("Cloned repository")
gioe72b54f2024-04-22 10:44:41 +040075 repoIO, err := soft.NewRepoIO(repo, signer)
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +040076 if err != nil {
77 return err
78 }
Giorgi Lekveishvili27b2b572023-06-30 10:44:45 +040079 kube, err := newNSCreator()
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040080 if err != nil {
81 return err
82 }
gio308105e2024-04-19 13:12:13 +040083 m, err := installer.NewAppManager(repoIO, kube, "/apps")
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040084 if err != nil {
85 return err
86 }
gio3cdee592024-04-17 10:15:56 +040087 env, err := m.Config()
gio3af43942024-04-16 08:13:50 +040088 if err != nil {
89 return err
90 }
91 log.Println("Read config")
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +040092 log.Println("Creating repository")
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +040093 var r installer.AppRepository
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +040094 if appManagerFlags.appRepoAddr != "" {
95 fs := memfs.New()
96 err = installer.FetchAppsFromHTTPRepository(appManagerFlags.appRepoAddr, fs)
97 if err != nil {
98 return err
99 }
100 r, err = installer.NewFSAppRepository(fs)
101 if err != nil {
102 return err
103 }
104 } else {
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400105 r = installer.NewInMemoryAppRepository(installer.CreateStoreApps())
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +0400106 }
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400107 s := welcome.NewAppManagerServer(
108 appManagerFlags.port,
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400109 m,
110 r,
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +0400111 tasks.NewFluxcdReconciler( // TODO(gio): make reconciler address a flag
112 "http://fluxcd-reconciler.dodo-fluxcd-reconciler.svc.cluster.local",
gio3cdee592024-04-17 10:15:56 +0400113 env.Id,
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +0400114 ),
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400115 )
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +0400116 return s.Start()
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +0400117}