Skip to content

Commit

Permalink
sysutils/auto-recovery: a new plugin, closes #2976
Browse files Browse the repository at this point in the history
  • Loading branch information
fraenki committed Feb 22, 2023
1 parent f3bf7e8 commit 952af26
Show file tree
Hide file tree
Showing 13 changed files with 679 additions and 0 deletions.
6 changes: 6 additions & 0 deletions sysutils/auto-recovery/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
PLUGIN_NAME= auto-recovery
PLUGIN_VERSION= 1.0
PLUGIN_COMMENT= A configuration rollback functionality for OPNsense
PLUGIN_MAINTAINER= opnsense@moov.de

.include "../../Mk/plugins.mk"
9 changes: 9 additions & 0 deletions sysutils/auto-recovery/pkg-descr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Set a timer to perform a rollback to a previous configuration state.
It is a safety net when making critical configuration on a remote device.

Plugin Changelog
================

1.0

* initial release
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

/**
* Copyright (C) 2023 Frank Wall
* Copyright (C) 2015 Deciso B.V.
*
* All rights reserved.
*
* 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.
*
* 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\AutoRecovery\Api;

use OPNsense\Base\ApiMutableServiceControllerBase;
use OPNsense\Core\Backend;
use OPNsense\AutoRecovery\AutoRecovery;

/**
* Class ServiceController
* @package OPNsense\AutoRecovery
*/
class ServiceController extends ApiMutableServiceControllerBase
{
protected static $internalServiceClass = '\OPNsense\AutoRecovery\AutoRecovery';
protected static $internalServiceTemplate = 'OPNsense/AutoRecovery';
protected static $internalServiceEnabled = 'general.Enabled';
protected static $internalServiceName = 'autorecovery';

/**
* start countdown
* @return string
*/
public function countdownAction()
{
$model = new AutoRecovery();
$action = (string)$model->general->action;
if (!empty((string)$model->general->configd_command)) {
// Replace spaces with colons before passing the value to configdRun().
$configdcmd = str_replace(' ', ':',(string)$model->general->configd_command);
} else {
$configdcmd = 'not_found';
}
// Convert minutes to seconds
$countdown = 60 * (string)$model->general->countdown;

$backend = new Backend();
$response = $backend->configdRun("autorecovery countdown ${action} ${configdcmd} ${countdown}");
return array("response" => $response);
}

/**
* get remaining time for current countdown
* @return string
*/
public function timeAction()
{
$backend = new Backend();
$response = $backend->configdRun("autorecovery status");
return array("response" => $response);
}

/**
* abort countdown
* @return string
*/
public function abortAction()
{
$backend = new Backend();
$response = $backend->configdRun("autorecovery abort");
return array("response" => $response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/**
* Copyright (C) 2023 Frank Wall
* Copyright (C) 2015-2019 Deciso B.V.
*
* All rights reserved.
*
* 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.
*
* 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\AutoRecovery\Api;

use OPNsense\Base\ApiMutableModelControllerBase;
use OPNsense\Core\Config;
use OPNsense\AutoRecovery\AutoRecovery;

/**
* Class SettingsController Handles settings related API actions for the AutoRecovery module
* @package OPNsense\AutoRecovery
*/
class SettingsController extends ApiMutableModelControllerBase
{
protected static $internalModelName = 'autorecovery';
protected static $internalModelClass = 'OPNsense\AutoRecovery\AutoRecovery';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/**
* Copyright (C) 2023 Frank Wall
* Copyright (C) 2015 Deciso B.V.
*
* All rights reserved.
*
* 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.
*
* 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\AutoRecovery;

/**
* Class IndexController
* @package OPNsense\AutoRecovery
*/
class IndexController extends \OPNsense\Base\IndexController
{
public function indexAction()
{
// pick the template to serve to our users.
$this->view->pick('OPNsense/AutoRecovery/index');
// fetch form data "general" in
$this->view->generalForm = $this->getForm("general");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<form>
<field>
<label>Configuration</label>
<type>header</type>
</field>
<field>
<id>autorecovery.general.countdown</id>
<label>Countdown in minutes</label>
<type>text</type>
<help>The number of minutes to wait before starting recovery procedures.</help>
</field>
<field>
<id>autorecovery.general.action</id>
<label>Recovery action</label>
<type>dropdown</type>
<help>The recovery action that should be performed.</help>
</field>
<field>
<label>Required Parameters</label>
<type>header</type>
<style>table_configd</style>
</field>
<field>
<id>autorecovery.general.configd_command</id>
<label>System Command</label>
<type>dropdown</type>
<help>Select a pre-defined system command which should be run.</help>
</field>
</form>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<acl>
<page-user-autorecovery>
<name>System: Auto Recovery</name>
<patterns>
<pattern>ui/autorecovery/*</pattern>
<pattern>api/autorecovery/*</pattern>
</patterns>
</page-user-autorecovery>
</acl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/**
* Copyright (C) 2023 Frank Wall
* Copyright (C) 2015 Deciso B.V.
*
* All rights reserved.
*
* 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.
*
* 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\AutoRecovery;

use OPNsense\Base\BaseModel;

class AutoRecovery extends BaseModel
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<model>
<mount>//OPNsense/AutoRecovery</mount>
<version>1.0.0</version>
<description>A configuration rollback functionality for OPNsense</description>
<items>
<general>
<countdown type="IntegerField">
<default>10</default>
<MinimumValue>1</MinimumValue>
<!-- max value is 7 days -->
<MaximumValue>10080</MaximumValue>
<ValidationMessage>Please specify a value between 1 and 10080 minutes.</ValidationMessage>
<Required>Y</Required>
</countdown>
<action type="OptionField">
<Required>Y</Required>
<default>restore_reboot</default>
<OptionValues>
<restore_reboot>Restore config and reboot [default]</restore_reboot>
<restore_reload>Restore config and restart all services</restore_reload>
<restore_configd>Restore config and run system command</restore_configd>
<restore>Restore config</restore>
<reboot>Reboot OPNsense</reboot>
<configd>Run system command</configd>
<noop>Do nothing (for testing purpose)</noop>
</OptionValues>
</action>
<configd_command type="ConfigdActionsField">
<filters>
<description>/(.){1,255}/</description>
</filters>
<ValidationMessage>Select a command from the list.</ValidationMessage>
<Required>N</Required>
</configd_command>
</general>
</items>
</model>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<menu>
<System>
<AutoRecovery VisibleName="Auto Recovery" cssClass="fa fa-history fa-fw" url="/ui/autorecovery"/>
</System>
</menu>
Loading

0 comments on commit 952af26

Please sign in to comment.