Added a modify endpoint to change name and port
This commit is contained in:
75
daemon.go
75
daemon.go
@@ -47,6 +47,7 @@ func StartDaemon(cfg *AppConfig, configPath string) error {
|
||||
mux.HandleFunc("/instances/list", ds.handleList)
|
||||
mux.HandleFunc("/instances/logs", ds.handleGetLogs)
|
||||
mux.HandleFunc("/instances/delete", ds.handleDelete)
|
||||
mux.HandleFunc("/instances/modify", ds.handleModify)
|
||||
|
||||
corsWrappedHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
@@ -474,3 +475,77 @@ func (ds *DaemonServer) handleDelete(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintf(w, "Successfully deleted instance %s", name)
|
||||
|
||||
}
|
||||
|
||||
func (ds *DaemonServer) handleModify(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
|
||||
}
|
||||
|
||||
newName := r.URL.Query().Get("newName")
|
||||
if newName == "" {
|
||||
http.Error(w, "Missing newName parameter", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
newPort := r.URL.Query().Get("newPort")
|
||||
if newPort == "" {
|
||||
http.Error(w, "Missing newPort parameter", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
convertedPort, err := strconv.Atoi(newPort)
|
||||
if err != nil {
|
||||
http.Error(w, "Could not convert provided port to a valid port number", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
ds.Lock()
|
||||
defer ds.Unlock()
|
||||
|
||||
ds.procManager.RLock()
|
||||
defer ds.procManager.RUnlock()
|
||||
|
||||
instance, exists := ds.cfg.Instances[name]
|
||||
if !exists {
|
||||
http.Error(w, fmt.Sprintf("Cannot modify instance '%s'; Does not exist.", name), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if _, running := ds.procManager.ActiveInstances[name]; running {
|
||||
http.Error(w, fmt.Sprintf("Cannot modify instance '%s'; It is currently running.", name), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
instance.ServerName = newName
|
||||
instance.Port = convertedPort
|
||||
ds.cfg.Instances[name] = instance
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
instanceConfigPath := filepath.Join(ds.cfg.Storage.InstancesDir, name, "serverconfig.json")
|
||||
err = SyncInstanceConfig(instance.Version, instanceConfigPath, instance, ds.cfg)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to sync config: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
fmt.Fprintf(w, "Successfully modified instance %s", name)
|
||||
|
||||
}
|
||||
|
||||
9
main.go
9
main.go
@@ -129,6 +129,15 @@ func main() {
|
||||
|
||||
sendIPCRequest(cfg, "POST", fmt.Sprintf("/instances/delete?name=%s&purge=%s", url.QueryEscape(name), url.QueryEscape(strconv.FormatBool(*purge))), nil)
|
||||
|
||||
case "modify":
|
||||
if len(args) < 4 {
|
||||
log.Fatalf("Usage: vssm modify <instance_name> <new_name> <new_port>")
|
||||
}
|
||||
name := args[1]
|
||||
newName := args[2]
|
||||
newPort := args[3]
|
||||
sendIPCRequest(cfg, "POST", fmt.Sprintf("/instances/modify?name=%s&newName=%s&newPort=%s", url.QueryEscape(name), url.QueryEscape(newName), url.QueryEscape(newPort)), nil)
|
||||
|
||||
default:
|
||||
printUsage()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user