API CHANGE

Add a new API class/call to sysadm:
namespace: sysadm
name: services
This class is for managing all the background daemons on the system.

Initial API call:
args : {"action" : "list_services" }
This will return a list of all services available on the system.

*Note: return message shortened for example purposes - there are usually tons of services available

REST Request (example):
-------------------------------
PUT /sysadm/services
{
   "action" : "list_services"
}

WebSocket Request:
-------------------------------
{
   "args" : {
      "action" : "list_services"
   },
   "id" : "fooid",
   "namespace" : "sysadm",
   "name" : "services"
}

Response:
-------------------------------
{
  "args": {
    "services": {
      "accounting": {
        "name": "accounting",
        "tag": "accounting_enable"
      },
      "addswap": {
        "name": "addswap",
        "tag": "addswap_enable"
      },
      "amd": {
        "name": "amd",
        "tag": "amd_enable"
      },
      "apm": {
        "name": "apm",
        "tag": "apm_enable"
      },
      "apmd": {
        "name": "apmd",
        "tag": "apmd_enable"
      },
      "atm": {
        "name": "atm",
        "tag": "atm_enable"
      }
    }
  },
  "id": "fooid",
  "name": "response",
  "namespace": "sysadm"
}
This commit is contained in:
Ken Moore
2016-08-29 11:41:15 -04:00
parent c24bc90769
commit 1bf0ec425f
4 changed files with 58 additions and 16 deletions

View File

@@ -21,7 +21,7 @@
#include "library/sysadm-zfs.h"
#include "library/sysadm-pkg.h"
#include "library/sysadm-users.h"
#include "library/sysadm-servicemanager.h"
#define DEBUG 0
@@ -89,6 +89,8 @@ RestOutputStruct::ExitCode WebSocket::AvailableSubsystems(bool allaccess, QJsonO
// - User Manager
out->insert("sysadm/users","read/write");
//- Service Manager
out->insert("sysadm/services","read/write");
return RestOutputStruct::OK;
}
@@ -141,6 +143,8 @@ RestOutputStruct::ExitCode WebSocket::EvaluateBackendRequest(const RestInputStru
return EvaluateSysadmPkgRequest(IN.args, out);
}else if(namesp=="sysadm" && name=="users"){
return EvaluateSysadmUserRequest(IN.fullaccess, AUTHSYSTEM->userForToken(SockAuthToken), IN.args, out);
}else if(namesp=="sysadm" && name=="services"){
return EvaluateSysadmServiceRequest(IN.args, out);
}else{
return RestOutputStruct::BADREQUEST;
}
@@ -992,3 +996,26 @@ RestOutputStruct::ExitCode WebSocket::EvaluateSysadmUserRequest(bool allaccess,
return (ok ? RestOutputStruct::OK : RestOutputStruct::BADREQUEST);
}
// SERVICE MANAGER (sysadm/services)
RestOutputStruct::ExitCode WebSocket::EvaluateSysadmServiceRequest(const QJsonValue in_args, QJsonObject *out){
bool ok = false;
QString action = in_args.toObject().value("action").toString();
sysadm::ServiceManager SMGR;
if(action=="list_services"){
QList<sysadm::Service> list = SMGR.GetServices();
QJsonObject services;
for(int i=0; i<list.length(); i++){
QJsonObject S;
S.insert("name", list[i].Name);
S.insert("tag", list[i].Tag);
//S.insert("filename", list[i].Directory);
//Need to add status info as well (isRunning, isEnabled);
services.insert(list[i].Name, S);
}
ok = true;
out->insert("services",services);
}
return (ok ? RestOutputStruct::OK : RestOutputStruct::BADREQUEST);
}

View File

@@ -92,7 +92,9 @@ private:
RestOutputStruct::ExitCode EvaluateSysadmPkgRequest(const QJsonValue in_args, QJsonObject *out);
// -- sysadm User Mgmt API
RestOutputStruct::ExitCode EvaluateSysadmUserRequest(bool allaccess, QString user, const QJsonValue in_args, QJsonObject *out);
// -- sysadm Service Manager API
RestOutputStruct::ExitCode EvaluateSysadmServiceRequest(const QJsonValue in_args, QJsonObject *out);
private slots:
void sendReply(QString msg);
void checkConnection(); //see if the current connection is still open/valid

View File

@@ -1,25 +1,33 @@
#include "sysadm-servicemanager.h"
#include "sysadm-general.h"
#include <QFile>
#include <QDir>
using namespace sysadm;
ServiceManager::ServiceManager(QString chroot, QString ip)
{
this->chroot = chroot;
this->ip = ip;
loadServices();
//loadServices();
}
Service ServiceManager::GetService(QString serviceName)
{
for(Service service : services)
{
if(service.Name == serviceName)
return service;
if(!services.isEmpty()){
for(int i=0; i<services.length(); i++){
if(services[i].Name == serviceName)
return services[i];
}
return Service();
}else{
return loadServices(serviceName);
}
return Service(); //no service found
}
QVector<Service> ServiceManager::GetServices()
QList<Service> ServiceManager::GetServices()
{
if(services.isEmpty()){ loadServices(); }
return services;
}
@@ -83,7 +91,7 @@ void ServiceManager::Disable(Service service)
General::setConfFileValue( chroot + "/etc/rc.conf", service.Tag, service.Tag + "=\"NO\"", -1);
}
void ServiceManager::loadServices()
Service ServiceManager::loadServices(QString name)
{
QString tmp;
bool valid;
@@ -101,10 +109,11 @@ void ServiceManager::loadServices()
directory.setSorting( QDir::Name );
if ( directory.count() == 0 )
return;
return Service();
for (unsigned int i = 0; i < directory.count(); i++ )
{
if(!name.isEmpty() && directory[i]!=name){ continue; } //not the right service - go to the next one
service = Service();
QFile file( dir + "/" + directory[i] );
@@ -163,10 +172,11 @@ void ServiceManager::loadServices()
service.Tag = service.Directory + "_enable";
if ( service.Name.indexOf("$") == 0 )
service.Name = service.Directory;
if(!name.isEmpty() ){ return service; } //found the requested service - return it
services << service;
//qDebug() << "Added Service:" << cDir << service.Directory << service.Name << service.Tag;
}
}
}
return Service();
}

View File

@@ -1,6 +1,9 @@
#ifndef SERVICEMANAGER_H
#define SERVICEMANAGER_H
#include <QtCore>
#include <QString>
#include <QList>
namespace sysadm{
struct Service{
Service()
@@ -35,7 +38,7 @@ public:
* @brief GetServices getter for the vector of services
* @return returns the vector of services on the system
*/
QVector<Service> GetServices();
QList<Service> GetServices();
/**
* @brief Start starts a service
@@ -64,8 +67,8 @@ public:
*/
void Disable(Service service);
private:
QVector<Service> services;
void loadServices();
QList<Service> services;
Service loadServices(QString service = ""); //Return struct is optional - only used for a single service search
QString chroot;
QString ip;
};