From 6c3505418846f37a69a1d3627e082f782d41b4d9 Mon Sep 17 00:00:00 2001 From: "hubert.lenoir" Date: Mon, 31 Oct 2016 11:07:18 +0100 Subject: [PATCH 1/3] :gift: #32 - Add DOMAIN_NAME for networks --- src/Synchronizer.php | 67 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 60 insertions(+), 7 deletions(-) diff --git a/src/Synchronizer.php b/src/Synchronizer.php index b393ea5..17bb846 100644 --- a/src/Synchronizer.php +++ b/src/Synchronizer.php @@ -126,6 +126,8 @@ private function getHostsLines($container) // Networks if (isset($container['NetworkSettings']['Networks']) && is_array($container['NetworkSettings']['Networks'])) { foreach ($container['NetworkSettings']['Networks'] as $networkName => $conf) { + preg_match(sprintf('/^%s_(.+)$/', $this->getProjectName($container)), $networkName, $matches); + $networkShortName = $matches[1]; $ip = $conf['IPAddress']; $aliases = isset($conf['Aliases']) && is_array($conf['Aliases']) ? $conf['Aliases'] : []; @@ -136,6 +138,12 @@ private function getHostsLines($container) $hosts[] = $alias.'.'.$networkName; } + foreach ($this->getAdditionalContainerHosts($container) as $host) { + if (preg_match('/^(.+):(.+)$/', $host, $matches) && $matches[1] === $networkShortName) { + $hosts[] = $matches[2]; + } + } + $lines[$ip] = sprintf('%s%s', isset($lines[$ip]) ? $lines[$ip].' ' : '', implode(' ', $hosts)); } } @@ -148,22 +156,67 @@ private function getHostsLines($container) } /** - * @param Container $container + * @param array $container + * + * @return string + */ + private function getProjectName($container) + { + if (isset($container['Config']['Labels']['com.docker.compose.project'])) { + return $container['Config']['Labels']['com.docker.compose.project']; + } + + return ''; + } + + /** + * @param array $container * * @return array */ private function getContainerHosts($container) { - $hosts = [substr($container['Name'], 1).$this->tld]; + return array_merge( + [substr($container['Name'], 1).$this->tld], + $this->getAdditionalHosts($container) + ); + } + + /** + * @param array $container + * + * @return array + */ + private function getAdditionalContainerHosts($container) + { + $hosts = []; + + $domainName = $this->getEnv($container, 'DOMAIN_NAME'); + if ($domainName) { + $hosts = array_merge($hosts, explode(',', $domainName)); + } + + return $hosts; + } + + /** + * @param array $container + * @param string $name + * @param string $default + * + * @return string + */ + private function getEnv($container, $name, $default = null) + { if (isset($container['Config']['Env']) && is_array($container['Config']['Env'])) { - $env = $container['Config']['Env']; - foreach (preg_grep('/DOMAIN_NAME=/', $env) as $row) { - $row = substr($row, strlen('DOMAIN_NAME=')); - $hosts = array_merge($hosts, explode(',', $row)); + foreach ($container['Config']['Env'] as $env) { + if (preg_match(sprintf('/^%s=(.*)$/', $name), $env, $matches)) { + return $matches[1]; + } } } - return $hosts; + return $default; } /** From 053e011fe190cae71006f87e7b2fabb189e139e0 Mon Sep 17 00:00:00 2001 From: Romain Gautier Date: Sat, 21 Apr 2018 11:18:41 +0200 Subject: [PATCH 2/3] fix method name to call ./bin/docker-hostmanager PHP Fatal error: Uncaught Error: Call to undefined method DockerHostManager\Synchronizer::getAdditionalHosts() in src/Synchronizer.php:181 --- src/Synchronizer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Synchronizer.php b/src/Synchronizer.php index 17bb846..41cf7f4 100644 --- a/src/Synchronizer.php +++ b/src/Synchronizer.php @@ -178,7 +178,7 @@ private function getContainerHosts($container) { return array_merge( [substr($container['Name'], 1).$this->tld], - $this->getAdditionalHosts($container) + $this->getAdditionalContainerHosts($container) ); } From 1f343f5cee7fc865262ac6b6c6d210b7341f81f2 Mon Sep 17 00:00:00 2001 From: Romain Gautier Date: Sat, 21 Apr 2018 11:23:43 +0200 Subject: [PATCH 3/3] add check if no short network short name is found --- src/Synchronizer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Synchronizer.php b/src/Synchronizer.php index 41cf7f4..74e957a 100644 --- a/src/Synchronizer.php +++ b/src/Synchronizer.php @@ -127,7 +127,7 @@ private function getHostsLines($container) if (isset($container['NetworkSettings']['Networks']) && is_array($container['NetworkSettings']['Networks'])) { foreach ($container['NetworkSettings']['Networks'] as $networkName => $conf) { preg_match(sprintf('/^%s_(.+)$/', $this->getProjectName($container)), $networkName, $matches); - $networkShortName = $matches[1]; + $networkShortName = isset($matches[1]) ? $matches[1] : null; $ip = $conf['IPAddress']; $aliases = isset($conf['Aliases']) && is_array($conf['Aliases']) ? $conf['Aliases'] : [];