diff --git a/emhttp/plugins/dynamix.docker.manager/include/CreateDocker.php b/emhttp/plugins/dynamix.docker.manager/include/CreateDocker.php index 2966cf214a..739c7cce58 100755 --- a/emhttp/plugins/dynamix.docker.manager/include/CreateDocker.php +++ b/emhttp/plugins/dynamix.docker.manager/include/CreateDocker.php @@ -80,8 +80,15 @@ function cpu_pinning() { // Saving the generated configuration file. $userTmplDir = $dockerManPaths['templates-user']; if (!is_dir($userTmplDir)) mkdir($userTmplDir, 0777, true); + $sourceTemplate = _var($_POST,'sourceTemplate',false); + if ($sourceTemplate) $sourceTemplate = unscript(urldecode($sourceTemplate)); + $sourceUserTemplate = $sourceTemplate && basename($sourceTemplate)==$sourceTemplate && preg_match('/^my-[^\/\\\\]+\.xml$/', $sourceTemplate); + if ($sourceUserTemplate) { + $sourceTemplate = "$userTmplDir/$sourceTemplate"; + $sourceUserTemplate = is_file($sourceTemplate); + } if ($Name) { - $filename = sprintf('%s/my-%s.xml', $userTmplDir, $Name); + $filename = ($sourceUserTemplate && $existing === $Name) ? $sourceTemplate : $DockerTemplates->getUserTemplatePath($Name); if (is_file($filename)) { $oldXML = simplexml_load_file($filename); if ($oldXML->Icon != $_POST['contIcon']) { @@ -137,8 +144,9 @@ function cpu_pinning() { // force kill container if still running after 10 seconds removeContainer($existing,1); // remove old template - if (strtolower($filename) != strtolower("$userTmplDir/my-$existing.xml")) { - @unlink("$userTmplDir/my-$existing.xml"); + $oldFilename = $sourceUserTemplate ? $sourceTemplate : $DockerTemplates->getUserTemplatePath($existing); + if (strtolower($filename) != strtolower($oldFilename)) { + @unlink($oldFilename); } } // Extract real Entrypoint and Cmd from container for Tailscale @@ -898,6 +906,9 @@ function prepareCategory() {
+ + + doesContainerExist($templateName)):?> diff --git a/emhttp/plugins/dynamix.docker.manager/include/DockerClient.php b/emhttp/plugins/dynamix.docker.manager/include/DockerClient.php index 5475cfe0b0..e7277b3375 100755 --- a/emhttp/plugins/dynamix.docker.manager/include/DockerClient.php +++ b/emhttp/plugins/dynamix.docker.manager/include/DockerClient.php @@ -139,6 +139,22 @@ public function getTemplates($type) { return $tmpls; } + public function getUserTemplatePath($Container) { + global $dockerManPaths; + $dir = $dockerManPaths['templates-user']; + if (!is_dir($dir)) @mkdir($dir, 0755, true); + $Container = str_replace(['/', '\\'], '', $Container); + $target = "my-$Container.xml"; + $targetLower = strtolower($target); + $match = false; + foreach (glob("$dir/my-*.xml") ?: [] as $template) { + $name = basename($template); + if ($name == $target) return $template; + if (!$match && strtolower($name) == $targetLower) $match = $template; + } + return $match ?: "$dir/$target"; + } + public function downloadTemplates($Dest=null, $Urls=null) { /* Don't download any templates. Leave code in place for future reference. */ /* remove existing limetech templates that are all not valid */ diff --git a/emhttp/plugins/dynamix.docker.manager/scripts/docker_init b/emhttp/plugins/dynamix.docker.manager/scripts/docker_init index 8431d2a254..07d81bf062 100755 --- a/emhttp/plugins/dynamix.docker.manager/scripts/docker_init +++ b/emhttp/plugins/dynamix.docker.manager/scripts/docker_init @@ -4,19 +4,33 @@ $autostart = @file("/var/lib/docker/unraid-autostart",FILE_IGNORE_NEW_LINES); if ( ! $autostart ) exit(); +function user_template($container) { + $dir = "/boot/config/plugins/dockerMan/templates-user"; + $target = "my-$container.xml"; + $targetLower = strtolower($target); + $match = false; + foreach (glob("$dir/my-*.xml") ?: [] as $template) { + $name = basename($template); + if ($name == $target) return $template; + if (!$match && strtolower($name) == $targetLower) $match = $template; + } + return $match; +} + $flag = false; $newAuto = []; foreach ($autostart as $container) { if (! trim($container) ) continue; $cont = explode(" ",$container); - if ( ! is_file("/boot/config/plugins/dockerMan/templates-user/my-{$cont[0]}.xml")) { + $template = user_template($cont[0]); + if (!$template) { $newAuto[] = $container; continue; } $doc = new DOMDocument(); - if (!$doc->load("/boot/config/plugins/dockerMan/templates-user/my-{$cont[0]}.xml")) { + if (!$doc->load($template)) { $newAuto[] = $container; continue; } diff --git a/etc/rc.d/rc.docker b/etc/rc.d/rc.docker index a420587985..576174ca29 100755 --- a/etc/rc.d/rc.docker +++ b/etc/rc.d/rc.docker @@ -47,6 +47,24 @@ active(){ fi } +# return the user template for a container, preserving legacy case-insensitive +# matching without returning multiple files on case-sensitive boot devices +docker_user_template(){ + local dir=/boot/config/plugins/dockerMan/templates-user + local target="my-$1.xml" + local target_lc=${target,,} + local match= + local file + local name + for file in "$dir"/my-*.xml; do + [[ -e $file ]] || continue + name=${file##*/} + [[ $name == "$target" ]] && echo "$file" && return + [[ -z $match && ${name,,} == "$target_lc" ]] && match=$file + done + [[ -n $match ]] && echo "$match" +} + # wait for interface to go up carrier(){ local n e @@ -360,23 +378,22 @@ docker_network_start(){ # get container settings for custom networks to reconnect later declare -A NETRESTORE PRIMARY_NETWORK REBUILD_CONTAINERS USED_SUBNETS4 USED_SUBNETS6 RESTORED_NETWORKS for CONTAINER in $(docker container ls -a --format='{{.Names}}'); do - # the file case (due to fat32) might be different so use find to match - XMLFILE=$(find /boot/config/plugins/dockerMan/templates-user -maxdepth 1 -iname my-${CONTAINER}.xml) - if [[ -n $XMLFILE ]]; then + XMLFILE=$(docker_user_template "$CONTAINER") + if [[ -f $XMLFILE ]]; then REBUILD= MAIN= # update custom network reference (if changed) for NIC in $NICS; do [[ ${NIC:0:3} == eth ]] && NIC=$(active $NIC) X=${NIC//[^0-9]/} - REF=$(grep -Pom1 "\K(br|bond|eth|wlan)$X" $XMLFILE) + REF=$(grep -Pom1 "\K(br|bond|eth|wlan)$X" "$XMLFILE") if [[ $X == 0 ]] && ! carrier $NIC 1; then continue fi [[ $X == 0 && $NIC != wlan0 ]] && MAIN=$NIC [[ $NIC == wlan0 && -n $MAIN ]] && continue if [[ -n $REF && $REF != $NIC ]]; then - sed -ri "s/(br|bond|eth|wlan)$X(\.[0-9]+)?<\/Network>/$NIC\2<\/Network>/" $XMLFILE + sed -ri "s/(br|bond|eth|wlan)$X(\.[0-9]+)?<\/Network>/$NIC\2<\/Network>/" "$XMLFILE" REBUILD=1 fi done @@ -385,7 +402,7 @@ docker_network_start(){ [[ $ENTITY == Network ]] && MY_NETWORK=$CONTENT [[ $ENTITY == MyIP ]] && MY_IP=${CONTENT// /,} && MY_IP=$(echo "$MY_IP" | tr -s "," ";") [[ $ENTITY == MyMAC ]] && XML_MAC=${CONTENT// /} - done <$XMLFILE + done <"$XMLFILE" # only restore valid networks if [[ -n $MY_NETWORK ]]; then [[ $MY_NETWORK =~ ^(br|bond|eth|wlan)[0-9]+(\.[0-9]+)?$ ]] && CUSTOM_PRIMARY=1