Skip to content

Commit

Permalink
Working on #2
Browse files Browse the repository at this point in the history
  • Loading branch information
austinwbest committed Nov 19, 2023
1 parent 02fe336 commit 79027e2
Show file tree
Hide file tree
Showing 12 changed files with 208 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/root/app/www/public/logs/*
57 changes: 57 additions & 0 deletions root/app/www/public/ajax/logs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/*
----------------------------------
------ Created: 111923 ------
------ Austin Best ------
----------------------------------
*/

require 'shared.php';

if ($_POST['m'] == 'init') {
$logFiles = [];
$logDir = ABSOLUTE_PATH . LOGS_PATH;
$dir = opendir($logDir);
while ($log = readdir($dir)) {
if ($log[0] != '.' && !is_dir($log)) {
$logFiles[] = ['name' => $log, 'size' => filesize($logDir . $log)];
}
}
closedir($dir);

if (!$logFiles) {
echo 'No log files have been generated yet.';
} else {
?>
<div class="container-fluid pt-4 px-4">
<div class="bg-secondary rounded h-100 p-4">
<div class="row">
<div class="col-sm-3">
<ul>
<?php
foreach ($logFiles as $log) {
$logHash = md5($log['name']);
?>
<li id="logList-<?= $logHash ?>" onclick="viewLog('<?= $log['name'] ?>', '<?= $logHash ?>')" style="cursor: pointer;" class="text-info"><?= $log['name'] ?> (<?= byteConversion($log['size']) ?>)</li>
<?php } ?>
</ul>
</div>
<div class="col-sm-9"><span id="logHeader"></span><pre id="logViewer" style="max-height: 500px; overflow: auto;"></pre></div>
</div>
</div>
</div>
<?php
}
}

if ($_POST['m'] == 'viewLog') {
$log = file(ABSOLUTE_PATH . LOGS_PATH . $_POST['name']);
$header = 'Lines: ' . count($log);

foreach ($log as $index => $line) {
$content .= str_pad(($index + 1), strlen(count($log)), ' ', STR_PAD_RIGHT) .' | '. $line;
}

echo json_encode(['header' => $header, 'log' => $content]);
}
33 changes: 33 additions & 0 deletions root/app/www/public/crons/housekeeper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
----------------------------------
------ Created: 111923 ------
------ Austin Best ------
----------------------------------
*/

define('ABSOLUTE_PATH', str_replace('crons', '', __DIR__));
require ABSOLUTE_PATH . 'loader.php';

$logfile = ABSOLUTE_PATH . LOGS_PATH . 'cron-housekeeper-' . date('Ymd') . '.log';
logger($logfile, 'Cron run started');
echo 'Cron run started: housekeeper' . "\n";

//-- LOG FILE CLEANUP (DAILY @ MIDNIGHT)
logger($logfile, 'Checking time: Log file cleanup (daily @ midnight)');
if (date('H') == 0) {
$logDir = ABSOLUTE_PATH . LOGS_PATH;
$dir = opendir($logDir);
while ($log = readdir($dir)) {
if ($log[0] != '.' && !is_dir($log)) {
if (date('Ymd', filemtime($logDir . $log)) != date('Ymd')) {
logger($logfile, 'Removing logfile: ' . $logDir . $log);
unlink($logDir . $log);
}
}
}
closedir($dir);
}

logger($logfile, 'Cron run finished');
33 changes: 21 additions & 12 deletions root/app/www/public/crons/pulls.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
require ABSOLUTE_PATH . 'loader.php';
set_time_limit(0);

$logfile = ABSOLUTE_PATH . LOGS_PATH . 'cron-pulls-' . date('Ymd') . '.log';
logger($logfile, 'Cron run started');
echo 'Cron run started: pulls' . "\n";

$updateSettings = $settings['containers'];
$states = is_array($state) ? $state : json_decode($state, true);
$pulls = getFile(PULL_FILE);
Expand All @@ -22,41 +26,46 @@
continue;
}

$containerState = [];
foreach ($states as $state) {
if (md5($state['Names']) == $containerHash) {
$containerState = $state;
break;
}
}
$containerState = findContainerFromHash($containerHash);

if ($containerState) {
if (date('H') == $settings['hour']) {
$image = $containerState['inspect'][0]['Config']['Image'];

echo 'Pulling: ' . $image. "\n";
$msg = 'Pulling: ' . $image;
logger($logfile, $msg);
echo $msg. "\n";
$pull = dockerPullContainer($image);

echo 'Inspecting container: ' . $containerState['Names'] . "\n";
$msg = 'Inspecting container: ' . $containerState['Names'];
logger($logfile, $msg);
echo $msg. "\n";
$inspectContainer = dockerInspect($containerState['Names']);
$inspectContainer = json_decode($inspectContainer, true);

echo 'Inspecting image: ' . $image . "\n";
$msg = 'Inspecting image: ' . $image;
logger($logfile, $msg);
echo $msg. "\n";
$inspectImage = dockerInspect($image);
$inspectImage = json_decode($inspectImage, true);

echo 'Updating pull data: ' . $containerState['Names'] . "\n";
$msg = 'Updating pull data: ' . $containerState['Names'];
logger($logfile, $msg);
echo $msg. "\n";
$pulls[md5($containerState['Names'])] = [
'checked' => time(),
'name' => $containerState['Names'],
'image' => $inspectImage[0]['Id'],
'container' => $inspectContainer[0]['Image']
];
} else {
echo 'Skipping: ' . $containerState['Names'] . "\n";
$msg = 'Skipping: ' . $containerState['Names'];
logger($logfile, $msg);
echo $msg. "\n";
}
}
}

setFile(PULL_FILE, $pulls);
}
logger($logfile, 'Cron run finished');
4 changes: 4 additions & 0 deletions root/app/www/public/crons/state.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@
define('ABSOLUTE_PATH', str_replace('crons', '', __DIR__));
require ABSOLUTE_PATH . 'loader.php';

$logfile = ABSOLUTE_PATH . LOGS_PATH . 'cron-state-' . date('Ymd') . '.log';
logger($logfile, 'Cron run started');
echo 'Cron run started: state' . "\n";
setFile(STATE_FILE, dockerState());
logger($logfile, 'Cron run finished');
5 changes: 5 additions & 0 deletions root/app/www/public/functions/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
----------------------------------
*/

function createDirectoryTree($tree)
{
system('mkdir -p ' . $tree);
}

function findContainerFromHash($hash)
{
$state = getFile(STATE_FILE);
Expand Down
56 changes: 56 additions & 0 deletions root/app/www/public/functions/logger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

/*
----------------------------------
------ Created: 111823 ------
------ Austin Best ------
----------------------------------
*/

function logger($logfile, $msg, $type = 'info')
{
if (!$logfile) {
return;
}

$backtrace = debug_backtrace();
$line = $backtrace[0]['line'];
$file = $backtrace[0]['file'];
$fileParts = explode('/', $file);
$fileName = $fileParts[count($fileParts) - 1];
$fileFolder = $fileParts[count($fileParts) - 2];
$file = $fileFolder . '/' . $fileName;
$log = date('g:i:s') . ' ' . ($type ? '[' . strtoupper($type) . '] ' : '') . $file . ' LN: ' . $line . ' :: ' . (is_array($msg) || is_object($msg) ? loggerLoopArray($msg, 0) : $msg) . "\n";

file_put_contents($logfile, $log, FILE_APPEND);
}

function loggerLoopArray($stack, $depth = 0, $output = '')
{
$tabs = '';

for ($x = 0; $x < $depth; $x++) {
$tabs .= "\t";
}

if (!is_array($stack)) {
$output .= $tabs . $stack;
return $output;
}

foreach ($stack as $key => $val) {
if (is_object($val)) {
$val = (array) $val;
}

if (is_array($val)) {
$output .= $tabs . '[' . $key . ']' . "\n";
$output = loggerLoopArray($val, ($depth += 1), $output);
$depth -= 1;
} else {
$output .= $tabs . '[' . $key . '] => ' . $val . "\n";
}
}

return $output;
}
1 change: 1 addition & 0 deletions root/app/www/public/includes/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
define('SETTINGS_FILE', '/config/settings.json');
define('STATE_FILE', '/config/state.json');
define('PULL_FILE', '/config/pull.json');
define('LOGS_PATH', 'logs/');

//-- WHAT DATA TO GET WHEN VIEWING A PAGE
$getStats = ['overview', 'containers'];
Expand Down
2 changes: 2 additions & 0 deletions root/app/www/public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<a id="menu-containers" onclick="initPage('containers')" style="cursor: pointer;" class="nav-item nav-link"><i class="fas fa-th me-2"></i>Containers</a>
<a id="menu-notification" onclick="initPage('notification')" style="cursor: pointer;" class="nav-item nav-link"><i class="fas fa-comment-dots me-2"></i>Notifications</a>
<a id="menu-settings" onclick="initPage('settings')" style="cursor: pointer;" class="nav-item nav-link"><i class="fas fa-tools me-2"></i>Settings</a>
<a id="menu-logs" onclick="initPage('logs')" style="cursor: pointer;" class="nav-item nav-link"><i class="fas fa-file-code me-2"></i>Logs</a>
</div>
</nav>
</div>
Expand All @@ -86,6 +87,7 @@
<div id="content-containers" style="display: none;"></div>
<div id="content-notification" style="display: none;"></div>
<div id="content-settings" style="display: none;"></div>
<div id="content-logs" style="display: none;"></div>
<div id="content-dockerPermissions" style="display: <?= ($dockerPerms ? 'none' : 'block') ?>;">
If you are seeing this, it means the user running this container does not have permission to run docker commands. Please fix that, restart the container and try again.
</div>
Expand Down
21 changes: 21 additions & 0 deletions root/app/www/public/js/logs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function viewLog(name, hash)
{
loadingStart();

$('[id^=logList-]').removeClass('*').addClass('text-info');
$('#logList-' + hash).removeClass('text-info').addClass('text-success');

$.ajax({
type: 'POST',
url: '../ajax/logs.php',
data: '&m=viewLog&name=' + name,
dataType: 'json',
success: function (resultData) {
$('#logHeader').html(resultData.header);
$('#logViewer').html(resultData.log);
loadingStop();
}
});

}
// ---------------------------------------------------------------------------------------------
3 changes: 3 additions & 0 deletions root/app/www/public/loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
}
}

//-- CREATE DIRECTORIES
createDirectoryTree(ABSOLUTE_PATH . LOGS_PATH);

//-- INITIALIZE THE NOTIFY CLASS
$notifications = new Notifications();

Expand Down
6 changes: 4 additions & 2 deletions root/etc/crontabs/abc
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
*/5 * * * * /usr/bin/php /config/www/crons/state.php
*/60 * * * * /usr/bin/php /config/www/crons/pulls.php
# min hour day month weekday command
*/5 * * * * /usr/bin/php /app/www/crons/state.php
*/10 * * * * /usr/bin/php /app/www/crons/housekeeper.php
*/60 * * * * /usr/bin/php /app/www/crons/pulls.php

0 comments on commit 79027e2

Please sign in to comment.