Skip to content

Commit

Permalink
Add minimum age check for updates
Browse files Browse the repository at this point in the history
  • Loading branch information
nzxl101 committed Dec 27, 2024
1 parent c30bf23 commit 524819e
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 5 deletions.
20 changes: 16 additions & 4 deletions root/app/www/public/ajax/containers.php
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,7 @@
<th scope="col">Name</th>
<th scope="col">Update</th>
<th scope="col">Frequency</th>
<th scope="col">Minimum Age (Days)</th>
</tr>
</thead>
<tbody id="containerUpdateRows">
Expand All @@ -961,6 +962,9 @@
<td>
<input id="container-frequency-<?= $nameHash ?>" type="text" class="form-control container-frequency" onclick="frequencyCronEditor(this.value, '<?= $nameHash ?>', '<?= $process['Names'] ?>')" value="<?= $container['frequency'] ?>" readonly>
</td>
<td>
<input id="container-update-minage-<?= $nameHash ?>" type="number" class="form-control container-update-minage" value="<?= $container['minage'] ?? 0 ?>">
</td>
</tr>
<?php
}
Expand All @@ -969,7 +973,7 @@
</table>
</div>
<div class="row">
<div class="col-sm-12 col-lg-6 text-end">
<div class="col-sm-12 col-lg-4 text-end">
<select id="container-update-all" class="form-select d-inline-block w-75">
<option value="-1">-- Select Option --</option>
<option value="0">Ignore</option>
Expand All @@ -979,11 +983,16 @@
<i class="fas fa-angle-up ms-1 me-1" style="cursor: pointer;" onclick="massChangeContainerUpdates(1)" title="Apply to selected containers"></i>
<i class="fas fa-angle-double-up" style="cursor: pointer;" onclick="massChangeContainerUpdates(2)" title="Apply to all containers"></i>
</div>
<div class="col-sm-12 col-lg-6 text-end">
<div class="col-sm-12 col-lg-4 text-end">
<input id="container-frequency-all" type="text" class="form-control d-inline-block w-75" onclick="frequencyCronEditor(this.value, 'all', 'all')" value="<?= DEFAULT_CRON ?>" readonly>
<i class="fas fa-angle-up ms-1 me-1" style="cursor: pointer;" onclick="massChangeFrequency(1)" title="Apply to selected containers"></i>
<i class="fas fa-angle-double-up" style="cursor: pointer;" onclick="massChangeFrequency(2)" title="Apply to all containers"></i>
</div>
<div class="col-sm-12 col-lg-4 text-end">
<input id="container-update-minage-all" type="number" class="form-control d-inline-block w-75" value="0">
<i class="fas fa-angle-up ms-1 me-1" style="cursor: pointer;" onclick="massChangeMinAge(1)" title="Apply to selected containers"></i>
<i class="fas fa-angle-double-up" style="cursor: pointer;" onclick="massChangeMinAge(2)" title="Apply to all containers"></i>
</div>
</div>
</div>
<?php
Expand All @@ -1004,8 +1013,11 @@

list($minute, $hour, $dom, $month, $dow) = explode(' ', $val);
$frequency = $minute . ' ' . $hour . ' ' . $dom . ' ' . $month . ' ' . $dow;

$updates = intval($_POST['container-update-' . $hash]);

$minage = intval($_POST['container-update-minage-' . $hash]);

try {
$cron = Cron\CronExpression::factory($frequency);
} catch (Exception $e) {
Expand All @@ -1014,8 +1026,8 @@

//-- ONLY UPDATE WHAT HAS CHANGED
$container = apiRequest('database-getContainerFromHash', ['hash' => $hash])['result'];
if ($container['updates'] != $updates || $container['frequency'] != $frequency) {
apiRequest('database-updateContainer', [], ['hash' => $hash, 'updates' => $updates, 'frequency' => $database->prepare($frequency)]);
if ($container['updates'] != $updates || $container['frequency'] != $frequency || $container['minage'] != $minage) {
apiRequest('database-updateContainer', [], ['hash' => $hash, 'updates' => $updates, 'frequency' => $database->prepare($frequency), 'minage' => $database->prepare($minage)]);
}
}
}
Expand Down
19 changes: 18 additions & 1 deletion root/app/www/public/crons/pulls.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,19 @@
'imageDigest' => $imageDigest
];

//-- SKIP IF AGE < MINAGE
$digestTag = explode(':', $image)[0] . '@' . $regctlDigest;
$checkImageAge = regctlGetCreatedDate($digestTag);
if ($checkImageAge < $containerSettings['minage']) {
$msg = 'Skipping container update: ' . $containerState['Names'] . ' (does not meet the minimum age requirement of '.$containerSettings['minage'].' days, got '.$checkImageAge.' days)';
logger(CRON_PULLS_LOG, $msg);
echo date('c') . ' ' . $msg . "\n";

if ($containerSettings['updates'] == 1) {
$containerSettings['updates'] = 2;
}
}

//-- DONT AUTO UPDATE THIS CONTAINER, CHECK ONLY
if (skipContainerActions($image, $skipContainerActions)) {
if ($isDockwatch) {
Expand Down Expand Up @@ -314,7 +327,11 @@
break;
case 2: //-- Check for updates
if (apiRequest('database-isNotificationTriggerEnabled', ['trigger' => 'updates'])['result'] && !$containerSettings['disableNotifications'] && $inspectImage[0]['Id'] != $inspectContainer[0]['Image']) {
$notify['available'][] = ['container' => $containerState['Names']];
$notify['available'][] = [
'container' => $containerState['Names'],
'minage' => $containerSettings['minage'] ?? 0,
'currentage' => $checkImageAge ?? 0
];
}
break;
}
Expand Down
23 changes: 23 additions & 0 deletions root/app/www/public/functions/regctl.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,26 @@ function regctlCheck($image)

return $regctl;
}

function regctlGetCreatedDate($image)
{
global $shell;

if (!file_exists(REGCTL_PATH . REGCTL_BINARY)) {
return 'Error: regctl binary (\'' . REGCTL_PATH . REGCTL_BINARY . '\') is not avaialable or there is a permissions error';
}

$regctl = $shell->exec(REGCTL_PATH . REGCTL_BINARY . ' image inspect ' . $image . ' | jq -r \'.created\'');

//-- RETRY
if (!$regctl) {
sleep(3);
$regctl = $shell->exec(REGCTL_PATH . REGCTL_BINARY . ' image inspect ' . $image . ' | jq -r \'.created\'');
}

$created = new DateTime($regctl);
$now = new DateTime();
$diff = $created->diff($now);

return $diff->days;
}
19 changes: 19 additions & 0 deletions root/app/www/public/js/containers.js
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,25 @@ function massChangeFrequency(option)
}
}
// ---------------------------------------------------------------------------------------------
function massChangeMinAge(option)
{
const minage = $('#container-update-minage-all').val();

switch (option) {
case 1: //-- SELECTED
$.each($('.container-update-checkbox'), function () {
if ($(this).prop('checked')) {
const hash = $(this).attr('id').match(/container-update-(.+)-checkbox/);
$('#container-update-minage-' + hash[1]).val(minage);
}
});
break;
case 2: //-- ALL
$('.container-update-minage').val(minage)
break;
}
}
// ---------------------------------------------------------------------------------------------
function containerInfo(hash)
{
pageLoadingStart();
Expand Down
23 changes: 23 additions & 0 deletions root/app/www/public/migrations/009_container_min_age.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

$q = [];

$q[] = "ALTER TABLE " . CONTAINER_SETTINGS_TABLE . "
ADD minage INTEGER NOT NULL DEFAULT 0";

//-- ALWAYS NEED TO BUMP THE MIGRATION ID
$q[] = "UPDATE " . SETTINGS_TABLE . "
SET value = '009'
WHERE name = 'migration'";

foreach ($q as $query) {
logger(MIGRATION_LOG, '<span class="text-success">[Q]</span> ' . preg_replace('!\s+!', ' ', $query));

$database->query($query);

if ($database->error() != 'not an error') {
logger(MIGRATION_LOG, '<span class="text-info">[R]</span> ' . $database->error(), 'error');
} else {
logger(MIGRATION_LOG, '<span class="text-info">[R]</span> query applied!');
}
}

0 comments on commit 524819e

Please sign in to comment.