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

This commit is contained in:
Ken Moore
2016-03-03 15:19:47 -05:00
4 changed files with 414 additions and 2 deletions

View File

@@ -21,7 +21,7 @@ Every iohyve class request contains the following parameters:
| | | |
+---------------------------------+---------------+----------------------------------------------------------------------------------------------------------------------+
| action | | supported actions include "listvms", "fetchiso", "listisos", "renameiso", "rmiso", "setup", "issetup", "create", |
| | | "install", "start", "stop" |
| | | "install", "start", "stop", "delete", "adddisk", "listdisks", and "deletedisk" |
| | | |
+---------------------------------+---------------+----------------------------------------------------------------------------------------------------------------------+
@@ -587,4 +587,203 @@ The "stop" action stops the specified VM.
"id": "fooid",
"name": "response",
"namespace": "sysadm"
}
.. index:: delete, iohyve
.. _Delete a VM:
Delete a VM
===========
The "delete" action deletes the specified iohyve guest.
**REST Request**
.. code-block:: json
PUT /sysadm/iohyve
{
"action" : "delete",
"name" : "bsdguest"
}
**WebSocket Request**
.. code-block:: json
{
"namespace" : "sysadm",
"id" : "fooid",
"args" : {
"action" : "delete",
"name" : "bsdguest"
},
"name" : "iohyve"
}
**Response**
.. code-block:: json
{
"args": {
"delete": {
"name": "bsdguest"
}
},
"id": "fooid",
"name": "response",
"namespace": "sysadm"
}
.. index:: adddisk, iohyve
.. _Add a Disk:
Add a Disk
==========
The "adddisk" action adds and creates a disk for a VM.
**REST Request**
.. code-block:: json
PUT /sysadm/iohyve
{
"name" : "bsdguest",
"action" : "adddisk",
"size" : "10G"
}
**WebSocket Request**
.. code-block:: json
{
"args" : {
"size" : "10G",
"name" : "bsdguest",
"action" : "adddisk"
},
"id" : "fooid",
"namespace" : "sysadm",
"name" : "iohyve"
}
**Response**
.. code-block:: json
{
"args": {
"adddisk": {
"bsdguest": {
"size": "10G"
}
}
},
"id": "fooid",
"name": "response",
"namespace": "sysadm"
}
.. index:: listdisks, iohyve
.. _List Disks:
List Disks
==========
The "listdisks" action lists the disks connected to the specified VM.
**REST Request**
.. code-block:: json
PUT /sysadm/iohyve
{
"name" : "bsdguest",
"action" : "listdisks"
}
**WebSocket Request**
.. code-block:: json
{
"args" : {
"action" : "listdisks",
"name" : "bsdguest"
},
"id" : "fooid",
"namespace" : "sysadm",
"name" : "iohyve"
}
**Response**
.. code-block:: json
{
"args": {
"listdisks": {
"disk0": "10G"
}
},
"id": "fooid",
"name": "response",
"namespace": "sysadm"
}
.. index:: deletedisk, iohyve
.. _Delete a Disk:
Delete a Disk
=============
The "deletedisk" action removes the specified disk from the specified VM.
**REST Request**
.. code-block:: json
PUT /sysadm/iohyve
{
"disk" : "disk1",
"name" : "bsdguest",
"action" : "deletedisk"
}
**WebSocket Request**
.. code-block:: json
{
"namespace" : "sysadm",
"id" : "fooid",
"name" : "iohyve",
"args" : {
"name" : "bsdguest",
"action" : "deletedisk",
"disk" : "disk1"
}
}
**Response**
.. code-block:: json
{
"args": {
"deletedisk": {
"disk": "disk1",
"name": "bsdguest"
}
},
"id": "fooid",
"name": "response",
"namespace": "sysadm"
}

View File

@@ -581,10 +581,26 @@ RestOutputStruct::ExitCode WebSocket::EvaluateSysadmIohyveRequest(const QJsonVal
if(keys.contains("action")){
QString act = JsonValueToString(in_args.toObject().value("action"));
//qDebug() << " - iohyve action:" << act;
if(act=="adddisk"){
ok = true;
out->insert("adddisk", sysadm::Iohyve::addDisk(in_args.toObject()));
}
if(act=="create"){
ok = true;
out->insert("create", sysadm::Iohyve::createGuest(in_args.toObject()));
}
if(act=="delete"){
ok = true;
out->insert("delete", sysadm::Iohyve::deleteGuest(in_args.toObject()));
}
if(act=="deletedisk"){
ok = true;
out->insert("deletedisk", sysadm::Iohyve::deleteDisk(in_args.toObject()));
}
else if(act=="listdisks"){
ok = true;
out->insert("listdisks", sysadm::Iohyve::listDisks(in_args.toObject()));
}
else if(act=="listvms"){
ok = true;
out->insert("listvms", sysadm::Iohyve::listVMs());
@@ -614,6 +630,10 @@ RestOutputStruct::ExitCode WebSocket::EvaluateSysadmIohyveRequest(const QJsonVal
ok = true;
out->insert("rmiso", sysadm::Iohyve::rmISO(in_args.toObject()));
}
else if(act=="resizedisk"){
ok = true;
out->insert("resizedisk", sysadm::Iohyve::resizeDisk(in_args.toObject()));
}
else if(act=="setup"){
ok = true;
out->insert("setup", sysadm::Iohyve::setupIohyve(in_args.toObject()));
@@ -626,6 +646,10 @@ RestOutputStruct::ExitCode WebSocket::EvaluateSysadmIohyveRequest(const QJsonVal
ok = true;
out->insert("stop", sysadm::Iohyve::stopGuest(in_args.toObject()));
}
else if(act=="version"){
ok = true;
out->insert("version", sysadm::Iohyve::version());
}
//qDebug() << " - iohyve action finished:" << act << ok;
} //end of "action" key usage

View File

@@ -14,6 +14,40 @@ using namespace sysadm;
//PLEASE: Keep the functions in the same order as listed in pcbsd-general.h
// Add a new disk for a VM
QJsonObject Iohyve::addDisk(QJsonObject jsin) {
QJsonObject retObject;
QStringList keys = jsin.keys();
if (! keys.contains("name") || ! keys.contains("size") ) {
retObject.insert("error", "Missing required key(s) 'name', 'size'");
return retObject;
}
// Get the key values
QString name = jsin.value("name").toString();
QString size = jsin.value("size").toString();
QString pool = jsin.value("pool").toString();
QStringList output = General::RunCommand("iohyve add " + name + " " + size + " " + pool).split("\n");
for ( int i = 0; i < output.size(); i++)
{
if ( output.at(i).indexOf("cannot create") != -1 ) {
retObject.insert("error", output.at(i));
return retObject;
}
}
QJsonObject vm;
vm.insert("size", size);
if ( ! pool.isEmpty() )
vm.insert("pool", pool);
retObject.insert(name, vm);
return retObject;
}
// Create a new guest VM
QJsonObject Iohyve::createGuest(QJsonObject jsin) {
QJsonObject retObject;
@@ -43,6 +77,75 @@ QJsonObject Iohyve::createGuest(QJsonObject jsin) {
return retObject;
}
// Delete a guest disk
QJsonObject Iohyve::deleteDisk(QJsonObject jsin) {
QJsonObject retObject;
QStringList keys = jsin.keys();
if (! keys.contains("name") || !keys.contains("disk") ) {
retObject.insert("error", "Missing required key(s) 'name/disk'");
return retObject;
}
// Get the key values
QString name = jsin.value("name").toString();
QString disk = jsin.value("disk").toString();
// Can't remove disk0
if ( disk == "disk0" ) {
retObject.insert("error", "disk0 cannot be removed!");
return retObject;
}
// Remove the disk now
QStringList output = General::RunCommand("iohyve remove " + name + " " + disk).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;
}
if ( output.at(i).indexOf("Not") != -1 ) {
retObject.insert("error", output.at(i));
return retObject;
}
}
retObject.insert("name", name);
retObject.insert("disk", disk);
return retObject;
}
// Delete a guest
QJsonObject Iohyve::deleteGuest(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();
// Do the stop right now
QStringList output = General::RunCommand("iohyve delete " + name).split("\n");
qDebug() << output;
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;
}
// Queue the fetch of an ISO
QJsonObject Iohyve::fetchISO(QJsonObject jsin) {
QJsonObject retObject;
@@ -113,6 +216,41 @@ QJsonObject Iohyve::isSetup() {
return retObject;
}
// List the disks for a VM
QJsonObject Iohyve::listDisks(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();
QStringList output = General::RunCommand("iohyve disks " + name).split("\n");
for ( int i = 0; i < output.size(); i++)
{
if ( output.at(i).indexOf("Listing") != -1 )
continue;
if ( output.at(i).indexOf("diskN") != -1 )
continue;
if ( output.at(i).isEmpty() )
break;
QJsonObject vm;
QString line = output.at(i).simplified();
QString disk = line.section(" ", 0, 0);
QString size = line.section(" ", 1, 1);
retObject.insert(disk, size);
}
return retObject;
}
// List the VMs on the box
QJsonObject Iohyve::listVMs() {
QJsonObject retObject;
@@ -182,6 +320,42 @@ QJsonObject Iohyve::renameISO(QJsonObject jsin) {
return retObject;
}
// Resize a guest disk
QJsonObject Iohyve::resizeDisk(QJsonObject jsin) {
QJsonObject retObject;
QStringList keys = jsin.keys();
if (! keys.contains("name") || !keys.contains("disk") || !keys.contains("size") ) {
retObject.insert("error", "Missing required key(s) 'name/disk/size'");
return retObject;
}
// Get the key values
QString name = jsin.value("name").toString();
QString disk = jsin.value("disk").toString();
QString size = jsin.value("size").toString();
// Resize the disk now
QStringList output = General::RunCommand("iohyve resize " + name + " " + disk + " " + size).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("Please stop") != -1 ) {
retObject.insert("error", output.at(i));
return retObject;
}
if ( output.at(i).indexOf("Not a valid") != -1 ) {
retObject.insert("error", output.at(i));
return retObject;
}
}
retObject.insert("name", name);
retObject.insert("disk", disk);
retObject.insert("size", size);
return retObject;
}
// Remove an ISO file
QJsonObject Iohyve::rmISO(QJsonObject jsin) {
QJsonObject retObject;
@@ -303,3 +477,12 @@ QJsonObject Iohyve::stopGuest(QJsonObject jsin) {
retObject.insert("name", name);
return retObject;
}
// List the version of iohyve
QJsonObject Iohyve::version() {
QJsonObject retObject;
QString output = General::RunCommand("iohyve version").simplified();
retObject.insert("version", output);
return retObject;
}

View File

@@ -15,17 +15,23 @@ namespace sysadm{
class Iohyve{
public:
static QJsonObject addDisk(QJsonObject);
static QJsonObject createGuest(QJsonObject);
static QJsonObject deleteDisk(QJsonObject);
static QJsonObject deleteGuest(QJsonObject);
static QJsonObject fetchISO(QJsonObject);
static QJsonObject installGuest(QJsonObject);
static QJsonObject isSetup();
static QJsonObject listVMs();
static QJsonObject listDisks(QJsonObject);
static QJsonArray listISOs();
static QJsonObject listVMs();
static QJsonObject renameISO(QJsonObject);
static QJsonObject resizeDisk(QJsonObject);
static QJsonObject rmISO(QJsonObject);
static QJsonObject setupIohyve(QJsonObject);
static QJsonObject startGuest(QJsonObject);
static QJsonObject stopGuest(QJsonObject);
static QJsonObject version();
};