Cleanup; Adding locks where needed, and checking port availibility
This commit is contained in:
49
daemon.go
49
daemon.go
@@ -6,6 +6,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
@@ -24,6 +25,7 @@ type InstanceStatusResponse struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type DaemonServer struct {
|
type DaemonServer struct {
|
||||||
|
sync.RWMutex
|
||||||
cfg *AppConfig
|
cfg *AppConfig
|
||||||
configPath string
|
configPath string
|
||||||
procManager *ProcessManager
|
procManager *ProcessManager
|
||||||
@@ -82,6 +84,19 @@ func StartDaemon(cfg *AppConfig, configPath string) 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()
|
||||||
|
|
||||||
@@ -157,11 +172,26 @@ func (ds *DaemonServer) handleCreate(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for name, options := range ds.cfg.Instances {
|
||||||
|
if options.Port == converted_port {
|
||||||
|
http.Error(w, fmt.Sprintf("Port already occupied by instance '%s'", name), http.StatusConflict)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
options := VsServerConfigOptions{
|
options := VsServerConfigOptions{
|
||||||
Version: version,
|
Version: version,
|
||||||
ServerName: name,
|
ServerName: name,
|
||||||
@@ -199,6 +229,7 @@ func (ds *DaemonServer) handleCreate(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
@@ -210,13 +241,21 @@ 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)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
instanceConfigPath := ds.configPath
|
instanceConfigPath := filepath.Join(ds.cfg.Storage.InstancesDir, name, "serverconfig.json")
|
||||||
err := SyncInstanceConfig(options.Version, instanceConfigPath, options, ds.cfg)
|
err := SyncInstanceConfig(options.Version, instanceConfigPath, options, ds.cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Failed to sync config: "+err.Error(), http.StatusInternalServerError)
|
http.Error(w, "Failed to sync config: "+err.Error(), http.StatusInternalServerError)
|
||||||
@@ -289,6 +328,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()
|
||||||
|
|
||||||
|
|||||||
11
instance.go
11
instance.go
@@ -3,7 +3,6 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -14,16 +13,6 @@ const (
|
|||||||
StateRunning InstanceState = "RUNNING"
|
StateRunning InstanceState = "RUNNING"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ManagedInstance struct {
|
|
||||||
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 {
|
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)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user