Skip to content
Snippets Groups Projects
Commit d64f3100 authored by jurgenhaas's avatar jurgenhaas
Browse files

#5 Make traefik configuration configurable

parent 5ff10b8e
No related branches found
No related tags found
No related merge requests found
...@@ -19,23 +19,64 @@ class Traefik { ...@@ -19,23 +19,64 @@ class Traefik {
*/ */
protected $name; protected $name;
/**
* @var string
*/
protected $domain;
/**
* @var int
*/
protected $http_port;
/**
* @var int
*/
protected $https_port;
/**
* @var string
*/
protected $cert_filename;
/**
* @var string
*/
protected $key_filename;
/** /**
* Traefik constructor. * Traefik constructor.
* *
* @param string $name * @param string $name
* Name of the network to be created which needs to match the container * Name of the network to be created which needs to match the container
* prefix of your project you would like to handle with Traefik. * prefix of your project you would like to handle with Traefik.
* @param string $domain
* The domain name used for all local projects.
* @param int $http_port
* Port for non secure requests.
* @param int $https_port
* Port for secure requests.
* @param string $cert_filename
* Filename of the SSL certificate.
* @param string $key_filename
* Filename of the private key for the SSL certificate.
*/ */
public function __construct($name) { public function __construct(string $name, $domain = 'docker.localhost', $http_port = 8000, $https_port = 8443, $cert_filename = 'cert.pem', $key_filename = 'key.pem') {
$this->name = $name; $this->name = $name;
$this->domain = $domain;
$this->http_port = $http_port;
$this->https_port = $https_port;
$this->cert_filename = $cert_filename;
$this->key_filename = $key_filename;
} }
/** /**
* Update the Traefik container. * Update the Traefik container.
*/ */
public function update() { public function update(): void {
// Update host wider traefik container. // Update host wider traefik container.
$traefikPath = $_SERVER['HOME'] . '/.traefik'; $traefikPath = $_SERVER['HOME'] . '/.traefik';
$traefikCertPath = $_SERVER['HOME'] . '/.traefik/certs';
$traefikFile = $traefikPath . '/docker-compose.yml'; $traefikFile = $traefikPath . '/docker-compose.yml';
$fs = new Filesystem(); $fs = new Filesystem();
...@@ -50,12 +91,14 @@ class Traefik { ...@@ -50,12 +91,14 @@ class Traefik {
'traefik' => [ 'traefik' => [
'image' => 'traefik:1.7.17', 'image' => 'traefik:1.7.17',
'restart' => 'unless-stopped', 'restart' => 'unless-stopped',
'command' => '-c /dev/null --web --docker --logLevel=DEBUG', 'command' => '-c /dev/null --web --docker --defaultEntryPoints="https" --defaultEntryPoints="http" --entryPoints="Name:https Address::443 TLS:/certs/' . $this->cert_filename . ',/certs/' . $this->key_filename . '" --entryPoints="Name:http Address::80"',
'networks' => [], 'networks' => [],
'ports' => [ 'ports' => [
'8000:80', $this->http_port . ':80',
$this->https_port . ':443',
], ],
'volumes' => [ 'volumes' => [
'./certs:/certs/',
'/var/run/docker.sock:/var/run/docker.sock', '/var/run/docker.sock:/var/run/docker.sock',
], ],
], ],
...@@ -63,6 +106,9 @@ class Traefik { ...@@ -63,6 +106,9 @@ class Traefik {
'networks' => [], 'networks' => [],
]; ];
} }
if (!$fs->exists($traefikCertPath)) {
$fs->mkdir($traefikCertPath);
}
if (!in_array($this->name, $traefik['services']['traefik']['networks'], TRUE)) { if (!in_array($this->name, $traefik['services']['traefik']['networks'], TRUE)) {
$traefik['services']['traefik']['networks'][] = $this->name; $traefik['services']['traefik']['networks'][] = $this->name;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment