blob: a6224a0eac2c5f89a8a37926343955d82514b3f9 [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 {
gio36b23b32024-08-25 12:20:54 +040019 sshKey string
20 repoAddr string
21 port int
22 appRepoAddr string
23 headscaleAPIAddr string
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040024}
25
26func appManagerCmd() *cobra.Command {
27 cmd := &cobra.Command{
28 Use: "appmanager",
Giorgi Lekveishvili7efe22f2023-05-30 13:01:53 +040029 RunE: appManagerCmdRun,
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040030 }
gio308105e2024-04-19 13:12:13 +040031 cmd.Flags().IntVar(
32 &appManagerFlags.port,
33 "port",
34 8080,
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040035 "",
36 )
37 cmd.Flags().StringVar(
Giorgi Lekveishvili7efe22f2023-05-30 13:01:53 +040038 &appManagerFlags.repoAddr,
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040039 "repo-addr",
40 "",
41 "",
42 )
gio308105e2024-04-19 13:12:13 +040043 cmd.Flags().StringVar(
44 &appManagerFlags.sshKey,
45 "ssh-key",
46 "",
Giorgi Lekveishvili7efe22f2023-05-30 13:01:53 +040047 "",
48 )
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +040049 cmd.Flags().StringVar(
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +040050 &appManagerFlags.appRepoAddr,
51 "app-repo-addr",
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +040052 "",
53 "",
54 )
gio36b23b32024-08-25 12:20:54 +040055 cmd.Flags().StringVar(
56 &appManagerFlags.headscaleAPIAddr,
57 "headscale-api-addr",
58 "",
59 "",
60 )
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040061 return cmd
62}
63
64func appManagerCmdRun(cmd *cobra.Command, args []string) error {
Giorgi Lekveishvili7efe22f2023-05-30 13:01:53 +040065 sshKey, err := os.ReadFile(appManagerFlags.sshKey)
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040066 if err != nil {
67 return err
68 }
69 signer, err := ssh.ParsePrivateKey(sshKey)
70 if err != nil {
71 return err
72 }
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040073 addr, err := soft.ParseRepositoryAddress(appManagerFlags.repoAddr)
74 if err != nil {
75 return err
76 }
Giorgi Lekveishvili57dffb32023-08-07 15:45:43 +040077 repo, err := soft.CloneRepository(addr, signer)
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040078 if err != nil {
79 return err
80 }
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +040081 log.Println("Cloned repository")
gioe72b54f2024-04-22 10:44:41 +040082 repoIO, err := soft.NewRepoIO(repo, signer)
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +040083 if err != nil {
84 return err
85 }
giof8843412024-05-22 16:38:05 +040086 nsc, err := newNSCreator()
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040087 if err != nil {
88 return err
89 }
giof8843412024-05-22 16:38:05 +040090 jc, err := newJobCreator()
91 if err != nil {
92 return err
93 }
94 hf := installer.NewGitHelmFetcher()
gio36b23b32024-08-25 12:20:54 +040095 vpnKeyGen := installer.NewHeadscaleAPIClient(appManagerFlags.headscaleAPIAddr)
96 m, err := installer.NewAppManager(repoIO, nsc, jc, hf, vpnKeyGen, "/apps")
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040097 if err != nil {
98 return err
99 }
gio3cdee592024-04-17 10:15:56 +0400100 env, err := m.Config()
gio3af43942024-04-16 08:13:50 +0400101 if err != nil {
102 return err
103 }
104 log.Println("Read config")
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +0400105 log.Println("Creating repository")
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400106 var r installer.AppRepository
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +0400107 if appManagerFlags.appRepoAddr != "" {
108 fs := memfs.New()
109 err = installer.FetchAppsFromHTTPRepository(appManagerFlags.appRepoAddr, fs)
110 if err != nil {
111 return err
112 }
113 r, err = installer.NewFSAppRepository(fs)
114 if err != nil {
115 return err
116 }
117 } else {
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400118 r = installer.NewInMemoryAppRepository(installer.CreateStoreApps())
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +0400119 }
gio778577f2024-04-29 09:44:38 +0400120 helmMon, err := newHelmReleaseMonitor()
121 if err != nil {
122 return err
123 }
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400124 s, err := welcome.NewAppManagerServer(
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400125 appManagerFlags.port,
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400126 m,
127 r,
gio43b0f422024-08-21 10:40:13 +0400128 tasks.NewFixedReconciler(env.Id, env.Id),
gio778577f2024-04-29 09:44:38 +0400129 helmMon,
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400130 )
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400131 if err != nil {
132 return err
133 }
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +0400134 return s.Start()
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +0400135}