blob: afc7d32a9f4ae69adf69963c1d9e2a51254cce75 [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"
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 }
83 signer, err := ssh.ParsePrivateKey(sshKey)
84 if err != nil {
85 return err
86 }
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040087 addr, err := soft.ParseRepositoryAddress(appManagerFlags.repoAddr)
88 if err != nil {
89 return err
90 }
Giorgi Lekveishvili57dffb32023-08-07 15:45:43 +040091 repo, err := soft.CloneRepository(addr, signer)
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040092 if err != nil {
93 return err
94 }
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +040095 log.Println("Cloned repository")
gioe72b54f2024-04-22 10:44:41 +040096 repoIO, err := soft.NewRepoIO(repo, signer)
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +040097 if err != nil {
98 return err
99 }
giof8843412024-05-22 16:38:05 +0400100 nsc, err := newNSCreator()
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +0400101 if err != nil {
102 return err
103 }
giof8843412024-05-22 16:38:05 +0400104 jc, err := newJobCreator()
105 if err != nil {
106 return err
107 }
108 hf := installer.NewGitHelmFetcher()
giof6ad2982024-08-23 17:42:49 +0400109 vpnAPIClient := installer.NewHeadscaleAPIClient(appManagerFlags.headscaleAPIAddr)
110 cnc := &installer.NginxProxyConfigurator{
111 // TODO(gio): read from env config
112 PrivateSubdomain: "p",
113 DNSAPIAddr: appManagerFlags.dnsAPIAddr,
114 Repo: repoIO,
115 NginxConfigPath: appManagerFlags.clusterProxyConfigPath,
116 }
117 m, err := installer.NewAppManager(repoIO, nsc, jc, hf, vpnAPIClient, cnc, "/apps")
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +0400118 if err != nil {
119 return err
120 }
gio3cdee592024-04-17 10:15:56 +0400121 env, err := m.Config()
gio3af43942024-04-16 08:13:50 +0400122 if err != nil {
123 return err
124 }
125 log.Println("Read config")
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +0400126 log.Println("Creating repository")
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400127 var r installer.AppRepository
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +0400128 if appManagerFlags.appRepoAddr != "" {
129 fs := memfs.New()
130 err = installer.FetchAppsFromHTTPRepository(appManagerFlags.appRepoAddr, fs)
131 if err != nil {
132 return err
133 }
134 r, err = installer.NewFSAppRepository(fs)
135 if err != nil {
136 return err
137 }
138 } else {
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400139 r = installer.NewInMemoryAppRepository(installer.CreateStoreApps())
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +0400140 }
giof6ad2982024-08-23 17:42:49 +0400141 fr := installer.NewInMemoryAppRepository(installer.CreateAllEnvApps())
gio778577f2024-04-29 09:44:38 +0400142 helmMon, err := newHelmReleaseMonitor()
143 if err != nil {
144 return err
145 }
gio59946282024-10-07 12:55:51 +0400146 s, err := appmanager.NewServer(
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400147 appManagerFlags.port,
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,
giof6ad2982024-08-23 17:42:49 +0400154 cnc,
155 vpnAPIClient,
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400156 )
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400157 if err != nil {
158 return err
159 }
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +0400160 return s.Start()
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +0400161}