Compare commits

...

4 Commits

7 changed files with 40 additions and 41 deletions

View File

@@ -34,18 +34,19 @@ Alongside this project, I am developing a seperate web-based dashboard that can
- [x] Daemon instance remembers config path (*so you don't have to pass `--config` everytime you run an ipc command*) - [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) - [ ] Daemon listen port (kind of implemented, could be cleaner)
- [x] Declarative Server configuration (Create servers in config) - [x] Declarative Server configuration (Create servers in config)
- [ ] More configuration options - [x] All configuration options
- [ ] Server management - [ ] Server management
- [x] Create servers - [x] Create servers
- [x] Automatic server binary downloading by version - [x] Automatic server binary downloading by version
- [x] Delete servers - [x] Delete servers
- [x] Start/Stop servers - [x] Start/Stop servers
- [ ] Server configuration - [x] Server configuration
- [x] Download correct configuration template for version (see availible templates [here](https://git.bellsworne.tech/chrisbell/vssm_config_templates)) - [x] Download correct configuration template for version (see availible templates [here](https://git.bellsworne.tech/chrisbell/vssm_config_templates))
- [x] Version - [x] Version
- [x] Server Name - [x] Server Name
- [x] Port - [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
- [x] Send commands to servers - [x] Send commands to servers

View File

@@ -18,25 +18,25 @@ type AppConfig struct {
Daemon struct { Daemon struct {
ListenAddress string `json:"listen_address"` ListenAddress string `json:"listen_address"`
Port int `json:"port"`
} `json:"daemon"` } `json:"daemon"`
Instances map[string]VsServerConfigOptions `json:"instances"` Instances map[string]InstanceMetadata `json:"instances"`
} }
func CreateConfigWithDefaults(configPath string) *AppConfig { func CreateConfigWithDefaults(configPath string) *AppConfig {
home, _ := os.UserHomeDir() basePath := filepath.Join(filepath.Dir(configPath), "vssm_data")
basePath := filepath.Join(home, ".local", "share", "vssm")
cfg := &AppConfig{} cfg := &AppConfig{}
cfg.Storage.AppDataDir = basePath
cfg.Storage.InstallDir = filepath.Join(basePath, "installs") cfg.Storage.InstallDir = filepath.Join(basePath, "installs")
cfg.Storage.InstancesDir = filepath.Join(basePath, "instances") cfg.Storage.InstancesDir = filepath.Join(basePath, "instances")
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
} }
@@ -70,7 +70,6 @@ func LoadOrCreateConfig(configPath string) (*AppConfig, error) {
} }
dirs := []string{ dirs := []string{
cfg.Storage.AppDataDir,
cfg.Storage.InstallDir, cfg.Storage.InstallDir,
cfg.Storage.InstancesDir, cfg.Storage.InstancesDir,
cfg.Storage.BackupDir, cfg.Storage.BackupDir,

View File

@@ -60,8 +60,10 @@ func StartDaemon(cfg *AppConfig, configPath string) 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,
} }
@@ -69,7 +71,7 @@ func StartDaemon(cfg *AppConfig, configPath string) 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)
} }
@@ -193,11 +195,10 @@ func (ds *DaemonServer) handleCreate(w http.ResponseWriter, r *http.Request) {
} }
} }
options := VsServerConfigOptions{ metadata := InstanceMetadata{
Version: version, Version: version,
ServerName: name, ServerName: name,
Port: converted_port, Port: converted_port,
MaxClients: 10,
} }
err = DownloadAndExtractServer(version, ds.cfg.Storage.InstallDir) err = DownloadAndExtractServer(version, ds.cfg.Storage.InstallDir)
@@ -206,13 +207,13 @@ func (ds *DaemonServer) handleCreate(w http.ResponseWriter, r *http.Request) {
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
data, err := json.MarshalIndent(ds.cfg, "", " ") data, err := json.MarshalIndent(ds.cfg, "", " ")
if err != nil { if err != nil {

View File

@@ -13,7 +13,7 @@ const (
StateRunning InstanceState = "RUNNING" StateRunning InstanceState = "RUNNING"
) )
func CreateNewInstance(name string, version string, options VsServerConfigOptions, cfg *AppConfig) error { func CreateNewInstance(name string, version string, meta InstanceMetadata, cfg *AppConfig) error {
instanceDir := filepath.Join(cfg.Storage.InstancesDir, name) instanceDir := filepath.Join(cfg.Storage.InstancesDir, name)
dirs := []string{ dirs := []string{
@@ -29,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)
} }

View File

@@ -10,23 +10,18 @@ import (
"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 { if err := ensureTemplateExists(templateVersion, templatePath, cfg); err != nil {
@@ -66,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")

10
main.go
View File

@@ -63,6 +63,9 @@ func main() {
log.Fatalf("Initialization failed: %v", err) 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] subCommand := args[0]
switch subCommand { switch subCommand {
@@ -132,7 +135,9 @@ func main() {
} }
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)
@@ -158,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)

View File

@@ -32,7 +32,7 @@ func NewProcessManager() *ProcessManager {
} }
} }
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()