Files
vssm/daemon.go
2026-06-05 17:21:40 -05:00

217 lines
5.5 KiB
Go

package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"os/signal"
"path/filepath"
"syscall"
)
type CommandPayload struct {
Command string `json:"command"`
}
type DaemonServer struct {
cfg *AppConfig
procManager *ProcessManager
}
type InstanceStatusResponse struct {
Name string `json:"name"`
Version string `json:"version"`
Port int `json:"port"`
Status string `json:"status"`
}
func StartDaemon(cfg *AppConfig) error {
ds := &DaemonServer{
cfg: cfg,
procManager: NewProcessManager(),
}
mux := http.NewServeMux()
mux.HandleFunc("/instances/create", ds.handleCreate)
mux.HandleFunc("/instances/start", ds.handleStart)
mux.HandleFunc("/instances/stop", ds.handleStop)
mux.HandleFunc("/instances/command", ds.handleCommand)
mux.HandleFunc("/instances/list", ds.handleList)
server := &http.Server{
Addr: cfg.Daemon.ListenAddress,
Handler: mux,
}
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
go func() {
fmt.Printf("Engine daemon actively listening on http://%s\n", cfg.Daemon.ListenAddress)
if err := server.ListenAndServe(); err != http.ErrServerClosed {
fmt.Errorf("Daemon runtime failure: %v", err)
}
}()
<-sigChan
fmt.Println("\nShutting down supervisor daemon threads...")
return nil
}
func (ds *DaemonServer) handleCreate(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
name := r.URL.Query().Get("name")
version := r.URL.Query().Get("version")
if name == "" || version == "" {
http.Error(w, "Missing name or version parameters", http.StatusBadRequest)
return
}
if _, exists := ds.cfg.Instances[name]; exists {
http.Error(w, fmt.Sprintf("Instance '%s' already exists in configuration", name), http.StatusConflict)
return
}
options := VsServerConfigOptions{
Version: version,
ServerName: name,
Port: 42424,
MaxClients: 10,
}
err := DownloadAndExtractServer(version, ds.cfg.Storage.InstallDir)
if err != nil {
http.Error(w, fmt.Sprintf("Installation failed: %v", err), http.StatusInternalServerError)
return
}
err = CreateNewInstance(name, version, options, ds.cfg)
if err != nil {
http.Error(w, fmt.Sprintf("Instance provisioning failed: %v", err), http.StatusInternalServerError)
return
}
ds.cfg.Instances[name] = options
home, _ := os.UserHomeDir()
configPath := filepath.Join(home, ".config", "vs-manager", "config.json")
data, _ := json.MarshalIndent(ds.cfg, "", " ")
_ = os.WriteFile(configPath, data, 0644)
w.WriteHeader(http.StatusCreated)
fmt.Fprintf(w, "Successfully created and stored profile for instance %s", name)
}
func (ds *DaemonServer) handleStart(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
name := r.URL.Query().Get("name")
if name == "" {
http.Error(w, "Missing name parameter", http.StatusBadRequest)
return
}
options, exists := ds.cfg.Instances[name]
if !exists {
http.Error(w, fmt.Sprintf("Instance '%s' does not exist. Run 'create' first", name), http.StatusNotFound)
return
}
err := ds.procManager.StartInstance(name, options.Version, options, ds.cfg)
if err != nil {
http.Error(w, fmt.Sprintf("Process startup failed: %v", err), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "Successfully started instance %s", name)
}
func (ds *DaemonServer) handleStop(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
name := r.URL.Query().Get("name")
if name == "" {
http.Error(w, "Missing name parameter", http.StatusBadRequest)
return
}
err := ds.procManager.SendCommand(name, "/stop")
if err != nil {
http.Error(w, fmt.Sprintf("Failed to dispatch stop command: %v", err), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "Termination signal routed to instance %s", name)
}
func (ds *DaemonServer) handleCommand(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
name := r.URL.Query().Get("name")
if name == "" {
http.Error(w, "Missing name parameter", http.StatusBadRequest)
return
}
var payload CommandPayload
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
http.Error(w, "Malformed JSON body", http.StatusBadRequest)
return
}
err := ds.procManager.SendCommand(name, payload.Command)
if err != nil {
http.Error(w, fmt.Sprintf("Command delivery failed: %v", err), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "Command delivered successfully to %s", name)
}
func (ds *DaemonServer) handleList(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
ds.procManager.RLock()
defer ds.procManager.RUnlock()
var responseList []InstanceStatusResponse
for name, options := range ds.cfg.Instances {
status := "STOPPED"
if _, running := ds.procManager.ActiveInstances[name]; running {
status = "RUNNING"
}
responseList = append(responseList, InstanceStatusResponse{
Name: name,
Version: options.Version,
Port: options.Port,
Status: status,
})
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(responseList)
}