179 lines
4.6 KiB
Go
179 lines
4.6 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
|
|
}
|
|
|
|
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)
|
|
|
|
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, // Pull from custom parameters later if desired
|
|
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)
|
|
}
|