| gio | 0eaf271 | 2024-04-14 13:08:46 +0400 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "fmt" |
| gio | 183e834 | 2024-08-20 06:01:24 +0400 | [diff] [blame] | 7 | "io" |
| gio | 0eaf271 | 2024-04-14 13:08:46 +0400 | [diff] [blame] | 8 | "net/http" |
| 9 | "os" |
| 10 | "os/exec" |
| gio | fc441e3 | 2024-11-11 16:26:14 +0400 | [diff] [blame] | 11 | "path/filepath" |
| gio | bcd25e9 | 2025-05-03 19:14:10 +0400 | [diff] [blame] | 12 | "strings" |
| gio | 0eaf271 | 2024-04-14 13:08:46 +0400 | [diff] [blame] | 13 | "sync" |
| gio | ff0ee0f | 2024-10-15 23:11:54 +0400 | [diff] [blame] | 14 | "syscall" |
| gio | 0eaf271 | 2024-04-14 13:08:46 +0400 | [diff] [blame] | 15 | "time" |
| 16 | |
| 17 | "golang.org/x/crypto/ssh" |
| 18 | ) |
| 19 | |
| gio | 5155c1a | 2025-05-21 15:36:21 +0400 | [diff] [blame] | 20 | type CommandState string |
| 21 | |
| 22 | type CommandStatus struct { |
| 23 | Command string `json:"command"` |
| 24 | State CommandState `json:"state"` |
| 25 | } |
| 26 | |
| 27 | type Status struct { |
| gio | 9635ccb | 2025-05-22 08:33:38 +0400 | [diff] [blame] | 28 | Commit *Commit `json:"commit"` |
| gio | 5155c1a | 2025-05-21 15:36:21 +0400 | [diff] [blame] | 29 | Commands []CommandStatus `json:"commands"` |
| 30 | } |
| 31 | |
| gio | 0eaf271 | 2024-04-14 13:08:46 +0400 | [diff] [blame] | 32 | type Server struct { |
| 33 | l sync.Locker |
| gio | e65d9a9 | 2025-06-19 09:02:32 +0400 | [diff] [blame] | 34 | agentMode bool |
| gio | 0eaf271 | 2024-04-14 13:08:46 +0400 | [diff] [blame] | 35 | port int |
| gio | 266c04f | 2024-07-03 14:18:45 +0400 | [diff] [blame] | 36 | appId string |
| gio | b87415c | 2025-05-08 22:32:11 +0400 | [diff] [blame] | 37 | service string |
| gio | 5155c1a | 2025-05-21 15:36:21 +0400 | [diff] [blame] | 38 | id string |
| gio | 0eaf271 | 2024-04-14 13:08:46 +0400 | [diff] [blame] | 39 | ready bool |
| 40 | cmd *exec.Cmd |
| 41 | repoAddr string |
| gio | 2b1157a | 2024-10-24 08:45:07 +0400 | [diff] [blame] | 42 | branch string |
| gio | fc441e3 | 2024-11-11 16:26:14 +0400 | [diff] [blame] | 43 | rootDir string |
| gio | 0eaf271 | 2024-04-14 13:08:46 +0400 | [diff] [blame] | 44 | signer ssh.Signer |
| 45 | appDir string |
| 46 | runCommands []Command |
| 47 | self string |
| gio | a60f0de | 2024-07-08 10:49:48 +0400 | [diff] [blame] | 48 | managerAddr string |
| gio | 183e834 | 2024-08-20 06:01:24 +0400 | [diff] [blame] | 49 | logs *Log |
| gio | 45c3182 | 2024-10-24 10:58:02 +0400 | [diff] [blame] | 50 | currDir string |
| gio | 5155c1a | 2025-05-21 15:36:21 +0400 | [diff] [blame] | 51 | status *Status |
| gio | 0eaf271 | 2024-04-14 13:08:46 +0400 | [diff] [blame] | 52 | } |
| 53 | |
| gio | e65d9a9 | 2025-06-19 09:02:32 +0400 | [diff] [blame] | 54 | func NewServer(agentMode bool, port int, appId, service, id, repoAddr, branch, rootDir string, signer ssh.Signer, appDir string, runCommands []Command, self string, manager string) *Server { |
| gio | 0eaf271 | 2024-04-14 13:08:46 +0400 | [diff] [blame] | 55 | return &Server{ |
| 56 | l: &sync.Mutex{}, |
| gio | e65d9a9 | 2025-06-19 09:02:32 +0400 | [diff] [blame] | 57 | agentMode: agentMode, |
| gio | 0eaf271 | 2024-04-14 13:08:46 +0400 | [diff] [blame] | 58 | port: port, |
| 59 | ready: false, |
| gio | 266c04f | 2024-07-03 14:18:45 +0400 | [diff] [blame] | 60 | appId: appId, |
| gio | b87415c | 2025-05-08 22:32:11 +0400 | [diff] [blame] | 61 | service: service, |
| gio | 5155c1a | 2025-05-21 15:36:21 +0400 | [diff] [blame] | 62 | id: id, |
| gio | 0eaf271 | 2024-04-14 13:08:46 +0400 | [diff] [blame] | 63 | repoAddr: repoAddr, |
| gio | 2b1157a | 2024-10-24 08:45:07 +0400 | [diff] [blame] | 64 | branch: branch, |
| gio | fc441e3 | 2024-11-11 16:26:14 +0400 | [diff] [blame] | 65 | rootDir: rootDir, |
| gio | 0eaf271 | 2024-04-14 13:08:46 +0400 | [diff] [blame] | 66 | signer: signer, |
| 67 | appDir: appDir, |
| 68 | runCommands: runCommands, |
| 69 | self: self, |
| gio | a60f0de | 2024-07-08 10:49:48 +0400 | [diff] [blame] | 70 | managerAddr: manager, |
| gio | 183e834 | 2024-08-20 06:01:24 +0400 | [diff] [blame] | 71 | logs: &Log{}, |
| gio | 45c3182 | 2024-10-24 10:58:02 +0400 | [diff] [blame] | 72 | currDir: "", |
| gio | 5155c1a | 2025-05-21 15:36:21 +0400 | [diff] [blame] | 73 | status: nil, |
| gio | 0eaf271 | 2024-04-14 13:08:46 +0400 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
| 77 | func (s *Server) Start() error { |
| 78 | http.HandleFunc("/update", s.handleUpdate) |
| 79 | http.HandleFunc("/ready", s.handleReady) |
| gio | 183e834 | 2024-08-20 06:01:24 +0400 | [diff] [blame] | 80 | http.HandleFunc("/logs", s.handleLogs) |
| gio | ec744fa | 2025-05-20 12:47:03 +0400 | [diff] [blame] | 81 | if s.managerAddr != "" && s.appId != "" { |
| 82 | go s.pingManager() |
| 83 | } |
| gio | 0eaf271 | 2024-04-14 13:08:46 +0400 | [diff] [blame] | 84 | if err := s.run(); err != nil { |
| 85 | return err |
| 86 | } |
| gio | 0eaf271 | 2024-04-14 13:08:46 +0400 | [diff] [blame] | 87 | return http.ListenAndServe(fmt.Sprintf(":%d", s.port), nil) |
| 88 | } |
| 89 | |
| gio | 183e834 | 2024-08-20 06:01:24 +0400 | [diff] [blame] | 90 | func (s *Server) handleLogs(w http.ResponseWriter, r *http.Request) { |
| 91 | fmt.Fprint(w, s.logs.Contents()) |
| 92 | } |
| 93 | |
| gio | 0eaf271 | 2024-04-14 13:08:46 +0400 | [diff] [blame] | 94 | func (s *Server) handleReady(w http.ResponseWriter, r *http.Request) { |
| 95 | s.l.Lock() |
| 96 | defer s.l.Unlock() |
| 97 | if s.ready { |
| 98 | fmt.Fprintln(w, "ok") |
| 99 | } else { |
| 100 | http.Error(w, "not ready", http.StatusInternalServerError) |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | func (s *Server) handleUpdate(w http.ResponseWriter, r *http.Request) { |
| 105 | fmt.Println("update") |
| 106 | s.l.Lock() |
| 107 | s.ready = false |
| 108 | s.l.Unlock() |
| gio | 4bfed00 | 2025-05-22 13:23:22 +0400 | [diff] [blame] | 109 | fmt.Fprintf(s.logs, "!!! dodo: Reloading service\n") |
| gio | 0eaf271 | 2024-04-14 13:08:46 +0400 | [diff] [blame] | 110 | if err := s.run(); err != nil { |
| 111 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 112 | return |
| 113 | } |
| 114 | s.l.Lock() |
| 115 | s.ready = true |
| 116 | s.l.Unlock() |
| 117 | } |
| 118 | |
| gio | e65d9a9 | 2025-06-19 09:02:32 +0400 | [diff] [blame] | 119 | type command struct { |
| 120 | cmd string |
| 121 | env []string |
| 122 | } |
| 123 | |
| gio | 0eaf271 | 2024-04-14 13:08:46 +0400 | [diff] [blame] | 124 | func (s *Server) run() error { |
| gio | e65d9a9 | 2025-06-19 09:02:32 +0400 | [diff] [blame] | 125 | newDir := s.appDir |
| 126 | commands := []command{} |
| 127 | if !s.agentMode { |
| 128 | var err error |
| 129 | newDir, err = os.MkdirTemp(s.appDir, "code-*") |
| 130 | if err != nil { |
| 131 | return err |
| gio | 5155c1a | 2025-05-21 15:36:21 +0400 | [diff] [blame] | 132 | } |
| gio | 0eaf271 | 2024-04-14 13:08:46 +0400 | [diff] [blame] | 133 | } |
| gio | e65d9a9 | 2025-06-19 09:02:32 +0400 | [diff] [blame] | 134 | if s.repoAddr != "" { |
| 135 | commit, err := CloneRepositoryBranch(s.repoAddr, s.branch, s.rootDir, s.signer, newDir) |
| 136 | if err != nil { |
| 137 | fmt.Fprintf(s.logs, "!!! dodo: Failed to clone repository\n") |
| 138 | s.status = &Status{ |
| 139 | Commit: nil, |
| 140 | } |
| 141 | return err |
| 142 | } |
| 143 | fmt.Fprintf(s.logs, "!!! dodo: Successfully cloned repository %s\n", commit.Hash) |
| 144 | s.status = &Status{ |
| 145 | Commit: commit, |
| 146 | Commands: []CommandStatus{}, |
| 147 | } |
| 148 | } else { |
| 149 | s.status = &Status{ |
| 150 | Commit: nil, |
| 151 | Commands: []CommandStatus{}, |
| 152 | } |
| gio | 5155c1a | 2025-05-21 15:36:21 +0400 | [diff] [blame] | 153 | } |
| gio | e65d9a9 | 2025-06-19 09:02:32 +0400 | [diff] [blame] | 154 | if s.agentMode { |
| 155 | if _, err := os.Stat(filepath.Join(newDir, ".git")); err != nil && os.IsNotExist(err) { |
| 156 | commands = append(commands, command{cmd: "git config --global user.name dodo"}) |
| 157 | s.status.Commands = append(s.status.Commands, CommandStatus{ |
| 158 | Command: commands[len(commands)-1].cmd, |
| 159 | State: "waiting", |
| 160 | }) |
| 161 | commands = append(commands, command{cmd: "git config --global user.email dodo@dodo.cloud"}) |
| 162 | s.status.Commands = append(s.status.Commands, CommandStatus{ |
| 163 | Command: commands[len(commands)-1].cmd, |
| 164 | State: "waiting", |
| 165 | }) |
| 166 | commands = append(commands, command{cmd: "git init ."}) |
| 167 | s.status.Commands = append(s.status.Commands, CommandStatus{ |
| 168 | Command: commands[len(commands)-1].cmd, |
| 169 | State: "waiting", |
| 170 | }) |
| 171 | commands = append(commands, command{cmd: "echo \"TODO: Describe project\" > README.md"}) |
| 172 | s.status.Commands = append(s.status.Commands, CommandStatus{ |
| 173 | Command: commands[len(commands)-1].cmd, |
| 174 | State: "waiting", |
| 175 | }) |
| 176 | commands = append(commands, command{cmd: "git add README.md"}) |
| 177 | s.status.Commands = append(s.status.Commands, CommandStatus{ |
| 178 | Command: commands[len(commands)-1].cmd, |
| 179 | State: "waiting", |
| 180 | }) |
| 181 | commands = append(commands, command{cmd: "git commit -m \"init\""}) |
| 182 | s.status.Commands = append(s.status.Commands, CommandStatus{ |
| 183 | Command: commands[len(commands)-1].cmd, |
| 184 | State: "waiting", |
| 185 | }) |
| 186 | } |
| 187 | } |
| gio | 5155c1a | 2025-05-21 15:36:21 +0400 | [diff] [blame] | 188 | for _, c := range s.runCommands { |
| gio | 0eaf271 | 2024-04-14 13:08:46 +0400 | [diff] [blame] | 189 | args := []string{c.Bin} |
| 190 | args = append(args, c.Args...) |
| gio | 5155c1a | 2025-05-21 15:36:21 +0400 | [diff] [blame] | 191 | cmd := strings.Join(args, " ") |
| gio | e65d9a9 | 2025-06-19 09:02:32 +0400 | [diff] [blame] | 192 | commands = append(commands, command{cmd, c.Env}) |
| gio | 5155c1a | 2025-05-21 15:36:21 +0400 | [diff] [blame] | 193 | s.status.Commands = append(s.status.Commands, CommandStatus{ |
| 194 | Command: cmd, |
| 195 | State: "waiting", |
| 196 | }) |
| 197 | } |
| 198 | logM := io.MultiWriter(os.Stdout, s.logs) |
| 199 | for i, c := range commands { |
| 200 | if i > 0 { |
| 201 | s.status.Commands[i-1].State = "success" |
| 202 | } |
| gio | 0eaf271 | 2024-04-14 13:08:46 +0400 | [diff] [blame] | 203 | cmd := &exec.Cmd{ |
| gio | fc441e3 | 2024-11-11 16:26:14 +0400 | [diff] [blame] | 204 | Dir: filepath.Join(newDir, s.rootDir), |
| gio | bcd25e9 | 2025-05-03 19:14:10 +0400 | [diff] [blame] | 205 | Path: "/bin/sh", |
| gio | e65d9a9 | 2025-06-19 09:02:32 +0400 | [diff] [blame] | 206 | Args: []string{"/bin/sh", "-c", c.cmd}, |
| 207 | Env: append(os.Environ(), c.env...), |
| gio | 183e834 | 2024-08-20 06:01:24 +0400 | [diff] [blame] | 208 | Stdout: logM, |
| 209 | Stderr: logM, |
| gio | 0eaf271 | 2024-04-14 13:08:46 +0400 | [diff] [blame] | 210 | } |
| gio | ff0ee0f | 2024-10-15 23:11:54 +0400 | [diff] [blame] | 211 | cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} |
| gio | 5155c1a | 2025-05-21 15:36:21 +0400 | [diff] [blame] | 212 | fmt.Printf("Running: %s\n", c) |
| gio | 4bfed00 | 2025-05-22 13:23:22 +0400 | [diff] [blame] | 213 | fmt.Fprintf(s.logs, "!!! dodo: Running: %s\n", c) |
| gio | 5155c1a | 2025-05-21 15:36:21 +0400 | [diff] [blame] | 214 | s.status.Commands[i].State = "running" |
| gio | e65d9a9 | 2025-06-19 09:02:32 +0400 | [diff] [blame] | 215 | if i < len(commands)-1 { |
| gio | 0eaf271 | 2024-04-14 13:08:46 +0400 | [diff] [blame] | 216 | if err := cmd.Run(); err != nil { |
| 217 | return err |
| 218 | } |
| 219 | } else { |
| gio | 45c3182 | 2024-10-24 10:58:02 +0400 | [diff] [blame] | 220 | if s.cmd != nil { |
| 221 | // TODO(gio): make this point configurable |
| 222 | if err := s.kill(); err != nil { |
| 223 | return err |
| 224 | } |
| gio | e65d9a9 | 2025-06-19 09:02:32 +0400 | [diff] [blame] | 225 | if s.currDir != "" && !s.agentMode { |
| gio | 5155c1a | 2025-05-21 15:36:21 +0400 | [diff] [blame] | 226 | if err := os.RemoveAll(s.currDir); err != nil { |
| 227 | return err |
| 228 | } |
| gio | 45c3182 | 2024-10-24 10:58:02 +0400 | [diff] [blame] | 229 | } |
| 230 | } |
| gio | 0eaf271 | 2024-04-14 13:08:46 +0400 | [diff] [blame] | 231 | if err := cmd.Start(); err != nil { |
| 232 | return err |
| 233 | } |
| 234 | s.cmd = cmd |
| 235 | } |
| 236 | } |
| gio | 45c3182 | 2024-10-24 10:58:02 +0400 | [diff] [blame] | 237 | s.currDir = newDir |
| gio | 0eaf271 | 2024-04-14 13:08:46 +0400 | [diff] [blame] | 238 | return nil |
| 239 | } |
| 240 | |
| 241 | type pingReq struct { |
| gio | 5155c1a | 2025-05-21 15:36:21 +0400 | [diff] [blame] | 242 | Id string `json:"id"` |
| 243 | Service string `json:"service"` |
| 244 | Address string `json:"address"` |
| 245 | Status *Status `json:"status,omitempty"` |
| 246 | Logs string `json:"logs"` |
| gio | 0eaf271 | 2024-04-14 13:08:46 +0400 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | func (s *Server) pingManager() { |
| 250 | defer func() { |
| 251 | go func() { |
| 252 | time.Sleep(5 * time.Second) |
| 253 | s.pingManager() |
| 254 | }() |
| 255 | }() |
| gio | a60f0de | 2024-07-08 10:49:48 +0400 | [diff] [blame] | 256 | buf, err := json.Marshal(pingReq{ |
| gio | 5155c1a | 2025-05-21 15:36:21 +0400 | [diff] [blame] | 257 | Id: s.id, |
| gio | b87415c | 2025-05-08 22:32:11 +0400 | [diff] [blame] | 258 | Service: s.service, |
| 259 | Address: fmt.Sprintf("http://%s:%d", s.self, s.port), |
| gio | 5155c1a | 2025-05-21 15:36:21 +0400 | [diff] [blame] | 260 | Status: s.status, |
| gio | 183e834 | 2024-08-20 06:01:24 +0400 | [diff] [blame] | 261 | Logs: s.logs.Contents(), |
| gio | a60f0de | 2024-07-08 10:49:48 +0400 | [diff] [blame] | 262 | }) |
| gio | 0eaf271 | 2024-04-14 13:08:46 +0400 | [diff] [blame] | 263 | if err != nil { |
| 264 | return |
| 265 | } |
| gio | b87415c | 2025-05-08 22:32:11 +0400 | [diff] [blame] | 266 | registerWorkerAddr := fmt.Sprintf("%s/api/project/%s/workers", s.managerAddr, s.appId) |
| 267 | resp, err := http.Post(registerWorkerAddr, "application/json", bytes.NewReader(buf)) |
| 268 | if err != nil { |
| 269 | fmt.Println(err) |
| 270 | } else { |
| 271 | // check resp code |
| 272 | io.Copy(os.Stdout, resp.Body) |
| 273 | } |
| gio | 0eaf271 | 2024-04-14 13:08:46 +0400 | [diff] [blame] | 274 | } |
| gio | 45c3182 | 2024-10-24 10:58:02 +0400 | [diff] [blame] | 275 | |
| 276 | func (s *Server) kill() error { |
| 277 | if s.cmd == nil { |
| 278 | return nil |
| 279 | } |
| 280 | |
| 281 | err := syscall.Kill(-s.cmd.Process.Pid, syscall.SIGKILL) |
| 282 | if err != nil { |
| 283 | return err |
| 284 | } |
| 285 | // NOTE(gio): No need to check err as we just killed the process |
| 286 | s.cmd.Wait() |
| 287 | s.cmd = nil |
| 288 | return nil |
| 289 | } |