blob: ebd0ef8727fd276455d3b5983a96153191854ae1 [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")
gioe65d9a92025-06-19 09:02:32 +040024var agentMode = flag.Bool("agent-mode", false, "Sketch agent mode")
gio0eaf2712024-04-14 13:08:46 +040025var repoAddr = flag.String("repo-addr", "", "Git repository address")
gio2b1157a2024-10-24 08:45:07 +040026var branch = flag.String("branch", "", "Name of the branch to process")
giofc441e32024-11-11 16:26:14 +040027var rootDir = flag.String("root-dir", "/", "Path to the app code")
gio0eaf2712024-04-14 13:08:46 +040028var sshKey = flag.String("ssh-key", "", "Private SSH key to access Git repository")
29var appDir = flag.String("app-dir", "", "Path to store application repository locally")
30var runCfg = flag.String("run-cfg", "", "Run configuration")
gioa60f0de2024-07-08 10:49:48 +040031var managerAddr = flag.String("manager-addr", "", "Address of the manager")
gio0eaf2712024-04-14 13:08:46 +040032
33type Command struct {
34 Bin string `json:"bin"`
35 Args []string `json:"args"`
gio1364e432024-06-29 11:39:18 +040036 Env []string `json:"env"`
gio0eaf2712024-04-14 13:08:46 +040037}
38
gio9635ccb2025-05-22 08:33:38 +040039type Commit struct {
40 Hash string `json:"hash"`
41 Message string `json:"message"`
42}
43
44func CloneRepositoryBranch(addr, branch, rootDir string, signer ssh.Signer, path string) (*Commit, error) {
gio2b1157a2024-10-24 08:45:07 +040045 ref := fmt.Sprintf("refs/heads/%s", branch)
giofc441e32024-11-11 16:26:14 +040046 opts := &git.CloneOptions{
47 URL: addr,
48 RemoteName: "origin",
49 ReferenceName: plumbing.ReferenceName(ref),
50 SingleBranch: true,
51 Depth: 1,
52 InsecureSkipTLS: true,
53 Progress: os.Stdout,
54 }
55 if signer != nil {
56 opts.Auth = &gitssh.PublicKeys{
gio0eaf2712024-04-14 13:08:46 +040057 User: "git",
58 Signer: signer,
59 HostKeyCallbackHelper: gitssh.HostKeyCallbackHelper{
60 HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
61 // TODO(giolekva): verify server public key
62 fmt.Printf("--- %+v\n", ssh.MarshalAuthorizedKey(key))
63 return nil
64 },
65 },
giofc441e32024-11-11 16:26:14 +040066 }
67 }
gio9635ccb2025-05-22 08:33:38 +040068 repo, err := git.Clone(memory.NewStorage(), osfs.New(path, osfs.WithBoundOS()), opts)
giof5ffedb2024-06-19 14:14:43 +040069 if err != nil {
gio9635ccb2025-05-22 08:33:38 +040070 return nil, err
giof5ffedb2024-06-19 14:14:43 +040071 }
gio9635ccb2025-05-22 08:33:38 +040072 head, err := repo.Head()
giof5ffedb2024-06-19 14:14:43 +040073 if err != nil {
gio9635ccb2025-05-22 08:33:38 +040074 return nil, err
giof5ffedb2024-06-19 14:14:43 +040075 }
gio9635ccb2025-05-22 08:33:38 +040076 commit, err := repo.CommitObject(head.Hash())
77 if err != nil {
78 return nil, err
79 }
80 return &Commit{
81 Hash: head.Hash().String(),
82 Message: commit.Message,
83 }, nil
gio0eaf2712024-04-14 13:08:46 +040084}
85
86func main() {
87 flag.Parse()
88 self, ok := os.LookupEnv("SELF_IP")
89 if !ok {
90 panic("no SELF_IP")
91 }
gio5155c1a2025-05-21 15:36:21 +040092 id, ok := os.LookupEnv("SELF_ID")
93 if !ok {
94 panic("no SELF_ID")
95 }
giofc441e32024-11-11 16:26:14 +040096 var signer ssh.Signer
gioa421b062025-04-21 09:45:04 +040097 // TODO(gio): revisit this logic
98 if *sshKey != "" && !(strings.HasPrefix(*repoAddr, "http://") || strings.HasPrefix(*repoAddr, "https://")) {
giofc441e32024-11-11 16:26:14 +040099 key, err := os.ReadFile(*sshKey)
100 if err != nil {
101 panic(err)
102 }
103 signer, err = ssh.ParsePrivateKey(key)
104 if err != nil {
105 panic(err)
106 }
gio0eaf2712024-04-14 13:08:46 +0400107 }
gioe65d9a92025-06-19 09:02:32 +0400108 if !*agentMode {
109 if err := os.Mkdir(*appDir, os.ModePerm); err != nil {
110 panic(err)
111 }
gio0eaf2712024-04-14 13:08:46 +0400112 }
113 r, err := os.Open(*runCfg)
114 if err != nil {
115 panic(err)
116 }
117 defer r.Close()
118 var cmds []Command
119 if err := json.NewDecoder(r).Decode(&cmds); err != nil {
120 panic(err)
121 }
gioe65d9a92025-06-19 09:02:32 +0400122 s := NewServer(*agentMode, *port, *appId, *service, id, *repoAddr, *branch, *rootDir, signer, *appDir, cmds, self, *managerAddr)
gio5e4d1a72024-10-09 15:25:29 +0400123 if err := s.Start(); err != nil {
124 log.Fatal(err)
125 }
gio0eaf2712024-04-14 13:08:46 +0400126}