Christopher Vagnetoft
538383c33d
* com.noccy.pdo: Implemented reflection for PDO databases, tables and columns. Reflectors for MySQL and Sqlite. * com.noccy.pdo: Added pdo:inspect command. * com.noccy.docker: Added basic stack management and commands. * com.noccy.docker: Moved commands to dedicated namespace. * Environment: readConfig and writeConfig helper added, with a flag to use the global config dir ~/.config/spark.
89 lines
2.9 KiB
PHP
89 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace SparkPlug\Com\Noccy\Docker\Stack;
|
|
|
|
use Spark\Environment\Environment;
|
|
use Symfony\Component\Console\Helper\Table;
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
class Stack
|
|
{
|
|
|
|
const BULLET="\u{25cf}";
|
|
|
|
private string $path;
|
|
|
|
private array $options = [];
|
|
|
|
public function __construct(string $path, array $options)
|
|
{
|
|
$this->path = $path;
|
|
$this->options = $options;
|
|
}
|
|
|
|
public function getPath(): string
|
|
{
|
|
return $this->path;
|
|
}
|
|
|
|
public function getName(): string
|
|
{
|
|
return $this->options['name'] ?? basename($this->path);
|
|
}
|
|
|
|
public function getComposeFile(): string
|
|
{
|
|
return $this->path . "/" . ($this->options['compose']??"docker-compose.yml");
|
|
}
|
|
|
|
public function getContainersTable(OutputInterface $output): Table
|
|
{
|
|
exec("docker-compose -f " . escapeshellarg($this->getComposeFile()) . " ps -q", $ids, $ret);
|
|
|
|
if (count($ids) === 0) {
|
|
$json = [];
|
|
} else {
|
|
exec("docker inspect ".join(" ",$ids), $out, $ret);
|
|
$json = json_decode(join("", $out));
|
|
}
|
|
|
|
$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) ]);
|
|
}
|
|
|
|
return $table;
|
|
}
|
|
|
|
|
|
} |