blob: 0142bee12c8c5608ca973867e30cc197599a3dd6 [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"
9 "github.com/giolekva/pcloud/core/installer/soft"
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +040010 "github.com/giolekva/pcloud/core/installer/welcome"
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040011)
12
13var envManagerFlags struct {
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040014 repoAddr string
15 repoName string
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040016 sshKey string
17 port int
18}
19
20func envManagerCmd() *cobra.Command {
21 cmd := &cobra.Command{
22 Use: "envmanager",
23 RunE: envManagerCmdRun,
24 }
25 cmd.Flags().StringVar(
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040026 &envManagerFlags.repoAddr,
27 "repo-addr",
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040028 "",
29 "",
30 )
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040031 cmd.Flags().StringVar(
32 &envManagerFlags.repoName,
33 "repo-name",
34 "",
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040035 "",
36 )
37 cmd.Flags().StringVar(
38 &envManagerFlags.sshKey,
39 "ssh-key",
40 "",
41 "",
42 )
43 cmd.Flags().IntVar(
44 &envManagerFlags.port,
45 "port",
46 8080,
47 "",
48 )
49 return cmd
50}
51
52func envManagerCmdRun(cmd *cobra.Command, args []string) error {
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +040053 sshKey, err := installer.NewSSHKeyPair(envManagerFlags.sshKey)
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040054 if err != nil {
55 return err
56 }
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +040057 ss, err := soft.WaitForClient(envManagerFlags.repoAddr, sshKey.RawPrivateKey(), log.Default())
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040058 if err != nil {
59 return err
60 }
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +040061 log.Printf("Created Soft Serve client\n")
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040062 repo, err := ss.GetRepo(envManagerFlags.repoName)
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040063 if err != nil {
64 return err
65 }
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +040066 log.Printf("Cloned repo: %s\n", envManagerFlags.repoName)
gio3af43942024-04-16 08:13:50 +040067 repoIO, err := installer.NewRepoIO(repo, sshKey.Signer())
68 if err != nil {
69 return err
70 }
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 }
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +040079 s := welcome.NewEnvServer(
80 envManagerFlags.port,
81 ss,
82 repoIO,
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040083 nsCreator,
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +040084 dnsFetcher,
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +040085 installer.NewFixedLengthRandomNameGenerator(4),
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +040086 )
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +040087 log.Printf("Starting server\n")
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +040088 s.Start()
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040089 return nil
90}