From 8a594044bbd5086a8f26891bad78ad22d18bb3b5 Mon Sep 17 00:00:00 2001 From: Ken Moore Date: Tue, 30 Aug 2016 11:27:51 -0400 Subject: [PATCH] API CHANGE Add 3 new API calls (all almost the same - just different "actions" and the return message will be slightly different) "action":"start" OR "stop" OR "restart" This will [start/stop/restart] services on the system. REQUIRED ARGUMENTS: "services" : EXAMPLE "start" command (change "services_started" in responce to "services_[started/stopped/restarted]" as needed to match the action: REST Request (example): ------------------------------- PUT /sysadm/services { "action" : "start", "services" : [ "cupsd" ] } WebSocket Request: ------------------------------- { "args" : { "action" : "start", "services" : [ "cupsd" ] }, "name" : "services", "id" : "fooid", "namespace" : "sysadm" } Response: ------------------------------- { "args": { "services_started": [ "cupsd" ] }, "id": "fooid", "name": "response", "namespace": "sysadm" } --- src/server/WebBackend.cpp | 43 ++++++++++++++++++++ src/server/library/sysadm-servicemanager.cpp | 5 +++ 2 files changed, 48 insertions(+) diff --git a/src/server/WebBackend.cpp b/src/server/WebBackend.cpp index d5e0f5b..d9e25b6 100644 --- a/src/server/WebBackend.cpp +++ b/src/server/WebBackend.cpp @@ -1019,7 +1019,50 @@ RestOutputStruct::ExitCode WebSocket::EvaluateSysadmServiceRequest(const QJsonVa } ok = true; out->insert("services",services); + + }else if(action=="start" && in_args.toObject().contains("services") ){ + QJsonValue sval = in_args.toObject().value("services"); + QStringList services; + if(sval.isString()){ services << sval.toString(); } + else if(sval.isArray()){ services = JsonArrayToStringList(sval.toArray()); } + if(!services.isEmpty()){ + QStringList success; + ok = true; + for(int i=0; iinsert("services_started", QJsonArray::fromStringList(success)); + } + + }else if(action=="stop" && in_args.toObject().contains("services") ){ + QJsonValue sval = in_args.toObject().value("services"); + QStringList services; + if(sval.isString()){ services << sval.toString(); } + else if(sval.isArray()){ services = JsonArrayToStringList(sval.toArray()); } + if(!services.isEmpty()){ + QStringList success; + ok = true; + for(int i=0; iinsert("services_stopped", QJsonArray::fromStringList(success)); + } + }else if(action=="restart" && in_args.toObject().contains("services") ){ + QJsonValue sval = in_args.toObject().value("services"); + QStringList services; + if(sval.isString()){ services << sval.toString(); } + else if(sval.isArray()){ services = JsonArrayToStringList(sval.toArray()); } + if(!services.isEmpty()){ + QStringList success; + ok = true; + for(int i=0; iinsert("services_restarted", QJsonArray::fromStringList(success)); + } } + + if(out->keys().isEmpty()){ if(ok){ out->insert("result","success"); } else{ out->insert("error","error"); } diff --git a/src/server/library/sysadm-servicemanager.cpp b/src/server/library/sysadm-servicemanager.cpp index 13625da..5cfcf7b 100644 --- a/src/server/library/sysadm-servicemanager.cpp +++ b/src/server/library/sysadm-servicemanager.cpp @@ -67,6 +67,7 @@ bool ServiceManager::isEnabled(Service service){ //single-item overload bool ServiceManager::Start(Service service) { + if(service.Directory.isEmpty()){ return false; } // Start the process QString prog; QStringList args; @@ -84,6 +85,7 @@ bool ServiceManager::Start(Service service) bool ServiceManager::Stop(Service service) { + if(service.Directory.isEmpty()){ return false; } // Start the process QString prog; QStringList args; @@ -101,6 +103,7 @@ bool ServiceManager::Stop(Service service) bool ServiceManager::Restart(Service service) { + if(service.Directory.isEmpty()){ return false; } QString prog; QStringList args; bool once = !isEnabled(service); @@ -117,11 +120,13 @@ bool ServiceManager::Restart(Service service) void ServiceManager::Enable(Service service) { + if(service.Tag.isEmpty()){ return; } General::setConfFileValue( chroot + "/etc/rc.conf", service.Tag, service.Tag + "=\"YES\"", -1); } void ServiceManager::Disable(Service service) { + if(service.Tag.isEmpty()){ return; } General::setConfFileValue( chroot + "/etc/rc.conf", service.Tag, service.Tag + "=\"NO\"", -1); }