blob: b9acaa6a5f23f20a22f7e65adf3b393a2fe90264 [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"
gioa421b062025-04-21 09:45:04 +040010 "strings"
gio0eaf2712024-04-14 13:08:46 +040011
12 "golang.org/x/crypto/ssh"
13
14 "github.com/go-git/go-billy/v5/osfs"
15 "github.com/go-git/go-git/v5"
gio2b1157a2024-10-24 08:45:07 +040016 "github.com/go-git/go-git/v5/plumbing"
gio0eaf2712024-04-14 13:08:46 +040017 gitssh "github.com/go-git/go-git/v5/plumbing/transport/ssh"
18 "github.com/go-git/go-git/v5/storage/memory"
19)
20
21var port = flag.Int("port", 3000, "Port to listen on")
gio266c04f2024-07-03 14:18:45 +040022var appId = flag.String("app-id", "", "Application ID")
giob87415c2025-05-08 22:32:11 +040023var service = flag.String("service", "", "Service name")
gio0eaf2712024-04-14 13:08:46 +040024var repoAddr = flag.String("repo-addr", "", "Git repository address")
gio2b1157a2024-10-24 08:45:07 +040025var branch = flag.String("branch", "", "Name of the branch to process")
giofc441e32024-11-11 16:26:14 +040026var rootDir = flag.String("root-dir", "/", "Path to the app code")
gio0eaf2712024-04-14 13:08:46 +040027var sshKey = flag.String("ssh-key", "", "Private SSH key to access Git repository")
28var appDir = flag.String("app-dir", "", "Path to store application repository locally")
29var runCfg = flag.String("run-cfg", "", "Run configuration")
gioa60f0de2024-07-08 10:49:48 +040030var managerAddr = flag.String("manager-addr", "", "Address of the manager")
gio0eaf2712024-04-14 13:08:46 +040031
32type Command struct {
33 Bin string `json:"bin"`
34 Args []string `json:"args"`
gio1364e432024-06-29 11:39:18 +040035 Env []string `json:"env"`
gio0eaf2712024-04-14 13:08:46 +040036}
37
gio5155c1a2025-05-21 15:36:21 +040038func CloneRepositoryBranch(addr, branch, rootDir string, signer ssh.Signer, path string) (string, error) {
gio2b1157a2024-10-24 08:45:07 +040039 ref := fmt.Sprintf("refs/heads/%s", branch)
giofc441e32024-11-11 16:26:14 +040040 opts := &git.CloneOptions{
41 URL: addr,
42 RemoteName: "origin",
43 ReferenceName: plumbing.ReferenceName(ref),
44 SingleBranch: true,
45 Depth: 1,
46 InsecureSkipTLS: true,
47 Progress: os.Stdout,
48 }
49 if signer != nil {
50 opts.Auth = &gitssh.PublicKeys{
gio0eaf2712024-04-14 13:08:46 +040051 User: "git",
52 Signer: signer,
53 HostKeyCallbackHelper: gitssh.HostKeyCallbackHelper{
54 HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
55 // TODO(giolekva): verify server public key
56 fmt.Printf("--- %+v\n", ssh.MarshalAuthorizedKey(key))
57 return nil
58 },
59 },
giofc441e32024-11-11 16:26:14 +040060 }
61 }
62 c, err := git.Clone(memory.NewStorage(), osfs.New(path, osfs.WithBoundOS()), opts)
giof5ffedb2024-06-19 14:14:43 +040063 if err != nil {
gio5155c1a2025-05-21 15:36:21 +040064 return "", err
giof5ffedb2024-06-19 14:14:43 +040065 }
gio5155c1a2025-05-21 15:36:21 +040066 head, err := c.Head()
giof5ffedb2024-06-19 14:14:43 +040067 if err != nil {
gio5155c1a2025-05-21 15:36:21 +040068 return "", err
giof5ffedb2024-06-19 14:14:43 +040069 }
gio5155c1a2025-05-21 15:36:21 +040070 return head.Hash().String(), nil
gio0eaf2712024-04-14 13:08:46 +040071}
72
73func main() {
74 flag.Parse()
75 self, ok := os.LookupEnv("SELF_IP")
76 if !ok {
77 panic("no SELF_IP")
78 }
gio5155c1a2025-05-21 15:36:21 +040079 id, ok := os.LookupEnv("SELF_ID")
80 if !ok {
81 panic("no SELF_ID")
82 }
giofc441e32024-11-11 16:26:14 +040083 var signer ssh.Signer
gioa421b062025-04-21 09:45:04 +040084 // TODO(gio): revisit this logic
85 if *sshKey != "" && !(strings.HasPrefix(*repoAddr, "http://") || strings.HasPrefix(*repoAddr, "https://")) {
giofc441e32024-11-11 16:26:14 +040086 key, err := os.ReadFile(*sshKey)
87 if err != nil {
88 panic(err)
89 }
90 signer, err = ssh.ParsePrivateKey(key)
91 if err != nil {
92 panic(err)
93 }
gio0eaf2712024-04-14 13:08:46 +040094 }
gio5155c1a2025-05-21 15:36:21 +040095 if err := os.Mkdir(*appDir, os.ModePerm); err != nil {
gio0eaf2712024-04-14 13:08:46 +040096 panic(err)
97 }
98 r, err := os.Open(*runCfg)
99 if err != nil {
100 panic(err)
101 }
102 defer r.Close()
103 var cmds []Command
104 if err := json.NewDecoder(r).Decode(&cmds); err != nil {
105 panic(err)
106 }
gio5155c1a2025-05-21 15:36:21 +0400107 s := NewServer(*port, *appId, *service, id, *repoAddr, *branch, *rootDir, signer, *appDir, cmds, self, *managerAddr)
gio5e4d1a72024-10-09 15:25:29 +0400108 if err := s.Start(); err != nil {
109 log.Fatal(err)
110 }
gio0eaf2712024-04-14 13:08:46 +0400111}