blob: 8e21c6779e2736bd6fb309eceb616703f4c671b2 [file] [log] [blame]
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +04001package main
2
3import (
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +04004 "log"
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +04005 "net/netip"
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +04006
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +04007 "github.com/spf13/cobra"
8
9 "github.com/giolekva/pcloud/core/installer"
10 "github.com/giolekva/pcloud/core/installer/soft"
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +040011 "github.com/giolekva/pcloud/core/installer/welcome"
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040012)
13
14var envManagerFlags struct {
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040015 repoAddr string
16 repoName string
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040017 sshKey string
18 port int
19}
20
21func envManagerCmd() *cobra.Command {
22 cmd := &cobra.Command{
23 Use: "envmanager",
24 RunE: envManagerCmdRun,
25 }
26 cmd.Flags().StringVar(
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040027 &envManagerFlags.repoAddr,
28 "repo-addr",
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040029 "",
30 "",
31 )
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040032 cmd.Flags().StringVar(
33 &envManagerFlags.repoName,
34 "repo-name",
35 "",
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040036 "",
37 )
38 cmd.Flags().StringVar(
39 &envManagerFlags.sshKey,
40 "ssh-key",
41 "",
42 "",
43 )
44 cmd.Flags().IntVar(
45 &envManagerFlags.port,
46 "port",
47 8080,
48 "",
49 )
50 return cmd
51}
52
53func envManagerCmdRun(cmd *cobra.Command, args []string) error {
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +040054 sshKey, err := installer.NewSSHKeyPair(envManagerFlags.sshKey)
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040055 if err != nil {
56 return err
57 }
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040058 repoAddr, err := netip.ParseAddrPort(envManagerFlags.repoAddr)
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040059 if err != nil {
60 return err
61 }
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +040062 ss, err := soft.NewClient(repoAddr, sshKey.RawPrivateKey(), log.Default())
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040063 if err != nil {
64 return err
65 }
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +040066 log.Printf("Created Soft Serve client\n")
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040067 repo, err := ss.GetRepo(envManagerFlags.repoName)
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040068 if err != nil {
69 return err
70 }
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +040071 log.Printf("Cloned repo: %s\n", envManagerFlags.repoName)
72 repoIO := installer.NewRepoIO(repo, sshKey.Signer())
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040073 nsCreator, err := newNSCreator()
74 if err != nil {
75 return err
76 }
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +040077 s := welcome.NewEnvServer(
78 envManagerFlags.port,
79 ss,
80 repoIO,
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040081 nsCreator,
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +040082 )
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +040083 log.Printf("Starting server\n")
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +040084 s.Start()
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040085 return nil
86}