blob: b11a977c967a44c31a234f34996f184043fbd80d [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"
gio37fba252025-07-03 14:02:04 +040010 "os/signal"
gio24d6e9a2025-06-24 15:02:29 +040011 "path/filepath"
gioa421b062025-04-21 09:45:04 +040012 "strings"
gio37fba252025-07-03 14:02:04 +040013 "syscall"
gio0eaf2712024-04-14 13:08:46 +040014
15 "golang.org/x/crypto/ssh"
16
17 "github.com/go-git/go-billy/v5/osfs"
18 "github.com/go-git/go-git/v5"
gio2b1157a2024-10-24 08:45:07 +040019 "github.com/go-git/go-git/v5/plumbing"
gio24d6e9a2025-06-24 15:02:29 +040020 "github.com/go-git/go-git/v5/plumbing/cache"
gio0eaf2712024-04-14 13:08:46 +040021 gitssh "github.com/go-git/go-git/v5/plumbing/transport/ssh"
gio24d6e9a2025-06-24 15:02:29 +040022 "github.com/go-git/go-git/v5/storage/filesystem"
gio0eaf2712024-04-14 13:08:46 +040023)
24
25var port = flag.Int("port", 3000, "Port to listen on")
gio266c04f2024-07-03 14:18:45 +040026var appId = flag.String("app-id", "", "Application ID")
giob87415c2025-05-08 22:32:11 +040027var service = flag.String("service", "", "Service name")
gioe65d9a92025-06-19 09:02:32 +040028var agentMode = flag.Bool("agent-mode", false, "Sketch agent mode")
gio0eaf2712024-04-14 13:08:46 +040029var repoAddr = flag.String("repo-addr", "", "Git repository address")
gio2b1157a2024-10-24 08:45:07 +040030var branch = flag.String("branch", "", "Name of the branch to process")
giofc441e32024-11-11 16:26:14 +040031var rootDir = flag.String("root-dir", "/", "Path to the app code")
gio0eaf2712024-04-14 13:08:46 +040032var sshKey = flag.String("ssh-key", "", "Private SSH key to access Git repository")
33var appDir = flag.String("app-dir", "", "Path to store application repository locally")
34var runCfg = flag.String("run-cfg", "", "Run configuration")
gioa60f0de2024-07-08 10:49:48 +040035var managerAddr = flag.String("manager-addr", "", "Address of the manager")
gio0eaf2712024-04-14 13:08:46 +040036
37type Command struct {
38 Bin string `json:"bin"`
39 Args []string `json:"args"`
gio1364e432024-06-29 11:39:18 +040040 Env []string `json:"env"`
gio0eaf2712024-04-14 13:08:46 +040041}
42
gio9635ccb2025-05-22 08:33:38 +040043type Commit struct {
44 Hash string `json:"hash"`
45 Message string `json:"message"`
46}
47
48func CloneRepositoryBranch(addr, branch, rootDir string, signer ssh.Signer, path string) (*Commit, error) {
gio2b1157a2024-10-24 08:45:07 +040049 ref := fmt.Sprintf("refs/heads/%s", branch)
giofc441e32024-11-11 16:26:14 +040050 opts := &git.CloneOptions{
51 URL: addr,
52 RemoteName: "origin",
53 ReferenceName: plumbing.ReferenceName(ref),
54 SingleBranch: true,
55 Depth: 1,
56 InsecureSkipTLS: true,
57 Progress: os.Stdout,
58 }
59 if signer != nil {
60 opts.Auth = &gitssh.PublicKeys{
gio0eaf2712024-04-14 13:08:46 +040061 User: "git",
62 Signer: signer,
63 HostKeyCallbackHelper: gitssh.HostKeyCallbackHelper{
64 HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
65 // TODO(giolekva): verify server public key
66 fmt.Printf("--- %+v\n", ssh.MarshalAuthorizedKey(key))
67 return nil
68 },
69 },
giofc441e32024-11-11 16:26:14 +040070 }
71 }
gio24d6e9a2025-06-24 15:02:29 +040072 repo, err := git.Clone(
73 filesystem.NewStorage(
74 osfs.New(filepath.Join(path, ".git"), osfs.WithBoundOS()),
75 cache.NewObjectLRUDefault(),
76 ),
77 osfs.New(path, osfs.WithBoundOS()),
78 opts,
79 )
giof5ffedb2024-06-19 14:14:43 +040080 if err != nil {
gio9635ccb2025-05-22 08:33:38 +040081 return nil, err
giof5ffedb2024-06-19 14:14:43 +040082 }
gio9635ccb2025-05-22 08:33:38 +040083 head, err := repo.Head()
giof5ffedb2024-06-19 14:14:43 +040084 if err != nil {
gio9635ccb2025-05-22 08:33:38 +040085 return nil, err
giof5ffedb2024-06-19 14:14:43 +040086 }
gio9635ccb2025-05-22 08:33:38 +040087 commit, err := repo.CommitObject(head.Hash())
88 if err != nil {
89 return nil, err
90 }
91 return &Commit{
92 Hash: head.Hash().String(),
93 Message: commit.Message,
94 }, nil
gio0eaf2712024-04-14 13:08:46 +040095}
96
97func main() {
98 flag.Parse()
99 self, ok := os.LookupEnv("SELF_IP")
100 if !ok {
101 panic("no SELF_IP")
102 }
gio5155c1a2025-05-21 15:36:21 +0400103 id, ok := os.LookupEnv("SELF_ID")
104 if !ok {
105 panic("no SELF_ID")
106 }
giofc441e32024-11-11 16:26:14 +0400107 var signer ssh.Signer
gioa421b062025-04-21 09:45:04 +0400108 // TODO(gio): revisit this logic
109 if *sshKey != "" && !(strings.HasPrefix(*repoAddr, "http://") || strings.HasPrefix(*repoAddr, "https://")) {
giofc441e32024-11-11 16:26:14 +0400110 key, err := os.ReadFile(*sshKey)
111 if err != nil {
112 panic(err)
113 }
114 signer, err = ssh.ParsePrivateKey(key)
115 if err != nil {
116 panic(err)
117 }
gio0eaf2712024-04-14 13:08:46 +0400118 }
gioe65d9a92025-06-19 09:02:32 +0400119 if !*agentMode {
120 if err := os.Mkdir(*appDir, os.ModePerm); err != nil {
121 panic(err)
122 }
gio0eaf2712024-04-14 13:08:46 +0400123 }
124 r, err := os.Open(*runCfg)
125 if err != nil {
126 panic(err)
127 }
128 defer r.Close()
129 var cmds []Command
130 if err := json.NewDecoder(r).Decode(&cmds); err != nil {
131 panic(err)
132 }
gioe65d9a92025-06-19 09:02:32 +0400133 s := NewServer(*agentMode, *port, *appId, *service, id, *repoAddr, *branch, *rootDir, signer, *appDir, cmds, self, *managerAddr)
gio37fba252025-07-03 14:02:04 +0400134 go func() {
135 if err := s.Start(); err != nil {
136 log.Fatal(err)
137 } else {
138 log.Println("Done")
139 os.Exit(0)
140 }
141 }()
142 sigChan := make(chan os.Signal, 1)
143 signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
144 <-sigChan
145 s.Stop()
gio0eaf2712024-04-14 13:08:46 +0400146}