process.go

90 lines
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
package internal

import (
	"fmt"
	"os/exec"
	"strings"
)

// Process management for binary services running inside code-server.
// Binary deploy builds a Go binary and runs it as a background process,
// which is faster than Docker for simple Go services during development.

// BinaryName returns the binary path inside code-server for a service.
func BinaryName(repoName, serviceName string) string {
	return fmt.Sprintf("/tmp/%s-%s", repoName, serviceName)
}

// pidFile returns the PID file path for a binary service.
func pidFile(repoName, serviceName string) string {
	return BinaryName(repoName, serviceName) + ".pid"
}

// logFile returns the log file path for a binary service.
func LogFile(repoName, serviceName string) string {
	return BinaryName(repoName, serviceName) + ".log"
}

// BuildBinary builds a Go binary inside code-server.
func BuildBinary(repoName, source string, serviceName string) error {
	bin := BinaryName(repoName, serviceName)
	buildDir := fmt.Sprintf("/home/coder/repos/%s", repoName)
	buildPath := "."
	if source != "" && source != "." {
		buildPath = "./" + strings.TrimPrefix(source, "./")
	}

	cmd := fmt.Sprintf("cd %s && go build -o %s %s 2>&1", buildDir, bin, buildPath)
	output, err := CoderExec(cmd)
	if err != nil {
		return fmt.Errorf("build failed: %s", strings.TrimSpace(output))
	}
	return nil
}

// RunBinary starts a binary as a background process inside code-server.
// env is a map of environment variables to set (e.g., PORT).
func RunBinary(repoName, serviceName string, env map[string]string) error {
	bin := BinaryName(repoName, serviceName)
	pid := pidFile(repoName, serviceName)
	logPath := LogFile(repoName, serviceName)

	// Build env args for docker exec (-e KEY=VALUE for each)
	args := []string{"exec"}
	for k, v := range env {
		args = append(args, "-e", fmt.Sprintf("%s=%s", k, v))
	}
	args = append(args, coderContainer, "/bin/bash", "-c",
		fmt.Sprintf("nohup %s > %s 2>&1 & echo $! > %s", bin, logPath, pid))

	cmd := exec.Command("docker", args...)
	_, err := cmd.CombinedOutput()
	return err
}

// StopBinary kills a running binary process.
func StopBinary(repoName, serviceName string) error {
	pid := pidFile(repoName, serviceName)
	CoderExec(fmt.Sprintf("kill $(cat %s 2>/dev/null) 2>/dev/null; rm -f %s", pid, pid))
	return nil
}

// IsBinaryRunning checks if a binary process is alive.
func IsBinaryRunning(repoName, serviceName string) bool {
	pid := pidFile(repoName, serviceName)
	out, err := CoderExec(fmt.Sprintf("kill -0 $(cat %s 2>/dev/null) 2>/dev/null && echo running", pid))
	return err == nil && strings.TrimSpace(out) == "running"
}

// BinaryLogs returns recent log output from a binary service.
func BinaryLogs(repoName, serviceName string, lines int) (string, error) {
	if lines <= 0 {
		lines = 50
	}
	log := LogFile(repoName, serviceName)
	out, err := CoderExec(fmt.Sprintf("tail -n %d %s 2>/dev/null", lines, log))
	if err != nil {
		return "", err
	}
	return out, nil
}