#12905 cleared warning message a functions for migrate

This commit is contained in:
Daniel Cebrian 2024-03-04 10:00:17 +01:00
parent 0dd3d14d30
commit f6f41efc85
5 changed files with 0 additions and 432 deletions

View File

@ -81,9 +81,6 @@ class Applications extends Wizard
'index.php?sec=gservers&sec2=godmode/servers/discovery&wiz=app'
);
// Print Warning Message.
$this->printWarningMessage();
return $this;
}

View File

@ -190,9 +190,6 @@ class Cloud extends Wizard
echo $output;
echo '<div class="app_mssg"><i>*'.__('All company names used here are for identification purposes only. Use of these names, logos, and brands does not imply endorsement.').'</i></div>';
// Print Warning Message.
$this->printWarningMessage();
}

View File

@ -222,10 +222,6 @@ class DiscoveryTaskList extends HTML
html_print_action_buttons($this->printForm($form, true));
}
// Warning Message.
$wizar_main = new Wizard();
$wizar_main->printWarningMessage();
return $ret;
}

View File

@ -766,24 +766,6 @@ class ManageExtensions extends HTML
],
);
}
$migrationHash = $this->canMigrate($row['short_name']);
if ($migrationHash !== false && empty($migrationHash) !== true) {
// Migrate button.
$data[$key]['actions'] .= html_print_input_image(
'button_migrate-'.$row['short_name'],
'images/reset.png',
'',
'',
true,
[
'onclick' => 'show_migration_form(\''.$row['short_name'].'\',\''.$migrationHash.'\')',
'title' => __('Migrate old discovery tasks.'),
'alt' => __('Migrate old discovery tasks.'),
'class' => 'main_menu_icon invert_filter',
]
);
}
}
if (empty($data) === true) {
@ -1122,386 +1104,6 @@ class ManageExtensions extends HTML
}
/**
* Checks if the discovery app can be migrated to .disco system.
* If app is migrated or is not in .ini file, it cannot be migrated.
*
* @param string $shortName Short name of the discovery app.
*
* @return string App hash, false in case hash doesnt exist on ini file, or is already migraeted..
*/
private function canMigrate(string $shortName='')
{
global $config;
if (empty($shortName) === true) {
return false;
}
// 1. Check if app is already migrated:
// Get migrated Discovery Apps from config.
$migratedAppsJson = db_get_value('value', 'tconfig', 'token', 'migrated_discovery_apps');
if ($migratedAppsJson === false || empty($migratedAppsJson) === true) {
return false;
}
// Decode JSON migrated apps.
$migrateApps = json_decode(io_safe_output($migratedAppsJson), true);
if (json_last_error() !== JSON_ERROR_NONE) {
return false;
}
// Check app migrated.
if (array_key_exists($shortName, $migrateApps)) {
if (empty($migrateApps[$shortName]) === false && (bool) $migrateApps[$shortName] === true) {
// Already migrated.
return false;
}
}
// 2. If app not migrated yet, check DiscoveryApplicationsMigrateCodes.ini
// Path to the INI file
$filePath = $config['homedir'].'/extras/discovery/DiscoveryApplicationsMigrateCodes.ini';
// Parse the INI file.
$migrationCodes = parse_ini_file($filePath, true);
if ($migrationCodes === false) {
return false;
}
// Check shortname in ini file.
if (array_key_exists($shortName, $migrationCodes) === false) {
return false;
} else {
return $migrationCodes[$shortName];
}
// All checks ok, discovery app can be migrated.
return false;
}
/**
* Prints html for migrate modal
*
* @return void
*/
public function loadMigrateModal()
{
$shortname = get_parameter('shortname', null);
$hash = get_parameter('hash', null);
$form = [
'action' => '#',
'id' => 'modal_migrate_form',
'onsubmit' => 'return false;',
'class' => 'modal',
'name' => 'migrate_form',
];
$inputs = [];
$migrateMessage = __(
'All legacy tasks for this application will be migrated to the new .disco package system. All configurations and executions will be managed with the new system. This process will not be reversible.</br></br>'
);
$migrateMessage .= _('Please check the migration code for the application before proceeding.');
$inputs[] = [
'wrapper' => 'div',
'block_id' => 'div_migrate_message',
'class' => 'hole flex-row flex-items-center w98p',
'direct' => 1,
'block_content' => [
[
'label' => $migrateMessage,
'arguments' => [
'class' => 'first_lbl w98p',
'name' => 'lbl_migrate_message',
'id' => 'lbl_migrate_message',
],
],
],
];
$inputs[] = [
'label' => __('Applicattion hash'),
'id' => 'div-hash',
'arguments' => [
'name' => 'hash',
'type' => 'text',
'value' => $hash,
'return' => true,
],
];
$inputs[] = [
'block_id' => 'migrate_buttons',
'class' => 'flex-row flex-items-center w98p',
'direct' => 1,
'block_content' => [
[
'arguments' => [
'name' => 'cancel',
'label' => __('Cancel'),
'type' => 'button',
'attributes' => [
'icon' => 'left',
'mode' => 'secondary',
'class' => 'sub cancel float-left',
],
],
],
[
'arguments' => [
'name' => 'migrate',
'label' => __('Migrate'),
'type' => 'submit',
'attributes' => [
'icon' => 'wand',
'class' => 'sub wand float-right',
],
],
],
],
];
$spinner = '<div id="migration-spinner" class="invisible spinner-fixed"></div>';
$migration_form = $this->printForm(
[
'form' => $form,
'inputs' => $inputs,
],
true,
);
echo $migration_form.$spinner;
}
/**
* Migrate app to new .disco system
*
* @return true if success, false in case of error.
*/
public function migrateApp()
{
global $config;
$hash = get_parameter('hash', false);
$shortName = get_parameter('shortName', false);
if ($hash === false || $shortName === false) {
return false;
}
// 1. Gets md5
try {
$console_md5 = $this->calculateDirectoryMD5($shortName, false);
$server_md5 = $this->calculateDirectoryMD5($shortName, true);
} catch (Exception $e) {
$return = [
'error' => $e->getMessage(),
];
echo json_encode($return);
return;
}
if ($console_md5 === false || $server_md5 === false) {
$return = [
'error' => __('Error calculating app MD5'),
];
echo json_encode($return);
return;
}
// 2. Checks MD5
if ($hash === $console_md5 && $hash === $server_md5) {
// Init migration script.
$return = $this->executeMigrationScript($shortName);
} else {
$return = [
'error' => __('App hash does not match.'),
];
}
// Add shotrname to return for showing messages.
$return['shortname'] = $shortName;
echo \json_encode($return);
}
/**
* Calculates directory MD% and saves it into array
*
* @param string $shortName Shorname of app.
* @param boolean $server If true, perform checks into server folder.
*
* @return $md5 Array of md5 of filess.
*/
private function calculateDirectoryMD5($shortName, $server)
{
global $config;
$md5List = [];
$serverPath = $config['remote_config'].'/discovery/'.$shortName;
$consolePath = $config['homedir'].'/'.$this->path.'/'.$shortName;
if ($server === true) {
$directory = $serverPath;
} else {
$directory = $consolePath;
}
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
foreach ($iterator as $file) {
if ($file->isFile()) {
$md5List[] = md5_file($file->getPathname());
}
}
if ($server === true) {
$console_ini = $consolePath.'/discovery_definition.ini';
$logo = $consolePath.'/logo.png';
if (file_exists($console_ini)) {
$md5List[] = md5_file($console_ini);
}
if (file_exists($logo)) {
$md5List[] = md5_file($logo);
}
}
sort($md5List);
$concatenatedChecksums = implode('', $md5List);
return md5($concatenatedChecksums);
}
/**
* Executed migration script for app
*
* @param string $shortName Shortname of the app.
*
* @return true on success, false in case of error.
*/
private function executeMigrationScript(string $shortName)
{
global $config;
$dblock = db_get_lock('migrate-working');
// Try to get a lock from DB.
if ($dblock !== 1) {
// Locked!
return false;
}
$scriptName = preg_replace('/^pandorafms\.(\w+\.?\w*)$/m', 'migrate.$1.sql', $shortName);
$script_path = $config['homedir'].'/extras/discovery/migration_scripts/'.$scriptName;
if (file_exists($script_path) === false) {
$return = [
'error' => __('Migration script '.$scriptName.' could not be found'),
];
} else {
try {
$res = db_process_file($script_path, false);
} catch (\Exception $e) {
$return = [
'error' => $e->getMessage(),
];
} finally {
db_release_lock('migrate_working');
}
if ($res === true) {
$migrateAppsJson = io_safe_output(
db_get_value(
'value',
'tconfig',
'token',
'migrated_discovery_apps'
)
);
$migrateApps = json_decode(
$migrateAppsJson,
true
);
$migrateApps[$shortName] = 1;
$migratedAppsJson = json_encode($migrateApps);
if (json_last_error() === JSON_ERROR_NONE) {
config_update_value(
'migrated_discovery_apps',
$migratedAppsJson
);
} else {
$return = [
'error' => __('Error decoding migrated apps json.'),
];
}
$return = [
'result' => __('App migrated successfully'),
'shortName' => $shortName,
];
} else {
$return = [
'error' => __('Error migrating app'),
];
}
}
return $return;
}
/**
* Check if legacy app has been migrated.
*
* @param string $shortName Shorn name of the app.
*
* @return boolean
*/
static public function isMigrated($shortName)
{
global $config;
$migrateAppsJson = io_safe_output(
db_get_value(
'value',
'tconfig',
'token',
'migrated_discovery_apps'
)
);
$migratedApps = json_decode($migrateAppsJson, true);
if (json_last_error() !== JSON_ERROR_NONE) {
return false;
}
if (array_key_exists($shortName, $migratedApps) === true && empty($migratedApps[$shortName] === false)) {
return (bool) $migratedApps[$shortName];
} else {
return false;
}
}
/**
* Read metadata CSV from system and store data structure in memory.
*

View File

@ -566,28 +566,4 @@ class Wizard
}
/**
* Generates warning message.
*
* @return void Warning message.
*/
public function printWarningMessage()
{
return ui_print_warning_message(
__(
'Starting with version 773, the new modular system of discovery 2.0 has been implemented. The current
discovery (1.0) and its defined tasks will continue to function normally until the next LTS version,
in which migration to the new system will be mandatory.
The tasks of the current discovery (1.0) will be marked as legacy although it will not affect their
operation, it will only be a visual indicator to identify and differentiate the tasks of discovery 1.0
from those of the new version 2.0.
In the intermediate versions between the 773 and the next LTS version, more applications of the new
discovery 2.0 will be added. Both new and those that will come to replace the applications of the
current discovery 1.0. In addition, an automatic migration tool for legacy (1.0) tasks to the new 2.0
model will be included.'
)
);
}
}