blob: 769ea5a50da7de07edec8602d9925182bbb6826c [file] [log] [blame]
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +04001package main
2
3import (
4 "os"
5
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +04006 "github.com/spf13/cobra"
7 "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 Lekveishvilibd6be7f2023-05-26 15:51:28 +040012)
13
14var appManagerFlags struct {
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +040015 sshKey string
16 repoAddr string
17 port int
18 webAppAddr string
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040019}
20
21func appManagerCmd() *cobra.Command {
22 cmd := &cobra.Command{
23 Use: "appmanager",
Giorgi Lekveishvili7efe22f2023-05-30 13:01:53 +040024 RunE: appManagerCmdRun,
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040025 }
26 cmd.Flags().StringVar(
Giorgi Lekveishvili7efe22f2023-05-30 13:01:53 +040027 &appManagerFlags.sshKey,
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040028 "ssh-key",
29 "",
30 "",
31 )
32 cmd.Flags().StringVar(
Giorgi Lekveishvili7efe22f2023-05-30 13:01:53 +040033 &appManagerFlags.repoAddr,
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040034 "repo-addr",
35 "",
36 "",
37 )
Giorgi Lekveishvili7efe22f2023-05-30 13:01:53 +040038 cmd.Flags().IntVar(
39 &appManagerFlags.port,
40 "port",
41 8080,
42 "",
43 )
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +040044 cmd.Flags().StringVar(
45 &appManagerFlags.webAppAddr,
46 "web-app-addr",
47 "",
48 "",
49 )
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040050 return cmd
51}
52
53func appManagerCmdRun(cmd *cobra.Command, args []string) error {
Giorgi Lekveishvili7efe22f2023-05-30 13:01:53 +040054 sshKey, err := os.ReadFile(appManagerFlags.sshKey)
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040055 if err != nil {
56 return err
57 }
58 signer, err := ssh.ParsePrivateKey(sshKey)
59 if err != nil {
60 return err
61 }
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040062 addr, err := soft.ParseRepositoryAddress(appManagerFlags.repoAddr)
63 if err != nil {
64 return err
65 }
Giorgi Lekveishvili57dffb32023-08-07 15:45:43 +040066 repo, err := soft.CloneRepository(addr, signer)
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040067 if err != nil {
68 return err
69 }
Giorgi Lekveishvili27b2b572023-06-30 10:44:45 +040070 kube, err := newNSCreator()
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040071 if err != nil {
72 return err
73 }
74 m, err := installer.NewAppManager(
75 installer.NewRepoIO(repo, signer),
76 kube,
77 )
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040078 if err != nil {
79 return err
80 }
Giorgi Lekveishvili27b2b572023-06-30 10:44:45 +040081 r := installer.NewInMemoryAppRepository[installer.StoreApp](installer.CreateStoreApps())
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +040082 s := welcome.NewAppManagerServer(
83 appManagerFlags.port,
84 appManagerFlags.webAppAddr,
85 m,
86 r,
87 )
88 s.Start()
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040089 return nil
90}