blob: 2f9aef768338b27b637e00d61544afb2b28d6d3d [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"
gio721c0042025-04-03 11:56:36 +04006 "path/filepath"
7 "strings"
Giorgi Lekveishvili8fe056b2023-06-23 12:01:43 +04008
9 "github.com/giolekva/pcloud/core/installer"
gio59946282024-10-07 12:55:51 +040010 "github.com/giolekva/pcloud/core/installer/server/appmanager"
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040011 "github.com/giolekva/pcloud/core/installer/soft"
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +040012 "github.com/giolekva/pcloud/core/installer/tasks"
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 {
giof6ad2982024-08-23 17:42:49 +040019 sshKey string
20 repoAddr string
21 port int
22 appRepoAddr string
23 headscaleAPIAddr string
24 dnsAPIAddr string
25 clusterProxyConfigPath string
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040026}
27
28func appManagerCmd() *cobra.Command {
29 cmd := &cobra.Command{
30 Use: "appmanager",
Giorgi Lekveishvili7efe22f2023-05-30 13:01:53 +040031 RunE: appManagerCmdRun,
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040032 }
gio308105e2024-04-19 13:12:13 +040033 cmd.Flags().IntVar(
34 &appManagerFlags.port,
35 "port",
36 8080,
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040037 "",
38 )
39 cmd.Flags().StringVar(
Giorgi Lekveishvili7efe22f2023-05-30 13:01:53 +040040 &appManagerFlags.repoAddr,
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040041 "repo-addr",
42 "",
43 "",
44 )
gio308105e2024-04-19 13:12:13 +040045 cmd.Flags().StringVar(
46 &appManagerFlags.sshKey,
47 "ssh-key",
48 "",
Giorgi Lekveishvili7efe22f2023-05-30 13:01:53 +040049 "",
50 )
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +040051 cmd.Flags().StringVar(
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +040052 &appManagerFlags.appRepoAddr,
53 "app-repo-addr",
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +040054 "",
55 "",
56 )
gio36b23b32024-08-25 12:20:54 +040057 cmd.Flags().StringVar(
58 &appManagerFlags.headscaleAPIAddr,
59 "headscale-api-addr",
60 "",
61 "",
62 )
giof6ad2982024-08-23 17:42:49 +040063 cmd.Flags().StringVar(
64 &appManagerFlags.dnsAPIAddr,
65 "dns-api-addr",
66 "",
67 "",
68 )
69 cmd.Flags().StringVar(
70 &appManagerFlags.clusterProxyConfigPath,
71 "cluster-proxy-config-path",
72 "",
73 "",
74 )
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040075 return cmd
76}
77
78func appManagerCmdRun(cmd *cobra.Command, args []string) error {
Giorgi Lekveishvili7efe22f2023-05-30 13:01:53 +040079 sshKey, err := os.ReadFile(appManagerFlags.sshKey)
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040080 if err != nil {
81 return err
82 }
gio721c0042025-04-03 11:56:36 +040083 items := strings.Split(appManagerFlags.repoAddr, "/")
84 ipPort := items[len(items)-2]
85 repoName := items[len(items)-1]
86 ssClient, err := soft.NewClient(ipPort, sshKey, log.Default())
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040087 if err != nil {
88 return err
89 }
gio721c0042025-04-03 11:56:36 +040090 repoIO, err := ssClient.GetRepo(repoName)
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +040091 if err != nil {
92 return err
93 }
giof8843412024-05-22 16:38:05 +040094 nsc, err := newNSCreator()
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040095 if err != nil {
96 return err
97 }
giof8843412024-05-22 16:38:05 +040098 jc, err := newJobCreator()
99 if err != nil {
100 return err
101 }
102 hf := installer.NewGitHelmFetcher()
giof6ad2982024-08-23 17:42:49 +0400103 vpnAPIClient := installer.NewHeadscaleAPIClient(appManagerFlags.headscaleAPIAddr)
104 cnc := &installer.NginxProxyConfigurator{
105 // TODO(gio): read from env config
106 PrivateSubdomain: "p",
107 DNSAPIAddr: appManagerFlags.dnsAPIAddr,
108 Repo: repoIO,
gio721c0042025-04-03 11:56:36 +0400109 ConfigPath: appManagerFlags.clusterProxyConfigPath,
110 ServicePath: filepath.Join(filepath.Dir(appManagerFlags.clusterProxyConfigPath), "proxy-backend-service.yaml"),
giof6ad2982024-08-23 17:42:49 +0400111 }
112 m, err := installer.NewAppManager(repoIO, nsc, jc, hf, vpnAPIClient, cnc, "/apps")
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +0400113 if err != nil {
114 return err
115 }
gio3cdee592024-04-17 10:15:56 +0400116 env, err := m.Config()
gio3af43942024-04-16 08:13:50 +0400117 if err != nil {
118 return err
119 }
120 log.Println("Read config")
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +0400121 log.Println("Creating repository")
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400122 var r installer.AppRepository
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +0400123 if appManagerFlags.appRepoAddr != "" {
124 fs := memfs.New()
125 err = installer.FetchAppsFromHTTPRepository(appManagerFlags.appRepoAddr, fs)
126 if err != nil {
127 return err
128 }
129 r, err = installer.NewFSAppRepository(fs)
130 if err != nil {
131 return err
132 }
133 } else {
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400134 r = installer.NewInMemoryAppRepository(installer.CreateStoreApps())
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +0400135 }
giof6ad2982024-08-23 17:42:49 +0400136 fr := installer.NewInMemoryAppRepository(installer.CreateAllEnvApps())
gio778577f2024-04-29 09:44:38 +0400137 helmMon, err := newHelmReleaseMonitor()
138 if err != nil {
139 return err
140 }
giof8acc612025-04-26 08:20:55 +0400141 im, err := newInstanceMonitor()
142 if err != nil {
143 return err
144 }
gio59946282024-10-07 12:55:51 +0400145 s, err := appmanager.NewServer(
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400146 appManagerFlags.port,
gio721c0042025-04-03 11:56:36 +0400147 ssClient,
giof6ad2982024-08-23 17:42:49 +0400148 repoIO,
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400149 m,
150 r,
giof6ad2982024-08-23 17:42:49 +0400151 fr,
gio43b0f422024-08-21 10:40:13 +0400152 tasks.NewFixedReconciler(env.Id, env.Id),
gio778577f2024-04-29 09:44:38 +0400153 helmMon,
giof8acc612025-04-26 08:20:55 +0400154 im,
giof6ad2982024-08-23 17:42:49 +0400155 cnc,
156 vpnAPIClient,
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400157 )
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400158 if err != nil {
159 return err
160 }
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +0400161 return s.Start()
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +0400162}