Make sure the sysadm/pkg "list_categories" API call only returns non-empty categories. Also add most of the backend systems for additional pkg-modification calls (no API for them yet - that is coming soon).

This commit is contained in:
Ken Moore
2016-03-28 09:54:47 -04:00
parent 5e0cc42ab2
commit a5a34512c9
4 changed files with 176 additions and 1 deletions

View File

@@ -43,6 +43,20 @@ QJsonObject Dispatcher::CreateDispatcherEventNotification(QString ID, QJsonObjec
namesp = "sysadm"; name="update";
//No special parsing here: the pc-updatemanager output should be available as-is
args.insert("update_log",cLog);
// == sysadm/pkg ==
}else if(ID.startsWith("sysadm_pkg")){
namesp = "sysadm"; name="pkg";
//most pkg commands have no special parsing the pkg output should be available as-is
args.insert("pkg_log",cLog);
args.insert("action", ID.section("-",0,0).section("_pkg_",-1) ); //so the client/user can tell which type of pkg action this is for
if(ID.section("-",0,0)=="sysadm_pkg_check_upgrade"){
if(isFinished){
bool hasupdates = !cLog.section("\n",-1,QString::SectionSkipEmpty).contains("packages are up to date");
args.insert("updates_available", hasupdates ? "true" : "false");
}
}
}
//Now assemble the output as needed

View File

@@ -40,6 +40,7 @@
#include <QFileSystemWatcher>
#include <QQueue>
#include <QRegExp>
#include <QUuid>
#include <QList>
#include <QHash>

View File

@@ -225,6 +225,15 @@ QStringList PKG::pkg_search(QString repo, QString searchterm, QString category){
while(query.next()){
found << query.value("origin").toString(); //need the origin for later
}
if(found.isEmpty()){
//Expand the search to names containing the term
q_string = "SELECT origin FROM packages WHERE name LIKE '%"+searchterm+"%'";
if(!category.isEmpty()){ q_string.append(" AND origin LIKE '"+category+"/%'"); }
QSqlQuery q2(q_string);
while(q2.next()){
found << q2.value("origin").toString(); //need the origin for later
}
}
if(found.isEmpty()){
//Expand the search to comments
q_string = "SELECT origin FROM packages WHERE comment LIKE '%"+searchterm+"%'";
@@ -275,12 +284,23 @@ QJsonArray PKG::list_categories(QString repo){
//qDebug() << " - could not be opened";
return QJsonArray();
}
//Get all the pkg origins for this repo
QStringList origins;
QSqlQuery q_o("SELECT origin FROM packages");
while(q_o.next()){
origins << q_o.value("origin").toString(); //need the origin for later
}
//Now get all the categories
QString q_string = "SELECT name FROM categories";
QSqlQuery query(q_string);
QStringList found;
while(query.next()){
found << query.value("name").toString(); //need the origin for later
}
//Now check all the categories to ensure that pkgs exist within it
for(int i=0; i<found.length(); i++){
if(origins.filter(found[i]+"/").isEmpty()){ found.removeAt(i); i--; }
}
if(!found.isEmpty()){ return QJsonArray::fromStringList(found); }
else{ return QJsonArray(); }
}
@@ -299,4 +319,129 @@ QJsonArray PKG::list_repos(){
}
}
return QJsonArray::fromStringList(found);
}
}
//=================
//pkg modification routines (dispatcher events for notifications)
//=================
QJsonObject PKG::pkg_install(QStringList origins, QString repo){
//Generate the command to run
QString cmd = "pkg install -y %1";
if(!repo.isEmpty() && repo!="local"){ cmd = cmd.arg("--repository \""+repo+"\" %1"); }
cmd = cmd.arg( origins.join(" ") );
//Now kick off the dispatcher process (within the pkg queue - since only one pkg process can run at a time)
QString ID = "sysadm_pkg_install-"+QUuid::createUuid().toString(); //create a random tag for the process
DISPATCHER->queueProcess(Dispatcher::PKG_QUEUE, ID, cmd);
//Now return the info about the process
QJsonObject obj;
obj.insert("status", "pending");
obj.insert("proc_cmd",cmd);
obj.insert("proc_id",ID);
return obj;
}
QJsonObject PKG::pkg_remove(QStringList origins, bool recursive){
//Generate the command to run
QString cmd = "pkg delete -y %1";
if(recursive){ cmd = cmd.arg("-R %1"); } //also remove all packages which depend on these pkgs
cmd = cmd.arg( origins.join(" ") );
//Now kick off the dispatcher process (within the pkg queue - since only one pkg process can run at a time)
QString ID = "sysadm_pkg_remove-"+QUuid::createUuid().toString(); //create a random tag for the process
DISPATCHER->queueProcess(Dispatcher::PKG_QUEUE, ID, cmd);
//Now return the info about the process
QJsonObject obj;
obj.insert("status", "pending");
obj.insert("proc_cmd",cmd);
obj.insert("proc_id",ID);
return obj;
}
QJsonObject PKG::pkg_lock(QStringList origins){
//Generate the command to run
QString cmd = "pkg lock -y %1";
cmd = cmd.arg( origins.join(" ") );
//Now kick off the dispatcher process (within the pkg queue - since only one pkg process can run at a time)
QString ID = "sysadm_pkg_lock-"+QUuid::createUuid().toString(); //create a random tag for the process
DISPATCHER->queueProcess(Dispatcher::PKG_QUEUE, ID, cmd);
//Now return the info about the process
QJsonObject obj;
obj.insert("status", "pending");
obj.insert("proc_cmd",cmd);
obj.insert("proc_id",ID);
return obj;
}
QJsonObject PKG::pkg_unlock(QStringList origins){
//Generate the command to run
QString cmd = "pkg unlock -y %1";
cmd = cmd.arg( origins.join(" ") );
//Now kick off the dispatcher process (within the pkg queue - since only one pkg process can run at a time)
QString ID = "sysadm_pkg_unlock-"+QUuid::createUuid().toString(); //create a random tag for the process
DISPATCHER->queueProcess(Dispatcher::PKG_QUEUE, ID, cmd);
//Now return the info about the process
QJsonObject obj;
obj.insert("status", "pending");
obj.insert("proc_cmd",cmd);
obj.insert("proc_id",ID);
return obj;
}
//==================
//pkg administration routines
//==================
QJsonObject PKG::pkg_update(bool force){
//Generate the command to run
QString cmd = "pkg update";
if(force){ cmd.append(" -f"); }
//Now kick off the dispatcher process (within the pkg queue - since only one pkg process can run at a time)
QString ID = "sysadm_pkg_update-"+QUuid::createUuid().toString(); //create a random tag for the process
DISPATCHER->queueProcess(Dispatcher::PKG_QUEUE, ID, cmd);
//Now return the info about the process
QJsonObject obj;
obj.insert("status", "pending");
obj.insert("proc_cmd",cmd);
obj.insert("proc_id",ID);
return obj;
}
QJsonObject PKG::pkg_check_upgrade(){
//Generate the command to run
QString cmd = "pkg upgrade -n";
//Now kick off the dispatcher process (within the pkg queue - since only one pkg process can run at a time)
QString ID = "sysadm_pkg_check_upgrade-"+QUuid::createUuid().toString(); //create a random tag for the process
DISPATCHER->queueProcess(Dispatcher::PKG_QUEUE, ID, cmd);
//Now return the info about the process
QJsonObject obj;
obj.insert("status", "pending");
obj.insert("proc_cmd",cmd);
obj.insert("proc_id",ID);
return obj;
}
QJsonObject PKG::pkg_upgrade(){
//Generate the command to run
QString cmd = "pkg upgrade -y";
//Now kick off the dispatcher process (within the pkg queue - since only one pkg process can run at a time)
QString ID = "sysadm_pkg_upgrade-"+QUuid::createUuid().toString(); //create a random tag for the process
DISPATCHER->queueProcess(Dispatcher::PKG_QUEUE, ID, cmd);
//Now return the info about the process
QJsonObject obj;
obj.insert("status", "pending");
obj.insert("proc_cmd",cmd);
obj.insert("proc_id",ID);
return obj;
}
QJsonObject PKG::pkg_audit(){
//Generate the command to run
QString cmd = "pkg audit -qr";
//Now kick off the dispatcher process (within the pkg queue - since only one pkg process can run at a time)
QString ID = "sysadm_pkg_audit-"+QUuid::createUuid().toString(); //create a random tag for the process
DISPATCHER->queueProcess(Dispatcher::PKG_QUEUE, ID, cmd);
//Now return the info about the process
QJsonObject obj;
obj.insert("status", "pending");
obj.insert("proc_cmd",cmd);
obj.insert("proc_id",ID);
return obj;
}

View File

@@ -14,14 +14,29 @@
#include <QSqlIndex>
#include <QSqlRecord>
#include <QSqlQuery>
namespace sysadm{
class PKG{
public:
//Information fetch routines
static QJsonObject pkg_info(QStringList origins, QString repo, QString category = "", bool fullresults = true);
static QStringList pkg_search(QString repo, QString searchterm, QString category = "");
static QJsonArray list_categories(QString repo);
static QJsonArray list_repos();
//pkg modification routines (dispatcher events for notifications)
static QJsonObject pkg_install(QStringList origins, QString repo);
static QJsonObject pkg_remove(QStringList origins, bool recursive = true);
static QJsonObject pkg_lock(QStringList origins); //local/installed pkgs only
static QJsonObject pkg_unlock(QStringList origins); //local/installed pkgs only
//pkg administration routines
static QJsonObject pkg_update(bool force = false); //update databases
static QJsonObject pkg_check_upgrade(); //Check for updates to pkgs
static QJsonObject pkg_upgrade(); //upgrade all pkgs (use sysadm/updates if possible instead)
static QJsonObject pkg_audit(); //List details of vulnerable packages
};
} //end of sysadm namespace