From 1048b3a6dadbd49ca98ef39c6e2f1e7e8051cb36 Mon Sep 17 00:00:00 2001 From: Brandon Schneider Date: Wed, 3 Feb 2016 19:14:18 -0600 Subject: [PATCH] Add a new iocage API call to activate zpools. It has 2 different outputs depending on if `pool` was actually specified. If you specify a pool this is what you get: REST Request: ------------------------------- PUT /sysadm/iocage { "action" : "activatepool", "pool" : "tank" } REST Response: ------------------------------- { "args": { "activatepool": { "success": "pool tank activated." } } } WebSocket Request: ------------------------------- { "args" : { "action" : "activatepool", "pool" : "tank" }, "name" : "iocage", "id" : "fooid", "namespace" : "sysadm" } WebSocket Response: ------------------------------- { "args": { "activatepool": { "success": "pool tank activated." } }, "id": "fooid", "name": "response", "namespace": "sysadm" } If you do not specify a pool, this is your output: REST Request: ------------------------------- PUT /sysadm/iocage { "action" : "activatepool" } REST Response: ------------------------------- { "args": { "activatepool": { "currently active": { "pool": " tank" } } } } WebSocket Request: ------------------------------- { "args" : { "action" : "activatepool" }, "namespace" : "sysadm", "name" : "iocage", "id" : "fooid" } WebSocket Response: ------------------------------- { "args": { "activatepool": { "currently active": { "pool": " tank" } } }, "id": "fooid", "name": "response", "namespace": "sysadm" } --- src/server/WebBackend.cpp | 4 +++ src/server/library/sysadm-iocage.cpp | 41 ++++++++++++++++++++++++---- src/server/library/sysadm-iocage.h | 1 + 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/server/WebBackend.cpp b/src/server/WebBackend.cpp index 6cfa14c..7dfabdb 100644 --- a/src/server/WebBackend.cpp +++ b/src/server/WebBackend.cpp @@ -389,6 +389,10 @@ RestOutputStruct::ExitCode WebSocket::EvaluateSysadmIocageRequest(const QJsonVal bool ok = false; if(keys.contains("action")){ QString act = JsonValueToString(in_args.toObject().value("action")); + if(act=="activatepool"){ + ok = true; + out->insert("activatepool", sysadm::Iocage::activatePool(in_args.toObject())); + } if(act=="stopjail"){ ok = true; out->insert("stopjail", sysadm::Iocage::stopJail(in_args.toObject())); diff --git a/src/server/library/sysadm-iocage.cpp b/src/server/library/sysadm-iocage.cpp index db73e18..ece6c39 100644 --- a/src/server/library/sysadm-iocage.cpp +++ b/src/server/library/sysadm-iocage.cpp @@ -12,6 +12,41 @@ using namespace sysadm; //PLEASE: Keep the functions in the same order as listed in pcbsd-general.h +// Activate a zpool for iocage on the box +QJsonObject Iocage::activatePool(QJsonObject jsin) { + QJsonObject retObject; + QStringList keys = jsin.keys(); + + // Get the key values + QString pool = jsin.value("pool").toString(); + QStringList output = General::RunCommand("iocage activate " + pool).split("\n"); + QJsonObject vals; + + for ( int i = 0; i < output.size(); i++) + { + // Otherwise we get null output before the actual active pool + if ( i > 0 ) + break; + + // When a pool activation is successful, iocage doesn't return anything, + // so we have to fudge the output a bit. + if ( output.at(i).isEmpty()) + { + retObject.insert("success", "pool " + pool + " activated."); + break; + } else { + QString key = output.at(i).simplified().section(":", 0, 0); + QString value = output.at(i).simplified().section(":", 1, 1); + + vals.insert(key, value); + + retObject.insert("currently active", vals); + } + } + + return retObject; +} + // Stop a jail on the box QJsonObject Iocage::stopJail(QJsonObject jsin) { QJsonObject retObject; @@ -30,9 +65,6 @@ QJsonObject Iocage::stopJail(QJsonObject jsin) { QJsonObject vals; for ( int i = 0; i < output.size(); i++) { - if ( output.at(i).indexOf("JID") != -1 ) - continue; - if ( output.at(i).isEmpty() ) break; @@ -64,9 +96,6 @@ QJsonObject Iocage::startJail(QJsonObject jsin) { QJsonObject vals; for ( int i = 0; i < output.size(); i++) { - if ( output.at(i).indexOf("JID") != -1 ) - continue; - if ( output.at(i).isEmpty() ) break; diff --git a/src/server/library/sysadm-iocage.h b/src/server/library/sysadm-iocage.h index ee40ef9..57560b0 100644 --- a/src/server/library/sysadm-iocage.h +++ b/src/server/library/sysadm-iocage.h @@ -14,6 +14,7 @@ namespace sysadm{ class Iocage{ public: + static QJsonObject activatePool(QJsonObject); static QJsonObject stopJail(QJsonObject); static QJsonObject startJail(QJsonObject); static QJsonObject getDefaultSettings();