From 188f20ff165ed4cbdb349c1a50b788192a0ee076 Mon Sep 17 00:00:00 2001 From: Kris Moore Date: Fri, 29 Jan 2016 14:45:37 -0500 Subject: [PATCH] Add new 'getjailsettings' API call which returns all the 'iocage get all ' properties in json REST Request: ------------------------------- PUT /sysadm/iocage { "jail" : "testjail", "action" : "getjailsettings" } REST Response: ------------------------------- { "args": { "getjailsettings": { "testjail": { "allow_chflags": "0", "allow_mount": "0", "allow_mount_devfs": "0", "allow_mount_nullfs": "0", "allow_mount_procfs": "0", "allow_mount_tmpfs": "0", "allow_mount_zfs": "0", "allow_quotas": "0", "allow_raw_sockets": "0", "allow_set_hostname": "1", "allow_socket_af": "0", "allow_sysvipc": "0", "boot": "off", "bpf": "off", "branch": "-", "children_max": "0", "coredumpsize": "off", "count": "1", "cpuset": "off", "cputime": "off", "datasize": "off", "defaultrouter": "none", "defaultrouter6": "none", "devfs_ruleset": "4", "dhcp": "off", "enforce_statfs": "2", "exec_clean": "1", "exec_fib": "0", "exec_jail_user": "root", "exec_poststart": "/usr/bin/true", "exec_poststop": "/usr/bin/true", "exec_prestart": "/usr/bin/true", "exec_prestop": "/usr/bin/true", "exec_start": "/bin/sh /etc/rc", "exec_stop": "/bin/sh /etc/rc.shutdown", "exec_system_jail_user": "0", "exec_system_user": "root", "exec_timeout": "60", "ftpdir": "-", "ftpfiles": "-", "ftphost": "-", "ftplocaldir": "-", "gitlocation": "https", "hack88": "0", "host_domainname": "none", "host_hostname": "4bb3f929-c6bf-11e5-bbe9-fcaa14deb15d", "host_hostuuid": "4bb3f929-c6bf-11e5-bbe9-fcaa14deb15d", "hostid": "4145fbb8-c5b6-11e5-9f2f-fcaa14deb15d", "interfaces": "vnet0", "ip4": "new", "ip4_addr": "none", "ip4_autoend": "none", "ip4_autostart": "none", "ip4_autosubnet": "none", "ip4_saddrsel": "1", "ip6": "new", "ip6_addr": "none", "ip6_saddrsel": "1", "istemplate": "no", "jail_zfs": "off", "jail_zfs_dataset": "iocage/jails/4ba5d76b-c6bf-11e5-bbe9-fcaa14deb15d/data", "jail_zfs_mountpoint": "none", "last_started": "none", "login_flags": "-f root", "maxproc": "off", "memorylocked": "off", "memoryuse": "8G", "mount_devfs": "1", "mount_fdescfs": "1", "mount_linprocfs": "0", "mount_procfs": "0", "msgqqueued": "off", "msgqsize": "off", "nmsgq": "off", "notes": "none", "nsemop": "off", "nshm": "off", "nthr": "off", "openfiles": "off", "owner": "root", "pcpu": "off", "pkglist": "none", "priority": "99", "pseudoterminals": "off", "release": "10.2-RELEASE", "resolver": "none", "rlimits": "off", "securelevel": "2", "shmsize": "off", "stacksize": "off", "start": "-", "stop_timeout": "30", "swapuse": "off", "sync_stat": "-", "sync_target": "none", "sync_tgt_zpool": "none", "tag": "testjail", "template": "-", "type": "basejail", "vmemoryuse": "off", "vnet": "off", "vnet0_mac": "none", "vnet1_mac": "none", "vnet2_mac": "none", "vnet3_mac": "none", "wallclock": "off" } } } } WebSocket Request: ------------------------------- { "args" : { "jail" : "testjail", "action" : "getjailsettings" }, "id" : "fooid", "name" : "iocage", "namespace" : "sysadm" } WebSocket Response: ------------------------------- { "args": { "getjailsettings": { "testjail": { "allow_chflags": "0", "allow_mount": "0", "allow_mount_devfs": "0", "allow_mount_nullfs": "0", "allow_mount_procfs": "0", "allow_mount_tmpfs": "0", "allow_mount_zfs": "0", "allow_quotas": "0", "allow_raw_sockets": "0", "allow_set_hostname": "1", "allow_socket_af": "0", "allow_sysvipc": "0", "boot": "off", "bpf": "off", "branch": "-", "children_max": "0", "coredumpsize": "off", "count": "1", "cpuset": "off", "cputime": "off", "datasize": "off", "defaultrouter": "none", "defaultrouter6": "none", "devfs_ruleset": "4", "dhcp": "off", "enforce_statfs": "2", "exec_clean": "1", "exec_fib": "0", "exec_jail_user": "root", "exec_poststart": "/usr/bin/true", "exec_poststop": "/usr/bin/true", "exec_prestart": "/usr/bin/true", "exec_prestop": "/usr/bin/true", "exec_start": "/bin/sh /etc/rc", "exec_stop": "/bin/sh /etc/rc.shutdown", "exec_system_jail_user": "0", "exec_system_user": "root", "exec_timeout": "60", "ftpdir": "-", "ftpfiles": "-", "ftphost": "-", "ftplocaldir": "-", "gitlocation": "https", "hack88": "0", "host_domainname": "none", "host_hostname": "4bb3f929-c6bf-11e5-bbe9-fcaa14deb15d", "host_hostuuid": "4bb3f929-c6bf-11e5-bbe9-fcaa14deb15d", "hostid": "4145fbb8-c5b6-11e5-9f2f-fcaa14deb15d", "interfaces": "vnet0", "ip4": "new", "ip4_addr": "none", "ip4_autoend": "none", "ip4_autostart": "none", "ip4_autosubnet": "none", "ip4_saddrsel": "1", "ip6": "new", "ip6_addr": "none", "ip6_saddrsel": "1", "istemplate": "no", "jail_zfs": "off", "jail_zfs_dataset": "iocage/jails/4ba5d76b-c6bf-11e5-bbe9-fcaa14deb15d/data", "jail_zfs_mountpoint": "none", "last_started": "none", "login_flags": "-f root", "maxproc": "off", "memorylocked": "off", "memoryuse": "8G", "mount_devfs": "1", "mount_fdescfs": "1", "mount_linprocfs": "0", "mount_procfs": "0", "msgqqueued": "off", "msgqsize": "off", "nmsgq": "off", "notes": "none", "nsemop": "off", "nshm": "off", "nthr": "off", "openfiles": "off", "owner": "root", "pcpu": "off", "pkglist": "none", "priority": "99", "pseudoterminals": "off", "release": "10.2-RELEASE", "resolver": "none", "rlimits": "off", "securelevel": "2", "shmsize": "off", "stacksize": "off", "start": "-", "stop_timeout": "30", "swapuse": "off", "sync_stat": "-", "sync_target": "none", "sync_tgt_zpool": "none", "tag": "testjail", "template": "-", "type": "basejail", "vmemoryuse": "off", "vnet": "off", "vnet0_mac": "none", "vnet1_mac": "none", "vnet2_mac": "none", "vnet3_mac": "none", "wallclock": "off" } } }, "id": "fooid", "name": "response", "namespace": "sysadm" } --- src/library/sysadm-iocage.cpp | 34 ++++++++++++++++++++++++++++++++++ src/library/sysadm-iocage.h | 1 + src/server/WebBackend.cpp | 4 ++++ tests/api-test.sh | 14 ++++++++++---- 4 files changed, 49 insertions(+), 4 deletions(-) diff --git a/src/library/sysadm-iocage.cpp b/src/library/sysadm-iocage.cpp index 3652059..5c6a35d 100644 --- a/src/library/sysadm-iocage.cpp +++ b/src/library/sysadm-iocage.cpp @@ -12,6 +12,40 @@ using namespace sysadm; //PLEASE: Keep the functions in the same order as listed in pcbsd-general.h +// List the jails on the box +QJsonObject Iocage::getJailSettings(QJsonObject jsin) { + QJsonObject retObject; + + QStringList keys = jsin.keys(); + if (! keys.contains("jail") ) { + retObject.insert("error", "Missing required keys"); + return retObject; + } + + // Get the key values + QString jail = jsin.value("jail").toString(); + + QStringList output = General::RunCommand("iocage get all " + jail).split("\n"); + + QJsonObject vals; + for ( int i = 0; i < output.size(); i++) + { + if ( output.at(i).indexOf("JID") != -1 ) + continue; + + if ( output.at(i).isEmpty() ) + break; + + QString key = output.at(i).simplified().section(":", 0, 0); + QString value = output.at(i).simplified().section(":", 1, 1); + + vals.insert(key, value); + } + + retObject.insert(jail, vals); + return retObject; +} + // List the jails on the box QJsonObject Iocage::listJails() { QJsonObject retObject; diff --git a/src/library/sysadm-iocage.h b/src/library/sysadm-iocage.h index d9f191e..d23a444 100644 --- a/src/library/sysadm-iocage.h +++ b/src/library/sysadm-iocage.h @@ -14,6 +14,7 @@ namespace sysadm{ class Iocage{ public: + static QJsonObject getJailSettings(QJsonObject); static QJsonObject listJails(); }; diff --git a/src/server/WebBackend.cpp b/src/server/WebBackend.cpp index 352c67c..26d331b 100644 --- a/src/server/WebBackend.cpp +++ b/src/server/WebBackend.cpp @@ -343,6 +343,10 @@ RestOutputStruct::ExitCode WebSocket::EvaluateSysadmIocageRequest(const QJsonVal bool ok = false; if(keys.contains("action")){ QString act = JsonValueToString(in_args.toObject().value("action")); + if(act=="getjailsettings"){ + ok = true; + out->insert("getjailsettings", sysadm::Iocage::getJailSettings(in_args.toObject())); + } if(act=="listjails"){ ok = true; out->insert("listjails", sysadm::Iocage::listJails()); diff --git a/tests/api-test.sh b/tests/api-test.sh index 2321f04..00f947b 100755 --- a/tests/api-test.sh +++ b/tests/api-test.sh @@ -7,14 +7,20 @@ JSAWK="./utils/jsawk -j js24" which npm >/dev/null 2>/dev/null if [ $? -ne 0 ] ; then - echo "Please install npm first" - exit 1 + pkg install -y npm + if [ $? -ne 0 ] ; then + echo "Requires npm!" + exit 1 + fi fi pkg info p5-JSON >/dev/null 2>/dev/null if [ $? -ne 0 ] ; then - echo "Please install p5-JSON first" - exit 1 + pkg install -y p5-JSON + if [ $? -ne 0 ] ; then + echo "Requires p5-JSON!" + exit 1 + fi fi if [ ! -d "${HOME}/.npm/ws" ] ; then