API CHANGE

Modify the output fields for the sysadm/services, "list_services" action:
Now each service entry will look like this:
"accounting": {
        "description": "",
        "is_enabled": "false",
        "name": "accounting",
        "path": "/etc/rc.d/accounting",
        "tag": "accounting_enable"
      }

I will probably be adding an "is_running" [true/false] field here soon as well - the backend for that still needs to be written first.
This commit is contained in:
Ken Moore
2016-08-29 14:09:20 -04:00
parent 1bf0ec425f
commit 2f7bfe06aa
3 changed files with 84 additions and 1 deletions

View File

@@ -1004,11 +1004,15 @@ RestOutputStruct::ExitCode WebSocket::EvaluateSysadmServiceRequest(const QJsonVa
sysadm::ServiceManager SMGR;
if(action=="list_services"){
QList<sysadm::Service> list = SMGR.GetServices();
QList<bool> listEnabled = SMGR.isEnabled(list);
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("path", list[i].Path);
S.insert("description", list[i].Description);
S.insert("is_enabled", listEnabled[i] ? "true" : "false" );
//S.insert("filename", list[i].Directory);
//Need to add status info as well (isRunning, isEnabled);
services.insert(list[i].Name, S);

View File

@@ -31,6 +31,73 @@ QList<Service> ServiceManager::GetServices()
return services;
}
QList<bool> ServiceManager::isRunning(QList<Service> services){
//return list in the same order as the input list
QList<bool> out;
for(int i=0; i<services.length(); i++){
out << false; //TO-DO - need to figure out a way to detect process status
}
return out;
}
bool ServiceManager::isRunning(Service service){ //single-item overload
QList<bool> run = isRunning( QList<Service>() << service);
if(!run.isEmpty()){ return run.first(); }
else{ return false; }
}
QList<bool> ServiceManager::isEnabled(QList<Service> services){
//return list in the same order as the input list
QList<bool> out;
//Read all the rc.conf files in highest-priority order
QHash<QString, QString> data;
QDir dir("/etc");
QStringList confs = dir.entryList(QStringList() << "rc.conf*", QDir::Files, QDir::Name | QDir::Reversed);
qDebug() << "Conf file order:" << confs;
for(int i=0; i<confs.length(); i++){
QFile file(dir.absoluteFilePath(confs[i]));
if( file.open(QIODevice::ReadOnly) ){
qDebug() << "Read File:" << confs[i];
bool insection = true;
QTextStream stream(&file);
while(!stream.atEnd()){
QString info = stream.readLine();
//qDebug() << "Read Line:" << info;
if(info.contains("=") && insection){
data.insert(info.section("=",0,0).simplified(), info.section("=",1,-1));
qDebug() << "Got data entry:" << info;
}else if(info.simplified()=="fi"){
insection= true;
}else if(info.simplified().startsWith("if [ ") ){
QStringList args = info.section("]",0,0).section("[",1,-1).split("\""); //odd numbers are files, even are arguments
for(int j=0; j<args.length()-1; j+=2){
qDebug() << "Check if arguments:" << args[j] << args[j+1] << insection;
if(!args[j].contains("-e")){ insection = false; break; } //don't know how to handle this - skip section
if(args[j].contains("-o")){ insection == insection || QFile::exists(args[j+1]); }
else if(args[j].contains("-a")){ insection == insection && QFile::exists(args[j+1]); }
else{ insection = QFile::exists(args[j+1]); } //typically the first argument check
qDebug() << " - Now:" << insection;
} //end loop over existance arguments
}
}//end loop over lines
file.close();
}
}
//Now go through the list of services and report which ones are enabled
for(int i=0; i<services.length(); i++){
bool enabled = false;
if(data.contains(services[i].Tag)){ enabled = data.value(services[i].Tag)=="\"YES\""; }
out << enabled;
}
return out;
}
bool ServiceManager::isEnabled(Service service){ //single-item overload
QList<bool> use = isEnabled( QList<Service>() << service);
if(!use.isEmpty()){ return use.first(); }
else{ return false; }
}
void ServiceManager::Start(Service service)
{
// Start the process
@@ -120,7 +187,8 @@ Service ServiceManager::loadServices(QString name)
if ( file.open( QIODevice::ReadOnly ) )
{
valid=false;
service.Directory=directory[i];
service.Directory=directory[i]; //filename only
service.Path = dir+"/"+directory[i]; //full path w/ filename
QTextStream stream( &file );
stream.setCodec("UTF-8");
QString line;
@@ -159,6 +227,9 @@ Service ServiceManager::loadServices(QString name)
service.Tag=service.Tag + "_enable";
break;
}
if (line.simplified().startsWith("desc=")) {
service.Description = line.section("=\"",1,-1).section("\"",0,0);
}
}
file.close();

View File

@@ -16,6 +16,8 @@ struct Service{
QString Name;
QString Tag;
QString Directory;
QString Path;
QString Description;
};
class ServiceManager
@@ -40,6 +42,12 @@ public:
*/
QList<Service> GetServices();
QList<bool> isRunning(QList<Service> services); //return list in the same order as the input list
bool isRunning(Service service); //single-item overload
QList<bool> isEnabled(QList<Service> services); //return list in the same order as the input list
bool isEnabled(Service service); //single-item overload
/**
* @brief Start starts a service
* @param service the service to start