#!/usr/bin/php -q $docroot ??= ($_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp'); require_once "$docroot/webGui/include/Wrappers.php"; extract(parse_plugin_cfg('dynamix',true)); // add translations $_SERVER['REQUEST_URI'] = ''; $login_locale = _var($display,'locale'); require_once "$docroot/plugins/dynamix.docker.manager/include/DockerClient.php"; $var = parse_ini_file('/var/local/emhttp/var.ini'); $DockerClient = new DockerClient(); $DockerUpdate = new DockerUpdate(); $DockerTemplates = new DockerTemplates(); $custom = DockerUtil::custom(); $subnet = DockerUtil::network($custom); $cpus = DockerUtil::cpus(); function write(...$messages){ $com = curl_init(); curl_setopt_array($com,[ CURLOPT_URL => 'http://localhost/pub/docker?buffer_length=1', CURLOPT_UNIX_SOCKET_PATH => '/var/run/nginx.socket', CURLOPT_POST => 1, CURLOPT_RETURNTRANSFER => true ]); foreach ($messages as $message) { curl_setopt($com, CURLOPT_POSTFIELDS, $message); curl_exec($com); } curl_close($com); } function stopContainer_nchan($name) { global $DockerClient; $waitID = mt_rand(); write("
","addLog\0","show_Wait\0$waitID"); $retval = $DockerClient->stopContainer($name); $out = ($retval === true) ? _('Successfully stopped container').": $name" : _('Error').": ".$retval; write("stop_Wait\0$waitID","addLog\0".htmlspecialchars($out).""); } function removeContainer_nchan($name) { global $DockerClient; $waitID = mt_rand(); write("","addLog\0","show_Wait\0$waitID"); $retval = $DockerClient->removeContainer($name); $out = ($retval === true) ? _('Successfully removed container').": $name" : _('Error').": ".$retval; write("stop_Wait\0$waitID","addLog\0".htmlspecialchars($out).""); } function removeImage_nchan($image) { global $DockerClient; $waitID = mt_rand(); write("","addLog\0","show_Wait\0$waitID"); $retval = $DockerClient->removeImage($image); $out = ($retval === true) ? _('Successfully removed orphan image').": $image" : _('Error').": ".$retval; write("stop_Wait\0$waitID","addLog\0".htmlspecialchars($out).""); } function pullImage_nchan($name, $image) { global $DockerClient, $DockerTemplates, $DockerUpdate; $waitID = mt_rand(); if (!preg_match("/:\S+$/", $image)) $image .= ":latest"; write("","addLog\0","show_Wait\0$waitID"); $alltotals = []; $laststatus = []; $strError = ''; $DockerClient->pullImage($image, function ($line) use (&$alltotals, &$laststatus, &$waitID, &$strError, $image, $DockerClient, $DockerUpdate) { $cnt = json_decode($line, true); $id = $cnt['id'] ?? ''; $status = $cnt['status'] ?? ''; if (isset($cnt['error'])) $strError = $cnt['error']; if ($waitID !== false) { write("stop_Wait\0$waitID"); $waitID = false; } if (empty($status)) return; if (!empty($id)) { if (!empty($cnt['progressDetail']) && !empty($cnt['progressDetail']['total'])) { $alltotals[$id] = $cnt['progressDetail']['total']; } if (empty($laststatus[$id])) { $laststatus[$id] = ''; } switch ($status) { case 'Waiting': // Omit break; case 'Downloading': if ($laststatus[$id] != $status) { write("addToID\0$id\0".htmlspecialchars($status)); } $total = $cnt['progressDetail']['total']; $current = $cnt['progressDetail']['current']; if ($total > 0) { $percentage = round(($current / $total) * 100); write("progress\0$id\0 ".$percentage."% "._('of')." ".$DockerClient->formatBytes($total)); } else { // Docker must not know the total download size (http-chunked or something?) // just show the current download progress without the percentage $alltotals[$id] = $current; write("progress\0$id\0".$DockerClient->formatBytes($current)); } break; default: if ($laststatus[$id] == "Downloading") { write("progress\0$id\0 100% "._('of')." ".$DockerClient->formatBytes($alltotals[$id])); } if ($laststatus[$id] != $status) { write("addToID\0".($id=='latest'?mt_rand():$id)."\0".htmlspecialchars($status)); } break; } $laststatus[$id] = $status; } else { if (strpos($status, 'Status: ') === 0) { write("addLog\0".htmlspecialchars($status)); } if (strpos($status, 'Digest: ') === 0) { $DockerUpdate->setUpdateStatus($image, substr($status,8)); } } }); write("addLog\0