blob: 33f122de72e82ded924d49793fbaaab9c503293c [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 }
giof8843412024-05-22 16:38:05 +040079 nsc, err := newNSCreator()
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040080 if err != nil {
81 return err
82 }
giof8843412024-05-22 16:38:05 +040083 jc, err := newJobCreator()
84 if err != nil {
85 return err
86 }
87 hf := installer.NewGitHelmFetcher()
88 m, err := installer.NewAppManager(repoIO, nsc, jc, hf, "/apps")
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040089 if err != nil {
90 return err
91 }
gio3cdee592024-04-17 10:15:56 +040092 env, err := m.Config()
gio3af43942024-04-16 08:13:50 +040093 if err != nil {
94 return err
95 }
96 log.Println("Read config")
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +040097 log.Println("Creating repository")
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +040098 var r installer.AppRepository
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +040099 if appManagerFlags.appRepoAddr != "" {
100 fs := memfs.New()
101 err = installer.FetchAppsFromHTTPRepository(appManagerFlags.appRepoAddr, fs)
102 if err != nil {
103 return err
104 }
105 r, err = installer.NewFSAppRepository(fs)
106 if err != nil {
107 return err
108 }
109 } else {
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400110 r = installer.NewInMemoryAppRepository(installer.CreateStoreApps())
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +0400111 }
gio778577f2024-04-29 09:44:38 +0400112 helmMon, err := newHelmReleaseMonitor()
113 if err != nil {
114 return err
115 }
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400116 s, err := welcome.NewAppManagerServer(
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400117 appManagerFlags.port,
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400118 m,
119 r,
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400120 tasks.NewFluxcdReconciler(
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +0400121 "http://fluxcd-reconciler.dodo-fluxcd-reconciler.svc.cluster.local",
gio3cdee592024-04-17 10:15:56 +0400122 env.Id,
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +0400123 ),
gio778577f2024-04-29 09:44:38 +0400124 helmMon,
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400125 )
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400126 if err != nil {
127 return err
128 }
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +0400129 return s.Start()
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +0400130}