Commit b7856e38 authored by Jos Schellevis's avatar Jos Schellevis

(cron) Add contoller implementation

parent 9f9ac874
<?php
/**
* 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\Cron\Api;
use \OPNsense\Base\ApiControllerBase;
use \OPNsense\Core\Backend;
use \OPNsense\Cron\Cron;
/**
* Class ServiceController
* @package OPNsense\Cron
*/
class ServiceController extends ApiControllerBase
{
/**
* restart cron service
* @return array
*/
public function reconfigureAction()
{
if ($this->request->isPost()) {
// close session for long running action
$this->sessionClose();
$backend = new Backend();
// generate template
$backend->configdRun("template reload OPNsense.Cron");
// (res)start daemon
$backend->configdRun("cron restart");
return array("status" => "ok");
} else {
return array("status" => "failed");
}
}
}
\ No newline at end of file
<?php
/**
* 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\Cron\Api;
use \OPNsense\Base\ApiControllerBase;
use \OPNsense\Core\Config;
use \OPNsense\Cron\Cron;
use \OPNsense\Base\UIModelGrid;
/**
* Class SettingsController Handles settings related API actions for the Cron
* @package OPNsense\Cron
*/
class SettingsController extends ApiControllerBase
{
/**
* retrieve job settings or return defaults
* @param $uuid item unique id
* @return array
*/
public function getJobAction($uuid = null)
{
$mdlCron = new Cron();
if ($uuid != null) {
$node = $mdlCron->getNodeByReference('jobs.job.' . $uuid);
if ($node != null) {
// return node
return array("job" => $node->getNodes());
}
} else {
// generate new node, but don't save to disc
$node = $mdlCron->jobs->job->add();
return array("job" => $node->getNodes());
}
return array();
}
/**
* update job with given properties
* @param $uuid item unique id
* @return array
*/
public function setJobAction($uuid)
{
if ($this->request->isPost() && $this->request->hasPost("job")) {
$mdlCron = new Cron();
if ($uuid != null) {
$node = $mdlCron->getNodeByReference('jobs.job.' . $uuid);
if ($node != null) {
$result = array("result" => "failed", "validations" => array());
$jobInfo = $this->request->getPost("job");
if ( $node->origin->__toString() != "cron" ){
if ( $jobInfo["command"]!=$node->command->__toString() ) {
$result["validations"]["job.command"] = "This item has been created by another service, command and parameter may not be changed.";
}
if ( $jobInfo["parameters"]!=$node->parameters->__toString() ) {
$result["validations"]["job.parameters"] = "This item has been created by another service, command and parameter may not be changed. (was: " . $node->parameters->__toString() . " )";
}
}
$node->setNodes($jobInfo);
$valMsgs = $mdlCron->performValidation();
foreach ($valMsgs as $field => $msg) {
$fieldnm = str_replace($node->__reference, "job", $msg->getField());
if ($fieldnm != $msg->getField()) {
// only collect validation errors for the item we're currently editing.
$result["validations"][$fieldnm] = $msg->getMessage();
}
}
if (count($result['validations']) == 0) {
// we've already performed a validation, prevent issues from other items in the model reflecting back to us.
$mdlCron->serializeToConfig($disable_validation = true);
// save config if validated correctly
Config::getInstance()->save();
$result = array("result" => "saved");
}
return $result;
}
}
}
return array("result" => "failed");
}
/**
* add new job and set with attributes from post
* @return array
*/
public function addJobAction()
{
$result = array("result" => "failed");
if ($this->request->isPost() && $this->request->hasPost("job")) {
$result = array("result" => "failed", "validations" => array());
$mdlCron = new Cron();
$node = $mdlCron->jobs->job->Add();
$node->setNodes($this->request->getPost("job"));
$node->origin = "cron"; // set origin to this component - cron are manually created rules.
$valMsgs = $mdlCron->performValidation();
foreach ($valMsgs as $field => $msg) {
$fieldnm = str_replace($node->__reference, "job", $msg->getField());
if ($fieldnm != $msg->getField()) {
// only collect validation errors for the item we're currently editing.
$result["validations"][$fieldnm] = $msg->getMessage();
}
}
if (count($result['validations']) == 0) {
// we've already performed a validation, prevent issues from other items in the model reflecting back to us.
$mdlCron->serializeToConfig($disable_validation = true);
// save config if validated correctly
Config::getInstance()->save();
$result = array("result" => "saved");
}
return $result;
}
return $result;
}
/**
* delete job by uuid
* @param $uuid item unique id
* @return array status
*/
public function delJobAction($uuid)
{
$result = array("result" => "failed");
if ($this->request->isPost()) {
$mdlCron = new Cron();
if ($uuid != null) {
if ($mdlCron->jobs->job->del($uuid)) {
// if item is removed, serialize to config and save
$mdlCron->serializeToConfig($disable_validation = true);
Config::getInstance()->save();
$result['result'] = 'deleted';
} else {
$result['result'] = 'not found';
}
}
}
return $result;
}
/**
* toggle job by uuid (enable/disable)
* @param $uuid item unique id
* @return array status
*/
public function toggleJobAction($uuid)
{
$result = array("result" => "failed");
if ($this->request->isPost()) {
$mdlCron = new Cron();
if ($uuid != null) {
$node = $mdlCron->getNodeByReference('jobs.job.' . $uuid);
if ($node != null) {
if ($node->enabled->__toString() == "1") {
$result['result'] = "Disabled";
$node->enabled = "0";
} else {
$result['result'] = "Enabled";
$node->enabled = "1";
}
// if item has toggled, serialize to config and save
$mdlCron->serializeToConfig($disable_validation = true);
Config::getInstance()->save();
}
}
}
return $result;
}
/**
*
* search cron jobs
* @return array
*/
public function searchJobsAction()
{
// if ($this->request->isPost()) {
$this->sessionClose();
// fetch query parameters
$itemsPerPage = $this->request->getPost('rowCount', 'int', 9999);
$currentPage = $this->request->getPost('current', 'int', 1);
$sortBy = array("description");
$sortDescending = false;
if ($this->request->hasPost('sort') && is_array($this->request->getPost("sort"))) {
$sortBy = array_keys($this->request->getPost("sort"));
if ($this->request->getPost("sort")[$sortBy[0]] == "desc") {
$sortDescending = true;
}
}
$searchPhrase = $this->request->getPost('searchPhrase', 'string', '');
// create model and fetch query resuls
$fields = array(
"enabled",
"minutes",
"hours",
"days",
"months",
"weekdays",
"description",
"command",
"origin",
"cronPermissions"
);
$mdlCron = new Cron();
$grid = new UIModelGrid($mdlCron->jobs->job);
return $grid->fetch($fields, $itemsPerPage, $currentPage, $sortBy, $sortDescending, $searchPhrase);
// } else {
// return array();
// }
}
}
\ No newline at end of file
<?php
/**
* 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\Cron;
/**
* Class IndexController
* @package OPNsense\Cron
*/
class IndexController extends \OPNsense\Base\IndexController
{
public function indexAction()
{
$this->view->title = "Cron settings";
// include dialog form definitions
$this->view->formDialogEdit = $this->getForm("dialogEdit");
$this->view->pick('OPNsense/Cron/index');
}
}
<form>
<field>
<id>job.enabled</id>
<label>enabled</label>
<type>checkbox</type>
<help>Select if job is enabled or not</help>
</field>
<field>
<id>job.minutes</id>
<label>Minutes</label>
<type>text</type>
<help>Enter the minutes for the job to act, can also be a comma separated list, * (each) or a range (ex. 10,20,30 or 10-30)</help>
</field>
<field>
<id>job.hours</id>
<label>Hours</label>
<type>text</type>
<help>Enter the hours for the job to act, can also be a comma separated list, * (each) or a range (ex. 10,11,12 or 10-12)</help>
</field>
<field>
<id>job.days</id>
<label>Day of the month</label>
<type>text</type>
<help>Enter the days of the month for the job to act, can also be a comma separated list, * (each) or a range (ex. 1,20,28 or 1-28)</help>
</field>
<field>
<id>job.months</id>
<label>Months</label>
<type>text</type>
<help>Enter the months for the job to act, can also be a comma separated list, * (each) or a range (ex. 1,2,3 or 1-3)</help>
</field>
<field>
<id>job.weekdays</id>
<label>Days of the week</label>
<type>text</type>
<help>Enter the days of the week for the job to act, can also be a comma separated list, * (each) or a range (ex. 1,2,3 or 1-3)</help>
</field>
<field>
<id>job.command</id>
<label>Command</label>
<type>dropdown</type>
<help>Select the command that needs to be executed at given time frame.</help>
</field>
<field>
<id>job.parameters</id>
<label>Parameters</label>
<type>text</type>
<help>Enter parameters for this job if required.</help>
</field>
<field>
<id>job.description</id>
<label>Description</label>
<type>text</type>
<help>Enter a description to explain what this job is intended for.</help>
</field>
</form>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment