blob: e6b8499d67c1bececd6b04de12d73aad645de4fa [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"
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +040012 "github.com/giolekva/pcloud/core/installer/welcome"
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040013)
14
15var envManagerFlags struct {
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040016 repoAddr string
17 repoName string
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040018 sshKey string
19 port int
20}
21
22func envManagerCmd() *cobra.Command {
23 cmd := &cobra.Command{
24 Use: "envmanager",
25 RunE: envManagerCmdRun,
26 }
27 cmd.Flags().StringVar(
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040028 &envManagerFlags.repoAddr,
29 "repo-addr",
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040030 "",
31 "",
32 )
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040033 cmd.Flags().StringVar(
34 &envManagerFlags.repoName,
35 "repo-name",
36 "",
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040037 "",
38 )
39 cmd.Flags().StringVar(
40 &envManagerFlags.sshKey,
41 "ssh-key",
42 "",
43 "",
44 )
45 cmd.Flags().IntVar(
46 &envManagerFlags.port,
47 "port",
48 8080,
49 "",
50 )
51 return cmd
52}
53
54func envManagerCmdRun(cmd *cobra.Command, args []string) error {
gioe72b54f2024-04-22 10:44:41 +040055 repoClient := soft.RealClientGetter{}
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +040056 sshKey, err := installer.NewSSHKeyPair(envManagerFlags.sshKey)
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040057 if err != nil {
58 return err
59 }
gioe72b54f2024-04-22 10:44:41 +040060 ss, err := repoClient.Get(envManagerFlags.repoAddr, sshKey.RawPrivateKey(), log.Default())
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040061 if err != nil {
62 return err
63 }
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +040064 log.Printf("Created Soft Serve client\n")
gioe72b54f2024-04-22 10:44:41 +040065 repoIO, err := ss.GetRepo(envManagerFlags.repoName)
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040066 if err != nil {
67 return err
68 }
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +040069 log.Printf("Cloned repo: %s\n", envManagerFlags.repoName)
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040070 nsCreator, err := newNSCreator()
71 if err != nil {
72 return err
73 }
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +040074 dnsFetcher, err := newZoneFetcher()
75 if err != nil {
76 return err
77 }
gioe72b54f2024-04-22 10:44:41 +040078 httpClient := http.NewClient()
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +040079 s := welcome.NewEnvServer(
80 envManagerFlags.port,
81 ss,
82 repoIO,
gioe72b54f2024-04-22 10:44:41 +040083 repoClient,
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040084 nsCreator,
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +040085 dnsFetcher,
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +040086 installer.NewFixedLengthRandomNameGenerator(4),
gioe72b54f2024-04-22 10:44:41 +040087 httpClient,
88 dns.NewClient(),
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +040089 )
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +040090 log.Printf("Starting server\n")
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +040091 s.Start()
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040092 return nil
93}