blob: aa33de8c37aaa4c887b6e7f48607baa2a868a527 [file] [log] [blame]
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +04001package main
2
3import (
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +04004 "log"
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +04005
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +04006 "github.com/spf13/cobra"
7
8 "github.com/giolekva/pcloud/core/installer"
gioe72b54f2024-04-22 10:44:41 +04009 "github.com/giolekva/pcloud/core/installer/dns"
10 "github.com/giolekva/pcloud/core/installer/http"
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040011 "github.com/giolekva/pcloud/core/installer/soft"
giod9c398e2024-06-06 13:33:03 +040012 "github.com/giolekva/pcloud/core/installer/tasks"
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +040013 "github.com/giolekva/pcloud/core/installer/welcome"
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040014)
15
16var envManagerFlags struct {
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040017 repoAddr string
18 repoName string
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040019 sshKey string
20 port int
21}
22
23func envManagerCmd() *cobra.Command {
24 cmd := &cobra.Command{
25 Use: "envmanager",
26 RunE: envManagerCmdRun,
27 }
28 cmd.Flags().StringVar(
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040029 &envManagerFlags.repoAddr,
30 "repo-addr",
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040031 "",
32 "",
33 )
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040034 cmd.Flags().StringVar(
35 &envManagerFlags.repoName,
36 "repo-name",
37 "",
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040038 "",
39 )
40 cmd.Flags().StringVar(
41 &envManagerFlags.sshKey,
42 "ssh-key",
43 "",
44 "",
45 )
46 cmd.Flags().IntVar(
47 &envManagerFlags.port,
48 "port",
49 8080,
50 "",
51 )
52 return cmd
53}
54
55func envManagerCmdRun(cmd *cobra.Command, args []string) error {
gioe72b54f2024-04-22 10:44:41 +040056 repoClient := soft.RealClientGetter{}
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +040057 sshKey, err := installer.NewSSHKeyPair(envManagerFlags.sshKey)
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040058 if err != nil {
59 return err
60 }
gioe72b54f2024-04-22 10:44:41 +040061 ss, err := repoClient.Get(envManagerFlags.repoAddr, sshKey.RawPrivateKey(), log.Default())
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040062 if err != nil {
63 return err
64 }
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +040065 log.Printf("Created Soft Serve client\n")
gioe72b54f2024-04-22 10:44:41 +040066 repoIO, err := ss.GetRepo(envManagerFlags.repoName)
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040067 if err != nil {
68 return err
69 }
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +040070 log.Printf("Cloned repo: %s\n", envManagerFlags.repoName)
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040071 nsCreator, err := newNSCreator()
72 if err != nil {
73 return err
74 }
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +040075 dnsFetcher, err := newZoneFetcher()
76 if err != nil {
77 return err
78 }
gioe72b54f2024-04-22 10:44:41 +040079 httpClient := http.NewClient()
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +040080 s := welcome.NewEnvServer(
81 envManagerFlags.port,
82 ss,
83 repoIO,
gioe72b54f2024-04-22 10:44:41 +040084 repoClient,
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040085 nsCreator,
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +040086 dnsFetcher,
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +040087 installer.NewFixedLengthRandomNameGenerator(4),
gioe72b54f2024-04-22 10:44:41 +040088 httpClient,
89 dns.NewClient(),
giod9c398e2024-06-06 13:33:03 +040090 tasks.NewTaskMap(),
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +040091 )
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +040092 log.Printf("Starting server\n")
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +040093 s.Start()
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040094 return nil
95}