Add new iocage API call to create jails.

The `switches` object is only for specifying empty jails currently, but kept it vague as more may eventually happen.
There are 2 different outputs depending on if a switch is supplied.

Without any switches:

REST Request:
-------------------------------
PUT /sysadm/iocage
{
   "action" : "createjail",
   "props" : "tag=test release=10.2-RELEASE"
}

WebSocket Request:
-------------------------------
{
   "args" : {
      "props" : "tag=test release=10.2-RELEASE",
      "action" : "createjail"
   },
   "namespace" : "sysadm",
   "name" : "iocage",
   "id" : "fooid"
}

Response:
-------------------------------
{
  "args": {
    "createjail": {
      "props": "tag=test release=10.2-RELEASE",
      "success": {
        "Successfully created": " 3030c554-d05e-11e5-8209-d05099728dbf (test)"
      },
      "switches": ""
    }
  },
  "id": "fooid",
  "name": "response",
  "namespace": "sysadm"
}

With switches:

REST Request:
-------------------------------
PUT /sysadm/iocage
{
   "switches" : "-e",
   "action" : "createjail",
   "props" : "tag=emptytest"
}

WebSocket Request:
-------------------------------
{
   "namespace" : "sysadm",
   "args" : {
      "props" : "tag=emptytest",
      "action" : "createjail",
      "switches" : "-e"
   },
   "name" : "iocage",
   "id" : "fooid"
}

Response:
-------------------------------
{
  "args": {
    "createjail": {
      "props": "tag=emptytest",
      "success": {
        "uuid": "1325b8bc-d05e-11e5-8209-d05099728dbf"
      },
      "switches": "-e"
    }
  },
  "id": "fooid",
  "name": "response",
  "namespace": "sysadm"
}
This commit is contained in:
Brandon Schneider
2016-02-10 19:27:30 -06:00
parent 06edd43945
commit 4eaa321034
3 changed files with 55 additions and 5 deletions

View File

@@ -33,7 +33,7 @@ RestOutputStruct::ExitCode WebSocket::AvailableSubsystems(bool allaccess, QJsonO
*/
// - server settings (always available)
out->insert("sysadm/settings","read/write");
// - syscache
if(QFile::exists("/var/run/syscache.pipe")){
out->insert("rpc/syscache","read"); //no write to syscache - only reads
@@ -150,9 +150,9 @@ RestOutputStruct::ExitCode WebSocket::EvaluateSysadmSettingsRequest(const QJsonV
}else if(act=="revoke_ssl_cert" && keys.contains("pub_key") ){
//Additional arguments: "user" (optional), "pub_key" (String)
QString user; if(keys.contains("user")){ user = argsO.value("user").toString(); }
ok = AUTHSYSTEM->RevokeCertificate(SockAuthToken,argsO.value("pub_key").toString(), user);
ok = AUTHSYSTEM->RevokeCertificate(SockAuthToken,argsO.value("pub_key").toString(), user);
}
if(ok){ return RestOutputStruct::OK; }
else{ return RestOutputStruct::BADREQUEST; }
}
@@ -471,6 +471,10 @@ RestOutputStruct::ExitCode WebSocket::EvaluateSysadmIocageRequest(const QJsonVal
bool ok = false;
if(keys.contains("action")){
QString act = JsonValueToString(in_args.toObject().value("action"));
if(act=="createjail"){
ok = true;
out->insert("createjail", sysadm::Iocage::createJail(in_args.toObject()));
}
if(act=="clonejail"){
ok = true;
out->insert("clonejail", sysadm::Iocage::cloneJail(in_args.toObject()));

View File

@@ -12,6 +12,51 @@ using namespace sysadm;
//PLEASE: Keep the functions in the same order as listed in pcbsd-general.h
// Create a jail on the box
QJsonObject Iocage::createJail(QJsonObject jsin) {
QJsonObject retObject;
QStringList keys = jsin.keys();
// Get the key values
QString switches = jsin.value("switches").toString();
QString props = jsin.value("props").toString();
QStringList output;
if ( keys.contains("switches" ) ) {
output = General::RunCommand("iocage create " + switches + " " + props).split("\n");
} else {
output = General::RunCommand("iocage create " + props).split("\n");
}
QJsonObject vals;
for ( int i = 0; i < output.size(); i++)
{
if ( output.at(i).isEmpty() )
break;
if ( output.at(i).indexOf("ERROR:") != -1 ) {
retObject.insert("error", output.at(i));
return retObject;
} else {
QString key = output.at(i).simplified().section(":", 0, 0);
QString value = output.at(i).simplified().section(":", 1, 1);
if ( keys.contains("switches" ) ) {
vals.insert("uuid", key);
} else {
vals.insert(key, value);
}
}
}
retObject.insert("switches", switches);
retObject.insert("props", props);
retObject.insert("success", vals);
return retObject;
}
// Clone a jail on the box
QJsonObject Iocage::cloneJail(QJsonObject jsin) {
QJsonObject retObject;
@@ -38,8 +83,8 @@ QJsonObject Iocage::cloneJail(QJsonObject jsin) {
retObject.insert("error", output.at(i));
return retObject;
} else {
QString key = output.at(i).simplified().section(":", 0, 0);
QString value = output.at(i).simplified().section(":", 1, 1);
QString key = output.at(i).simplified().section(":", 0, 0);
QString value = output.at(i).simplified().section(":", 1, 1);
vals.insert(key, value);
}

View File

@@ -14,6 +14,7 @@ namespace sysadm{
class Iocage{
public:
static QJsonObject createJail(QJsonObject);
static QJsonObject cloneJail(QJsonObject);
static QJsonObject cleanAll();
static QJsonObject cleanTemplates();