| 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 != "" { |
| gio | e840235 | 2025-06-24 21:28:25 +0400 | [diff] [blame] | 135 | if _, err := os.Stat(filepath.Join(newDir, ".git")); err != nil && os.IsNotExist(err) { |
| 136 | commit, err := CloneRepositoryBranch(s.repoAddr, s.branch, s.rootDir, s.signer, newDir) |
| 137 | if err != nil { |
| 138 | fmt.Fprintf(s.logs, "!!! dodo: Failed to clone repository\n") |
| 139 | s.status = &Status{ |
| 140 | Commit: nil, |
| 141 | } |
| 142 | return err |
| gio | e65d9a9 | 2025-06-19 09:02:32 +0400 | [diff] [blame] | 143 | } |
| gio | e840235 | 2025-06-24 21:28:25 +0400 | [diff] [blame] | 144 | fmt.Fprintf(s.logs, "!!! dodo: Successfully cloned repository %s\n", commit.Hash) |
| 145 | s.status = &Status{ |
| 146 | Commit: commit, |
| 147 | Commands: []CommandStatus{}, |
| 148 | } |
| 149 | } else { |
| 150 | s.status = &Status{ |
| 151 | Commands: []CommandStatus{}, |
| 152 | } |
| gio | e65d9a9 | 2025-06-19 09:02:32 +0400 | [diff] [blame] | 153 | } |
| 154 | } else { |
| 155 | s.status = &Status{ |
| 156 | Commit: nil, |
| 157 | Commands: []CommandStatus{}, |
| 158 | } |
| gio | 5155c1a | 2025-05-21 15:36:21 +0400 | [diff] [blame] | 159 | } |
| gio | 24d6e9a | 2025-06-24 15:02:29 +0400 | [diff] [blame] | 160 | if s.agentMode && s.repoAddr == "" { |
| gio | e65d9a9 | 2025-06-19 09:02:32 +0400 | [diff] [blame] | 161 | if _, err := os.Stat(filepath.Join(newDir, ".git")); err != nil && os.IsNotExist(err) { |
| 162 | commands = append(commands, command{cmd: "git config --global user.name dodo"}) |
| 163 | s.status.Commands = append(s.status.Commands, CommandStatus{ |
| 164 | Command: commands[len(commands)-1].cmd, |
| 165 | State: "waiting", |
| 166 | }) |
| 167 | commands = append(commands, command{cmd: "git config --global user.email dodo@dodo.cloud"}) |
| 168 | s.status.Commands = append(s.status.Commands, CommandStatus{ |
| 169 | Command: commands[len(commands)-1].cmd, |
| 170 | State: "waiting", |
| 171 | }) |
| 172 | commands = append(commands, command{cmd: "git init ."}) |
| 173 | s.status.Commands = append(s.status.Commands, CommandStatus{ |
| 174 | Command: commands[len(commands)-1].cmd, |
| 175 | State: "waiting", |
| 176 | }) |
| 177 | commands = append(commands, command{cmd: "echo \"TODO: Describe project\" > README.md"}) |
| 178 | s.status.Commands = append(s.status.Commands, CommandStatus{ |
| 179 | Command: commands[len(commands)-1].cmd, |
| 180 | State: "waiting", |
| 181 | }) |
| 182 | commands = append(commands, command{cmd: "git add README.md"}) |
| 183 | s.status.Commands = append(s.status.Commands, CommandStatus{ |
| 184 | Command: commands[len(commands)-1].cmd, |
| 185 | State: "waiting", |
| 186 | }) |
| 187 | commands = append(commands, command{cmd: "git commit -m \"init\""}) |
| 188 | s.status.Commands = append(s.status.Commands, CommandStatus{ |
| 189 | Command: commands[len(commands)-1].cmd, |
| 190 | State: "waiting", |
| 191 | }) |
| 192 | } |
| 193 | } |
| gio | 5155c1a | 2025-05-21 15:36:21 +0400 | [diff] [blame] | 194 | for _, c := range s.runCommands { |
| gio | 0eaf271 | 2024-04-14 13:08:46 +0400 | [diff] [blame] | 195 | args := []string{c.Bin} |
| 196 | args = append(args, c.Args...) |
| gio | 5155c1a | 2025-05-21 15:36:21 +0400 | [diff] [blame] | 197 | cmd := strings.Join(args, " ") |
| gio | e65d9a9 | 2025-06-19 09:02:32 +0400 | [diff] [blame] | 198 | commands = append(commands, command{cmd, c.Env}) |
| gio | 5155c1a | 2025-05-21 15:36:21 +0400 | [diff] [blame] | 199 | s.status.Commands = append(s.status.Commands, CommandStatus{ |
| 200 | Command: cmd, |
| 201 | State: "waiting", |
| 202 | }) |
| 203 | } |
| 204 | logM := io.MultiWriter(os.Stdout, s.logs) |
| 205 | for i, c := range commands { |
| 206 | if i > 0 { |
| 207 | s.status.Commands[i-1].State = "success" |
| 208 | } |
| gio | 0eaf271 | 2024-04-14 13:08:46 +0400 | [diff] [blame] | 209 | cmd := &exec.Cmd{ |
| gio | fc441e3 | 2024-11-11 16:26:14 +0400 | [diff] [blame] | 210 | Dir: filepath.Join(newDir, s.rootDir), |
| gio | bcd25e9 | 2025-05-03 19:14:10 +0400 | [diff] [blame] | 211 | Path: "/bin/sh", |
| gio | e65d9a9 | 2025-06-19 09:02:32 +0400 | [diff] [blame] | 212 | Args: []string{"/bin/sh", "-c", c.cmd}, |
| 213 | Env: append(os.Environ(), c.env...), |
| gio | 183e834 | 2024-08-20 06:01:24 +0400 | [diff] [blame] | 214 | Stdout: logM, |
| 215 | Stderr: logM, |
| gio | 0eaf271 | 2024-04-14 13:08:46 +0400 | [diff] [blame] | 216 | } |
| gio | ff0ee0f | 2024-10-15 23:11:54 +0400 | [diff] [blame] | 217 | cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} |
| gio | 5155c1a | 2025-05-21 15:36:21 +0400 | [diff] [blame] | 218 | fmt.Printf("Running: %s\n", c) |
| gio | 4bfed00 | 2025-05-22 13:23:22 +0400 | [diff] [blame] | 219 | fmt.Fprintf(s.logs, "!!! dodo: Running: %s\n", c) |
| gio | 5155c1a | 2025-05-21 15:36:21 +0400 | [diff] [blame] | 220 | s.status.Commands[i].State = "running" |
| gio | e65d9a9 | 2025-06-19 09:02:32 +0400 | [diff] [blame] | 221 | if i < len(commands)-1 { |
| gio | 0eaf271 | 2024-04-14 13:08:46 +0400 | [diff] [blame] | 222 | if err := cmd.Run(); err != nil { |
| 223 | return err |
| 224 | } |
| 225 | } else { |
| gio | 45c3182 | 2024-10-24 10:58:02 +0400 | [diff] [blame] | 226 | if s.cmd != nil { |
| 227 | // TODO(gio): make this point configurable |
| 228 | if err := s.kill(); err != nil { |
| 229 | return err |
| 230 | } |
| gio | e65d9a9 | 2025-06-19 09:02:32 +0400 | [diff] [blame] | 231 | if s.currDir != "" && !s.agentMode { |
| gio | 5155c1a | 2025-05-21 15:36:21 +0400 | [diff] [blame] | 232 | if err := os.RemoveAll(s.currDir); err != nil { |
| 233 | return err |
| 234 | } |
| gio | 45c3182 | 2024-10-24 10:58:02 +0400 | [diff] [blame] | 235 | } |
| 236 | } |
| gio | 0eaf271 | 2024-04-14 13:08:46 +0400 | [diff] [blame] | 237 | if err := cmd.Start(); err != nil { |
| 238 | return err |
| 239 | } |
| 240 | s.cmd = cmd |
| 241 | } |
| 242 | } |
| gio | 45c3182 | 2024-10-24 10:58:02 +0400 | [diff] [blame] | 243 | s.currDir = newDir |
| gio | 0eaf271 | 2024-04-14 13:08:46 +0400 | [diff] [blame] | 244 | return nil |
| 245 | } |
| 246 | |
| 247 | type pingReq struct { |
| gio | 5155c1a | 2025-05-21 15:36:21 +0400 | [diff] [blame] | 248 | Id string `json:"id"` |
| 249 | Service string `json:"service"` |
| 250 | Address string `json:"address"` |
| 251 | Status *Status `json:"status,omitempty"` |
| 252 | Logs string `json:"logs"` |
| gio | 0eaf271 | 2024-04-14 13:08:46 +0400 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | func (s *Server) pingManager() { |
| 256 | defer func() { |
| 257 | go func() { |
| 258 | time.Sleep(5 * time.Second) |
| 259 | s.pingManager() |
| 260 | }() |
| 261 | }() |
| gio | a60f0de | 2024-07-08 10:49:48 +0400 | [diff] [blame] | 262 | buf, err := json.Marshal(pingReq{ |
| gio | 5155c1a | 2025-05-21 15:36:21 +0400 | [diff] [blame] | 263 | Id: s.id, |
| gio | b87415c | 2025-05-08 22:32:11 +0400 | [diff] [blame] | 264 | Service: s.service, |
| 265 | Address: fmt.Sprintf("http://%s:%d", s.self, s.port), |
| gio | 5155c1a | 2025-05-21 15:36:21 +0400 | [diff] [blame] | 266 | Status: s.status, |
| gio | 183e834 | 2024-08-20 06:01:24 +0400 | [diff] [blame] | 267 | Logs: s.logs.Contents(), |
| gio | a60f0de | 2024-07-08 10:49:48 +0400 | [diff] [blame] | 268 | }) |
| gio | 0eaf271 | 2024-04-14 13:08:46 +0400 | [diff] [blame] | 269 | if err != nil { |
| 270 | return |
| 271 | } |
| gio | b87415c | 2025-05-08 22:32:11 +0400 | [diff] [blame] | 272 | registerWorkerAddr := fmt.Sprintf("%s/api/project/%s/workers", s.managerAddr, s.appId) |
| 273 | resp, err := http.Post(registerWorkerAddr, "application/json", bytes.NewReader(buf)) |
| 274 | if err != nil { |
| 275 | fmt.Println(err) |
| 276 | } else { |
| 277 | // check resp code |
| 278 | io.Copy(os.Stdout, resp.Body) |
| 279 | } |
| gio | 0eaf271 | 2024-04-14 13:08:46 +0400 | [diff] [blame] | 280 | } |
| gio | 45c3182 | 2024-10-24 10:58:02 +0400 | [diff] [blame] | 281 | |
| 282 | func (s *Server) kill() error { |
| 283 | if s.cmd == nil { |
| 284 | return nil |
| 285 | } |
| 286 | |
| 287 | err := syscall.Kill(-s.cmd.Process.Pid, syscall.SIGKILL) |
| 288 | if err != nil { |
| 289 | return err |
| 290 | } |
| 291 | // NOTE(gio): No need to check err as we just killed the process |
| 292 | s.cmd.Wait() |
| 293 | s.cmd = nil |
| 294 | return nil |
| 295 | } |