Migration dicovery fix bugs and changed script paths

This commit is contained in:
Calvo 2023-10-05 10:52:52 +02:00
parent db291c77f5
commit d091ee8194
4 changed files with 177 additions and 96 deletions

View File

@ -761,7 +761,7 @@ class DiscoveryTaskList extends HTML
$data[3] = __('Manual'); $data[3] = __('Manual');
} }
if ($task['id_recon_script'] == 0 || $ipam === true) { if ($task['id_recon_script'] == 0 || $ipam === true && $task['type'] !== 15) {
$data[4] = ui_print_truncate_text($subnet, 50, true, true, true, '[…]'); $data[4] = ui_print_truncate_text($subnet, 50, true, true, true, '[…]');
} else { } else {
$data[4] = '-'; $data[4] = '-';

View File

@ -757,7 +757,7 @@ class ManageExtensions extends HTML
$migrationHash = $this->canMigrate($row['short_name']); $migrationHash = $this->canMigrate($row['short_name']);
if ($migrationHash !== false && empty($migrationHash) !== true) { if ($migrationHash !== false && empty($migrationHash) !== true) {
// Migrate button // Migrate button.
$data[$key]['actions'] .= html_print_input_image( $data[$key]['actions'] .= html_print_input_image(
'button_migrate-'.$row['short_name'], 'button_migrate-'.$row['short_name'],
'images/reset.png', 'images/reset.png',
@ -1153,7 +1153,7 @@ class ManageExtensions extends HTML
// Path to the INI file // Path to the INI file
$filePath = $config['homedir'].'/extras/discovery/DiscoveryApplicationsMigrateCodes.ini'; $filePath = $config['homedir'].'/extras/discovery/DiscoveryApplicationsMigrateCodes.ini';
// Parse the INI file // Parse the INI file.
$migrationCodes = parse_ini_file($filePath, true); $migrationCodes = parse_ini_file($filePath, true);
if ($migrationCodes === false) { if ($migrationCodes === false) {
@ -1167,7 +1167,7 @@ class ManageExtensions extends HTML
return $migrationCodes[$shortName]; return $migrationCodes[$shortName];
} }
// All checks ok, discovery app can be migrated // All checks ok, discovery app can be migrated.
return false; return false;
} }
@ -1176,9 +1176,6 @@ class ManageExtensions extends HTML
/** /**
* Prints html for migrate modal * Prints html for migrate modal
* *
* @param string $shortName Short name of app
* @param string $hash App hash
*
* @return void * @return void
*/ */
public function loadMigrateModal() public function loadMigrateModal()
@ -1191,6 +1188,7 @@ class ManageExtensions extends HTML
'id' => 'modal_migrate_form', 'id' => 'modal_migrate_form',
'onsubmit' => 'return false;', 'onsubmit' => 'return false;',
'class' => 'modal', 'class' => 'modal',
'name' => 'migrate_form',
]; ];
$inputs = []; $inputs = [];
@ -1227,12 +1225,43 @@ class ManageExtensions extends HTML
], ],
]; ];
$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',
],
],
],
],
];
$this->printForm( $this->printForm(
[ [
'form' => $form, 'form' => $form,
'inputs' => $inputs, 'inputs' => $inputs,
], ],
false false,
); );
} }
@ -1254,16 +1283,28 @@ class ManageExtensions extends HTML
} }
// 1. Gets md5 // 1. Gets md5
$console_md5 = $this->calculateDirectoryMD5($shortName, false); try {
$server_md5 = $this->calculateDirectoryMD5($shortName, true); $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) { if ($console_md5 === false || $server_md5 === false) {
return false; $return = [
'error' => __('Error calculating app MD5'),
];
echo json_encode($return);
return;
} }
// 2. Checks MD5 // 2. Checks MD5
if ($hash === $console_md5 && $hash === $server_md5) { if ($hash === $console_md5 && $hash === $server_md5) {
// Init migration script // Init migration script.
$return = $this->executeMigrationScript($shortName); $return = $this->executeMigrationScript($shortName);
} else { } else {
$return = [ $return = [
@ -1274,18 +1315,19 @@ class ManageExtensions extends HTML
// Add shotrname to return for showing messages. // Add shotrname to return for showing messages.
$return['shortname'] = $shortName; $return['shortname'] = $shortName;
echo json_encode($return); echo \json_encode($return);
} }
/** /**
* Calculates directory MD% and saves it into array * Calculates directory MD% and saves it into array
* *
* @param string shortName Shorname of app. * @param string $shortName Shorname of app.
* @param boolean $directory * @param boolean $server If true, perform checks into server folder.
*
* @return $md5 Array of md5 of filess. * @return $md5 Array of md5 of filess.
*/ */
function calculateDirectoryMD5($shortName, $server) private function calculateDirectoryMD5($shortName, $server)
{ {
global $config; global $config;
@ -1345,57 +1387,63 @@ class ManageExtensions extends HTML
return false; return false;
} }
$scriptName = preg_replace('/^pandorafms\.(\w+)$/m', 'migrate.$1.sql', $shortName); $scriptName = preg_replace('/^pandorafms\.(\w+\.?\w*)$/m', 'migrate.$1.sql', $shortName);
$script_path = $config['homedir'].'/extras/discovery/migration_scripts/'.$scriptName; $script_path = $config['homedir'].'/extras/discovery/migration_scripts/'.$scriptName;
try { if (file_exists($script_path) === false) {
$res = db_process_file($script_path, true);
} catch (\Exception $e) {
$return = [ $return = [
'error' => $e->getMessage(), 'error' => __('Migration script '.$scriptName.' could not be found'),
]; ];
} finally { } else {
db_release_lock('migrate_working'); try {
} $res = db_process_file($script_path, false);
} catch (\Exception $e) {
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 = [ $return = [
'error' => __('Error decoding migrated apps json.'), '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 = [ $return = [
'result' => __('App migrated successfully'), 'error' => __('Error migrating app'),
'shortName' => $shortName,
]; ];
} else { }
$return = [
'error' => __('Error migrating app'),
];
} }
return $return; return $return;
@ -1406,7 +1454,7 @@ class ManageExtensions extends HTML
/** /**
* Check if legacy app has been migrated. * Check if legacy app has been migrated.
* *
* @param string $shortName * @param string $shortName Shorn name of the app.
* *
* @return boolean * @return boolean
*/ */

View File

@ -1,15 +1,5 @@
/* globals $, page, url, textsToTranslate, confirmDialog*/ /* globals $, page, url, textsToTranslate, confirmDialog*/
$(document).ready(function() { $(document).ready(function() {
function loading(status) {
if (status) {
$(".spinner-fixed").show();
$("#button-upload_button").attr("disabled", "true");
} else {
$(".spinner-fixed").hide();
$("#button-upload_button").removeAttr("disabled");
}
}
$("#uploadExtension").submit(function(e) { $("#uploadExtension").submit(function(e) {
e.preventDefault(); e.preventDefault();
var formData = new FormData(this); var formData = new FormData(this);
@ -75,40 +65,73 @@ $(document).ready(function() {
* Loads modal from AJAX to add a new key or edit an existing one. * Loads modal from AJAX to add a new key or edit an existing one.
*/ */
function show_migration_form(shortName, hash) { function show_migration_form(shortName, hash) {
var btn_ok_text = textsToTranslate["Migrate"];
var btn_cancel_text = textsToTranslate["Cancel"];
var title = textsToTranslate["Migrate"]; var title = textsToTranslate["Migrate"];
var method = "migrateApp"; var method = "migrateApp";
loading(true);
load_modal({ $("#migrate_modal").dialog({
target: $("#migrate_modal"), resizable: true,
form: "modal_migrate_form", draggable: true,
url: url, modal: true,
ajax_callback: showMsg, width: 630,
modal: { overlay: {
title: title, opacity: 0.5,
ok: btn_ok_text, background: "black"
cancel: btn_cancel_text
}, },
extradata: [ closeOnEscape: false,
{ title: title,
name: "shortName", open: function() {
value: shortName loading(false);
}, $("#migrate_modal").empty();
{ $.ajax({
name: "hash", type: "POST",
value: hash url: url,
} data: {
], page: page,
onshow: { method: "loadMigrateModal",
page: page, shortName: shortName,
method: "loadMigrateModal" hash: hash
}, },
onsubmit: { dataType: "html",
page: page, success: function(data) {
method: method $("#migrate_modal").append(data);
$("#button-migrate").click(function() {
// All fields are required.
loading(true);
$.ajax({
type: "POST",
url: url,
data: {
page: page,
method: method,
hash: $("#text-hash").val(),
shortName: shortName
},
success: function(data) {
loading(false);
showMsg(data);
$("input[name='button_migrate-" + shortName + "']").hide();
},
error: function(e) {
loading(false);
e.error = e.statusText;
showMsg(JSON.stringify(e));
}
});
});
$("#button-cancel").click(function() {
$("#migrate_modal").dialog("close");
});
}
});
} }
}); });
$(".ui-widget-overlay").css("background", "#000");
$(".ui-widget-overlay").css("opacity", 0.6);
$(".ui-draggable").css("cursor", "inherit");
} }
/** /**
@ -165,3 +188,13 @@ function showMsg(data) {
] ]
}); });
} }
function loading(status) {
if (status) {
$(".spinner-fixed").show();
$("#button-upload_button").attr("disabled", "true");
} else {
$(".spinner-fixed").hide();
$("#button-upload_button").removeAttr("disabled");
}
}