chg: [update] Added endpoint to release lock and integration with UI

pull/5002/head
mokaddem 2019-10-10 12:02:23 +02:00
parent 571b4887f0
commit 0e2205c061
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
4 changed files with 81 additions and 20 deletions

View File

@ -1692,6 +1692,9 @@ class ServersController extends AppController
$update_progress['db_version'] = $db_version;
$max_update_number = max(array_keys($this->Server->db_changes));
$update_progress['complete_update_remaining'] = $max_update_number - $db_version;
$update_progress['update_locked'] = $this->Server->isUpdateLocked();
$update_progress['lock_remaining_time'] = $this->Server->getLockRemainingTime();
$update_progress['update_fail_number_reached'] = $this->Server->UpdateFailNumberReached();
$current_index = $update_progress['current'];
$current_command = !isset($update_progress['commands'][$current_index]) ? '' : $update_progress['commands'][$current_index];
$lookup_string = preg_replace('/\s{2,}/', '', substr($current_command, 0, -1));
@ -2168,4 +2171,16 @@ misp.direct_call(relative_path, body)
return $this->RestResponse->saveFailResponse('Servers', 'changePriority', $id, $message, $this->response->type());
}
}
public function releaseUpdateLock()
{
if (!$this->request->is('post')) {
throw new MethodNotAllowedException(__('This endpoint expects POST requests.'));
}
if (!$this->_isSiteAdmin()) {
throw new MethodNotAllowedException(__('Site admin accounts cannot be used to release the update lock.'));
}
$this->Server->changeLockState(false);
$this->redirect(array('action' => 'updateProgress'));
}
}

View File

@ -91,6 +91,11 @@ class AppModel extends Model
'title' => 'Database Cleanup Scripts',
'description' => 'If you run into an issue with an infinite upgrade loop (when upgrading from version ~2.4.50) that ends up filling your database with upgrade script log messages, run the following script.',
'url' => '/logs/pruneUpdateLogs/'
),
'releaseUpdateLock' => array(
'title' => 'Release update lock',
'description' => 'If your your database is locked and is not updating, unlock it here.',
'url' => '/servers/releaseUpdateLock/'
)
);
@ -1677,7 +1682,7 @@ class AppModel extends Model
}
return true;
}
$this->__changeLockState(time());
$this->changeLockState(time());
$update_done = 0;
foreach ($updates as $update => $temp) {
@ -1708,10 +1713,7 @@ class AppModel extends Model
if (!empty($job)) {
$job['Job']['message'] = __('Update done');
}
$fail_number = $this->__getUpdateFailNumber();
if ($fail_number < 3) { // Do not release the lock if update failed more than 3 time
$this->__changeLockState(false);
}
$this->changeLockState(false);
$this->__queueCleanDB();
} else {
if (!empty($job)) {
@ -1820,7 +1822,7 @@ class AppModel extends Model
$this->AdminSetting->changeSetting('update_progress', $data);
}
private function __changeLockState($locked)
public function changeLockState($locked)
{
$this->AdminSetting->changeSetting('update_locked', $locked);
}
@ -1857,7 +1859,8 @@ class AppModel extends Model
public function isUpdateLocked()
{
$remainingTime = $this->getLockRemainingTime();
return $remainingTime > 0;
$failThresholdReached = $this->UpdateFailNumberReached();
return $remainingTime > 0 || $failThresholdReached;
}
public function getUpdateFailNumber()
@ -1867,19 +1870,24 @@ class AppModel extends Model
return ($update_fail_number !== false && $update_fail_number !== '') ? $update_fail_number : 0;
}
public function resetUpdateFailNumber()
public function __resetUpdateFailNumber()
{
$this->AdminSetting = ClassRegistry::init('AdminSetting');
$this->AdminSetting->changeSetting('update_fail_number', 0);
}
public function increaseUpdateFailNumber()
public function __increaseUpdateFailNumber()
{
$this->AdminSetting = ClassRegistry::init('AdminSetting');
$update_fail_number = $this->AdminSetting->getSetting('update_fail_number');
$this->AdminSetting->changeSetting('update_fail_number', $update_fail_number+1);
}
public function UpdateFailNumberReached()
{
return $this->getUpdateFailNumber() > 3;
}
private function __queueCleanDB()
{
$this->AdminSetting = ClassRegistry::init('AdminSetting');

View File

@ -1,6 +1,10 @@
<?php
if (!$isSiteAdmin) exit();
$completeUpdateRemaining = $updateProgress['complete_update_remaining'];
$towardDbVersion = $updateProgress['toward_db_version'];
$updateLocked = $updateProgress['update_locked'];
$lockRemainingTime = $updateProgress['lock_remaining_time'];
$updateFailNumberReached = $updateProgress['update_fail_number_reached'];
if ($updateProgress['total'] !== 0 ) {
$percentageFail = floor(count($updateProgress['failed_num']) / $updateProgress['total']*100);
$percentage = floor(($updateProgress['current']) / $updateProgress['total']*100);
@ -18,6 +22,28 @@ if (isset($updateProgress['preTestSuccess']) && $updateProgress['preTestSuccess'
<?php if (!$ajaxHtml): ?>
<div class="servers form">
<?php endif; ?>
<?php if ($updateLocked && $updateFailNumberReached): ?>
<div style="width: 55%;margin: 20px auto" class="alert alert-danger">
<button type="button" class="close" data-dismiss="alert">&times;</button>
<div style="display:flex;flex-direction: row;justify-content: space-between;">
<span>
<h5 style="margin: 5px 0px; display: inline-block"><?php echo __('Update are locked due to to many update fails'); ?></h5>
<i>(<?php echo sprintf(__('unlock in %smin %ssec'), '<span id="unlock_remaining_time_min">-</span>', '<span id="unlock_remaining_time_sec">-</span>'); ?>)</i>
</span>
<span>
<?php
echo $this->Form->postButton('<i class="fa fa-lock-open"></i> ' . __('Release update lock'), $baseurl . '/servers/releaseUpdateLock', array(
'style' => 'margin: 0px 0px;',
'class' => 'btn btn-danger',
'escape' => false
));
?>
</span>
</div>
</div>
<?php endif; ?>
<?php if ($completeUpdateRemaining != 0): ?>
<div style="width: 55%;margin: 20px auto;background-color: white;" class="panel-container completeUpdateRemainingContainer">
<h3>
@ -29,7 +55,7 @@ if (isset($updateProgress['preTestSuccess']) && $updateProgress['preTestSuccess'
<div style="width: 55%;margin: 0 auto;">
<?php if (count($updateProgress['commands']) > 0): ?>
<h3><?php echo(sprintf(__('Database Update progress for update %s'), h($updateProgress['toward_db_version'])));?></h3>
<h3><?php echo(sprintf(__('Database Update progress for update %s'), h($towardDbVersion)));?></h3>
<div class="" style="max-width: 1000px;">
<div>
@ -45,7 +71,7 @@ if (isset($updateProgress['preTestSuccess']) && $updateProgress['preTestSuccess'
<div id="pb-fail" class="bar" style="width: <?php echo h($percentageFail);?>%; background-color: #ee5f5b;"></div>
</div>
<table class="table table-bordered table-stripped updateProgressTable" data-towarddbversion="<?php echo h($updateProgress['toward_db_version']); ?>">
<table class="table table-bordered table-stripped updateProgressTable" data-towarddbversion="<?php echo h($towardDbVersion); ?>">
<thead>
<tr>
<th></th>
@ -181,14 +207,22 @@ if (!$ajaxHtml) {
var updateProgress = <?php echo json_encode($updateProgress); ?>;
var urlGetProgress = "<?php echo $baseurl; ?>/servers/updateProgress";
var checkboxLabel = "<?php echo __('Follow updates'); ?>";
// pooler = new TaskScheduler(update_state, { container: 'followUpdateSwitchContainer', checkboxLabel: checkboxLabel});
// pooler.start();
// if (pooler !== undefined) {
$(document).ready(function() {
var lockRemainingTimeSeconds = "<?php echo h($lockRemainingTime); ?>";
var timer = setInterval(function() {
lockRemainingTimeSeconds--;
var parsedTime = parseRemainingTime(lockRemainingTimeSeconds)
$('#unlock_remaining_time_min').text(parsedTime.mins);
$('#unlock_remaining_time_sec').text(parsedTime.secs);
}, 1000);
});
// // Need to fix this! redraw probably?
// // Also, animation is not playing with fast timer
// pooler.stop();
// pooler = new TaskScheduler(update_state, { container: 'followUpdateSwitchContainer', checkboxLabel: checkboxLabel});
// pooler.start();
// }
function parseRemainingTime(seconds) {
return {
days : Math.floor(seconds / (60 * 60 * 24)),
hours: Math.floor((seconds % (60 * 60 * 24)) / (60 * 60)),
mins : Math.floor((seconds % (60 * 60)) / (60)),
secs : Math.floor((seconds % (60))),
}
}
</script>

View File

@ -2394,3 +2394,7 @@ table tr:hover .down-expand-button {
#notice_message {
margin: 10px;
}
.alert form {
margin: 0 0;
}