Compare commits
10 Commits
8f3ef2a06d
...
dev
| Author | SHA1 | Date | |
|---|---|---|---|
| 96cc41ee43 | |||
| 988bdfcf6c | |||
| c3e17f9a80 | |||
| 336973443d | |||
| 0e4d96b1be | |||
| 3a01e835a1 | |||
| 9f4e27869b | |||
| 53b9f30d50 | |||
| 17117e2376 | |||
| 82029bf143 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -2,3 +2,5 @@
|
|||||||
.direnv
|
.direnv
|
||||||
.cache
|
.cache
|
||||||
.vscode
|
.vscode
|
||||||
|
vssm
|
||||||
|
test/
|
||||||
30
README.md
30
README.md
@@ -24,26 +24,40 @@ Setup is easy from source, all you need is:
|
|||||||
## Configuration
|
## Configuration
|
||||||
**For now, the configuration file is stored in `~/.config/vssm/config.json`, but this will be configurable later.**
|
**For now, the configuration file is stored in `~/.config/vssm/config.json`, but this will be configurable later.**
|
||||||
|
|
||||||
|
## Web Interface
|
||||||
|
Alongside this project, I am developing a seperate web-based dashboard that can be used to manage you servers with a very simple UI. You can get it [here](https://git.bellsworne.tech/chrisbell/vssm_web).
|
||||||
|
|
||||||
## Roadmap
|
## Roadmap
|
||||||
|
|
||||||
- [-] Configuration
|
- [ ] Configuration
|
||||||
- [x] Application data path
|
- [x] Custom configuration path (passed to the daemon with a flag `--config`)
|
||||||
- [x] Declarative Servers
|
- [x] Daemon instance remembers config path (*so you don't have to pass `--config` everytime you run an ipc command*)
|
||||||
|
- [ ] Daemon listen port (kind of implemented, could be cleaner)
|
||||||
|
- [x] Declarative Server configuration (Create servers in config)
|
||||||
|
- [x] All configuration options
|
||||||
|
|
||||||
- [-] Server management
|
- [ ] Server management
|
||||||
- [x] Create servers
|
- [x] Create servers
|
||||||
|
- [x] Automatic server binary downloading by version
|
||||||
|
- [x] Delete servers
|
||||||
- [x] Start/Stop servers
|
- [x] Start/Stop servers
|
||||||
|
- [x] Server configuration
|
||||||
|
- [x] Download correct configuration template for version (see availible templates [here](https://git.bellsworne.tech/chrisbell/vssm_config_templates))
|
||||||
|
- [x] Version
|
||||||
|
- [x] Server Name
|
||||||
|
- [x] Port
|
||||||
|
- [x] Full declarative configuration support in main app config
|
||||||
- [x] Multiple servers support
|
- [x] Multiple servers support
|
||||||
- [x] List servers and their status
|
- [x] List servers and their status
|
||||||
- [ ] Delete servers
|
- [x] Send commands to servers
|
||||||
- [ ] Automated server backups
|
- [ ] Automated server backups
|
||||||
|
|
||||||
- [ ] Binary releases
|
- [ ] Binary releases
|
||||||
|
|
||||||
- [-] Other
|
- [ ] Automatically set up background service
|
||||||
- [-] First class NixOS support
|
|
||||||
|
- [ ] Other
|
||||||
|
- [ ] First class NixOS support
|
||||||
- [x] Patch downloaded server binaries for NixOS
|
- [x] Patch downloaded server binaries for NixOS
|
||||||
- [ ] Official nix package or flake
|
- [ ] Official nix package or flake
|
||||||
- [ ] All configuration options
|
- [ ] All configuration options
|
||||||
|
|||||||
21
config.go
21
config.go
@@ -9,6 +9,7 @@ import (
|
|||||||
|
|
||||||
type AppConfig struct {
|
type AppConfig struct {
|
||||||
Storage struct {
|
Storage struct {
|
||||||
|
AppDataDir string `json:"app_data_dir"`
|
||||||
InstallDir string `json:"install_dir"`
|
InstallDir string `json:"install_dir"`
|
||||||
InstancesDir string `json:"instances_dir"`
|
InstancesDir string `json:"instances_dir"`
|
||||||
BackupDir string `json:"backup_dir"`
|
BackupDir string `json:"backup_dir"`
|
||||||
@@ -16,16 +17,15 @@ type AppConfig struct {
|
|||||||
} `json:"storage"`
|
} `json:"storage"`
|
||||||
|
|
||||||
Daemon struct {
|
Daemon struct {
|
||||||
// UseNixOs bool `json:"use_nixos"`
|
|
||||||
ListenAddress string `json:"listen_address"`
|
ListenAddress string `json:"listen_address"`
|
||||||
|
Port int `json:"port"`
|
||||||
|
} `json:"daemon"`
|
||||||
|
|
||||||
|
Instances map[string]InstanceMetadata `json:"instances"`
|
||||||
}
|
}
|
||||||
|
|
||||||
Instances map[string]VsServerConfigOptions `json:"instances"`
|
func CreateConfigWithDefaults(configPath string) *AppConfig {
|
||||||
}
|
basePath := filepath.Join(filepath.Dir(configPath), "vssm_data")
|
||||||
|
|
||||||
func DefaultConfig() *AppConfig {
|
|
||||||
home, _ := os.UserHomeDir()
|
|
||||||
basePath := filepath.Join(home, ".local", "share", "vssm")
|
|
||||||
|
|
||||||
cfg := &AppConfig{}
|
cfg := &AppConfig{}
|
||||||
cfg.Storage.InstallDir = filepath.Join(basePath, "installs")
|
cfg.Storage.InstallDir = filepath.Join(basePath, "installs")
|
||||||
@@ -33,16 +33,17 @@ func DefaultConfig() *AppConfig {
|
|||||||
cfg.Storage.BackupDir = filepath.Join(basePath, "backups")
|
cfg.Storage.BackupDir = filepath.Join(basePath, "backups")
|
||||||
cfg.Storage.ConfigTemplatesDir = filepath.Join(basePath, "config_templates")
|
cfg.Storage.ConfigTemplatesDir = filepath.Join(basePath, "config_templates")
|
||||||
|
|
||||||
cfg.Daemon.ListenAddress = "127.0.0.1:12345"
|
cfg.Daemon.ListenAddress = "127.0.0.1"
|
||||||
|
cfg.Daemon.Port = 65000
|
||||||
|
|
||||||
cfg.Instances = make(map[string]VsServerConfigOptions)
|
cfg.Instances = make(map[string]InstanceMetadata)
|
||||||
|
|
||||||
return cfg
|
return cfg
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadOrCreateConfig(configPath string) (*AppConfig, error) {
|
func LoadOrCreateConfig(configPath string) (*AppConfig, error) {
|
||||||
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
||||||
cfg := DefaultConfig()
|
cfg := CreateConfigWithDefaults(configPath)
|
||||||
|
|
||||||
if err := os.MkdirAll(filepath.Dir(configPath), 0755); err != nil {
|
if err := os.MkdirAll(filepath.Dir(configPath), 0755); err != nil {
|
||||||
return nil, fmt.Errorf("Failed to create config directory: %w", err)
|
return nil, fmt.Errorf("Failed to create config directory: %w", err)
|
||||||
|
|||||||
201
daemon.go
201
daemon.go
@@ -7,6 +7,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
@@ -24,13 +25,16 @@ type InstanceStatusResponse struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type DaemonServer struct {
|
type DaemonServer struct {
|
||||||
|
sync.RWMutex
|
||||||
cfg *AppConfig
|
cfg *AppConfig
|
||||||
|
configPath string
|
||||||
procManager *ProcessManager
|
procManager *ProcessManager
|
||||||
}
|
}
|
||||||
|
|
||||||
func StartDaemon(cfg *AppConfig) error {
|
func StartDaemon(cfg *AppConfig, configPath string) error {
|
||||||
ds := &DaemonServer{
|
ds := &DaemonServer{
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
|
configPath: configPath,
|
||||||
procManager: NewProcessManager(),
|
procManager: NewProcessManager(),
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,6 +44,8 @@ func StartDaemon(cfg *AppConfig) error {
|
|||||||
mux.HandleFunc("/instances/stop", ds.handleStop)
|
mux.HandleFunc("/instances/stop", ds.handleStop)
|
||||||
mux.HandleFunc("/instances/command", ds.handleCommand)
|
mux.HandleFunc("/instances/command", ds.handleCommand)
|
||||||
mux.HandleFunc("/instances/list", ds.handleList)
|
mux.HandleFunc("/instances/list", ds.handleList)
|
||||||
|
mux.HandleFunc("/instances/logs", ds.handleGetLogs)
|
||||||
|
mux.HandleFunc("/instances/delete", ds.handleDelete)
|
||||||
|
|
||||||
corsWrappedHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
corsWrappedHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
@@ -54,8 +60,10 @@ func StartDaemon(cfg *AppConfig) error {
|
|||||||
mux.ServeHTTP(w, r)
|
mux.ServeHTTP(w, r)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
listenAddress := cfg.Daemon.ListenAddress + ":" + strconv.Itoa(cfg.Daemon.Port)
|
||||||
|
|
||||||
server := &http.Server{
|
server := &http.Server{
|
||||||
Addr: cfg.Daemon.ListenAddress,
|
Addr: listenAddress,
|
||||||
Handler: corsWrappedHandler,
|
Handler: corsWrappedHandler,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,7 +71,7 @@ func StartDaemon(cfg *AppConfig) error {
|
|||||||
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
|
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
fmt.Printf("Engine daemon actively listening on http://%s\n", cfg.Daemon.ListenAddress)
|
fmt.Printf("Engine daemon actively listening on http://%s\n", listenAddress)
|
||||||
if err := server.ListenAndServe(); err != http.ErrServerClosed {
|
if err := server.ListenAndServe(); err != http.ErrServerClosed {
|
||||||
fmt.Printf("Daemon runtime failure: %v\n", err)
|
fmt.Printf("Daemon runtime failure: %v\n", err)
|
||||||
}
|
}
|
||||||
@@ -79,6 +87,19 @@ func StartDaemon(cfg *AppConfig) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ds *DaemonServer) reloadConfig() error {
|
||||||
|
cfg, err := LoadOrCreateConfig(ds.configPath)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Could not reload configuration file: %v\n", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
ds.Lock()
|
||||||
|
defer ds.Unlock()
|
||||||
|
ds.cfg = cfg
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (ds *DaemonServer) shutdownAllRunningServers() {
|
func (ds *DaemonServer) shutdownAllRunningServers() {
|
||||||
ds.procManager.Lock()
|
ds.procManager.Lock()
|
||||||
|
|
||||||
@@ -142,47 +163,75 @@ func (ds *DaemonServer) handleCreate(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
name := r.URL.Query().Get("name")
|
name := r.URL.Query().Get("name")
|
||||||
version := r.URL.Query().Get("version")
|
version := r.URL.Query().Get("version")
|
||||||
if name == "" || version == "" {
|
port := r.URL.Query().Get("port")
|
||||||
http.Error(w, "Missing name or version parameters", http.StatusBadRequest)
|
if name == "" || version == "" || port == "" {
|
||||||
|
http.Error(w, "Missing name, version, or port parameters", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
converted_port, err := strconv.Atoi(port)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Could not convert provided port to a valid port number", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if ds.reloadConfig() != nil {
|
||||||
|
http.Error(w, "Could not reload config", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ds.Lock()
|
||||||
|
defer ds.Unlock()
|
||||||
|
|
||||||
if _, exists := ds.cfg.Instances[name]; exists {
|
if _, exists := ds.cfg.Instances[name]; exists {
|
||||||
http.Error(w, fmt.Sprintf("Instance '%s' already exists in configuration", name), http.StatusConflict)
|
http.Error(w, fmt.Sprintf("Instance '%s' already exists in configuration", name), http.StatusConflict)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
options := VsServerConfigOptions{
|
for name, options := range ds.cfg.Instances {
|
||||||
Version: version,
|
if options.Port == converted_port {
|
||||||
ServerName: name,
|
http.Error(w, fmt.Sprintf("Port already occupied by instance '%s'", name), http.StatusConflict)
|
||||||
Port: 42424,
|
return
|
||||||
MaxClients: 10,
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err := DownloadAndExtractServer(version, ds.cfg.Storage.InstallDir)
|
metadata := InstanceMetadata{
|
||||||
|
Version: version,
|
||||||
|
ServerName: name,
|
||||||
|
Port: converted_port,
|
||||||
|
}
|
||||||
|
|
||||||
|
err = DownloadAndExtractServer(version, ds.cfg.Storage.InstallDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, fmt.Sprintf("Installation failed: %v", err), http.StatusInternalServerError)
|
http.Error(w, fmt.Sprintf("Installation failed: %v", err), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = CreateNewInstance(name, version, options, ds.cfg)
|
err = CreateNewInstance(name, version, metadata, ds.cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, fmt.Sprintf("Instance provisioning failed: %v", err), http.StatusInternalServerError)
|
http.Error(w, fmt.Sprintf("Instance provisioning failed: %v", err), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ds.cfg.Instances[name] = options
|
ds.cfg.Instances[name] = metadata
|
||||||
|
|
||||||
home, _ := os.UserHomeDir()
|
data, err := json.MarshalIndent(ds.cfg, "", " ")
|
||||||
configPath := filepath.Join(home, ".config", "vs-manager", "config.json")
|
if err != nil {
|
||||||
data, _ := json.MarshalIndent(ds.cfg, "", " ")
|
http.Error(w, fmt.Sprintf("Failed processing profile adjustments: %v", err), http.StatusInternalServerError)
|
||||||
_ = os.WriteFile(configPath, data, 0644)
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := os.WriteFile(ds.configPath, data, 0644); err != nil {
|
||||||
|
http.Error(w, fmt.Sprintf("Failed saving configuration adjustments to disk: %v", err), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusCreated)
|
w.WriteHeader(http.StatusCreated)
|
||||||
fmt.Fprintf(w, "Successfully created and stored profile for instance %s", name)
|
fmt.Fprintf(w, "Successfully created and stored profile for instance %s", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ds *DaemonServer) handleStart(w http.ResponseWriter, r *http.Request) {
|
func (ds *DaemonServer) handleStart(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
if r.Method != http.MethodPost {
|
if r.Method != http.MethodPost {
|
||||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
return
|
return
|
||||||
@@ -194,6 +243,14 @@ func (ds *DaemonServer) handleStart(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ds.reloadConfig() != nil {
|
||||||
|
http.Error(w, "Could not reload config", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ds.RLock()
|
||||||
|
defer ds.RUnlock()
|
||||||
|
|
||||||
options, exists := ds.cfg.Instances[name]
|
options, exists := ds.cfg.Instances[name]
|
||||||
if !exists {
|
if !exists {
|
||||||
http.Error(w, fmt.Sprintf("Instance '%s' does not exist. Run 'create' first", name), http.StatusNotFound)
|
http.Error(w, fmt.Sprintf("Instance '%s' does not exist. Run 'create' first", name), http.StatusNotFound)
|
||||||
@@ -273,6 +330,14 @@ func (ds *DaemonServer) handleList(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ds.reloadConfig() != nil {
|
||||||
|
http.Error(w, "Could not reload config", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ds.RLock()
|
||||||
|
defer ds.RUnlock()
|
||||||
|
|
||||||
ds.procManager.RLock()
|
ds.procManager.RLock()
|
||||||
defer ds.procManager.RUnlock()
|
defer ds.procManager.RUnlock()
|
||||||
|
|
||||||
@@ -296,3 +361,105 @@ func (ds *DaemonServer) handleList(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
json.NewEncoder(w).Encode(responseList)
|
json.NewEncoder(w).Encode(responseList)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ds *DaemonServer) handleGetLogs(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodGet {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
ds.procManager.RLock()
|
||||||
|
buf, exists := ds.procManager.LogBuffers[name]
|
||||||
|
ds.procManager.RUnlock()
|
||||||
|
|
||||||
|
if !exists || buf == nil {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.Write([]byte("[]"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
logLines := buf.GetSnapshot()
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
if err := json.NewEncoder(w).Encode(logLines); err != nil {
|
||||||
|
http.Error(w, fmt.Sprintf("Failed encoding log matrix: %v", err), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ds *DaemonServer) handleDelete(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
|
||||||
|
}
|
||||||
|
|
||||||
|
purge := false
|
||||||
|
purgeParam := r.URL.Query().Get("purge")
|
||||||
|
if purgeParam != "" {
|
||||||
|
result, err := strconv.ParseBool(purgeParam)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Invalid value for 'purge' parameter", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
purge = result
|
||||||
|
}
|
||||||
|
|
||||||
|
if ds.reloadConfig() != nil {
|
||||||
|
http.Error(w, "Could not reload config", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ds.Lock()
|
||||||
|
defer ds.Unlock()
|
||||||
|
|
||||||
|
ds.procManager.RLock()
|
||||||
|
defer ds.procManager.RUnlock()
|
||||||
|
|
||||||
|
_, exists := ds.cfg.Instances[name]
|
||||||
|
if !exists {
|
||||||
|
http.Error(w, fmt.Sprintf("Cannot delete instance '%s'; Does not exist.", name), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, running := ds.procManager.ActiveInstances[name]; running {
|
||||||
|
http.Error(w, fmt.Sprintf("Cannot delete instance '%s'; It is currently running.", name), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
delete(ds.cfg.Instances, name)
|
||||||
|
|
||||||
|
data, err := json.MarshalIndent(ds.cfg, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, fmt.Sprintf("Failed processing profile adjustments: %v", err), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := os.WriteFile(ds.configPath, data, 0644); err != nil {
|
||||||
|
http.Error(w, fmt.Sprintf("Failed saving configuration adjustments to disk: %v", err), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if purge {
|
||||||
|
path := filepath.Join(ds.cfg.Storage.InstancesDir, name)
|
||||||
|
err := os.RemoveAll(path)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, fmt.Sprintf("Failed to delete instance from disk: %v", err), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
fmt.Fprintf(w, "Successfully deleted instance %s", name)
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
15
instance.go
15
instance.go
@@ -3,7 +3,6 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -14,17 +13,7 @@ const (
|
|||||||
StateRunning InstanceState = "RUNNING"
|
StateRunning InstanceState = "RUNNING"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ManagedInstance struct {
|
func CreateNewInstance(name string, version string, meta InstanceMetadata, cfg *AppConfig) error {
|
||||||
Name string `json:"name"`
|
|
||||||
Version string `json:"version"`
|
|
||||||
Port int `json:"port"`
|
|
||||||
Status InstanceState `json:"status"`
|
|
||||||
|
|
||||||
Cmd *exec.Cmd `json:"-"`
|
|
||||||
Stdin interface{} `json:"-"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func CreateNewInstance(name string, version string, options VsServerConfigOptions, cfg *AppConfig) error {
|
|
||||||
instanceDir := filepath.Join(cfg.Storage.InstancesDir, name)
|
instanceDir := filepath.Join(cfg.Storage.InstancesDir, name)
|
||||||
|
|
||||||
dirs := []string{
|
dirs := []string{
|
||||||
@@ -40,7 +29,7 @@ func CreateNewInstance(name string, version string, options VsServerConfigOption
|
|||||||
}
|
}
|
||||||
|
|
||||||
instanceConfigPath := filepath.Join(instanceDir, "serverconfig.json")
|
instanceConfigPath := filepath.Join(instanceDir, "serverconfig.json")
|
||||||
if err := PrepareInstanceConfig(version, instanceConfigPath, options, cfg); err != nil {
|
if err := PrepareInstanceConfig(version, instanceConfigPath, meta, cfg); err != nil {
|
||||||
return fmt.Errorf("Failed provisioning server baseline configuration: %w", err)
|
return fmt.Errorf("Failed provisioning server baseline configuration: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,30 +4,30 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type VsServerConfigOptions struct {
|
type InstanceMetadata struct {
|
||||||
Version string `json:"Version"`
|
Version string `json:"Version"`
|
||||||
ServerName string `json:"ServerName"`
|
ServerName string `json:"ServerName"`
|
||||||
Port int `json:"Port"`
|
Port int `json:"Port"`
|
||||||
IpAddress string `json:"IpAddress"`
|
Config map[string]interface{} `json:"config"`
|
||||||
MaxClients int `json:"MaxClients"`
|
|
||||||
Password string `json:"Password"`
|
|
||||||
DefaultRole string `json:"DefaultRole"`
|
|
||||||
GuestRole string `json:"GuestRole"`
|
|
||||||
PreApprovedRole string `json:"PreApprovedRole"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func PrepareInstanceConfig(templateVersion string, instanceConfigPath string, options VsServerConfigOptions, cfg *AppConfig) error {
|
func PrepareInstanceConfig(templateVersion string, instanceConfigPath string, meta InstanceMetadata, cfg *AppConfig) error {
|
||||||
return SyncInstanceConfig(templateVersion, instanceConfigPath, options, cfg)
|
return SyncInstanceConfig(templateVersion, instanceConfigPath, meta, cfg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func SyncInstanceConfig(templateVersion string, instanceConfigPath string, options VsServerConfigOptions, cfg *AppConfig) error {
|
func SyncInstanceConfig(templateVersion string, instanceConfigPath string, meta InstanceMetadata, cfg *AppConfig) error {
|
||||||
templatePath := filepath.Join(cfg.Storage.ConfigTemplatesDir, templateVersion, "serverconfig.json")
|
templatePath := filepath.Join(cfg.Storage.ConfigTemplatesDir, templateVersion, "serverconfig.json")
|
||||||
|
|
||||||
|
if err := ensureTemplateExists(templateVersion, templatePath, cfg); err != nil {
|
||||||
|
return fmt.Errorf("failed ensuring configuration template availability: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
if _, err := os.Stat(instanceConfigPath); os.IsNotExist(err) {
|
if _, err := os.Stat(instanceConfigPath); os.IsNotExist(err) {
|
||||||
if err := os.MkdirAll(filepath.Dir(instanceConfigPath), 0755); err != nil {
|
if err := os.MkdirAll(filepath.Dir(instanceConfigPath), 0755); err != nil {
|
||||||
return fmt.Errorf("failed creating instance directory tree: %w", err)
|
return fmt.Errorf("failed creating instance directory tree: %w", err)
|
||||||
@@ -61,16 +61,13 @@ func SyncInstanceConfig(templateVersion string, instanceConfigPath string, optio
|
|||||||
return fmt.Errorf("failed parsing configuration JSON payload: %w", err)
|
return fmt.Errorf("failed parsing configuration JSON payload: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
rawConfig["ServerName"] = options.ServerName
|
for key, value := range meta.Config {
|
||||||
rawConfig["Port"] = options.Port
|
rawConfig[key] = value
|
||||||
rawConfig["MaxClients"] = options.MaxClients
|
|
||||||
|
|
||||||
if options.Password != "" {
|
|
||||||
rawConfig["Password"] = options.Password
|
|
||||||
} else {
|
|
||||||
rawConfig["Password"] = nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rawConfig["ServerName"] = meta.ServerName
|
||||||
|
rawConfig["Port"] = meta.Port
|
||||||
|
|
||||||
instanceDir := filepath.Dir(instanceConfigPath)
|
instanceDir := filepath.Dir(instanceConfigPath)
|
||||||
if worldConfig, ok := rawConfig["WorldConfig"].(map[string]interface{}); ok {
|
if worldConfig, ok := rawConfig["WorldConfig"].(map[string]interface{}); ok {
|
||||||
worldConfig["SaveFileLocation"] = filepath.Join(instanceDir, "Saves", "default.vcdbs")
|
worldConfig["SaveFileLocation"] = filepath.Join(instanceDir, "Saves", "default.vcdbs")
|
||||||
@@ -91,3 +88,43 @@ func SyncInstanceConfig(templateVersion string, instanceConfigPath string, optio
|
|||||||
|
|
||||||
return os.WriteFile(instanceConfigPath, updatedData, 0644)
|
return os.WriteFile(instanceConfigPath, updatedData, 0644)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ensureTemplateExists(version string, targetPath string, cfg *AppConfig) error {
|
||||||
|
if _, err := os.Stat(targetPath); err == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Template for version %s not found locally. Fetching remote layout from git server...\n", version)
|
||||||
|
|
||||||
|
if err := os.MkdirAll(filepath.Dir(targetPath), 0755); err != nil {
|
||||||
|
return fmt.Errorf("failed to create local template cache directory: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
remoteURL := fmt.Sprintf("https://git.bellsworne.tech/chrisbell/vssm_config_templates/raw/branch/main/%s/serverconfig.json", version)
|
||||||
|
|
||||||
|
resp, err := http.Get(remoteURL)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("network error attempting to pull config template: %w", err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode == http.StatusNotFound {
|
||||||
|
return fmt.Errorf("version template '%s' does not exist in the remote git repository asset tree", version)
|
||||||
|
} else if resp.StatusCode != http.StatusOK {
|
||||||
|
return fmt.Errorf("remote git mirror returned unexpected HTTP status: %s", resp.Status)
|
||||||
|
}
|
||||||
|
|
||||||
|
out, err := os.Create(targetPath)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed creating cache file hook on system storage: %w", err)
|
||||||
|
}
|
||||||
|
defer out.Close()
|
||||||
|
|
||||||
|
_, err = io.Copy(out, resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed stream-writing network payload cache frame to disk: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Successfully cached configuration layout template for version %s\n", version)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
117
main.go
117
main.go
@@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
@@ -10,6 +11,7 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
"text/tabwriter"
|
"text/tabwriter"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -19,54 +21,90 @@ func main() {
|
|||||||
log.Fatalf("Could not locate user home dir: %v", err)
|
log.Fatalf("Could not locate user home dir: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
configPath := filepath.Join(home, ".config", "vssm", "config.json")
|
defaultConfigPath := filepath.Join(home, ".config", "vssm", "config.json")
|
||||||
cfg, err := LoadOrCreateConfig(configPath)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Initialization failed: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(os.Args) < 2 {
|
configFlag := flag.String("config", defaultConfigPath, "Explicit path targeting a custom vssm config.json profile")
|
||||||
|
|
||||||
|
flag.Usage = printUsage
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
args := flag.Args()
|
||||||
|
if len(args) < 1 {
|
||||||
printUsage()
|
printUsage()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
subCommand := os.Args[1]
|
configExplicitlySet := false
|
||||||
|
flag.Visit(func(f *flag.Flag) {
|
||||||
|
if f.Name == "config" {
|
||||||
|
configExplicitlySet = true
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
if !configExplicitlySet {
|
||||||
|
if envConfig := os.Getenv("VSSM_CONFIG_PATH"); envConfig != "" {
|
||||||
|
configFlag = &envConfig
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
absConfigPath, err := filepath.Abs(*configFlag)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to resolve absolute configuration path target: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if configExplicitlySet {
|
||||||
|
if err := os.Setenv("VSSM_CONFIG_PATH", absConfigPath); err != nil {
|
||||||
|
log.Fatalf("Warning: could not set VSSM_CONFIG_PATH: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg, err := LoadOrCreateConfig(absConfigPath)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Initialization failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
listenAddress := cfg.Daemon.ListenAddress + ":" + strconv.Itoa(cfg.Daemon.Port)
|
||||||
|
fmt.Printf("Got listen address from config: %s\n", listenAddress)
|
||||||
|
|
||||||
|
subCommand := args[0]
|
||||||
|
|
||||||
switch subCommand {
|
switch subCommand {
|
||||||
|
|
||||||
case "daemon":
|
case "daemon":
|
||||||
fmt.Println("Initializing VS server manager background supervisor...")
|
fmt.Printf("Initializing VS server manager background supervisor [Config: %s]...\n", absConfigPath)
|
||||||
if err := StartDaemon(cfg); err != nil {
|
if err := StartDaemon(cfg, absConfigPath); err != nil {
|
||||||
log.Fatalf("Daemon runtime fatal error: %v", err)
|
log.Fatalf("Daemon runtime fatal error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
case "create":
|
case "create":
|
||||||
if len(os.Args) < 4 {
|
if len(args) < 4 {
|
||||||
log.Fatalf("Usage: go run . create <instance_name> <version>")
|
log.Fatalf("Usage: vssm create <instance_name> <version> <port>")
|
||||||
}
|
}
|
||||||
name := os.Args[2]
|
name := args[1]
|
||||||
version := os.Args[3]
|
version := args[2]
|
||||||
sendIPCRequest(cfg, "POST", fmt.Sprintf("/instances/create?name=%s&version=%s", url.QueryEscape(name), url.QueryEscape(version)), nil)
|
port := args[3]
|
||||||
|
sendIPCRequest(cfg, "POST", fmt.Sprintf("/instances/create?name=%s&version=%s&port=%s", url.QueryEscape(name), url.QueryEscape(version), url.QueryEscape(port)), nil)
|
||||||
|
|
||||||
case "start":
|
case "start":
|
||||||
if len(os.Args) < 3 {
|
if len(args) < 2 {
|
||||||
log.Fatalf("Usage: go run . start <instance_name>")
|
log.Fatalf("Usage: vssm start <instance_name>")
|
||||||
}
|
}
|
||||||
name := os.Args[2]
|
name := args[1]
|
||||||
sendIPCRequest(cfg, "POST", fmt.Sprintf("/instances/start?name=%s", url.QueryEscape(name)), nil)
|
sendIPCRequest(cfg, "POST", fmt.Sprintf("/instances/start?name=%s", url.QueryEscape(name)), nil)
|
||||||
|
|
||||||
case "stop":
|
case "stop":
|
||||||
if len(os.Args) < 3 {
|
if len(args) < 2 {
|
||||||
log.Fatalf("Usage: go run . stop <instance_name>")
|
log.Fatalf("Usage: vssm stop <instance_name>")
|
||||||
}
|
}
|
||||||
name := os.Args[2]
|
name := args[1]
|
||||||
sendIPCRequest(cfg, "POST", fmt.Sprintf("/instances/stop?name=%s", url.QueryEscape(name)), nil)
|
sendIPCRequest(cfg, "POST", fmt.Sprintf("/instances/stop?name=%s", url.QueryEscape(name)), nil)
|
||||||
|
|
||||||
case "cmd":
|
case "cmd":
|
||||||
if len(os.Args) < 4 {
|
if len(args) < 3 {
|
||||||
log.Fatalf("Usage: go run . cmd <instance_name> \"<server command>\"")
|
log.Fatalf("Usage: vssm cmd <instance_name> \"<server command>\"")
|
||||||
}
|
}
|
||||||
name := os.Args[2]
|
name := args[1]
|
||||||
serverCmd := os.Args[3]
|
serverCmd := args[2]
|
||||||
|
|
||||||
payload := CommandPayload{Command: serverCmd}
|
payload := CommandPayload{Command: serverCmd}
|
||||||
body, _ := json.Marshal(payload)
|
body, _ := json.Marshal(payload)
|
||||||
@@ -75,13 +113,31 @@ func main() {
|
|||||||
case "list", "status":
|
case "list", "status":
|
||||||
fetchAndPrintStatus(cfg)
|
fetchAndPrintStatus(cfg)
|
||||||
|
|
||||||
|
case "show-config":
|
||||||
|
fmt.Printf("%v", cfg.Storage.AppDataDir)
|
||||||
|
|
||||||
|
case "delete":
|
||||||
|
if len(args) < 2 {
|
||||||
|
log.Fatalf("Usage: vssm delete <instance_name>")
|
||||||
|
}
|
||||||
|
|
||||||
|
name := args[1]
|
||||||
|
|
||||||
|
deleteCmd := flag.NewFlagSet("delete", flag.ExitOnError)
|
||||||
|
purge := deleteCmd.Bool("purge", false, "Delete instance files from disk")
|
||||||
|
deleteCmd.Parse(args[2:])
|
||||||
|
|
||||||
|
sendIPCRequest(cfg, "POST", fmt.Sprintf("/instances/delete?name=%s&purge=%s", url.QueryEscape(name), url.QueryEscape(strconv.FormatBool(*purge))), nil)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
printUsage()
|
printUsage()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func sendIPCRequest(cfg *AppConfig, method string, path string, body io.Reader) {
|
func sendIPCRequest(cfg *AppConfig, method string, path string, body io.Reader) {
|
||||||
targetUrl := fmt.Sprintf("http://%s%s", cfg.Daemon.ListenAddress, path)
|
listenAddress := cfg.Daemon.ListenAddress + ":" + strconv.Itoa(cfg.Daemon.Port)
|
||||||
|
targetUrl := fmt.Sprintf("http://%s%s", listenAddress, path)
|
||||||
|
fmt.Printf("Listen Addr: %s; Target URL: %s\n", listenAddress, targetUrl)
|
||||||
req, err := http.NewRequest(method, targetUrl, body)
|
req, err := http.NewRequest(method, targetUrl, body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to construct IPC frame: %v", err)
|
log.Fatalf("Failed to construct IPC frame: %v", err)
|
||||||
@@ -98,7 +154,8 @@ func sendIPCRequest(cfg *AppConfig, method string, path string, body io.Reader)
|
|||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
respBody, _ := io.ReadAll(resp.Body)
|
respBody, _ := io.ReadAll(resp.Body)
|
||||||
if resp.StatusCode != http.StatusOK {
|
|
||||||
|
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
|
||||||
log.Fatalf("Error from daemon: %s", string(respBody))
|
log.Fatalf("Error from daemon: %s", string(respBody))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,7 +163,8 @@ func sendIPCRequest(cfg *AppConfig, method string, path string, body io.Reader)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func fetchAndPrintStatus(cfg *AppConfig) {
|
func fetchAndPrintStatus(cfg *AppConfig) {
|
||||||
targetUrl := fmt.Sprintf("http://%s/instances/list", cfg.Daemon.ListenAddress)
|
listenAddress := cfg.Daemon.ListenAddress + ":" + strconv.Itoa(cfg.Daemon.Port)
|
||||||
|
targetUrl := fmt.Sprintf("http://%s/instances/list", listenAddress)
|
||||||
resp, err := http.Get(targetUrl)
|
resp, err := http.Get(targetUrl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("IPC connection failed. Is the vs-manager daemon running? Error: %v", err)
|
log.Fatalf("IPC connection failed. Is the vs-manager daemon running? Error: %v", err)
|
||||||
@@ -140,10 +198,13 @@ func fetchAndPrintStatus(cfg *AppConfig) {
|
|||||||
|
|
||||||
func printUsage() {
|
func printUsage() {
|
||||||
fmt.Println("VintageStory Server Manager")
|
fmt.Println("VintageStory Server Manager")
|
||||||
fmt.Println("\nUsage:")
|
fmt.Println("\nGlobal Options:")
|
||||||
|
fmt.Println(" --config <path> Explicitly target a non-default config structure file location")
|
||||||
|
fmt.Println("\nUsage Commands:")
|
||||||
fmt.Println(" vssm daemon Starts the background process supervisor")
|
fmt.Println(" vssm daemon Starts the background process supervisor")
|
||||||
fmt.Println(" vssm create <name> <version> Provisions baseline configuration and stores instance profile")
|
fmt.Println(" vssm create <name> <version> Provisions baseline configuration and stores instance profile")
|
||||||
fmt.Println(" vssm start <name> Launches an existing server instance using stored profile")
|
fmt.Println(" vssm start <name> Launches an existing server instance using stored profile")
|
||||||
fmt.Println(" vssm stop <name> Gracefully shuts down a server instance")
|
fmt.Println(" vssm stop <name> Gracefully shuts down a server instance")
|
||||||
fmt.Println(" vssm cmd <name> \"<command>\" Dispatches a terminal console command down the pipe")
|
fmt.Println(" vssm cmd <name> \"<command>\" Dispatches a terminal console command down the pipe")
|
||||||
|
fmt.Println(" vssm list Displays the operational matrix of all instances")
|
||||||
}
|
}
|
||||||
|
|||||||
53
process.go
53
process.go
@@ -15,16 +15,24 @@ type ProcessManager struct {
|
|||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
ActiveInstances map[string]*exec.Cmd
|
ActiveInstances map[string]*exec.Cmd
|
||||||
StdinPipes map[string]io.WriteCloser
|
StdinPipes map[string]io.WriteCloser
|
||||||
|
LogBuffers map[string]*InstanceLogBuffer
|
||||||
|
}
|
||||||
|
|
||||||
|
type InstanceLogBuffer struct {
|
||||||
|
sync.RWMutex
|
||||||
|
Lines []string
|
||||||
|
MaxLines int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewProcessManager() *ProcessManager {
|
func NewProcessManager() *ProcessManager {
|
||||||
return &ProcessManager{
|
return &ProcessManager{
|
||||||
ActiveInstances: make(map[string]*exec.Cmd),
|
ActiveInstances: make(map[string]*exec.Cmd),
|
||||||
StdinPipes: make(map[string]io.WriteCloser),
|
StdinPipes: make(map[string]io.WriteCloser),
|
||||||
|
LogBuffers: map[string]*InstanceLogBuffer{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pm *ProcessManager) StartInstance(name string, version string, options VsServerConfigOptions, config *AppConfig) error {
|
func (pm *ProcessManager) StartInstance(name string, version string, meta InstanceMetadata, config *AppConfig) error {
|
||||||
pm.Lock()
|
pm.Lock()
|
||||||
defer pm.Unlock()
|
defer pm.Unlock()
|
||||||
|
|
||||||
@@ -66,6 +74,7 @@ func (pm *ProcessManager) StartInstance(name string, version string, options VsS
|
|||||||
|
|
||||||
pm.ActiveInstances[name] = cmd
|
pm.ActiveInstances[name] = cmd
|
||||||
pm.StdinPipes[name] = stdinPipe
|
pm.StdinPipes[name] = stdinPipe
|
||||||
|
pm.LogBuffers[name] = NewInstanceLogBuffer(200)
|
||||||
|
|
||||||
go pm.streamLogs(name, stdoutPipe)
|
go pm.streamLogs(name, stdoutPipe)
|
||||||
go pm.watchProcessExit(name, cmd)
|
go pm.watchProcessExit(name, cmd)
|
||||||
@@ -79,9 +88,17 @@ func (pm *ProcessManager) streamLogs(name string, stdout io.ReadCloser) {
|
|||||||
scanner := bufio.NewScanner(stdout)
|
scanner := bufio.NewScanner(stdout)
|
||||||
|
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
//stream straight to the manager console terminal,
|
line := scanner.Text()
|
||||||
// but later dispatch payloads to our web UI or a log file
|
|
||||||
fmt.Printf("[%s]: %s\n", name, scanner.Text())
|
fmt.Printf("[%s]: %s\n", name, line)
|
||||||
|
|
||||||
|
pm.RLock()
|
||||||
|
buf, exists := pm.LogBuffers[name]
|
||||||
|
pm.RUnlock()
|
||||||
|
|
||||||
|
if exists {
|
||||||
|
buf.Append(line)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -119,3 +136,31 @@ func (pm *ProcessManager) SendCommand(name string, command string) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewInstanceLogBuffer(maxLines int) *InstanceLogBuffer {
|
||||||
|
return &InstanceLogBuffer{
|
||||||
|
Lines: make([]string, 0, maxLines),
|
||||||
|
MaxLines: maxLines,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (lb *InstanceLogBuffer) Append(line string) {
|
||||||
|
lb.Lock()
|
||||||
|
defer lb.Unlock()
|
||||||
|
|
||||||
|
if len(lb.Lines) >= lb.MaxLines {
|
||||||
|
// Shift array out by dropping index 0
|
||||||
|
lb.Lines = lb.Lines[1:]
|
||||||
|
}
|
||||||
|
lb.Lines = append(lb.Lines, line)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (lb *InstanceLogBuffer) GetSnapshot() []string {
|
||||||
|
lb.RLock()
|
||||||
|
defer lb.RUnlock()
|
||||||
|
|
||||||
|
// Return a copy so the caller can safely iterate or serialize to JSON without lock conflicts
|
||||||
|
snapshot := make([]string, len(lb.Lines))
|
||||||
|
copy(snapshot, lb.Lines)
|
||||||
|
return snapshot
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user