Skip to content

Commit

Permalink
🎁 Issue iamluc#32 - Add DOMAIN_NAME for networks
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Beru committed Oct 31, 2016
1 parent d30e705 commit 11fbbe4
Showing 1 changed file with 60 additions and 7 deletions.
67 changes: 60 additions & 7 deletions src/Synchronizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] : [];
Expand All @@ -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));
}
}
Expand All @@ -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;
}

/**
Expand Down

0 comments on commit 11fbbe4

Please sign in to comment.