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

This commit is contained in:
Ken Moore
2017-07-03 10:36:52 -04:00
4 changed files with 145 additions and 1 deletions

View File

@@ -10,6 +10,10 @@
#include "sysadm-global.h"
using namespace sysadm;
#define PREFIX QString("/usr/local")
QString TRUEOS_ETCCONF(PREFIX + "/etc/trueos.conf"); // The default trueos.conf file
//=================
// RunCommand() variations
//=================
@@ -54,6 +58,21 @@ bool General::RunQuickCommand(QString command, QStringList arguments,QString wor
return success;
}
QStringList General::gitCMD(QString dir, QString cmd, QStringList args){
//Run a quick command in the proper dir and return the output
QProcess proc;
proc.setProcessChannelMode(QProcess::MergedChannels);
if( !dir.isEmpty() && QFile::exists(dir) ){ proc.setWorkingDirectory(dir); }
if(args.isEmpty()){ proc.start(cmd); }
else{ proc.start(cmd, args); }
while(!proc.waitForFinished(300)){ QCoreApplication::processEvents(); }
QStringList out;
while(proc.canReadLine()){
out << QString( proc.readLine() );
}
return out;
}
//=================
// TEXT FILE INTERACTION
//=================
@@ -241,6 +260,30 @@ bool General::setConfFileValue(QString fileName, QString oldKey, QString newKey,
return true;
}
QString General::getValFromTrueOSConf(QString key) {
return getValFromTOConf(TRUEOS_ETCCONF, key);
}
QString General::getValFromTOConf(QString conf, QString key) {
// Load from conf the requested key
QFile confFile(conf);
if ( confFile.open( QIODevice::ReadOnly ) ) {
QTextStream stream( &confFile );
stream.setCodec("UTF-8");
QString line;
while ( !stream.atEnd() ) {
line = stream.readLine().simplified();
if ( line.indexOf(key + ": ") == 0 ) {
confFile.close();
return line.replace(key + ": ", "");
}
}
confFile.close();
}
}
//===========================
// SYSCTL ACCESS (might require root)
//===========================
@@ -261,3 +304,40 @@ long long General::sysctlAsInt(QString var){
if(0!=sysctlbyname(var.toLocal8Bit(), &result, &len, NULL, 0) ){ return 0; }
return result;
}
//===========================
// Misc
//===========================
QString General::bytesToHumanReadable(long long bytes)
{
float num = bytes;
QStringList list;
list << "KB" << "MB" << "GB" << "TB";
QStringListIterator i(list);
QString unit("bytes");
while(num >= 1024.0 && i.hasNext())
{
unit = i.next();
num /= 1024.0;
}
return QString().setNum(num,'f',2)+" "+unit;
}
void General::emptyDir(QString dir){
QDir d(dir);
if(!d.exists()){ return; } //quick check to make sure directory exists first
//Remove all the files in this directory
QStringList tmp = d.entryList(QDir::Files | QDir::NoDotAndDotDot);
for(int i=0; i<tmp.length(); i++){
d.remove(tmp[i]);
}
//Now remove all the directories in this directory (recursive)
tmp = d.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
for(int i=0; i<tmp.length(); i++){
General::emptyDir(d.absoluteFilePath(tmp[i])); //Empty this directory first
d.rmdir(tmp[i]); //Now try to remove it
}
}

View File

@@ -21,7 +21,8 @@ public:
static QString RunCommand(QString command, QStringList arguments = QStringList(), QString workdir = "", QStringList env = QStringList() );
// - success output only
static bool RunQuickCommand(QString command, QStringList arguments = QStringList(), QString workdir = "", QStringList env = QStringList() );
static QStringList gitCMD(QString dir, QString cmd, QStringList args = QStringList());
static void emptyDir(QString dir);
//File Access Functions
static QStringList readTextFile(QString filename);
static bool writeTextFile(QString filename, QStringList contents, bool overwrite = true);
@@ -55,6 +56,10 @@ public:
//Retrieve a number-based sysctl
static long long sysctlAsInt(QString var);
static QString bytesToHumanReadable(long long bytes);
static QString getValFromTOConf(QString conf, QString key);
static QString getValFromTrueOSConf(QString key);
};
} //end of pcbsd namespace

View File

@@ -391,3 +391,60 @@ QJsonObject SysMgmt::systemReboot() {
return retObject;
}
// Source Management
/*
void SysMgmt::fetchPortsTree(QStringList &cmds, QStringList &dirs){
//Clear the output variables
cmds.clear(); dirs.clear();
//Does Ports tree exist? If not create it.
if(!QFile::exists("/usr/ports")){
cmds << "mkdir /usr/ports"; dirs << "";
}
//Does a local git repo exist? If not create it.
QString URL = "https://www.github.com/trueos/freebsd-ports.git";
if(QFile::exists("/usr/ports/.git")){
//Check if the remote URL is correct
QString origin = General::gitCMD("/usr/ports", "git remote show -n origin").filter("Fetch URL:").join("").section("URL:",1,30).simplified();
if(origin != URL){
cmds << "git remote remove origin"; dirs <<"/usr/ports";
cmds << "git remote add origin "+URL; dirs << "/usr/ports/.git";
}
}else{
//new GIT setup
General::emptyDir("/usr/ports");
cmds << "git init"; dirs << "/usr/ports"; //setup git
cmds << "git remote add origin "+URL; dirs << "/usr/ports/.git"; //setup TrueOS git repo
}
//Now update the tree with git
cmds << "git fetch --depth=1"; dirs << "/usr/ports/.git";
cmds << "git checkout master"; dirs << "/usr/ports";
}
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

@@ -26,6 +26,8 @@ public:
static QJsonObject systemInfo();
static QJsonObject systemReboot();
static QJsonObject systemHalt();
// static void fetchPortsTree(QStringList &cmds, QStringList &dirs); // This is not ready yet
// static void fetchSourceTree(QString branch, QStringList &cmds, QStringList &dirs); // This is not ready yet
};
} //end of pcbsd namespace