blob: a77061a895fef410cd63c1bf960543b66ee4e10c [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 "os"
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +04007
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +04008 "github.com/spf13/cobra"
9
10 "github.com/giolekva/pcloud/core/installer"
11 "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 {
55 sshKey, err := os.ReadFile(envManagerFlags.sshKey)
56 if err != nil {
57 return err
58 }
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040059 repoAddr, err := netip.ParseAddrPort(envManagerFlags.repoAddr)
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040060 if err != nil {
61 return err
62 }
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040063 ss, err := soft.NewClient(repoAddr, sshKey, log.Default())
64 if err != nil {
65 return err
66 }
67 repo, err := ss.GetRepo(envManagerFlags.repoName)
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040068 if err != nil {
69 return err
70 }
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040071 repoIO := installer.NewRepoIO(repo, ss.Signer)
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040072 nsCreator, err := newNSCreator()
73 if err != nil {
74 return err
75 }
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +040076 s := welcome.NewEnvServer(
77 envManagerFlags.port,
78 ss,
79 repoIO,
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040080 nsCreator,
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +040081 )
82 s.Start()
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040083 return nil
84}