blob: 60272048a82e6c4bc9aff286f22cf32e26addd38 [file] [log] [blame]
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +04001package main
2
3import (
Giorgi Lekveishvili8fe056b2023-06-23 12:01:43 +04004 "net"
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +04005 "os"
6
Giorgi Lekveishvili8fe056b2023-06-23 12:01:43 +04007 "github.com/go-git/go-billy/v5/memfs"
8 "github.com/go-git/go-git/v5"
9 gitssh "github.com/go-git/go-git/v5/plumbing/transport/ssh"
10 "github.com/go-git/go-git/v5/storage/memory"
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040011 "github.com/spf13/cobra"
12 "golang.org/x/crypto/ssh"
Giorgi Lekveishvili8fe056b2023-06-23 12:01:43 +040013
14 "github.com/giolekva/pcloud/core/installer"
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +040015 "github.com/giolekva/pcloud/core/installer/welcome"
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040016)
17
18var appManagerFlags struct {
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +040019 sshKey string
20 repoAddr string
21 port int
22 webAppAddr string
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040023}
24
25func appManagerCmd() *cobra.Command {
26 cmd := &cobra.Command{
27 Use: "appmanager",
Giorgi Lekveishvili7efe22f2023-05-30 13:01:53 +040028 RunE: appManagerCmdRun,
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040029 }
30 cmd.Flags().StringVar(
Giorgi Lekveishvili7efe22f2023-05-30 13:01:53 +040031 &appManagerFlags.sshKey,
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040032 "ssh-key",
33 "",
34 "",
35 )
36 cmd.Flags().StringVar(
Giorgi Lekveishvili7efe22f2023-05-30 13:01:53 +040037 &appManagerFlags.repoAddr,
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040038 "repo-addr",
39 "",
40 "",
41 )
Giorgi Lekveishvili7efe22f2023-05-30 13:01:53 +040042 cmd.Flags().IntVar(
43 &appManagerFlags.port,
44 "port",
45 8080,
46 "",
47 )
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +040048 cmd.Flags().StringVar(
49 &appManagerFlags.webAppAddr,
50 "web-app-addr",
51 "",
52 "",
53 )
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040054 return cmd
55}
56
57func appManagerCmdRun(cmd *cobra.Command, args []string) error {
Giorgi Lekveishvili7efe22f2023-05-30 13:01:53 +040058 sshKey, err := os.ReadFile(appManagerFlags.sshKey)
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040059 if err != nil {
60 return err
61 }
62 signer, err := ssh.ParsePrivateKey(sshKey)
63 if err != nil {
64 return err
65 }
Giorgi Lekveishvili7efe22f2023-05-30 13:01:53 +040066 repo, err := cloneRepo(appManagerFlags.repoAddr, signer)
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040067 if err != nil {
68 return err
69 }
Giorgi Lekveishvili27b2b572023-06-30 10:44:45 +040070 kube, err := newNSCreator()
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040071 if err != nil {
72 return err
73 }
74 m, err := installer.NewAppManager(
75 installer.NewRepoIO(repo, signer),
76 kube,
77 )
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040078 if err != nil {
79 return err
80 }
Giorgi Lekveishvili27b2b572023-06-30 10:44:45 +040081 r := installer.NewInMemoryAppRepository[installer.StoreApp](installer.CreateStoreApps())
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +040082 s := welcome.NewAppManagerServer(
83 appManagerFlags.port,
84 appManagerFlags.webAppAddr,
85 m,
86 r,
87 )
88 s.Start()
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040089 return nil
90}
Giorgi Lekveishvili7efe22f2023-05-30 13:01:53 +040091
Giorgi Lekveishvili8fe056b2023-06-23 12:01:43 +040092func cloneRepo(address string, signer ssh.Signer) (*git.Repository, error) {
93 return git.Clone(memory.NewStorage(), memfs.New(), &git.CloneOptions{
94 URL: address,
95 Auth: auth(signer),
96 RemoteName: "origin",
97 InsecureSkipTLS: true,
98 })
99}
100
101func auth(signer ssh.Signer) *gitssh.PublicKeys {
102 return &gitssh.PublicKeys{
103 Signer: signer,
104 HostKeyCallbackHelper: gitssh.HostKeyCallbackHelper{
105 HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
106 // TODO(giolekva): verify server public key
107 // fmt.Printf("## %s || %s -- \n", serverPubKey, ssh.MarshalAuthorizedKey(key))
108 return nil
109 },
110 },
111 }
112}