Skip to content

Commit

Permalink
Rewrite maintenance to a class
Browse files Browse the repository at this point in the history
Implement self update logic
Add link to external servers
  • Loading branch information
austinwbest committed Feb 10, 2024
1 parent 79542e2 commit d26e897
Show file tree
Hide file tree
Showing 9 changed files with 304 additions and 219 deletions.
7 changes: 5 additions & 2 deletions root/app/www/public/ajax/maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

require 'shared.php';

//-- INITIALIZE THE MAINTENANCE CLASS
$maintenance = new Maintenance();

if ($_POST['m'] == 'dockwatchMaintenance') {
initiaiteMaintenance($_POST['action']);
}
$maintenance->apply($_POST['action']);
}
117 changes: 117 additions & 0 deletions root/app/www/public/classes/Maintenance.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

/*
----------------------------------
------ Created: 021024 ------
------ Austin Best ------
----------------------------------
*/

/**
* Some of the code in these methods are duplicated and technically could be simplified
* It is done this way so if things need to be done per host or per maintenance it is easy to split & log
*/

//-- BRING IN THE TRAITS
$traits = ABSOLUTE_PATH . 'classes/traits/Maintenance/';
$traitsDir = opendir($traits);
while ($traitFile = readdir($traitsDir)) {
if (str_contains($traitFile, '.php')) {
require $traits . $traitFile;
}
}
closedir($traitsDir);

class Maintenance
{
use Host;
use Maint;

protected $maintenanceContainerName = 'dockwatch-maintenance';
protected $maintenancePort;
protected $maintenanceIP;
protected $settingsFile;
protected $hostContainer = [];
protected $maintenanceContainer = [];
protected $processList = [];

public function __construct()
{
global $settingsFile;

logger(MAINTENANCE_LOG, '$maintenance->__construct() ->');

if (!$settingsFile) {
$settingsFile = getServerFile('settings');
$settingsFile = $settingsFile['file'];
}

$this->settingsFile = $settingsFile;
$this->maintenancePort = $settingsFile['global']['maintenancePort'];
$this->maintenanceIP = $settingsFile['global']['maintenanceIP'];
$getExpandedProcessList = getExpandedProcessList(true, true, true, true);
$this->processList = is_array($getExpandedProcessList['processList']) ? $getExpandedProcessList['processList'] : [];
$imageMatch = str_replace(':main', '', APP_IMAGE);

logger(MAINTENANCE_LOG, 'Process list: ' . count($this->processList) . ' containers');

foreach ($this->processList as $process) {
logger(MAINTENANCE_LOG, 'Checking \'' . $process['inspect'][0]['Config']['Image'] . '\' contains \'' . $imageMatch . '\'');

if (str_contains($process['inspect'][0]['Config']['Image'], $imageMatch) && $process['Names'] != $this->maintenanceContainerName) {
$this->hostContainer = $process;
}

if ($process['Names'] == $this->maintenanceContainerName) {
$this->maintenanceContainer = $process;
}

if ($this->hostContainer && $this->maintenanceContainer) {
break;
}
}

logger(MAINTENANCE_LOG, '$maintenance->__construct() <-');
}

public function __toString()
{
return 'Maintenance initialized';
}

public function startup()
{
logger(MAINTENANCE_LOG, '$maintenance->startup() ->');

if (file_exists(TMP_PATH . 'restart.txt')) { //-- dockwatch-maintenance CHECKING ON dockwatch RESTART
logger(MAINTENANCE_LOG, 'restart requested for \'' . $this->hostContainer['Names'] . '\'');

unlink(TMP_PATH . 'restart.txt');
logger(MAINTENANCE_LOG, 'removed ' . TMP_PATH . 'restart.txt');

$this->stopHost();
$this->startHost();
} elseif (file_exists(TMP_PATH . 'update.txt')) { //-- dockwatch-maintenance CHECKING ON dockwatch UPDATE
logger(MAINTENANCE_LOG, 'update requested for \'' . $this->hostContainer['Names'] . '\'');

unlink(TMP_PATH . 'update.txt');
logger(MAINTENANCE_LOG, 'removed ' . TMP_PATH . 'update.txt');

$this->stopHost();
$this->removeHost();
$this->pullHost();
$this->createHost();
} else { //-- dockwatch CHECKING ON dockwatch-maintenance REMOVAL
logger(MAINTENANCE_LOG, 'removing \'' . $this->maintenanceContainerName . '\'');
$this->removeMaintenance();
}

logger(MAINTENANCE_LOG, '$maintenance->startup() <-');
}

function apply($action)
{
file_put_contents(TMP_PATH . $action . '.txt', $action);
$this->createMaintenance();
}
}
1 change: 0 additions & 1 deletion root/app/www/public/classes/Notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
----------------------------------
*/


//-- BRING IN THE TRAITS
$traits = ABSOLUTE_PATH . 'classes/traits/Notifications/';
$traitsDir = opendir($traits);
Expand Down
65 changes: 65 additions & 0 deletions root/app/www/public/classes/traits/Maintenance/host.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

/*
----------------------------------
------ Created: 021024 ------
------ Austin Best ------
----------------------------------
*/

trait Host
{
public function stopHost()
{
logger(MAINTENANCE_LOG, '$maintenance->stopHost() ->');

$docker = dockerStopContainer($this->hostContainer['Names']);
logger(MAINTENANCE_LOG, 'dockerStopContainer() ' . trim($docker));

logger(MAINTENANCE_LOG, '$maintenance->stopHost() <-');
}

public function startHost()
{
logger(MAINTENANCE_LOG, '$maintenance->startHost() ->');

$docker = dockerStartContainer($this->hostContainer['Names']);
logger(MAINTENANCE_LOG, 'dockerStartContainer() ' . trim($docker));

logger(MAINTENANCE_LOG, '$maintenance->startHost() <-');
}

public function removeHost()
{
logger(MAINTENANCE_LOG, '$maintenance->removeHost() ->');

$docker = dockerRemoveContainer($this->hostContainer['Names']);
logger(MAINTENANCE_LOG, 'dockerRemoveContainer() ' . trim($docker));

logger(MAINTENANCE_LOG, '$maintenance->removeHost() <-');
}

public function pullHost()
{
logger(MAINTENANCE_LOG, '$maintenance->pullHost() ->');

$docker = dockerPullContainer($this->hostContainer['inspect'][0]['Config']['Image']);
logger(MAINTENANCE_LOG, 'dockerPullContainer() ' . trim($docker));

logger(MAINTENANCE_LOG, '$maintenance->pullHost() <-');
}

public function createHost()
{
logger(MAINTENANCE_LOG, '$maintenance->createHost() ->');

$docker = dockerCreateContainer($this->hostContainer['inspect'][0]);
logger(MAINTENANCE_LOG, 'dockerCreateContainer() ' . json_encode($docker, JSON_UNESCAPED_SLASHES));

if (strlen($docker['Id']) == 64) {
$this->startHost();
}

logger(MAINTENANCE_LOG, '$maintenance->createHost() <-');
}
}
100 changes: 100 additions & 0 deletions root/app/www/public/classes/traits/Maintenance/maint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

/*
----------------------------------
------ Created: 021024 ------
------ Austin Best ------
----------------------------------
*/

trait Maint
{
public function startMaintenance()
{
logger(MAINTENANCE_LOG, '$maintenance->startMaintenance() ->');

$docker = dockerStartContainer($this->maintenanceContainerName);
logger(MAINTENANCE_LOG, 'dockerStartContainer() ' . trim($docker));

logger(MAINTENANCE_LOG, '$maintenance->startMaintenance() <-');
}

public function stopMaintenance()
{
logger(MAINTENANCE_LOG, '$maintenance->stopMaintenance() ->');

$docker = dockerStopContainer($this->maintenanceContainerName);
logger(MAINTENANCE_LOG, 'dockerStopContainer() ' . trim($docker));

logger(MAINTENANCE_LOG, '$maintenance->stopMaintenance() <-');
}

public function removeMaintenance()
{
logger(MAINTENANCE_LOG, '$maintenance->removeMaintenance() ->');

$docker = dockerRemoveContainer($this->maintenanceContainerName);
logger(MAINTENANCE_LOG, 'dockerRemoveContainer() ' . trim($docker));

logger(MAINTENANCE_LOG, '$maintenance->removeMaintenance() <-');
}

public function pullMaintenance()
{
logger(MAINTENANCE_LOG, '$maintenance->pullMaintenance() ->');

$docker = dockerPullContainer(APP_MAINTENANCE_IMAGE);
logger(MAINTENANCE_LOG, 'dockerPullContainer() ' . trim($docker));

logger(MAINTENANCE_LOG, '$maintenance->pullMaintenance() <-');
}

public function createMaintenance()
{
$port = intval($this->maintenancePort) > 0 ? intval($this->maintenancePort) : 9998;
$ip = $this->maintenanceIP;

logger(MAINTENANCE_LOG, '$maintenance->createMaintenance() ->');
logger(MAINTENANCE_LOG, 'using ip ' . $ip);
logger(MAINTENANCE_LOG, 'using port ' . $port);

$this->pullMaintenance();

$apiResponse = apiRequest('dockerInspect', ['name' => $this->hostContainer['Names'], 'useCache' => false, 'format' => true]);
logger(MAINTENANCE_LOG, 'dockerInspect:' . json_encode($apiResponse, JSON_UNESCAPED_SLASHES));
$inspectImage = $apiResponse['response']['docker'];
$inspectImage = json_decode($inspectImage, true);

$inspectImage[0]['Name'] = '/' . $this->maintenanceContainerName;
$inspectImage[0]['Config']['Image'] = APP_MAINTENANCE_IMAGE;
$inspectImage[0]['HostConfig']['PortBindings']['80/tcp'][0]['HostPort'] = strval($port);
$inspectImage[0]['NetworkSettings']['Ports']['80/tcp'][0]['HostPort'] = strval($port);

//-- STATIC IP CHECK
if ($ip) {
if ($inspectImage[0]['NetworkSettings']['Networks']) {
$network = array_keys($inspectImage[0]['NetworkSettings']['Networks'])[0];

if ($inspectImage[0]['NetworkSettings']['Networks'][$network]['IPAMConfig']['IPv4Address']) {
$inspectImage[0]['NetworkSettings']['Networks'][$network]['IPAMConfig']['IPv4Address'] = $ip;
}
if ($inspectImage[0]['NetworkSettings']['Networks'][$network]['IPAddress']) {
$inspectImage[0]['NetworkSettings']['Networks'][$network]['IPAddress'] = $ip;
}
}
}

$this->removeMaintenance();

logger(MAINTENANCE_LOG, 'dockerCreateContainer() ->');
$docker = dockerCreateContainer($inspectImage);
logger(MAINTENANCE_LOG, 'dockerCreateContainer() ' . json_encode($docker, JSON_UNESCAPED_SLASHES));
logger(MAINTENANCE_LOG, 'dockerCreateContainer() <-');

if (strlen($docker['Id']) == 64) {
$this->startMaintenance();
}

logger(MAINTENANCE_LOG, '$maintenance->createMaintenance() <-');
}
}
Loading

0 comments on commit d26e897

Please sign in to comment.