Add puppet-agent plugin (#2358)

This commit is contained in:
jan-win1993
2021-05-19 12:32:51 +02:00
committed by GitHub
parent 0334ceae56
commit b2a556f057
16 changed files with 489 additions and 0 deletions
+8
View File
@@ -0,0 +1,8 @@
PLUGIN_NAME= puppet-agent
PLUGIN_VERSION= 0.1
PLUGIN_DEVEL= yes
PLUGIN_COMMENT= Manage Puppet Agent
PLUGIN_DEPENDS= puppet7
PLUGIN_MAINTAINER= jan.wink93@gmail.com
.include "../../Mk/plugins.mk"
+7
View File
@@ -0,0 +1,7 @@
Puppet lets you centrally manage every important aspect of your system using
a cross-platform specification language that manages all the separate
elements normally aggregated in different files, like users, cron jobs, and
hosts, along with obviously discrete elements like packages, services, and
files.
WWW: https://puppet.com/docs/puppet/latest/man/agent.html
@@ -0,0 +1,61 @@
<?php
/**
* Copyright (C) 2021 Jan Winkler
* 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.
*/
function puppetagent_enabled()
{
global $config;
return isset($config['OPNsense']['puppetagent']['general']['Enabled']) &&
$config['OPNsense']['puppetagent']['general']['Enabled'] == 1;
}
/**
* register legacy service
* @return array
*/
function puppetagent_services()
{
$services = array();
if (!puppetagent_enabled()) {
return $services;
}
$services[] = array(
'description' => gettext('Puppet Agent Service'),
'pidfile' => '/var/run/puppet/agent.pid',
'configd' => array(
'restart' => array('puppetagent restart'),
'start' => array('puppetagent start'),
'stop' => array('puppetagent stop'),
),
'name' => 'puppet-agent',
);
return $services;
}
@@ -0,0 +1,75 @@
<?php
/**
* Copyright (C) 2021 Jan Winkler
* 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\PuppetAgent\Api;
use OPNsense\Base\ApiMutableServiceControllerBase;
use OPNsense\Core\Backend;
use OPNsense\PuppetAgent\PuppetAgent;
/**
* Class ServiceController
* @package OPNsense\PuppetAgent
*/
class ServiceController extends ApiMutableServiceControllerBase
{
protected static $internalServiceClass = '\OPNsense\PuppetAgent\PuppetAgent';
protected static $internalServiceTemplate = 'OPNsense/PuppetAgent';
protected static $internalServiceEnabled = 'general.Enabled';
protected static $internalServiceName = 'puppetagent';
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/PuppetAgent');
$mdlPuppetAgent = new PuppetAgent();
// (res)start daemon
if ($mdlPuppetAgent->general->Enabled->__toString() == 1) {
$this->startAction();
}
// stop Puppet Agent when disabled
else {
$this->stopAction();
}
return array("status" => "ok");
}
else {
return array("status" => "failed");
}
}
}
@@ -0,0 +1,93 @@
<?php
/**
* Copyright (C) 2021 Jan Winkler
* 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\PuppetAgent\Api;
use OPNsense\Base\ApiControllerBase;
use OPNsense\PuppetAgent\PuppetAgent;
use OPNsense\Core\Config;
/**
* Class SettingsController Handles settings related API actions for the PuppetAgent module
* @package OPNsense\PuppetAgent
*/
class SettingsController extends ApiControllerBase
{
/**
* retrieve PuppetAgent general settings
* @return array general settings
* @throws \OPNsense\Base\ModelException
* @throws \ReflectionException
*/
public function getAction()
{
// define list of configurable settings
$result = array();
if ($this->request->isGet()) {
$mdlPuppetAgent = new PuppetAgent();
$result['puppetagent'] = $mdlPuppetAgent->getNodes();
}
return $result;
}
/**
* update PupppetAgent settings
* @return array status
* @throws \OPNsense\Base\ModelException
* @throws \ReflectionException
*/
public function setAction()
{
$result = array("result" => "failed");
if ($this->request->isPost()) {
// load model and update with provided data
$mdlPuppetAgent = new PuppetAgent();
$mdlPuppetAgent->setNodes($this->request->getPost("puppetagent"));
// perform validation
$valMsgs = $mdlPuppetAgent->performValidation();
foreach ($valMsgs as $field => $msg) {
if (!array_key_exists("validations", $result)) {
$result["validations"] = array();
}
$result["validations"]["puppetagent." . $msg->getField()] = $msg->getMessage();
}
// serialize model to config and save
if ($valMsgs->count() == 0) {
$mdlPuppetAgent->serializeToConfig();
Config::getInstance()->save();
$result["result"] = "saved";
}
}
return $result;
}
}
@@ -0,0 +1,47 @@
<?php
/**
* Copyright (C) 2021 Jan Winkler
* 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\PuppetAgent;
/**
* Class IndexController
* @package OPNsense\PuppetAgent
*/
class IndexController extends \OPNsense\Base\IndexController
{
public function indexAction()
{
// pick the template to serve to our users.
$this->view->pick('OPNsense/PuppetAgent/index');
// fetch form data "general" in
$this->view->generalForm = $this->getForm("general");
}
}
@@ -0,0 +1,20 @@
<form>
<field>
<id>puppetagent.general.Enabled</id>
<label>Enabled</label>
<type>checkbox</type>
<help>Enable Puppet Agent</help>
</field>
<field>
<id>puppetagent.general.FQDN</id>
<label>Puppet Server FQDN</label>
<type>text</type>
<help>Change Puppet Server FQDN</help>
</field>
<field>
<id>puppetagent.general.Environment</id>
<label>Puppet Environment</label>
<type>text</type>
<help>Change Puppet Agent Environment</help>
</field>
</form>
@@ -0,0 +1,9 @@
<acl>
<page-user-puppetagent>
<name>Services: Puppet Agent</name>
<patterns>
<pattern>ui/puppetagent/*</pattern>
<pattern>api/puppetagent/*</pattern>
</patterns>
</page-user-puppetagent>
</acl>
@@ -0,0 +1,5 @@
<menu>
<Services>
<PuppetAgent VisibleName="Puppet Agent" url="/ui/puppetagent"/>
</Services>
</menu>
@@ -0,0 +1,38 @@
<?php
/**
* Copyright (C) 2021 Jan Winkler
* 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\PuppetAgent;
use OPNsense\Base\BaseModel;
class PuppetAgent extends BaseModel
{
}
@@ -0,0 +1,24 @@
<model>
<mount>//OPNsense/puppetagent</mount>
<description>
Manage Puppet Agent service
</description>
<items>
<!-- container -->
<general>
<!-- fields -->
<Enabled type="BooleanField">
<default>0</default>
<Required>Y</Required>
</Enabled>
<FQDN type="HostnameField">
<Required>Y</Required>
</FQDN>
<Environment type="TextField">
<Required>Y</Required>
<mask>/^.{1,100}$/u</mask>
<ValidationMessage>Should be a string between 1 and 100 characters.</ValidationMessage>
</Environment>
</general>
</items>
</model>
@@ -0,0 +1,62 @@
{#
OPNsense® is Copyright © 2021 Jan Winkler
OPNsense® is Copyright © 2014 2015 by 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.
#}
<script>
$( document ).ready(function() {
var data_get_map = {'frm_GeneralSettings':"/api/puppetagent/settings/get"};
mapDataToFormUI(data_get_map).done(function(data){
// place actions to run after load, for example update form styles.
});
updateServiceControlUI('puppetagent');
// link save button to API set action
$("#saveAct").click(function(){
saveFormToEndpoint(url="/api/puppetagent/settings/set",formid='frm_GeneralSettings',callback_ok=function(){
// action to run after successful save, for example reconfigure service.
ajaxCall(url="/api/puppetagent/service/reconfigure", sendData={},callback=function(data,status) {
// action to run after reload
updateServiceControlUI('puppetagent');
});
});
});
});
</script>
<div class="alert alert-info hidden" role="alert" id="responseMsg">
</div>
<div class="col-md-12">
{{ partial("layout_partials/base_form",['fields':generalForm,'id':'frm_GeneralSettings'])}}
</div>
<div class="col-md-12">
<button class="btn btn-primary" id="saveAct" type="button"><b>{{ lang._('Save') }}</b></button>
</div>
@@ -0,0 +1,23 @@
[start]
command:/usr/local/etc/rc.d/puppet start
parameters:
type:script
message:starting puppet agent
[stop]
command:/usr/local/etc/rc.d/puppet onestop
parameters:
type:script
message:stop puppet agent
[restart]
command:/usr/local/etc/rc.d/puppet restart
parameters:
type:script
message:restart puppet agent
[status]
command:/usr/local/etc/rc.d/puppet onestatus; exit 0
parameters:
type:script_output
message:request puppet agent status
@@ -0,0 +1,2 @@
puppetagent.conf:/usr/local/etc/puppet/puppet.conf
rc.conf.d:/etc/rc.conf.d/puppet
@@ -0,0 +1,10 @@
{% if helpers.exists('OPNsense.puppetagent.general') and OPNsense.puppetagent.general.Enabled|default("0") == "1" %}
[main]
certname = {{ system.hostname|lower }}.{{ system.domain|lower }}
server = {{ OPNsense.puppetagent.general.FQDN|default("") }}
{% if helpers.exists('OPNsense.puppetagent.general') and not helpers.empty('OPNsense.puppetagent.general.Environment') %}
[agent]
environment = {{ OPNsense.puppetagent.general.Environment|default("") }}
{% endif %}
{% endif %}
@@ -0,0 +1,5 @@
{% if helpers.exists('OPNsense.puppetagent.general') and OPNsense.puppetagent.general.Enabled|default("0") == "1" %}
puppet_enable="YES"
{% else %}
puppet_enable="NO"
{% endif %}