Add new API call to stop a guest VM

REST Request:
-------------------------------
PUT /sysadm/iohyve
{
   "action" : "stop",
   "name" : "bsdguest"
}

WebSocket Request:
-------------------------------
{
   "id" : "fooid",
   "args" : {
      "action" : "stop",
      "name" : "bsdguest"
   },
   "name" : "iohyve",
   "namespace" : "sysadm"
}

Response:
-------------------------------
{
  "args": {
    "stop": {
      "name": "bsdguest"
    }
  },
  "id": "fooid",
  "name": "response",
  "namespace": "sysadm"
}
This commit is contained in:
Kris Moore
2016-02-09 14:54:03 -05:00
parent c8b123df65
commit f48954ebf4
3 changed files with 40 additions and 0 deletions

View File

@@ -537,6 +537,10 @@ RestOutputStruct::ExitCode WebSocket::EvaluateSysadmIohyveRequest(const QJsonVal
ok = true;
out->insert("start", sysadm::Iohyve::startGuest(in_args.toObject()));
}
if(act=="stop"){
ok = true;
out->insert("stop", sysadm::Iohyve::stopGuest(in_args.toObject()));
}
} //end of "action" key usage
//If nothing done - return the proper code

View File

@@ -256,3 +256,38 @@ QJsonObject Iohyve::startGuest(QJsonObject jsin) {
retObject.insert("name", name);
return retObject;
}
// Stop a guest
QJsonObject Iohyve::stopGuest(QJsonObject jsin) {
QJsonObject retObject;
QStringList keys = jsin.keys();
if (! keys.contains("name") ) {
retObject.insert("error", "Missing required key 'name'");
return retObject;
}
// Get the key values
QString name = jsin.value("name").toString();
QString stoparg = "stop";
if (! keys.contains("force") ) {
if ( jsin.value("force").toString() == "true" ) {
stoparg = "forcekill";
}
}
// Do the stop right now
QStringList output = General::RunCommand("iohyve " + stoparg + " " + name).split("\n");
for ( int i = 0; i < output.size(); i++)
{
// This doesn't work, iohyve doesn't return error message right now
if ( output.at(i).indexOf("No such guest") != -1 ) {
retObject.insert("error", output.at(i));
return retObject;
}
}
retObject.insert("name", name);
return retObject;
}

View File

@@ -23,6 +23,7 @@ public:
static QJsonObject rmISO(QJsonObject);
static QJsonObject setupIohyve(QJsonObject);
static QJsonObject startGuest(QJsonObject);
static QJsonObject stopGuest(QJsonObject);
};
} //end of pcbsd namespace