blob: cfb69a4956d881898d1e11f7fc928ffa6375a000 [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 Lekveishvili4257b902023-07-07 17:08:42 +040011 "github.com/giolekva/pcloud/core/installer/welcome"
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +040012
13 "github.com/go-git/go-billy/v5/memfs"
14 "github.com/spf13/cobra"
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040015)
16
17var appManagerFlags struct {
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +040018 sshKey string
19 repoAddr string
20 port int
21 appRepoAddr string
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040022}
23
24func appManagerCmd() *cobra.Command {
25 cmd := &cobra.Command{
26 Use: "appmanager",
Giorgi Lekveishvili7efe22f2023-05-30 13:01:53 +040027 RunE: appManagerCmdRun,
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040028 }
29 cmd.Flags().StringVar(
Giorgi Lekveishvili7efe22f2023-05-30 13:01:53 +040030 &appManagerFlags.sshKey,
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040031 "ssh-key",
32 "",
33 "",
34 )
35 cmd.Flags().StringVar(
Giorgi Lekveishvili7efe22f2023-05-30 13:01:53 +040036 &appManagerFlags.repoAddr,
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040037 "repo-addr",
38 "",
39 "",
40 )
Giorgi Lekveishvili7efe22f2023-05-30 13:01:53 +040041 cmd.Flags().IntVar(
42 &appManagerFlags.port,
43 "port",
44 8080,
45 "",
46 )
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +040047 cmd.Flags().StringVar(
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +040048 &appManagerFlags.appRepoAddr,
49 "app-repo-addr",
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +040050 "",
51 "",
52 )
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040053 return cmd
54}
55
56func appManagerCmdRun(cmd *cobra.Command, args []string) error {
Giorgi Lekveishvili7efe22f2023-05-30 13:01:53 +040057 sshKey, err := os.ReadFile(appManagerFlags.sshKey)
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040058 if err != nil {
59 return err
60 }
61 signer, err := ssh.ParsePrivateKey(sshKey)
62 if err != nil {
63 return err
64 }
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040065 addr, err := soft.ParseRepositoryAddress(appManagerFlags.repoAddr)
66 if err != nil {
67 return err
68 }
Giorgi Lekveishvili57dffb32023-08-07 15:45:43 +040069 repo, err := soft.CloneRepository(addr, signer)
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040070 if err != nil {
71 return err
72 }
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +040073 log.Println("Cloned repository")
Giorgi Lekveishvili27b2b572023-06-30 10:44:45 +040074 kube, err := newNSCreator()
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040075 if err != nil {
76 return err
77 }
78 m, err := installer.NewAppManager(
79 installer.NewRepoIO(repo, signer),
80 kube,
81 )
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040082 if err != nil {
83 return err
84 }
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +040085 log.Println("Creating repository")
86 var r installer.AppRepository[installer.StoreApp]
87 if appManagerFlags.appRepoAddr != "" {
88 fs := memfs.New()
89 err = installer.FetchAppsFromHTTPRepository(appManagerFlags.appRepoAddr, fs)
90 if err != nil {
91 return err
92 }
93 r, err = installer.NewFSAppRepository(fs)
94 if err != nil {
95 return err
96 }
97 } else {
98 r = installer.NewInMemoryAppRepository[installer.StoreApp](installer.CreateStoreApps())
99 }
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400100 s := welcome.NewAppManagerServer(
101 appManagerFlags.port,
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400102 m,
103 r,
104 )
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +0400105 return s.Start()
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +0400106}