diff options
Diffstat (limited to 'internal/api/scripts.go')
| -rw-r--r-- | internal/api/scripts.go | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/internal/api/scripts.go b/internal/api/scripts.go index 492570b..9afbb75 100644 --- a/internal/api/scripts.go +++ b/internal/api/scripts.go @@ -18,6 +18,13 @@ func (s *Server) startNextTaskScriptPath() string { return filepath.Join(s.workDir, "scripts", "start-next-task") } +func (s *Server) deployScriptPath() string { + if s.deployScript != "" { + return s.deployScript + } + return filepath.Join(s.workDir, "scripts", "deploy") +} + func (s *Server) handleStartNextTask(w http.ResponseWriter, r *http.Request) { ctx, cancel := context.WithTimeout(r.Context(), scriptTimeout) defer cancel() @@ -48,3 +55,34 @@ func (s *Server) handleStartNextTask(w http.ResponseWriter, r *http.Request) { "exit_code": exitCode, }) } + +func (s *Server) handleDeploy(w http.ResponseWriter, r *http.Request) { + ctx, cancel := context.WithTimeout(r.Context(), scriptTimeout) + defer cancel() + + scriptPath := s.deployScriptPath() + cmd := exec.CommandContext(ctx, scriptPath) + + var stdout, stderr bytes.Buffer + cmd.Stdout = &stdout + cmd.Stderr = &stderr + + err := cmd.Run() + exitCode := 0 + if err != nil { + if exitErr, ok := err.(*exec.ExitError); ok { + exitCode = exitErr.ExitCode() + } else { + s.logger.Error("deploy: script execution failed", "error", err, "path", scriptPath) + writeJSON(w, http.StatusInternalServerError, map[string]string{ + "error": "script execution failed: " + err.Error(), + }) + return + } + } + + writeJSON(w, http.StatusOK, map[string]interface{}{ + "output": stdout.String() + stderr.String(), + "exit_code": exitCode, + }) +} |
