mirror of
https://github.com/opnsense/plugins.git
synced 2026-04-28 07:39:42 -05:00
sysutils/nut: new apimutable service + reconfigure fix (#654)
This commit is contained in:
@@ -28,118 +28,15 @@
|
||||
|
||||
namespace OPNsense\Nut\Api;
|
||||
|
||||
use \OPNsense\Base\ApiControllerBase;
|
||||
use \OPNsense\Core\Backend;
|
||||
use \OPNsense\Nut\Nut;
|
||||
use OPNsense\Base\ApiMutableServiceControllerBase;
|
||||
|
||||
class ServiceController extends ApiControllerBase
|
||||
class ServiceController extends ApiMutableServiceControllerBase
|
||||
{
|
||||
static protected $internalServiceClass = '\OPNsense\Nut\Nut';
|
||||
static protected $internalServiceTemplate = 'OPNsense/Nut';
|
||||
static protected $internalServiceEnabled = 'general.enabled';
|
||||
static protected $internalServiceName = 'nut';
|
||||
|
||||
/**
|
||||
* restart nut service
|
||||
* @return array
|
||||
*/
|
||||
public function restartAction()
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
// close session for long running action
|
||||
$this->sessionClose();
|
||||
$backend = new Backend();
|
||||
$response = $backend->configdRun('nut restart');
|
||||
return array('response' => $response);
|
||||
} else {
|
||||
return array('response' => array());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* retrieve status of nut
|
||||
* @return array
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function statusAction()
|
||||
{
|
||||
$backend = new Backend();
|
||||
$nut = new Nut();
|
||||
$response = $backend->configdRun('nut status');
|
||||
|
||||
if (strpos($response, 'not running') > 0) {
|
||||
if ((string)$nut->general->enabled == 1) {
|
||||
$status = 'stopped';
|
||||
} else {
|
||||
$status = 'disabled';
|
||||
}
|
||||
} elseif (strpos($response, 'is running') > 0) {
|
||||
$status = 'running';
|
||||
} elseif ((string)$nut->general->enabled == 0) {
|
||||
$status = 'disabled';
|
||||
} else {
|
||||
$status = 'unknown';
|
||||
}
|
||||
|
||||
return array('status' => $status);
|
||||
}
|
||||
|
||||
/**
|
||||
* reconfigure nut, generate config and reload
|
||||
*/
|
||||
public function reconfigureAction()
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
// close session for long running action
|
||||
$this->sessionClose();
|
||||
|
||||
$nut = new Nut();
|
||||
$backend = new Backend();
|
||||
|
||||
$this->stopAction();
|
||||
|
||||
// generate template
|
||||
$backend->configdRun('template reload OPNsense/Nut');
|
||||
|
||||
// (re)start daemon
|
||||
if ((string)$nut->general->enabled == '1') {
|
||||
$this->startAction();
|
||||
}
|
||||
|
||||
return array('status' => 'ok');
|
||||
} else {
|
||||
return array('status' => 'failed');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* stop nut service
|
||||
* @return array
|
||||
*/
|
||||
public function stopAction()
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
// close session for long running action
|
||||
$this->sessionClose();
|
||||
$backend = new Backend();
|
||||
$response = $backend->configdRun('nut stop');
|
||||
return array('response' => $response);
|
||||
} else {
|
||||
return array('response' => array());
|
||||
}
|
||||
}
|
||||
/**
|
||||
* start nut service
|
||||
* @return array
|
||||
*/
|
||||
public function startAction()
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
// close session for long running action
|
||||
$this->sessionClose();
|
||||
$backend = new Backend();
|
||||
$response = $backend->configdRun('nut start');
|
||||
return array('response' => $response);
|
||||
} else {
|
||||
return array('response' => array());
|
||||
}
|
||||
}
|
||||
public function upsstatusAction()
|
||||
{
|
||||
$backend = new Backend();
|
||||
|
||||
@@ -57,13 +57,14 @@
|
||||
saveFormToEndpoint(url="/api/nut/settings/set", formid=frm_id, callback_ok=function(){
|
||||
// on correct save, perform restart, set progress animation when reloading
|
||||
$("#"+frm_id+"_progress").addClass("fa fa-spinner fa-pulse");
|
||||
|
||||
ajaxCall(url="/api/nut/service/restart", sendData={}, callback=function(data,status){
|
||||
// when done, disable progress animation.
|
||||
$("#"+frm_id+"_progress").removeClass("fa fa-spinner fa-pulse");
|
||||
ajaxCall(url="/api/nut/service/status", sendData={}, callback=function(data,status) {
|
||||
updateServiceStatusUI(data['status']);
|
||||
});
|
||||
ajaxCall(url="/api/nut/service/reconfigure", sendData={}, callback=function(data,status){
|
||||
ajaxCall(url="/api/nut/service/restart", sendData={}, callback=function(data,status){
|
||||
// when done, disable progress animation.
|
||||
$("#"+frm_id+"_progress").removeClass("fa fa-spinner fa-pulse");
|
||||
ajaxCall(url="/api/nut/service/status", sendData={}, callback=function(data,status) {
|
||||
updateServiceStatusUI(data['status']);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -5,7 +5,7 @@ type:script
|
||||
message:starting nut
|
||||
|
||||
[stop]
|
||||
command:/usr/local/etc/rc.d/nut stop;/usr/local/etc/rc.d/nut_upsmon stop;exit 0
|
||||
command:/usr/local/etc/rc.d/nut stop;/usr/local/etc/rc.d/nut_upsmon stop
|
||||
parameters:
|
||||
type:script
|
||||
message:stopping nut
|
||||
|
||||
Reference in New Issue
Block a user