Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions emhttp/plugins/dynamix.docker.manager/include/CreateDocker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -898,6 +906,9 @@ function prepareCategory() {
<form markdown="1" method="POST" autocomplete="off" onsubmit="return prepareConfig(this)">
<input type="hidden" name="csrf_token" value="<?=$var['csrf_token']?>">
<input type="hidden" name="contCPUset" value="">
<?if ($xmlType=='edit' && is_file($xmlTemplate) && dirname($xmlTemplate)==$dockerManPaths['templates-user']):?>
<input type="hidden" name="sourceTemplate" value="<?=htmlspecialchars(basename($xmlTemplate))?>">
<?endif;?>
<?if ($xmlType=='edit'):?>
<?if ($DockerClient->doesContainerExist($templateName)):?>
<input type="hidden" name="existingContainer" value="<?=$templateName?>">
Expand Down
16 changes: 16 additions & 0 deletions emhttp/plugins/dynamix.docker.manager/include/DockerClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
18 changes: 16 additions & 2 deletions emhttp/plugins/dynamix.docker.manager/scripts/docker_init
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
29 changes: 23 additions & 6 deletions etc/rc.d/rc.docker
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 "<Network>\K(br|bond|eth|wlan)$X" $XMLFILE)
REF=$(grep -Pom1 "<Network>\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/<Network>(br|bond|eth|wlan)$X(\.[0-9]+)?<\/Network>/<Network>$NIC\2<\/Network>/" $XMLFILE
sed -ri "s/<Network>(br|bond|eth|wlan)$X(\.[0-9]+)?<\/Network>/<Network>$NIC\2<\/Network>/" "$XMLFILE"
REBUILD=1
fi
done
Expand All @@ -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
Expand Down
Loading