Docker-Swarm-Loadbalancer/docs
2019-06-11 16:59:58 +02:00

218 lines
No EOL
6.8 KiB
PHP
Executable file

#!/usr/bin/php
<?php
$versionedImages = [
'gone/php:cli-php%php_version%',
'gone/php:apache-php%php_version%',
'gone/php:nginx-php%php_version%',
];
$phpVersions = [
'5.6',
'7.0',
'7.1',
'7.2',
'7.3'
];
$images = [
'gone/marshall:latest',
'gone/php:cli',
'gone/php:apache',
'gone/php:nginx',
];
foreach($versionedImages as $versionedImage){
foreach($phpVersions as $phpVersion){
$images[] = str_replace("%php_version%", $phpVersion, $versionedImage);
}
}
sort($images);
$availabilityTable = [];
foreach ($images as $imageName){
@list($containerImageName, $version) = explode(":", $imageName,2);
$containerImageName = trim($containerImageName);
$version = trim(trim($version));
$cmdImage = "docker images | grep {$containerImageName}" . ($version ? " | grep {$version}" : "");
$cmdHistory = sprintf(
"docker history %s:%s -H=false",
trim($containerImageName),
trim($version)
);
$layers = 0;
foreach(explode("\n", shell_exec($cmdHistory)) as $layer) {
$layers++;
}
foreach(explode("\n", shell_exec($cmdImage)) as $imageStats) {
$imageStats = explode(" ", $imageStats);
$imageStats = array_values(array_filter($imageStats));
if(count($imageStats) == 5) {
list($name, $version, $id, $time, $size) = $imageStats;
$containerImageNameAndVersion = trim($name) . ":" . trim($version);
if($layers < 25){
$chosenLayerColour = 'green';
}elseif($layers < 35){
$chosenLayerColour = 'orange';
}else{
$chosenLayerColour = 'red';
}
$availabilityTable[$containerImageNameAndVersion] = [
'Image Name' => $containerImageNameAndVersion,
'File Size' => trim($size),
'Layers' => "[![Layers](https://img.shields.io/badge/Layers-{$layers}-{$chosenLayerColour}.svg)](https://hub.docker.com/r/" . trim($name) . ")",
'Microbadger' => "[![](https://images.microbadger.com/badges/image/{$containerImageNameAndVersion}.svg)](https://microbadger.com/images/{$containerImageNameAndVersion} \"Get your own image badge on microbadger.com\")",
'PHP' => stripos($containerImageNameAndVersion, "php") ? '✔' : ' ',
'Apache' =>stripos($containerImageNameAndVersion, "apache") ? '✔' : ' ',
'Nginx' => stripos($containerImageNameAndVersion, "nginx") ? '✔' : ' ',
'Node' => stripos($containerImageNameAndVersion, "nodejs") ? '✔' : ' ',
];
}
}
}
ksort($availabilityTable);
$outputArray = [];
foreach($availabilityTable as $item){
$outputArray[] = array_values($item);
}
$detectedPHPVersion = trim(shell_exec('docker run gone/php:cli php -v | head -1 | cut -d "-" -f 1'));
$availabilityTable = new TextTable(
array_keys(reset($availabilityTable))
);
$availabilityTable->maxlen = 100000;
$availabilityTable->addData($outputArray);
$template = file_get_contents("README.Template");
$replacements = [
"%%Availability_Table%%" => $availabilityTable->render(),
"%%Php_Version%%" => $detectedPHPVersion,
"%%Generated_At%%" => date("Y-m-d H:i:s"),
];
foreach($replacements as $search => $replacement){
$template = str_replace($search, $replacement, $template);
}
file_put_contents("README.md", $template);
/**
* Creates a markdown document based on the parsed documentation
*
* @author Peter-Christoph Haider <peter.haider@zeyon.net>
* @package Apidoc
* @version 1.00 (2014-04-04)
* @license GNU Lesser Public License
*/
class TextTable {
/** @var int The source path */
public $maxlen = 50;
/** @var array The source path */
private $data = array();
/** @var array The source path */
private $header = array();
/** @var array The source path */
private $len = array();
/** @var array The source path */
private $align = array(
'name' => 'L',
'type' => 'C'
);
/**
* @param array $header The header array [key => label, ...]
* @param array $content Content
* @param array $align Alignment optios [key => L|R|C, ...]
*/
public function __construct($header=null, $content=array(), $align=false) {
if ($header) {
$this->header = $header;
} elseif ($content) {
foreach ($content[0] as $key => $value)
$this->header[$key] = $key;
}
foreach ($this->header as $key => $label) {
$this->len[$key] = strlen($label);
}
if (is_array($align))
$this->setAlign($align);
$this->addData($content);
}
/**
* Overwrite the alignment array
*
* @param array $align Alignment optios [key => L|R|C, ...]
*/
public function setAlign($align) {
$this->align = $align;
}
/**
* Add data to the table
*
* @param array $content Content
*/
public function addData($content) {
foreach ($content as &$row) {
foreach ($this->header as $key => $value) {
if (!isset($row[$key])) {
$row[$key] = '-';
} elseif (mb_strlen($row[$key]) > $this->maxlen) {
$this->len[$key] = $this->maxlen;
$row[$key] = substr($row[$key], 0, $this->maxlen-3).'...';
} elseif (mb_strlen($row[$key]) > $this->len[$key]) {
$this->len[$key] = mb_strlen($row[$key]);
}
}
}
$this->data = $this->data + $content;
return $this;
}
/**
* Add a delimiter
*
* @return string
*/
private function renderDelimiter() {
$res = '|';
foreach ($this->len as $key => $l)
$res .= (isset($this->align[$key]) && ($this->align[$key] == 'C' || $this->align[$key] == 'L') ? ':' : ' ')
.str_repeat('-', $l)
.(isset($this->align[$key]) && ($this->align[$key] == 'C' || $this->align[$key] == 'R') ? ':' : ' ')
.'|';
return $res."\r\n";
}
/**
* Render a single row
*
* @param array $row
* @return string
*/
private function renderRow($row) {
$res = '|';
foreach ($this->len as $key => $l) {
$res .= ' '.$row[$key].($l > mb_strlen($row[$key]) ? str_repeat(' ', $l - mb_strlen($row[$key])) : '').' |';
}
return $res."\r\n";
}
/**
* Render the table
*
* @param array $content Additional table content
* @return string
*/
public function render($content=array()) {
$this->addData($content);
$res = $this->renderRow($this->header)
.$this->renderDelimiter();
foreach ($this->data as $row)
$res .= $this->renderRow($row);
return $res;
}
}