71 lines
2.6 KiB
PHP
71 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace SparkPlug\Com\Noccy\Docker;
|
|
|
|
use Spark\Commands\Command;
|
|
use SparkPlug;
|
|
use Symfony\Component\Console\Helper\Table;
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
class DockerStatusCommand extends Command
|
|
{
|
|
const BULLET="\u{25cf}";
|
|
|
|
protected function configure()
|
|
{
|
|
$this->setName("docker:status")
|
|
->setDescription("Show docker status");
|
|
}
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
{
|
|
|
|
exec("docker-compose ps -q", $ids, $ret);
|
|
if (count($ids) === 0) return Command::SUCCESS;
|
|
|
|
exec("docker inspect ".join(" ",$ids), $out, $ret);
|
|
$json = json_decode(join("", $out));
|
|
|
|
$stack = get_plugin('com.noccy.docker')->getComposeStack();
|
|
|
|
$table = new Table($output);
|
|
$table->setStyle("box");
|
|
$table->setHeaders([ "Name", "Status", "Image", "Ports" ]);
|
|
foreach ($json as $container) {
|
|
$startedTs = preg_replace('/(\.([0-9]+)Z)$/', '+0100', $container->State->StartedAt);
|
|
$s = date_parse($startedTs);
|
|
$started = mktime($s['hour'], $s['minute'], $s['second'], $s['month'], $s['day'], $s['year']) + 3600;
|
|
if ($container->State->Dead) {
|
|
$status = "<fg=red>".self::BULLET."</> ".$container->State->Status;
|
|
} elseif ($container->State->Restarting) {
|
|
$status = "<fg=yellow>".self::BULLET."</> ".$container->State->Status;
|
|
} elseif ($container->State->Running) {
|
|
$elapsed = time() - $started;
|
|
if ($elapsed > 60) {
|
|
$em = floor($elapsed / 60);
|
|
$es = $elapsed - ($em * 60);
|
|
if ($em>60) {
|
|
$eh = floor($em / 60);
|
|
$em = $em - ($eh * 60);
|
|
$elapsed = sprintf("%dh%dm%ds", $eh, $em, $es);
|
|
} else {
|
|
$elapsed = sprintf("%dm%ds", $em, $es);
|
|
}
|
|
} else {
|
|
$elapsed = sprintf("%ds", $elapsed);
|
|
}
|
|
$status = "<fg=green>".self::BULLET."</> ".$container->State->Status." (<fg=green>{$elapsed}</>)";
|
|
} else {
|
|
$status = "<fg=red>".self::BULLET."</> ".$container->State->Status;
|
|
}
|
|
$ports = $container->Config->ExposedPorts??[];
|
|
$ports = array_keys((array)$ports);
|
|
$table->addRow([ $container->Name, $status, $container->Config->Image, join(", ", $ports) ]);
|
|
}
|
|
$table->render();
|
|
|
|
return Command::SUCCESS;
|
|
}
|
|
}
|