blob: 2f4f5a80670588040ddaf5093058ef5977ad43d5 [file] [log] [blame]
gio0eaf2712024-04-14 13:08:46 +04001package main
2
3import (
4 "encoding/json"
5 "flag"
6 "fmt"
gio5e4d1a72024-10-09 15:25:29 +04007 "log"
gio0eaf2712024-04-14 13:08:46 +04008 "net"
9 "os"
10
11 "golang.org/x/crypto/ssh"
12
13 "github.com/go-git/go-billy/v5/osfs"
14 "github.com/go-git/go-git/v5"
gio2b1157a2024-10-24 08:45:07 +040015 "github.com/go-git/go-git/v5/plumbing"
gio0eaf2712024-04-14 13:08:46 +040016 gitssh "github.com/go-git/go-git/v5/plumbing/transport/ssh"
17 "github.com/go-git/go-git/v5/storage/memory"
18)
19
20var port = flag.Int("port", 3000, "Port to listen on")
gio266c04f2024-07-03 14:18:45 +040021var appId = flag.String("app-id", "", "Application ID")
gio0eaf2712024-04-14 13:08:46 +040022var repoAddr = flag.String("repo-addr", "", "Git repository address")
gio2b1157a2024-10-24 08:45:07 +040023var branch = flag.String("branch", "", "Name of the branch to process")
giofc441e32024-11-11 16:26:14 +040024var rootDir = flag.String("root-dir", "/", "Path to the app code")
gio0eaf2712024-04-14 13:08:46 +040025var sshKey = flag.String("ssh-key", "", "Private SSH key to access Git repository")
26var appDir = flag.String("app-dir", "", "Path to store application repository locally")
27var runCfg = flag.String("run-cfg", "", "Run configuration")
gioa60f0de2024-07-08 10:49:48 +040028var managerAddr = flag.String("manager-addr", "", "Address of the manager")
gio0eaf2712024-04-14 13:08:46 +040029
30type Command struct {
31 Bin string `json:"bin"`
32 Args []string `json:"args"`
gio1364e432024-06-29 11:39:18 +040033 Env []string `json:"env"`
gio0eaf2712024-04-14 13:08:46 +040034}
35
giofc441e32024-11-11 16:26:14 +040036func CloneRepositoryBranch(addr, branch, rootDir string, signer ssh.Signer, path string) error {
gio2b1157a2024-10-24 08:45:07 +040037 ref := fmt.Sprintf("refs/heads/%s", branch)
giofc441e32024-11-11 16:26:14 +040038 opts := &git.CloneOptions{
39 URL: addr,
40 RemoteName: "origin",
41 ReferenceName: plumbing.ReferenceName(ref),
42 SingleBranch: true,
43 Depth: 1,
44 InsecureSkipTLS: true,
45 Progress: os.Stdout,
46 }
47 if signer != nil {
48 opts.Auth = &gitssh.PublicKeys{
gio0eaf2712024-04-14 13:08:46 +040049 User: "git",
50 Signer: signer,
51 HostKeyCallbackHelper: gitssh.HostKeyCallbackHelper{
52 HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
53 // TODO(giolekva): verify server public key
54 fmt.Printf("--- %+v\n", ssh.MarshalAuthorizedKey(key))
55 return nil
56 },
57 },
giofc441e32024-11-11 16:26:14 +040058 }
59 }
60 c, err := git.Clone(memory.NewStorage(), osfs.New(path, osfs.WithBoundOS()), opts)
giof5ffedb2024-06-19 14:14:43 +040061 if err != nil {
62 return err
63 }
64 wt, err := c.Worktree()
65 if err != nil {
66 return err
67 }
giofc441e32024-11-11 16:26:14 +040068 if wt == nil {
69 panic(wt)
giof5ffedb2024-06-19 14:14:43 +040070 }
giofc441e32024-11-11 16:26:14 +040071 // TODO(gio): This should probably be removed.
72 // sb, err := wt.Submodules()
73 // if err != nil {
74 // return err
75 // }
76 // if err := sb.Init(); err != nil {
77 // return err
78 // }
79 // if err := sb.Update(&git.SubmoduleUpdateOptions{
80 // Depth: 1,
81 // }); err != nil {
82 // return err
83 // }
gio0eaf2712024-04-14 13:08:46 +040084 return err
85}
86
87func main() {
88 flag.Parse()
89 self, ok := os.LookupEnv("SELF_IP")
90 if !ok {
91 panic("no SELF_IP")
92 }
giofc441e32024-11-11 16:26:14 +040093 var signer ssh.Signer
94 if *sshKey != "" {
95 key, err := os.ReadFile(*sshKey)
96 if err != nil {
97 panic(err)
98 }
99 signer, err = ssh.ParsePrivateKey(key)
100 if err != nil {
101 panic(err)
102 }
gio0eaf2712024-04-14 13:08:46 +0400103 }
giofc441e32024-11-11 16:26:14 +0400104 if err := CloneRepositoryBranch(*repoAddr, *branch, *rootDir, signer, *appDir); err != nil {
gio0eaf2712024-04-14 13:08:46 +0400105 panic(err)
106 }
107 r, err := os.Open(*runCfg)
108 if err != nil {
109 panic(err)
110 }
111 defer r.Close()
112 var cmds []Command
113 if err := json.NewDecoder(r).Decode(&cmds); err != nil {
114 panic(err)
115 }
giofc441e32024-11-11 16:26:14 +0400116 s := NewServer(*port, *appId, *repoAddr, *branch, *rootDir, signer, *appDir, cmds, self, *managerAddr)
gio5e4d1a72024-10-09 15:25:29 +0400117 if err := s.Start(); err != nil {
118 log.Fatal(err)
119 }
gio0eaf2712024-04-14 13:08:46 +0400120}