blob: 96a647e73d213f1cb73bcbd5a3e4dcdf7ab15ab2 [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
gio9635ccb2025-05-22 08:33:38 +040038type Commit struct {
39 Hash string `json:"hash"`
40 Message string `json:"message"`
41}
42
43func CloneRepositoryBranch(addr, branch, rootDir string, signer ssh.Signer, path string) (*Commit, error) {
gio2b1157a2024-10-24 08:45:07 +040044 ref := fmt.Sprintf("refs/heads/%s", branch)
giofc441e32024-11-11 16:26:14 +040045 opts := &git.CloneOptions{
46 URL: addr,
47 RemoteName: "origin",
48 ReferenceName: plumbing.ReferenceName(ref),
49 SingleBranch: true,
50 Depth: 1,
51 InsecureSkipTLS: true,
52 Progress: os.Stdout,
53 }
54 if signer != nil {
55 opts.Auth = &gitssh.PublicKeys{
gio0eaf2712024-04-14 13:08:46 +040056 User: "git",
57 Signer: signer,
58 HostKeyCallbackHelper: gitssh.HostKeyCallbackHelper{
59 HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
60 // TODO(giolekva): verify server public key
61 fmt.Printf("--- %+v\n", ssh.MarshalAuthorizedKey(key))
62 return nil
63 },
64 },
giofc441e32024-11-11 16:26:14 +040065 }
66 }
gio9635ccb2025-05-22 08:33:38 +040067 repo, err := git.Clone(memory.NewStorage(), osfs.New(path, osfs.WithBoundOS()), opts)
giof5ffedb2024-06-19 14:14:43 +040068 if err != nil {
gio9635ccb2025-05-22 08:33:38 +040069 return nil, err
giof5ffedb2024-06-19 14:14:43 +040070 }
gio9635ccb2025-05-22 08:33:38 +040071 head, err := repo.Head()
giof5ffedb2024-06-19 14:14:43 +040072 if err != nil {
gio9635ccb2025-05-22 08:33:38 +040073 return nil, err
giof5ffedb2024-06-19 14:14:43 +040074 }
gio9635ccb2025-05-22 08:33:38 +040075 commit, err := repo.CommitObject(head.Hash())
76 if err != nil {
77 return nil, err
78 }
79 return &Commit{
80 Hash: head.Hash().String(),
81 Message: commit.Message,
82 }, nil
gio0eaf2712024-04-14 13:08:46 +040083}
84
85func main() {
86 flag.Parse()
87 self, ok := os.LookupEnv("SELF_IP")
88 if !ok {
89 panic("no SELF_IP")
90 }
gio5155c1a2025-05-21 15:36:21 +040091 id, ok := os.LookupEnv("SELF_ID")
92 if !ok {
93 panic("no SELF_ID")
94 }
giofc441e32024-11-11 16:26:14 +040095 var signer ssh.Signer
gioa421b062025-04-21 09:45:04 +040096 // TODO(gio): revisit this logic
97 if *sshKey != "" && !(strings.HasPrefix(*repoAddr, "http://") || strings.HasPrefix(*repoAddr, "https://")) {
giofc441e32024-11-11 16:26:14 +040098 key, err := os.ReadFile(*sshKey)
99 if err != nil {
100 panic(err)
101 }
102 signer, err = ssh.ParsePrivateKey(key)
103 if err != nil {
104 panic(err)
105 }
gio0eaf2712024-04-14 13:08:46 +0400106 }
gio5155c1a2025-05-21 15:36:21 +0400107 if err := os.Mkdir(*appDir, os.ModePerm); err != nil {
gio0eaf2712024-04-14 13:08:46 +0400108 panic(err)
109 }
110 r, err := os.Open(*runCfg)
111 if err != nil {
112 panic(err)
113 }
114 defer r.Close()
115 var cmds []Command
116 if err := json.NewDecoder(r).Decode(&cmds); err != nil {
117 panic(err)
118 }
gio5155c1a2025-05-21 15:36:21 +0400119 s := NewServer(*port, *appId, *service, id, *repoAddr, *branch, *rootDir, signer, *appDir, cmds, self, *managerAddr)
gio5e4d1a72024-10-09 15:25:29 +0400120 if err := s.Start(); err != nil {
121 log.Fatal(err)
122 }
gio0eaf2712024-04-14 13:08:46 +0400123}