blob: ca99831db6938e74d0622d9bdac00cc4b0d21ec0 [file] [log] [blame]
gio0eaf2712024-04-14 13:08:46 +04001package main
2
3import (
4 "encoding/json"
5 "flag"
6 "fmt"
7 "net"
8 "os"
9
10 "golang.org/x/crypto/ssh"
11
12 "github.com/go-git/go-billy/v5/osfs"
13 "github.com/go-git/go-git/v5"
14 gitssh "github.com/go-git/go-git/v5/plumbing/transport/ssh"
15 "github.com/go-git/go-git/v5/storage/memory"
16)
17
18var port = flag.Int("port", 3000, "Port to listen on")
19var repoAddr = flag.String("repo-addr", "", "Git repository address")
20var sshKey = flag.String("ssh-key", "", "Private SSH key to access Git repository")
21var appDir = flag.String("app-dir", "", "Path to store application repository locally")
22var runCfg = flag.String("run-cfg", "", "Run configuration")
23var manager = flag.String("manager", "", "Address of the manager")
24
25type Command struct {
26 Bin string `json:"bin"`
27 Args []string `json:"args"`
28}
29
30func CloneRepository(addr string, signer ssh.Signer, path string) error {
giof5ffedb2024-06-19 14:14:43 +040031 c, err := git.Clone(memory.NewStorage(), osfs.New(path, osfs.WithBoundOS()), &git.CloneOptions{
gio0eaf2712024-04-14 13:08:46 +040032 URL: addr,
33 Auth: &gitssh.PublicKeys{
34 User: "git",
35 Signer: signer,
36 HostKeyCallbackHelper: gitssh.HostKeyCallbackHelper{
37 HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
38 // TODO(giolekva): verify server public key
39 fmt.Printf("--- %+v\n", ssh.MarshalAuthorizedKey(key))
40 return nil
41 },
42 },
43 },
giof5ffedb2024-06-19 14:14:43 +040044 RemoteName: "origin",
45 ReferenceName: "refs/heads/master",
46 SingleBranch: true,
47 Depth: 1,
48 InsecureSkipTLS: true,
49 Progress: os.Stdout,
gio0eaf2712024-04-14 13:08:46 +040050 })
giof5ffedb2024-06-19 14:14:43 +040051 if err != nil {
52 return err
53 }
54 wt, err := c.Worktree()
55 if err != nil {
56 return err
57 }
58 sb, err := wt.Submodules()
59 if err != nil {
60 return err
61 }
62 if err := sb.Init(); err != nil {
63 return err
64 }
65 if err := sb.Update(&git.SubmoduleUpdateOptions{
66 Depth: 1,
67 }); err != nil {
68 return err
69 }
gio0eaf2712024-04-14 13:08:46 +040070 return err
71}
72
73func main() {
74 flag.Parse()
75 self, ok := os.LookupEnv("SELF_IP")
76 if !ok {
77 panic("no SELF_IP")
78 }
79 key, err := os.ReadFile(*sshKey)
80 if err != nil {
81 panic(err)
82 }
83 signer, err := ssh.ParsePrivateKey(key)
84 if err != nil {
85 panic(err)
86 }
87 if err := CloneRepository(*repoAddr, signer, *appDir); err != nil {
88 panic(err)
89 }
90 r, err := os.Open(*runCfg)
91 if err != nil {
92 panic(err)
93 }
94 defer r.Close()
95 var cmds []Command
96 if err := json.NewDecoder(r).Decode(&cmds); err != nil {
97 panic(err)
98 }
99 s := NewServer(*port, *repoAddr, signer, *appDir, cmds, self, *manager)
100 s.Start()
101}