WIP Plugins sync

This commit is contained in:
fbsanchez 2021-06-16 18:18:38 +02:00
parent b032beb559
commit 81254cf3fb
4 changed files with 166 additions and 46 deletions

View File

@ -99,4 +99,4 @@ include/javascript/update_manager.js
enterprise/include/functions_update_manager.php
include/ajax/rolling_release.ajax.php
extensions/plugin_registration.php
enterprise/include/functions_plugins.php

View File

@ -1,19 +1,36 @@
<?php
/**
* Manage plugins.
*
* @category Utility
* @package Pandora FMS
* @subpackage Plugins
* @version 1.0.0
* @license See below
*
* ______ ___ _______ _______ ________
* | __ \.-----.--.--.--| |.-----.----.-----. | ___| | | __|
* | __/| _ | | _ || _ | _| _ | | ___| |__ |
* |___| |___._|__|__|_____||_____|__| |___._| |___| |__|_|__|_______|
*
* ============================================================================
* Copyright (c) 2005-2021 Artica Soluciones Tecnologicas
* Please see http://pandorafms.org for full contribution list
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation for version 2.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* ============================================================================
*/
// Pandora FMS - http://pandorafms.com
// ==================================================
// Copyright (c) 2005-2021 Artica Soluciones Tecnologicas
// Please see http://pandorafms.org for full contribution list
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation for version 2.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Load global vars
// Begin.
global $config;
require_once $config['homedir'].'/include/functions_plugins.php';
if (is_ajax()) {
$get_plugin_description = get_parameter('get_plugin_description');
$get_list_modules_and_component_locked_plugin = (bool) get_parameter('get_list_modules_and_component_locked_plugin', 0);
@ -789,38 +806,15 @@ if (($create != '') || ($view != '')) {
}
}
if ($plugin_id != 0) {
// Delete all the modules with this plugin
$plugin_modules = db_get_all_rows_filter(
'tagente_modulo',
['id_plugin' => $plugin_id]
);
if (empty($plugin_modules)) {
$plugin_modules = [];
}
foreach ($plugin_modules as $pm) {
modules_delete_agent_module($pm['id_agente_modulo']);
}
if (enterprise_installed()) {
enterprise_include_once('include/functions_policies.php');
$policies_ids = db_get_all_rows_filter('tpolicy_modules', ['id_plugin' => $plugin_id]);
foreach ($policies_ids as $policies_id) {
policies_change_delete_pending_module($policies_id['id']);
}
}
if (is_metaconsole()) {
enterprise_include_once('include/functions_plugins.php');
$result = plugins_delete_plugin($plugin_id);
if (!$result) {
ui_print_error_message(__('Problem deleting plugin'));
} else {
ui_print_success_message(__('Plugin deleted successfully'));
}
if ((int) $plugin_id > 0) {
// Delete all iformation related with this plugin.
$result = plugins_delete_plugin($plugin_id);
if (empty($result) === false) {
ui_print_error_message(
implode('<br>', $result)
);
} else {
ui_print_success_message(__('Plugin deleted successfully'));
}
}
}

View File

@ -542,7 +542,10 @@ if (file_exists($uploaded_filename) === true) {
if ($error !== null && $error !== '') {
ui_print_error_message($error);
} else if (is_management_allowed() === true && is_metaconsole() === true) {
} else if ($error === null
&& is_management_allowed() === true
&& is_metaconsole() === true
) {
$attachment = '/'.str_replace(
$config['homedir'],
'',

View File

@ -0,0 +1,123 @@
<?php
/**
* Aux functions to manage plugins.
*
* @category library
* @package Pandora FMS
* @subpackage Plugins
* @version 1.0.0
* @license See below
*
* ______ ___ _______ _______ ________
* | __ \.-----.--.--.--| |.-----.----.-----. | ___| | | __|
* | __/| _ | | _ || _ | _| _ | | ___| |__ |
* |___| |___._|__|__|_____||_____|__| |___._| |___| |__|_|__|_______|
*
* ============================================================================
* Copyright (c) 2005-2021 Artica Soluciones Tecnologicas
* Please see http://pandorafms.org for full contribution list
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation for version 2.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* ============================================================================
*/
use PandoraFMS\Enterprise\Metaconsole\Synchronizer;
// Begin.
global $config;
/**
* Remove all modules associated to this plugin.
*
* @param integer $id_plugin Plugin id.
*
* @return array Of erros if any.
*/
function plugins_remove_modules(int $id_plugin)
{
// Delete all resources related with this plugin.
$plugin_modules = db_get_all_rows_filter(
'tagente_modulo',
[ 'id_plugin' => $id_plugin ],
'id_agente_modulo'
);
$ret = [];
foreach ($plugin_modules as $id) {
try {
$module = new PandoraFMS\Module($id);
$module->delete();
} catch (Exception $e) {
$ret[] = __('Failed to erase module %d: %s', $id, $e->getMessage());
}
}
if (enterprise_installed() === true) {
enterprise_include_once('include/functions_policies.php');
$policies_ids = db_get_all_rows_filter(
'tpolicy_modules',
['id_plugin' => $id_plugin],
'id'
);
foreach ($policies_ids as $id) {
if (policies_change_delete_pending_module($id) !== true) {
$ret[] = __('Failed to erase policy module: %d', $id);
}
}
}
return $ret;
}
/**
* Effectively remove a plugin from the system.
*
* @param integer $id_plugin Plugin id.
*
* @return array Of errors, empty if no errors.
*/
function plugins_delete_plugin(int $id_plugin)
{
$result = [];
$problem = plugins_remove_modules($id_plugin) !== true;
if (empty($problem) !== false) {
$result = $problem;
}
if (is_metaconsole() === true && is_management_allowed() === true) {
$sc = new Synchronizer();
$problems = $sc->apply(
function ($node) use ($id_plugin) {
$rt = [];
try {
$node->connect();
$rt = plugins_remove_modules($id_plugin);
$node->disconnect();
} catch (Exception $e) {
$rt[] = $e->getMessage();
}
return $rt;
},
false
);
foreach ($problems as $prob) {
$result = array_merge($result, $prob);
}
}
return $result;
}