Skip to content

Instantly share code, notes, and snippets.

@gfjardim
Last active March 2, 2016 20:22
Show Gist options
  • Save gfjardim/f7e2e9cb1eb525e2ea12 to your computer and use it in GitHub Desktop.
Save gfjardim/f7e2e9cb1eb525e2ea12 to your computer and use it in GitHub Desktop.
Translate unRAID templates to Docker Run commands
#!/usr/bin/php
<?PHP
function xmlToCommand($xmlFile){
global $var;
$doc = new DOMDocument();
$doc->loadXML($xmlFile);
$Name = $doc->getElementsByTagName( "Name" )->item(0)->nodeValue;
$cmdName = (strlen($Name)) ? '--name="' . $Name . '"' : "";
$Privileged = $doc->getElementsByTagName( "Privileged" )->item(0)->nodeValue;
$cmdPrivileged = (strtolower($Privileged) == 'true') ? '--privileged="true"' : "";
$Repository = $doc->getElementsByTagName( "Repository" )->item(0)->nodeValue;
$Mode = $doc->getElementsByTagName( "Mode" )->item(0)->nodeValue;
$cmdMode = '--net="'.strtolower($Mode).'"';
$BindTime = $doc->getElementsByTagName( "BindTime" )->item(0)->nodeValue;
// $cmdBindTime = (strtolower($BindTime) == "true") ? '"/etc/localtime":"/etc/localtime":ro' : '';
$cmdBindTime = (strtolower($BindTime) == "true") ? 'TZ="' . $var['timeZone'] . '"' : '';
$Ports = array('');
foreach($doc->getElementsByTagName('Port') as $port){
$ContainerPort = $port->getElementsByTagName( "ContainerPort" )->item(0)->nodeValue;
if (! strlen($ContainerPort)){ continue; }
$HostPort = $port->getElementsByTagName( "HostPort" )->item(0)->nodeValue;
$Protocol = $port->getElementsByTagName( "Protocol" )->item(0)->nodeValue;
$Ports[] = sprintf("%s:%s/%s", $HostPort, $ContainerPort, $Protocol);
}
$Volumes = array('');
foreach($doc->getElementsByTagName('Volume') as $volume){
$ContainerDir = $volume->getElementsByTagName( "ContainerDir" )->item(0)->nodeValue;
if (! strlen($ContainerDir)){ continue; }
$HostDir = $volume->getElementsByTagName( "HostDir" )->item(0)->nodeValue;
$DirMode = $volume->getElementsByTagName( "Mode" )->item(0)->nodeValue;
$Volumes[] = sprintf( '"%s":"%s":%s', $HostDir, $ContainerDir, $DirMode);
}
// if (strlen($cmdBindTime)) {
// $Volumes[] = $cmdBindTime;
// }
$Variables = array('');
foreach($doc->getElementsByTagName('Variable') as $variable){
$VariableName = $variable->getElementsByTagName( "Name" )->item(0)->nodeValue;
if (! strlen($VariableName)){ continue; }
$VariableValue = $variable->getElementsByTagName( "Value" )->item(0)->nodeValue;
$Variables[] = sprintf('%s="%s"', $VariableName, $VariableValue);
}
if (strlen($cmdBindTime)) {
$Variables[] = $cmdBindTime;
}
$templateExtraParams = '';
if ( $doc->getElementsByTagName( "ExtraParams" )->length > 0 ) {
$templateExtraParams = $doc->getElementsByTagName( "ExtraParams" )->item(0)->nodeValue;
}
$cmd = sprintf('/usr/bin/docker run -d %s %s %s %s %s %s %s %s', $cmdName, $cmdMode, $cmdPrivileged, implode(' -e ', $Variables),
implode(' -p ', $Ports), implode(' -v ', $Volumes), $templateExtraParams, $Repository);
$cmd = preg_replace('/\s+/', ' ', $cmd);
return array($cmd, $Name, $Repository);
}
function listDir($root, $ext = Null) {
$iter = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($root,
RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST,
RecursiveIteratorIterator::CATCH_GET_CHILD);
$paths = array();
foreach ($iter as $path => $fileinfo) {
if ( $fileinfo->isFile() && $fileinfo->getExtension() == $ext ) {
$paths[] = $path;
} elseif ( $fileinfo->isFile() && ! $ext) {
$paths[] = $path;
}
}
return $paths;
}
function Choose($Variable, $MaxCols = 3) {
$Columns = array_chunk($Variable, ceil(count($Variable) / 3));
$Rows = array();
$OptNum = 1;
for ($i=0; $i < count($Columns); $i++) {
foreach($Columns[$i] as $Value) {
$Row =& $Rows[$i];
$output = "{$OptNum}) {$Value}";
$Row[] = $output;
$Row["M"] = max($Row["M"], strlen($output));
$OptNum += 1;
}
}
for ($i=0; $i < count($Rows[0]); $i++) {
$max = 10;
foreach ($Rows as $row) {
echo str_pad($row[$i], $row["M"]+10);
}
echo "\n";
}
$ask = function($var) {
echo "\nChoose: ";
$answer = trim(fgets(STDIN));
if (is_numeric($answer)) {
$option = intval($answer) - 1;
if ($option >= 0 && $option <= count($var)) {
return $option;
} else {
echo "Out of interval.";
}
} else {
echo "Value is not numeric.";
}
return false;
};
while (is_bool($ans = $ask($Variable)) ) {
continue;
}
return $ans;
}
function fileToCommand($File) {
$doc = new DOMDocument();
$doc->load($File);
list($cmd, $Name, $Repository) = xmlToCommand($doc->saveXML());
return $cmd;
}
$user_templates = "/boot/config/plugins/dockerMan/templates-user";
if (count($argv) > 1) {
foreach (array_slice($argv,1) as $Template) {
$Temp = "$user_templates/my-{$argv[1]}.xml";
if (is_file($Temp)) {
echo fileToCommand( $Temp );
}
}
} else {
$Templates = listDir($user_templates,"xml");
natsort($Templates);
$Templates = array_values($Templates);
$Names = array_map(function($template) {
$info = new SplFileInfo($template);
$ext = $info->getExtension();
return str_replace("my-", "", $info->getBasename(".{$ext}"));
}, $Templates);
$Template = $Templates[Choose($Names)];
if ( $Template ) {
echo "\n".fileToCommand($Template)."\n\n";
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment