Skip to content

Instantly share code, notes, and snippets.

@nfreear
Created April 17, 2018 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nfreear/c413d9847515fd2624c90717ad192b5c to your computer and use it in GitHub Desktop.
Save nfreear/c413d9847515fd2624c90717ad192b5c to your computer and use it in GitHub Desktop.
CLI. Run 'docker ps' and 'docker exec ... git describe'. Output the result as JSON.
#!/usr/bin/env php
<?php
/**
* CLI. Run 'docker ps' and 'docker exec ... git describe'. Output the result as JSON.
*
* USAGE:
* sudo su
* ./docker-status.php
*
* @copyright © Nick Freear, 17-April-2018.
*/
error_reporting( E_ALL );
ini_set( 'display_errors', true );
// echo "Start\n";
define( 'D_PS_REGEX', '/(?P<ctr>\w+) +(?P<img>.+)\/(?P<srv>[\w\-]+)\-service:(?P<v>RC\d\.\d+) +(?P<cmd>.+) +(?P<cr>\d.+ ago) +(?P<st>Up \d+ \w+) +(?P<port>.+) {2,}(?P<name>.+)/' );
$labels = [
'lti' => 'LTI',
'rt' => 'Reporting tool',
];
$result = [];
$output = [];
$return_var = null;
$last_line = exec( 'docker ps --no-trunc', $output, $return_var);
fwrite( STDERR, "docker ps - exit code: $return_var\n" );
foreach ($output as $line) {
if (preg_match( D_PS_REGEX, $line, $matches)) {
$matches = (object) $matches;
$desc_cmd = "docker exec -t {$matches->ctr} sh -c \"git describe --always --long --tags --dirty\"";
$diff_cmd = "docker exec -t {$matches->ctr} sh -c \"git diff --no-color --raw\"";
$desc_output = [];
$diff_output = [];
$last_line = exec( $desc_cmd, $desc_output, $return_var );
if (0 === $return_var) {
$last_line = exec( $diff_cmd, $diff_output, $return_var );
}
fwrite( STDERR, $desc_cmd . PHP_EOL );
$result[ $matches->srv ] = [
'label' => isset( $labels[ $matches->srv ]) ? $labels[ $matches->srv ] : '(unknown)',
'service' => $matches->srv,
'version' => $matches->v,
'container_id' => $matches->ctr,
'image' => $matches->img . '/' . $matches->srv . '-service:..',
'command' => trim( $matches->cmd ),
'created' => $matches->cr,
'status' => $matches->st,
'ports' => trim( $matches->port ),
'names' => $matches->name,
'git_describe_cmd' => $desc_cmd,
'git_describe' => $desc_output,
'git_diff' => $diff_output,
];
}
}
echo json_encode( $result, JSON_PRETTY_PRINT );
# End.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment