aboutsummaryrefslogtreecommitdiffstats
path: root/util.php
blob: b5cb9c7bff87ba8fd1abd58b353f0093303f43f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
namespace Util;

class ServiceStatus
{
    public $name;
    public $containerId;
    public $status;
    public $startedAt;
    public $finishedAt;
    public $isNotFound;

    public function __construct($name, $containerId, $status, $startedAt, $finishedAt, $isNotFound)
    {
        $this->name = $name;
        $this->containerId = $containerId;
        $this->status = $status;
        $this->startedAt = $startedAt;
        $this->finishedAt = $finishedAt;
	$this->isNotFound = $isNotFound;
    }
}

function getDockerStatus($containerName): ServiceStatus
{
    $dockerOutput = exec('sudo docker inspect --format=\'{{.Id}} {{.State.Status}} {{.State.StartedAt}} {{.State.FinishedAt}}\' ' . $containerName);
    if (empty($dockerOutput)) {
        return new ServiceStatus($containerName, '-', '-', '-', '-', true);
    }
    $parts = explode(' ', $dockerOutput);
    $status = new ServiceStatus($containerName, substr($parts[0], 0, 12), $parts[1], $parts[2], $parts[3], false);
    return $status;
}

function createStatusTable(ServiceStatus $status)
{
    echo ('<table>');
    echo ('<tr><th>Container ID</th><th>Name</th><th>Status</th><th>Started at</th><th>Finished at</th></tr>');
    echo ('<tr>');
    echo ('<td>' . $status->containerId . '</td>');
    echo ('<td>' . $status->name . '</td>');
    echo ('<td>' . $status->status . '</td>');
    echo ('<td>' . $status->startedAt . '</td>');
    echo ('<td>' . $status->finishedAt . '</td>');
    echo ('</tr>');
    echo ('</table>');
}

function createStatusBanner(ServiceStatus $status)
{
    if ($status->isNotFound) {
        createBanner('✗', "Container '" . $status->name . "' not found", 'bad');
        return;
    }
    $state = $status->status === 'running' ? 'good' : 'bad';
    $symbol = $status->status === 'running' ? '✓' : '✗';
    createBanner($symbol, "Status of '$status->name' is '$status->status'", $state);
}

function createBanner($symbol, $message, $state)
{
    echo ('<div class="status-banner ' . $state . '">');
    echo ("<p><b>$symbol</b> $message</p>");
    echo ('</div>');
}

function doShellExec($command, $redirect, $action)
{
    $output = shell_exec($command);
    //if (empty($output)) {
    //    header("Location: $redirect");
    //    exit;
    //}
    echo "<p>Output of $action</p>";
    echo "<pre>$output</pre>";
    echo "<p class='control-list'><a href='$redirect'>[Acknowledge]</a></p>";
    exit;
}

function doSessionCheck($redirect)
{
    if (!isset($_SESSION['token']) || $_SESSION['token'] !== getSuperSecretToken()) {
        header('Location: authenticate.php?redirect=/' . $redirect);
        exit;
    }
}

include('key.php');

function getSuperSecretToken()
{
    global $superSecretToken;
    return $superSecretToken;
}