Skip to content

Commit

Permalink
New Networks (+ aliases) management + Some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ph3nol committed Oct 9, 2017
1 parent db5ac4c commit cde46f0
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 20 deletions.
15 changes: 5 additions & 10 deletions src/Application/DockerContainer/NginxDockerContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Ph3\DockerArch\Application\DockerContainer;

use Cocur\Slugify\Slugify;
use Ph3\DockerArch\Application\Architect;
use Ph3\DockerArch\Domain\DockerContainer\Model\DockerContainer;
use Ph3\DockerArch\Domain\DockerContainer\Model\DockerContainerInterface;
Expand Down Expand Up @@ -63,14 +64,6 @@ public function execute(): void
'type' => 'rw',
]);

// Networks.
$this->addNetwork(
self::DOCKER_MAIN_NETWORK,
array_filter(array_keys($this->vhostsServicesByHost), function (string $host): bool {
return ('localhost' !== $host);
})
);

// Ports.
$this->addEnvPort('NGINX', ['from' => '8080', 'to' => '80']);
}
Expand Down Expand Up @@ -113,15 +106,17 @@ private function addVhostsTemplatedFiles(): bool
$vhostIndex = 0;
foreach ($this->vhostsServicesByHost as $service) {
$appType = $service->getOptions()['app_type'] ?? null;
preg_match('/(\w+)Service$/i', get_class($service), $matches);
$vhostFileName = (new Slugify())->slugify($matches[1], '-');
$templatePath = sprintf(
'Service/Nginx/vhosts/%s%s.conf.twig',
$service->getIdentifier(),
$vhostFileName,
$appType ? '-'.$appType : null
);
$filePath = sprintf(
'conf.d/%s-%s%s.vhost.conf',
str_pad($vhostIndex + 10, 3, '0', STR_PAD_LEFT),
$service->getIdentifier(),
$vhostFileName,
$appType ? '-'.$appType : null
);

Expand Down
12 changes: 9 additions & 3 deletions src/Domain/DockerContainer/Model/DockerContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,18 @@ public function __construct(ServiceInterface $service)
*/
private function init(): void
{
$service = $this->getService();

$this
->setMaintainer('Docker Arch <https://github.com/Ph3nol/Docker-Arch>')
->addEnv('DOCKER_CONTAINER_NAME', $this->getService()->getIdentifier())
->addEnv('DEBIAN_FRONTEND', 'noninteractive')
->addNetwork(self::DOCKER_MAIN_NETWORK);
->addEnv('DEBIAN_FRONTEND', 'noninteractive');

$this
->addPackage('openssh-client')
->addPackage('vim');

if ($this->getService()->isWeb()) {
if ($service->isWeb()) {
$this
->addEnv('TERM', 'xterm-256color')
->addEnv('GIT_DISCOVERY_ACROSS_FILESYSTEM', 'true')
Expand All @@ -75,6 +76,11 @@ private function init(): void
->addCommand('update-ca-certificates');
}

$this->addNetwork(self::DOCKER_MAIN_NETWORK);
if (null !== $service->getHost() && 'localhost' !== $service->getHost()) {
$this->addNetworkAlias(self::DOCKER_MAIN_NETWORK, $service->getHost());
}

$this->initLocale();
}

Expand Down
39 changes: 37 additions & 2 deletions src/Domain/DockerContainer/Model/DockerContainerNetworksTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,50 @@ public function getNetworks(): array
return $this->networks;
}

/**
* @param string $name
*
* @return self
*/
public function addNetwork($name): self
{
if (false === array_key_exists($name, $this->networks)) {
$this->networks[$name] = [];
}

return $this;
}

/**
* @param string $name
* @param string $alias
*
* @return self
*/
public function addNetworkAlias($name, string $alias): self
{
if (false === array_key_exists($name, $this->networks)) {
$this->addNetwork($name);
}

if (false === in_array($alias, $this->networks[$name])) {
$this->networks[$name][] = $alias;
}

return $this;
}

/**
* @param string $name
* @param array $aliases
*
* @return self
*/
public function addNetwork($name, array $aliases = []): self
public function addNetworkAliases($name, array $aliases = []): self
{
$this->networks[$name] = $aliases;
foreach ($aliases as $alias) {
$this->addNetworkAlias($name, $alias);
}

return $this;
}
Expand Down
16 changes: 16 additions & 0 deletions src/Domain/DockerContainer/Model/DockerContainerPortsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ public function addPort(array $port): self
return $this;
}

/**
* @param string $port
*
* @return self
*/
public function addPortFromString(string $port): self
{
$port = explode(':', $port);
$this->addPort([
'from' => $port[0],
'to' => $port[1],
]);

return $this;
}

/**
* @param array $ports
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@ public function updateModel(DockerContainerInterface $dockerContainer, array $da
if ($data['working_dir'] ?? null) {
$dockerContainer->setWorkingDir($data['working_dir']);
}
foreach ($data['ports'] ?? [] as $from => $to) {
$dockerContainer->addPort([
'from' => $from,
'to' => $to,
]);
foreach ($data['ports'] ?? [] as $port) {
$dockerContainer->addPortFromString($port);
}
foreach ($data['envs'] ?? [] as $key => $value) {
$dockerContainer->addEnv($key, $value);
Expand Down

0 comments on commit cde46f0

Please sign in to comment.