Merge branch 'master' of github.com:trueos/sysadm

This commit is contained in:
Ken Moore
2017-11-22 10:28:44 -05:00
10 changed files with 361 additions and 73 deletions

View File

@@ -10,6 +10,8 @@
#include "EventWatcher.h"
#include "Dispatcher.h"
#include "library/sysadm-update.h"
#include "library/sysadm-sourcectl.h"
QJsonObject Dispatcher::CreateDispatcherEventNotification(QString ID, QJsonObject log, bool full_log){
//NOTE: full_log = true when the process has finished. If it is false, the process is still running and you are probably getting an incremental update of the process log
@@ -79,8 +81,68 @@ QJsonObject Dispatcher::CreateDispatcherEventNotification(QString ID, QJsonObjec
args.insert("impacts_pkgs",QJsonArray::fromStringList(effects));
}
// == sysadm/sourcecrl ==
}else if(ID.startsWith("sysadm_sourcectl")){
if(ID.section("::",0,0)=="sysadm_sourcectl_downloadsource"){
namesp = "sysadm"; name="soucectl";
//No special parsing here: the git output should be available as-is
args.insert("update_log",cLog);
}else if(full_log && ID.section("::",0,0)=="sysadm_sourcectl_downloadsource"){
//qDebug() << "Got update check process finished";
sysadm::sourcectl::saveSourceLog(cLog); //save this for use later
}else if(ID.startsWith("sysadm_sourcectl")){
if(ID.section("::",0,0)=="sysadm_sourcectl_updatesource"){
namesp = "sysadm"; name="soucectl";
//No special parsing here: the git output should be available as-is
args.insert("update_log",cLog);
}else if(full_log && ID.section("::",0,0)=="sysadm_sourcectl_updatesource"){
//qDebug() << "Got update check process finished";
sysadm::sourcectl::saveSourceLog(cLog); //save this for use later
}
}else if(ID.startsWith("sysadm_sourcectl")){
if(ID.section("::",0,0)=="sysadm_sourcectl_deleteports"){
namesp = "sysadm"; name="soucectl";
//No special parsing here: the git output should be available as-is
args.insert("update_log",cLog);
}else if(full_log && ID.section("::",0,0)=="sysadm_sourcectl_deleteports"){
//qDebug() << "Got update check process finished";
sysadm::sourcectl::savePortsLog(cLog); //save this for use later
}
}else if(ID.startsWith("sysadm_sourcectl")){
if(ID.section("::",0,0)=="sysadm_sourcectl_downloadports"){
namesp = "sysadm"; name="soucectl";
//No special parsing here: the git output should be available as-is
args.insert("update_log",cLog);
}else if(full_log && ID.section("::",0,0)=="sysadm_sourcectl_downloadports"){
//qDebug() << "Got update check process finished";
sysadm::sourcectl::savePortsLog(cLog); //save this for use later
}
}else if(ID.startsWith("sysadm_sourcectl")){
if(ID.section("::",0,0)=="sysadm_sourcectl_updateports"){
namesp = "sysadm"; name="soucectl";
//No special parsing here: the git output should be available as-is
args.insert("update_log",cLog);
}else if(full_log && ID.section("::",0,0)=="sysadm_sourcectl_updateports"){
//qDebug() << "Got update check process finished";
sysadm::sourcectl::savePortsLog(cLog); //save this for use later
}
}else if(ID.startsWith("sysadm_sourcectl")){
if(ID.section("::",0,0)=="sysadm_sourcectl_deleteports"){
namesp = "sysadm"; name="soucectl";
//No special parsing here: the git output should be available as-is
args.insert("update_log",cLog);
}else if(full_log && ID.section("::",0,0)=="sysadm_sourcectl_deleteports"){
//qDebug() << "Got update check process finished";
sysadm::sourcectl::savePortsLog(cLog); //save this for use later
}
}
}
//Now assemble the output as needed
if(namesp.isEmpty() || name.isEmpty()){ return QJsonObject(); } //no event
args.insert("event_system",namesp+"/"+name);
return args;
//Now assemble the output as needed
if(namesp.isEmpty() || name.isEmpty() || args.isEmpty()){ return QJsonObject(); } //no event
args.insert("event_system",namesp+"/"+name);

View File

@@ -384,7 +384,7 @@ void EventWatcher::CheckSystemState(){
QDateTime finished = sysadm::Update::rebootRequiredSince();
QDateTime cdt = QDateTime::currentDateTime();
if( (finished.addSecs(60*60*24)<cdt) || cdt.time().hour() == hour){ //more than 24 hours have passed, or time has come
sysadm::SysMgmt::systemReboot();
sysadm::Update::applyUpdates();
}
}
}

View File

@@ -25,6 +25,7 @@
#include "library/sysadm-firewall.h"
#include "library/sysadm-moused.h"
#include "library/sysadm-powerd.h"
#include "library/sysadm-sourcectl.h"
#define DEBUG 0
//#define SCLISTDELIM QString("::::") //SysCache List Delimiter
@@ -45,7 +46,6 @@ RestOutputStruct::ExitCode WebSocket::AvailableSubsystems(bool allaccess, QJsonO
out->insert("sysadm/beadm", "read/write");
}
// - dispatcher (Internal to server - always available)
//"read" is the event notifications, "write" is the ability to queue up jobs
out->insert("rpc/dispatcher", allaccess ? "read/write" : "read");
@@ -104,6 +104,10 @@ RestOutputStruct::ExitCode WebSocket::AvailableSubsystems(bool allaccess, QJsonO
if(QFile::exists("/usr/sbin/powerd")){
out->insert("sysadm/powerd", "read/write");
}
// - sourcectl
if(QFile::exists("/usr/local/bin/git")){
out->insert("sysadm/sourcectl", "read/write");
}
return RestOutputStruct::OK;
}
@@ -164,6 +168,8 @@ RestOutputStruct::ExitCode WebSocket::EvaluateBackendRequest(const RestInputStru
return EvaluateSysadmMousedRequest(IN.args, out);
}else if(namesp=="sysadm" && name=="powerd"){
return EvaluateSysadmPowerdRequest(IN.args, out);
}else if(namesp=="sysadm" && name=="sourcectl"){
return EvaluateSysadmSourceCTLRequest(IN.args, out);
}else{
return RestOutputStruct::BADREQUEST;
}
@@ -606,12 +612,6 @@ RestOutputStruct::ExitCode WebSocket::EvaluateSysadmSystemMgmtRequest(const QJso
ok = true;
out->insert("deviceinfo", sysadm::SysMgmt::systemDevices());
}
else if(act=="fetch_ports"){
ok = true;
QString altdir;
if(keys.contains("ports_dir")){ altdir = in_args.toObject().value("ports_dir").toString(); }
out->insert("fetch_ports", sysadm::SysMgmt::fetchPortsTree(altdir));
}
} //end of "action" key usage
@@ -650,6 +650,10 @@ RestOutputStruct::ExitCode WebSocket::EvaluateSysadmUpdateRequest(const QJsonVal
ok = true;
out->insert("stopupdate", sysadm::Update::stopUpdate() );
}else if(act=="applyupdate"){
ok = true;
out->insert("applyupdate", sysadm::Update::applyUpdates() );
}else if(act=="listsettings"){
ok = true;
out->insert("listsettings", sysadm::Update::readSettings() );
@@ -1329,3 +1333,33 @@ RestOutputStruct::ExitCode WebSocket::EvaluateSysadmPowerdRequest(const QJsonVal
return RestOutputStruct::BADREQUEST;
}
}
// ==== SYSADM SOURCECTL API ====
RestOutputStruct::ExitCode WebSocket::EvaluateSysadmSourceCTLRequest(const QJsonValue in_args, QJsonObject *out){
QString action = in_args.toObject().value("action").toString();
QJsonObject outobj;
if(action == "downloadports"){
outobj = sysadm::sourcectl::downloadports();
}else if(action == "updateports"){
outobj = sysadm::sourcectl::updateports();
}else if(action == "deleteports"){
outobj = sysadm::sourcectl::deleteports();
}else if(action == "stopports"){
outobj = sysadm::sourcectl::stopports();
}else if(action == "downloadsource"){
outobj = sysadm::sourcectl::downloadsource();
}else if(action == "updatesource"){
outobj = sysadm::sourcectl::updatesource();
}else if(action == "deletesource"){
outobj = sysadm::sourcectl::deletesource();
}else if(action == "stopsource"){
outobj = sysadm::sourcectl::stopsource();
//check return structure for validity
if(!outobj.keys().isEmpty()){
out->insert(action, outobj);
return RestOutputStruct::OK;
}else{
return RestOutputStruct::BADREQUEST;
}
}
}

View File

@@ -100,6 +100,8 @@ private:
RestOutputStruct::ExitCode EvaluateSysadmMousedRequest(const QJsonValue in_args, QJsonObject *out);
// -- sysadm powerd API
RestOutputStruct::ExitCode EvaluateSysadmPowerdRequest(const QJsonValue in_args, QJsonObject *out);
// -- sysadm sourcectl API
RestOutputStruct::ExitCode EvaluateSysadmSourceCTLRequest(const QJsonValue in_args, QJsonObject *out);
private slots:
void sendReply(QString msg);

View File

@@ -17,7 +17,9 @@ HEADERS += $${PWD}/sysadm-global.h \
$${PWD}/sysadm-zfs.h \
$${PWD}/sysadm-pkg.h \
$${PWD}/sysadm-moused.h \
$${PWD}/sysadm-powerd.h
$${PWD}/sysadm-powerd.h \
$${PWD}/sysadm-sourcectl.h
SOURCES += $${PWD}/NetDevice.cpp \
$${PWD}/sysadm-general.cpp \
@@ -35,4 +37,5 @@ SOURCES += $${PWD}/NetDevice.cpp \
$${PWD}/sysadm-zfs.cpp \
$${PWD}/sysadm-pkg.cpp \
$${PWD}/sysadm-moused.cpp \
$${PWD}/sysadm-powerd.cpp
$${PWD}/sysadm-powerd.cpp \
$${PWD}/sysadm-sourcectl.cpp

View File

@@ -0,0 +1,209 @@
//===========================================
// TrueOS source code
// Copyright (c) 2017, JT (q5sys)
// Available under the 3-clause BSD license
// See the LICENSE file for full details
//===========================================
#include "sysadm-general.h"
#include "sysadm-sourcectl.h"
#include "sysadm-global.h"
#include "globals.h"
#include <QUuid>
#include "sysadm-general.h"
#include "sysadm-update.h"
#include "sysadm-global.h"
#include "globals.h"
using namespace sysadm;
// ==================
// INLINE FUNCTIONS
// ==================
// =================
// MAIN FUNCTIONS
// =================
QJsonObject sourcectl::downloadsource(){
// cmd that will be run = git clone https://github.com/trueos/freebsd.git /usr/src
QJsonObject retObject;
QString ID = QUuid::createUuid().toString(); // Create a unique ID for this queued action
// Queue the update action
QString cmd;
cmd = "git clone https://github.com/trueos/freebsd.git /usr/src";
DISPATCHER->queueProcess("sysadm_sourcectl_downloadsource::"+ID, cmd);
// Return some details to user that the action was queued
retObject.insert("command", "Downloading TrueOS Source Tree");
retObject.insert("comment", "Task Queued");
retObject.insert("queueid", ID);
return retObject;
}
QJsonObject sourcectl::updatesource(){
// cmd that will be run = git reset --hard && git pull
QJsonObject retObject;
QString ID = QUuid::createUuid().toString(); // Create a unique ID for this queued action
// Queue the update action
QStringList cmds;
cmds << "git reset --hard" << "git pull";
DISPATCHER->queueProcess("sysadm_sourcectl_updatesource::"+ID, cmds, "/usr/src/");
// Return some details to user that the action was queued
retObject.insert("command", "Updating TrueOS Source Tree");
retObject.insert("comment", "Task Queued");
retObject.insert("queueid", ID);
return retObject;
}
QJsonObject sourcectl::deletesource(){
// cmd that will be run = rm -rf /usr/src/
QJsonObject retObject;
QString ID = QUuid::createUuid().toString(); // Create a unique ID for this queued action
// Queue the update action
QString cmd;
cmd = "rm -rf /usr/src/";
DISPATCHER->queueProcess("sysadm_sourcectl_deletesource::"+ID, cmd);
// Return some details to user that the action was queued
retObject.insert("command", "Deleting TrueOS Source Tree");
retObject.insert("comment", "Task Queued");
retObject.insert("queueid", ID);
return retObject;
}
QJsonObject sourcectl::stopsource(){}
QJsonObject sourcectl::downloadports(){
// cmd that will be run = git clone https://github.com/trueos/freebsd-ports.git /usr/ports
QJsonObject retObject;
QString ID = QUuid::createUuid().toString(); // Create a unique ID for this queued action
// Queue the update action
QString cmd;
cmd = "git clone https://github.com/trueos/freebsd-ports.git /usr/ports";
DISPATCHER->queueProcess("sysadm_sourcectl_downloadports::"+ID, cmd);
// Return some details to user that the action was queued
retObject.insert("command", "Downloading TrueOS PortsTree");
retObject.insert("comment", "Task Queued");
retObject.insert("queueid", ID);
return retObject;
}
QJsonObject sourcectl::updateports(){
// cmd that will be run = git reset --hard && git pull
QJsonObject retObject;
QString ID = QUuid::createUuid().toString(); // Create a unique ID for this queued action
// Queue the update action
QStringList cmds;
cmds << "git reset --hard" << "git pull";
DISPATCHER->queueProcess("sysadm_sourcectl_updateports::"+ID, cmds, "/usr/ports/");
// Return some details to user that the action was queued
retObject.insert("command", "Updating TrueOS PortsTree");
retObject.insert("comment", "Task Queued");
retObject.insert("queueid", ID);
return retObject;
}
QJsonObject sourcectl::deleteports(){
// cmd that will be run = rm -rf /usr/ports/
QJsonObject retObject;
QString ID = QUuid::createUuid().toString(); // Create a unique ID for this queued action
// Queue the update action
QString cmd;
cmd = "rm -rf /usr/ports/";
DISPATCHER->queueProcess("sysadm_sourcectl_deleteports::"+ID, cmd);
// Return some details to user that the action was queued
retObject.insert("command", "Deleting TrueOS PortsTree");
retObject.insert("comment", "Task Queued");
retObject.insert("queueid", ID);
return retObject;
}
QJsonObject sourcectl::stopports(){}
void sourcectl::saveSourceLog(QString){}
void sourcectl::savePortsLog(QString){}
/*
git clone https://github.com/trueos/freebsd.git /usr/src
git clone https://github.com/trueos/freebsd-ports.git /usr/ports
git reset --hard && git pull
rm -rf /usr/src/
rm -rf /usr/ports/
QJsonObject SysMgmt::fetchPortsTree(QString altDir){
//void SysMgmt::fetchPortsTree(QStringList &cmds, QStringList &dirs){
QJsonObject out;
if(altDir.isEmpty()){ altDir = "/usr/ports"; }
//Does Ports tree exist? If not create it.
if(!QFile::exists(altDir)){
QDir dir;
if(!dir.mkpath(altDir) ){
out.insert("error","Could not create directory: "+altDir);
return out;
}
}
//Does a local git repo exist? If not create it.
QString URL = "https://www.github.com/trueos/freebsd-ports.git";
if(QFile::exists(altDir+"/.git")){
//Check if the remote URL is correct
QString origin = General::gitCMD(altDir, "git remote show -n origin").filter("Fetch URL:").join("").section("URL:",1,30).simplified();
if(origin != URL){
General::gitCMD(altDir,"git",QStringList() << "remote" << "remove" << "origin");
General::gitCMD(altDir,"git", QStringList() << "remote" << "add" << "origin" << URL);
}
}else{
//new GIT setup
General::emptyDir(altDir);
General::gitCMD(altDir, "git", QStringList() << "init" << altDir );
General::gitCMD(altDir, "git", QStringList() << "remote" << "add" << "origin" << URL );
}
//Now update the tree with git
QString ID = "system_fetch_ports_tree";
DISPATCHER->queueProcess(ID, "git pull origin", altDir );
out.insert("result","process_started");
out.insert("process_id",ID);
return out;
}
/*void SysMgmt::fetchSourceTree(QString branch, QStringList &cmds, QStringList &dirs, QStringList &info){
//Clear the output variables
cmds.clear(); dirs.clear();
//Check if the source directory even exists
if(!QFile::exists("/usr/src")){
cmds << "mkdir /usr/src"; dirs << ""; //Create the ports tree
}
//Now check if the git directory exists
QString URL = "https://www.github.com/pcbsd/freebsd.git";
if(QFile::exists("/usr/src/.git")){
//Check if the remote URL is correct
QString origin = General::gitCMD("/usr/src", "git remote show -n origin").filter("Fetch URL:").join("").section("URL:",1,30).simplified();
if(origin != URL){
cmds << "git remote remove origin"; dirs <<"/usr/src";
cmds << "git remote add origin "+URL; dirs << "/usr/src/.git"; //setup PC-BSD git repo
}
}else{
//new GIT setup
General::emptyDir("/usr/src");
cmds << "git init"; dirs << "/usr/src"; //setup git
cmds << "git remote add origin "+URL; dirs << "/usr/src/.git"; //setup PC-BSD git repo
}
//Now update the tree with git
cmds << "git fetch --depth=1"; dirs << "/usr/src/.git";
cmds << "git checkout "+branch; dirs << "/usr/src";
}
*/

View File

@@ -0,0 +1,34 @@
//===========================================
// TrueOS source code
// Copyright (c) 2017, JT (q5sys)
// Available under the 3-clause BSD license
// See the LICENSE file for full details
//===========================================
#ifndef __PCBSD_LIB_UTILS_SOURCECTL_H
#define __PCBSD_LIB_UTILS_SOURCECTL_H
#include <QJsonObject>
#include "sysadm-global.h"
namespace sysadm{
class sourcectl{
public:
static QJsonObject downloadports();
static QJsonObject updateports();
static QJsonObject deleteports();
static QJsonObject stopports();
static QJsonObject downloadsource();
static QJsonObject updatesource();
static QJsonObject deletesource();
static QJsonObject stopsource();
static void saveSourceLog(QString);
static void savePortsLog(QString);
};
} //end of sysadm namespace
#endif

View File

@@ -455,66 +455,3 @@ QJsonObject SysMgmt::systemDevices(){
return pciconf_info;
}
// Source Management
QJsonObject SysMgmt::fetchPortsTree(QString altDir){
//void SysMgmt::fetchPortsTree(QStringList &cmds, QStringList &dirs){
QJsonObject out;
if(altDir.isEmpty()){ altDir = "/usr/ports"; }
//Does Ports tree exist? If not create it.
if(!QFile::exists(altDir)){
QDir dir;
if(!dir.mkpath(altDir) ){
out.insert("error","Could not create directory: "+altDir);
return out;
}
}
//Does a local git repo exist? If not create it.
QString URL = "https://www.github.com/trueos/freebsd-ports.git";
if(QFile::exists(altDir+"/.git")){
//Check if the remote URL is correct
QString origin = General::gitCMD(altDir, "git remote show -n origin").filter("Fetch URL:").join("").section("URL:",1,30).simplified();
if(origin != URL){
General::gitCMD(altDir,"git",QStringList() << "remote" << "remove" << "origin");
General::gitCMD(altDir,"git", QStringList() << "remote" << "add" << "origin" << URL);
}
}else{
//new GIT setup
General::emptyDir(altDir);
General::gitCMD(altDir, "git", QStringList() << "init" << altDir );
General::gitCMD(altDir, "git", QStringList() << "remote" << "add" << "origin" << URL );
}
//Now update the tree with git
QString ID = "system_fetch_ports_tree";
DISPATCHER->queueProcess(ID, "git pull origin", altDir );
out.insert("result","process_started");
out.insert("process_id",ID);
return out;
}
/*void SysMgmt::fetchSourceTree(QString branch, QStringList &cmds, QStringList &dirs, QStringList &info){
//Clear the output variables
cmds.clear(); dirs.clear();
//Check if the source directory even exists
if(!QFile::exists("/usr/src")){
cmds << "mkdir /usr/src"; dirs << ""; //Create the ports tree
}
//Now check if the git directory exists
QString URL = "https://www.github.com/pcbsd/freebsd.git";
if(QFile::exists("/usr/src/.git")){
//Check if the remote URL is correct
QString origin = General::gitCMD("/usr/src", "git remote show -n origin").filter("Fetch URL:").join("").section("URL:",1,30).simplified();
if(origin != URL){
cmds << "git remote remove origin"; dirs <<"/usr/src";
cmds << "git remote add origin "+URL; dirs << "/usr/src/.git"; //setup PC-BSD git repo
}
}else{
//new GIT setup
General::emptyDir("/usr/src");
cmds << "git init"; dirs << "/usr/src"; //setup git
cmds << "git remote add origin "+URL; dirs << "/usr/src/.git"; //setup PC-BSD git repo
}
//Now update the tree with git
cmds << "git fetch --depth=1"; dirs << "/usr/src/.git";
cmds << "git checkout "+branch; dirs << "/usr/src";
}*/

View File

@@ -267,6 +267,12 @@ QJsonObject Update::stopUpdate() {
return ret;
}
QJsonObject Update::applyUpdates(){
QJsonObject ret;
QProcess::startDetached("pc-updatemanager startupdate");
ret.insert("result","rebooting to complete updates");
return ret;
}
//SETTINGS OPTIONS
QJsonObject Update::readSettings(){
QJsonObject ret;

View File

@@ -25,6 +25,7 @@ public:
//Start/stop update routine
static QJsonObject startUpdate(QJsonObject);
static QJsonObject stopUpdate();
static QJsonObject applyUpdates();
//Read/write update settings
static QJsonObject readSettings();
static QJsonObject writeSettings(QJsonObject);