| package main |
| |
| import ( |
| "net" |
| "os" |
| |
| "github.com/go-git/go-billy/v5/memfs" |
| "github.com/go-git/go-git/v5" |
| gitssh "github.com/go-git/go-git/v5/plumbing/transport/ssh" |
| "github.com/go-git/go-git/v5/storage/memory" |
| "github.com/spf13/cobra" |
| "golang.org/x/crypto/ssh" |
| |
| "github.com/giolekva/pcloud/core/installer" |
| "github.com/giolekva/pcloud/core/installer/welcome" |
| ) |
| |
| var appManagerFlags struct { |
| sshKey string |
| repoAddr string |
| port int |
| webAppAddr string |
| } |
| |
| func appManagerCmd() *cobra.Command { |
| cmd := &cobra.Command{ |
| Use: "appmanager", |
| RunE: appManagerCmdRun, |
| } |
| cmd.Flags().StringVar( |
| &appManagerFlags.sshKey, |
| "ssh-key", |
| "", |
| "", |
| ) |
| cmd.Flags().StringVar( |
| &appManagerFlags.repoAddr, |
| "repo-addr", |
| "", |
| "", |
| ) |
| cmd.Flags().IntVar( |
| &appManagerFlags.port, |
| "port", |
| 8080, |
| "", |
| ) |
| cmd.Flags().StringVar( |
| &appManagerFlags.webAppAddr, |
| "web-app-addr", |
| "", |
| "", |
| ) |
| return cmd |
| } |
| |
| func appManagerCmdRun(cmd *cobra.Command, args []string) error { |
| sshKey, err := os.ReadFile(appManagerFlags.sshKey) |
| if err != nil { |
| return err |
| } |
| signer, err := ssh.ParsePrivateKey(sshKey) |
| if err != nil { |
| return err |
| } |
| repo, err := cloneRepo(appManagerFlags.repoAddr, signer) |
| if err != nil { |
| return err |
| } |
| kube, err := newNSCreator() |
| if err != nil { |
| return err |
| } |
| m, err := installer.NewAppManager( |
| installer.NewRepoIO(repo, signer), |
| kube, |
| ) |
| if err != nil { |
| return err |
| } |
| r := installer.NewInMemoryAppRepository[installer.StoreApp](installer.CreateStoreApps()) |
| s := welcome.NewAppManagerServer( |
| appManagerFlags.port, |
| appManagerFlags.webAppAddr, |
| m, |
| r, |
| ) |
| s.Start() |
| return nil |
| } |
| |
| func cloneRepo(address string, signer ssh.Signer) (*git.Repository, error) { |
| return git.Clone(memory.NewStorage(), memfs.New(), &git.CloneOptions{ |
| URL: address, |
| Auth: auth(signer), |
| RemoteName: "origin", |
| InsecureSkipTLS: true, |
| }) |
| } |
| |
| func auth(signer ssh.Signer) *gitssh.PublicKeys { |
| return &gitssh.PublicKeys{ |
| Signer: signer, |
| HostKeyCallbackHelper: gitssh.HostKeyCallbackHelper{ |
| HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error { |
| // TODO(giolekva): verify server public key |
| // fmt.Printf("## %s || %s -- \n", serverPubKey, ssh.MarshalAuthorizedKey(key)) |
| return nil |
| }, |
| }, |
| } |
| } |