Can now set custom config path flag when starting daemon
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -2,3 +2,5 @@
|
|||||||
.direnv
|
.direnv
|
||||||
.cache
|
.cache
|
||||||
.vscode
|
.vscode
|
||||||
|
vssm
|
||||||
|
test/
|
||||||
@@ -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,18 +17,18 @@ 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"`
|
||||||
}
|
}
|
||||||
|
|
||||||
Instances map[string]VsServerConfigOptions `json:"instances"`
|
Instances map[string]VsServerConfigOptions `json:"instances"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func DefaultConfig() *AppConfig {
|
func CreateConfigWithDefaults(configPath string) *AppConfig {
|
||||||
home, _ := os.UserHomeDir()
|
home, _ := os.UserHomeDir()
|
||||||
basePath := filepath.Join(home, ".local", "share", "vssm")
|
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")
|
||||||
@@ -42,7 +43,7 @@ func DefaultConfig() *AppConfig {
|
|||||||
|
|
||||||
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)
|
||||||
@@ -69,6 +70,7 @@ 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,
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -28,6 +29,10 @@ func PrepareInstanceConfig(templateVersion string, instanceConfigPath string, op
|
|||||||
func SyncInstanceConfig(templateVersion string, instanceConfigPath string, options VsServerConfigOptions, cfg *AppConfig) error {
|
func SyncInstanceConfig(templateVersion string, instanceConfigPath string, options VsServerConfigOptions, 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)
|
||||||
@@ -91,3 +96,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
|
||||||
|
}
|
||||||
|
|||||||
51
main.go
51
main.go
@@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
@@ -19,20 +20,28 @@ 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]
|
cfg, err := LoadOrCreateConfig(*configFlag)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Initialization failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
subCommand := args[0]
|
||||||
|
|
||||||
switch subCommand {
|
switch subCommand {
|
||||||
|
|
||||||
case "daemon":
|
case "daemon":
|
||||||
fmt.Println("Initializing VS server manager background supervisor...")
|
fmt.Println("Initializing VS server manager background supervisor...")
|
||||||
if err := StartDaemon(cfg); err != nil {
|
if err := StartDaemon(cfg); err != nil {
|
||||||
@@ -40,33 +49,33 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
case "create":
|
case "create":
|
||||||
if len(os.Args) < 4 {
|
if len(args) < 3 {
|
||||||
log.Fatalf("Usage: go run . create <instance_name> <version>")
|
log.Fatalf("Usage: vssm create <instance_name> <version>")
|
||||||
}
|
}
|
||||||
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)
|
sendIPCRequest(cfg, "POST", fmt.Sprintf("/instances/create?name=%s&version=%s", url.QueryEscape(name), url.QueryEscape(version)), 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) < 4 {
|
||||||
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user