net/zerotier: make apply responsive with service restart/stop and visual feedback

This commit is contained in:
Franco Fichtner 2017-07-28 22:03:26 +02:00
parent 9d30608597
commit 8a0dfdfc8e
4 changed files with 94 additions and 33 deletions

View file

@ -1,38 +1,38 @@
<?php
/**
* Copyright (C) 2017 David Harrigan
* Copyright (C) 2017 Deciso B.V.
/*
* Copyright (C) 2017 David Harrigan
* Copyright (C) 2017 Deciso B.V.
* All rights reserved.
*
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
namespace OPNsense\Zerotier\Api;
use \OPNsense\Core\Config;
use \OPNsense\Base\ApiMutableModelControllerBase;
use \OPNsense\Core\Backend;
use \OPNsense\Base\UIModelGrid;
use \OPNsense\Core\Backend;
use \OPNsense\Core\Config;
use \OPNsense\Zerotier\Zerotier;
class ZerotierController extends ApiMutableModelControllerBase
@ -85,12 +85,12 @@ class ZerotierController extends ApiMutableModelControllerBase
$mdlZerotier->setNodes($this->request->getPost("network"));
$validationMessages = $mdlZerotier->performValidation();
foreach ($validationMessages as $field => $msg) {
if(!array_key_exists("validation", $result)) {
if (!array_key_exists("validation", $result)) {
$result["validations"] = array();
}
$result["validation"]["network.".$msg->getField()] = $msg->getMessage();
}
if($validationMessages->count() == 0) {
if ($validationMessages->count() == 0) {
unset($result["validations"]);
$mdlZerotier->serializeToConfig();
Config::getInstance()->save();
@ -119,7 +119,6 @@ class ZerotierController extends ApiMutableModelControllerBase
Config::getInstance()->save();
$result["result"] = "saved";
}
}
return $result;
}
@ -177,17 +176,57 @@ class ZerotierController extends ApiMutableModelControllerBase
$this->sessionClose();
$backend = new Backend();
$backend->configdRun("template reload OPNsense/zerotier");
$result = trim($backend->configdRun("zerotier restart"));
$mdlZerotier = $this->getModel();
$action = 'stop';
foreach ($mdlZerotier->networks->network->__items as $network) {
if ($network->enabled == '1') {
$action = 'restart';
break;
}
}
$result = trim($backend->configdRun("zerotier $action"));
return array("status" => $result);
} else {
return array("status" => "failed");
}
}
public function statusAction()
{
$mdlZerotier = $this->getModel();
$enabled = false;
foreach ($mdlZerotier->networks->network->__items as $network) {
if ($network->enabled == '1') {
$enabled = true;
break;
}
}
$backend = new Backend();
$response = $backend->configdRun('zerotier status');
if (strpos($response, "not running") > 0) {
if ($enabled) {
$status = "stopped";
} else {
$status = "disabled";
}
} elseif (strpos($response, "is running") > 0) {
$status = "running";
} elseif (!$enabled) {
$status = "disabled";
} else {
$status = "unkown";
}
return array("status" => $status);
}
private function toggleZerotierNetwork($networkId, $enabled)
{
$backend = new Backend();
return trim($backend->configdRun("zerotier ".($enabled ? "join " : "leave ").$networkId));
$action = $enabled ? 'join' : 'leave';
return trim($backend->configdRun("zerotier $action $networkId"));
}
}

View file

@ -42,9 +42,16 @@ POSSIBILITY OF SUCH DAMAGE.
}
);
ajaxCall(url="/api/zerotier/zerotier/status", sendData={}, callback=function(data,status) {
updateServiceStatusUI(data['status']);
});
$("#reconfigureZerotier").click(function() {
$("#reconfigureZerotierProgress").addClass("fa fa-spinner fa-pulse");
ajaxCall(url="/api/zerotier/zerotier/reconfigureZerotier", sendData={}, callback=function(data, status) {
ajaxCall(url="/api/zerotier/zerotier/status", sendData={}, callback=function(data,status) {
updateServiceStatusUI(data['status']);
});
$("#reconfigureZerotierProgress").removeClass("fa fa-spinner fa-pulse");
if (status != "success" || data['status'] != 'OK') {
BootstrapDialog.show({
@ -56,7 +63,6 @@ POSSIBILITY OF SUCH DAMAGE.
}
});
});
});
</script>
@ -91,7 +97,7 @@ POSSIBILITY OF SUCH DAMAGE.
</div>
<div class="col-md-12">
<hr/>
<button class="btn btn-primary" id="reconfigureZerotier" type="button"><b>{{ lang._('Save') }}</b> <i id="reconfigureZerotierProgress" class=""></i></button>
<button class="btn btn-primary" id="reconfigureZerotier" type="button"><b>{{ lang._('Apply') }}</b> <i id="reconfigureZerotierProgress" class=""></i></button>
<br/><br/>
</div>
</div>

View file

@ -16,6 +16,12 @@ parameters:
type:script
message:Restarting Zerotier Service
[status]
command:/usr/local/etc/rc.d/zerotier status;exit 0
parameters:
type:script_output
message:Probing Zerotier Service
[join]
command:/usr/local/bin/zerotier-cli
parameters: join %s

View file

@ -1 +1,11 @@
{% set networks = [] %}
{% for network in helpers.toList('OPNsense.zerotier.networks.network') %}
{% if network.enabled == '1' %}
{% do networks.append(network) %}
{% endif %}
{% endfor %}
{% if networks|length > 0 %}
zerotier_enable="YES"
{% else %}
zerotier_enable="NO"
{% endif %}