Skip to content

Commit

Permalink
Fix mass update not working after the multiple server changes
Browse files Browse the repository at this point in the history
Add logging for UI and API (not fully implemented)
Set contants for the log paths, easier to edit in one place
Fix php error when state cron runs with no global settings in place
  • Loading branch information
austinwbest committed Nov 29, 2023
1 parent 1f1e1b0 commit de7b9dc
Show file tree
Hide file tree
Showing 17 changed files with 214 additions and 170 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Simple UI driven way to manage updates & notifications for containers. As this i
- Notify when a container changes state (running -> stopped)
- Nofity when an update is available
- Notify when an update is applied
- Notify when images are pruned
- Notify when volumes are pruned
- Notify when images/volumes have been pruned
- Notify if memory is > n%
- Notify if CPU is > n%
Expand All @@ -22,6 +24,8 @@ Simple UI driven way to manage updates & notifications for containers. As this i
- Check for update

## Features
- Link and control multiple servers
- Try to find/match icons for non unraid usage
- Setup update schedules on a container by container basis
- Setup notify only or update on a container by container basis
- Mass prune/remove orphan images and volumes
Expand Down
48 changes: 38 additions & 10 deletions root/app/www/public/ajax/containers.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,28 +301,40 @@
}

if ($_POST['m'] == 'massApplyContainerTrigger') {
logger(UI_LOG, 'massApplyContainerTrigger ->');
$container = findContainerFromHash($_POST['hash']);

logger(UI_LOG, 'trigger:' . $_POST['trigger']);
logger(UI_LOG, 'findContainerFromHash:' . json_encode($container));

switch ($_POST['trigger']) {
case '1': //-- Start
apiRequest('dockerStartContainer', [], ['name' => $container['Names']]);
$apiResult = apiRequest('dockerStartContainer', [], ['name' => $container['Names']]);
logger(UI_LOG, 'dockerStartContainer:' . json_encode($apiResult));
$result = 'Started ' . $container['Names'] . '<br>';
break;
case '2': //-- Restart
apiRequest('dockerStopContainer', [], ['name' => $container['Names']]);
apiRequest('dockerStartContainer', [], ['name' => $container['Names']]);
$apiResult = apiRequest('dockerStopContainer', [], ['name' => $container['Names']]);
logger(UI_LOG, 'dockerStopContainer:' . json_encode($apiResult));
$apiResult = apiRequest('dockerStartContainer', [], ['name' => $container['Names']]);
logger(UI_LOG, 'dockerStartContainer:' . json_encode($apiResult));
$result = 'Restarted ' . $container['Names'] . '<br>';
break;
case '3': //-- Stop
apiRequest('dockerStopContainer', [], ['name' => $container['Names']]);
$apiResult = apiRequest('dockerStopContainer', [], ['name' => $container['Names']]);
logger(UI_LOG, 'dockerStopContainer:' . json_encode($apiResult));
$result = 'Stopped ' . $container['Names'] . '<br>';
break;
case '4': //-- Pull
$image = $container['inspect'][0]['Config']['Image'];
logger(UI_LOG, 'image:' . $image);
$pull = apiRequest('dockerPullContainer', [], ['name' => $image]);
logger(UI_LOG, 'dockerPullContainer:' . json_encode($pull));
$inspectContainer = apiRequest('dockerInspect', ['name' => $container['Names'], 'useCache' => false]);
logger(UI_LOG, 'dockerInspect:' . json_encode($inspectContainer));
$inspectContainer = json_decode($inspectContainer['response']['docker'], true);
$inspectContainer = apiRequest('dockerInspect', ['name' => $image, 'useCache' => false]);
logger(UI_LOG, 'dockerInspect:' . json_encode($inspectContainer));
$inspectImage = json_decode($inspectContainer['response']['docker'], true);

$pullsFile[md5($container['Names'])] = [
Expand All @@ -337,6 +349,7 @@
break;
case '5': //-- GERNERATE RUN
$autoRun = apiRequest('dockerAutoRun', ['name' => $container['Names']]);
logger(UI_LOG, 'dockerAutoRun:' . json_encode($autoRun));
$autoRun = $autoRun['response']['docker'];
$result = '<pre>' . $autoRun . '</pre>';
break;
Expand All @@ -350,23 +363,35 @@
}

$autoCompose = apiRequest('dockerAutoCompose', ['name' => trim($containerList)]);
logger(UI_LOG, 'dockerAutoCompose:' . json_encode($autoCompose));
$autoCompose = $autoCompose['response']['docker'];
$result = '<pre>' . $autoCompose . '</pre>';
break;
case '7': //-- UPDATE
if (strpos($image, 'dockwatch') !== false) {
logger(UI_LOG, 'skipping dockwatch update');
$updateResult = 'skipped';
} else {
$autoRun = apiRequest('dockerAutoRun', ['name' => $container['Names']]);
$autoRun = json_decode($autoRun['response']['docker'], true);
$lines = explode("\n", $runCommand);
logger(UI_LOG, 'dockerAutoRun:' . json_encode($autoRun));

$autoRun = $autoRun['response']['docker'];
$lines = explode("\n", $autoRun);

foreach ($lines as $line) {
$newRun .= trim(str_replace('\\', '', $line)) . ' ';
}
$autoRun = $newRun;
apiRequest('dockerStopContainer', [], ['name' => $container['Names']]);
apiRequest('dockerRemoveContainer', [], ['id' => $container['ID']]);
$update = apiRequest('dockerUpdateContainer', [], ['command' => $command]);
logger(UI_LOG, 'run command:' . $autoRun);

$apiResponse = apiRequest('dockerStopContainer', [], ['name' => $container['Names']]);
logger(UI_LOG, 'dockerStopContainer:' . json_encode($apiResponse));

$apiResponse = apiRequest('dockerRemoveContainer', [], ['id' => $container['ID']]);
logger(UI_LOG, 'dockerRemoveContainer:' . json_encode($apiResponse));

$update = apiRequest('dockerUpdateContainer', [], ['command' => $autoRun]);
logger(UI_LOG, 'dockerUpdateContainer:' . json_encode($update));
$update = trim($update['response']['docker']);
$updateResult = 'failed';

Expand All @@ -379,7 +404,7 @@
'container' => $update
];

setServerFile('pull', $pulls);
setServerFile('pull', $pullsFile);
}
}

Expand All @@ -392,8 +417,10 @@
}

$processList = apiRequest('dockerProcessList', ['useCache' => false]);
logger(UI_LOG, 'dockerProcessList:' . json_encode($processList));
$processList = json_decode($processList['response']['docker'], true);
$dockerStats = apiRequest('dockerStats', ['useCache' => false]);
logger(UI_LOG, 'dockerStats:' . json_encode($dockerStats));
$dockerStats = json_decode($dockerStats['response']['docker'], true);
$containerProcess = $containerStats = [];

Expand Down Expand Up @@ -442,6 +469,7 @@
'result' => $result
];

logger(UI_LOG, 'massApplyContainerTrigger <-');
echo json_encode($return);
}

Expand Down
34 changes: 17 additions & 17 deletions root/app/www/public/ajax/login.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,27 @@
require 'shared.php';

if ($_POST['m'] == 'login') {
logger(SYSTEM_LOG, 'login ->', 'info');
logger(SYSTEM_LOG, 'login ->');

$_SESSION['authenticated'] = false;
$error = '';

if (!file_exists(LOGIN_FILE)) {
$error = 'Could not find login file \'' . LOGIN_FILE . '\'';
logger(SYSTEM_LOG, $error, 'info');
logger(SYSTEM_LOG, $error);
} else {
$loginsFile = file(LOGIN_FILE);

logger(SYSTEM_LOG, '$loginsFile=' . json_encode($loginsFile), 'info');
logger(SYSTEM_LOG, '$loginsFile=' . json_encode($loginsFile));

if (empty($loginsFile)) {
$error = 'Could not read login file data or it is empty';
logger(SYSTEM_LOG, $error, 'info');
logger(SYSTEM_LOG, $error);
}

if (!$error) {
foreach ($loginsFile as $login) {
logger(SYSTEM_LOG, 'credentials check', 'info');
logger(SYSTEM_LOG, 'credentials check');
list($user, $pass) = explode(':', $login);

//-- STRIP OUT THE SPACES AND LINE BREAKS USERS ACCIDENTALLY PROVIDE
Expand All @@ -39,38 +39,38 @@
$_POST['user'] = trim($_POST['user']);
$_POST['pass'] = trim($_POST['pass']);

logger(SYSTEM_LOG, 'file user: \'' . $user . '\'', 'info');
logger(SYSTEM_LOG, 'file pass: \'' . $pass . '\'', 'info');
logger(SYSTEM_LOG, 'post user: \'' . $_POST['user'] . '\'', 'info');
logger(SYSTEM_LOG, 'post pass: \'' . $_POST['pass'] . '\'', 'info');
logger(SYSTEM_LOG, 'file user: \'' . $user . '\'');
logger(SYSTEM_LOG, 'file pass: \'' . $pass . '\'');
logger(SYSTEM_LOG, 'post user: \'' . $_POST['user'] . '\'');
logger(SYSTEM_LOG, 'post pass: \'' . $_POST['pass'] . '\'');

if (strtolower($user) == strtolower($_POST['user']) && $pass == $_POST['pass']) {
if ($_POST['user'] == 'admin' && ($_POST['pass'] == 'pass' || $_POST['pass'] == 'password')) {
$error = 'Please use something other than admin:pass and admin:password';
logger(SYSTEM_LOG, $error, 'info');
logger(SYSTEM_LOG, $error);
} else {
logger(SYSTEM_LOG, 'match found, updating session key', 'info');
logger(SYSTEM_LOG, 'match found, updating session key');
$_SESSION['authenticated'] = true;
logger(SYSTEM_LOG, 'session key authenticated:' . $_SESSION['authenticated'], 'info');
logger(SYSTEM_LOG, 'session key authenticated:' . $_SESSION['authenticated']);
}
}
}

if (!$error && !$_SESSION['authenticated']) {
$error = 'Did not find a matching user:pass in the login file with what was provided';
logger(SYSTEM_LOG, $error, 'info');
logger(SYSTEM_LOG, $error);
}
}
}

logger(SYSTEM_LOG, 'session key authenticated:' . $_SESSION['authenticated'], 'info');
logger(SYSTEM_LOG, 'login <-', 'info');
logger(SYSTEM_LOG, 'session key authenticated:' . $_SESSION['authenticated']);
logger(SYSTEM_LOG, 'login <-');
echo $error;
}

if ($_POST['m'] == 'logout') {
logger(SYSTEM_LOG, 'logout ->', 'info');
logger(SYSTEM_LOG, 'logout ->');
session_unset();
session_destroy();
logger(SYSTEM_LOG, 'logout <-', 'info');
logger(SYSTEM_LOG, 'logout <-');
}
2 changes: 1 addition & 1 deletion root/app/www/public/ajax/logs.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
}

if ($_POST['m'] == 'viewLog') {
logger(SYSTEM_LOG, 'View log: ' . $_POST['name'], 'info');
logger(SYSTEM_LOG, 'View log: ' . $_POST['name']);

$apiResponse = apiRequest('viewLog', [], ['name' => $_POST['name']]);

Expand Down
14 changes: 14 additions & 0 deletions root/app/www/public/ajax/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,20 @@
</td>
<td>How long to store logs generated when notifications are sent (min 1 day)</td>
</tr>
<tr>
<th scope="row">UI</th>
<td>
<input class="form-control" type="number" id="globalSetting-uiLogLength" value="<?= ($globalSettings['uiLogLength'] <= 1 ? 1 : $globalSettings['uiLogLength']) ?>">
</td>
<td>How long to store logs generated from using the UI (min 1 day)</td>
</tr>
<tr>
<th scope="row">API</th>
<td>
<input class="form-control" type="number" id="globalSetting-apiLogLength" value="<?= ($globalSettings['apiLogLength'] <= 1 ? 1 : $globalSettings['apiLogLength']) ?>">
</td>
<td>How long to store logs generated when api requests are made (min 1 day)</td>
</tr>
</tbody>
</table>
</div>
Expand Down
2 changes: 1 addition & 1 deletion root/app/www/public/ajax/tasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
}

if ($_POST['m'] == 'runTask') {
logger(SYSTEM_LOG, 'Run task: ' . $_POST['task'], 'info');
logger(SYSTEM_LOG, 'Run task: ' . $_POST['task']);

$apiResponse = apiRequest('runTask', [], ['task' => $_POST['task']]);

Expand Down
4 changes: 2 additions & 2 deletions root/app/www/public/classes/notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public function notify($platform, $payload)
$platformData = $this->getNotificationPlatformFromId($platform);
$logfile = $this->logpath . $platformData['name'] . '-'. date('Ymd') .'.log';

logger($logfile, 'notification request to ' . $platformData['name'], 'info');
logger($logfile, 'notification payload: ' . json_encode($payload), 'info');
logger($logfile, 'notification request to ' . $platformData['name']);
logger($logfile, 'notification payload: ' . json_encode($payload));

/*
Everything should return an array with code => ..., error => ... (if no error, just code is fine)
Expand Down
Loading

0 comments on commit de7b9dc

Please sign in to comment.