diff --git a/.editorconfig b/.editorconfig index 12bcb27e4ee7bbca7f74423a87d205926d2ad1d2..ccc6a281e51a2f99832b3c2d5b7b42b3ce84c36e 100644 --- a/.editorconfig +++ b/.editorconfig @@ -12,6 +12,3 @@ indent_size = 2 charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true - -[{composer.json,composer.lock}] -indent_size = 4 diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index d06804671fb3de6d0c25fba46e1c5a3975ba9050..1d234297765c87ae1bd91b78b1c00083d4047fc0 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,4 +1,4 @@ include: - - project: 'gitlab-ci-cd/composer-packages' + - project: 'gitlab-ci-cd/drupal' ref: main - file: '/composer-packages.yml' + file: '/private-modules.yml' diff --git a/Traefik.php b/Traefik.php index c84ab522f2ba02b1620c661464be306283bcf378..c33980b543be7a85849491662d23cb52e4e1651d 100644 --- a/Traefik.php +++ b/Traefik.php @@ -6,7 +6,7 @@ use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Yaml\Yaml; /** - * Class Traefik. + * Provides configuration methods for Traefik. * * @package LakeDrops\DockerTraefik */ @@ -20,64 +20,88 @@ class Traefik { protected string $name; /** + * The domain. + * * @var string */ protected string $domain; /** + * The HTTP port. + * * @var int */ - protected int $http_port; + protected int $httpPort; /** + * The HTTPS port. + * * @var int */ - protected int $https_port; + protected int $httpsPort; /** + * The certificate filename. + * * @var string */ - protected string $cert_filename; + protected string $certFilename; /** + * The key filename. + * * @var string */ - protected string $key_filename; + protected string $keyFilename; /** + * Flag for enabling the Portainer addon. + * * @var bool */ - protected bool $addon_portainer = FALSE; + protected bool $addonPortainer = FALSE; /** + * The Trafik hub token. + * * @var string */ - protected string $hub_token = ''; + protected string $hubToken = ''; /** + * The environment variables. + * * @var array */ protected array $env; /** + * Flag, if tls should be used. + * * @var bool */ protected bool $tls; /** + * Flag, if the DNS challenge should be used. + * * @var bool */ - protected bool $dns_challenge; + protected bool $dnsChallenge; /** + * The DNS challenge provider. + * * @var string */ - protected string $dns_challenge_provider; + protected string $dnsChallengeProvider; /** + * The DNS challenge resolver. + * * @var string */ - protected string $dns_challenge_resolver; + protected string $dnsChallengeResolver; /** * Traefik constructor. @@ -87,67 +111,74 @@ class 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 + * @param int $httpPort * Port for non secure requests. - * @param int $https_port + * @param int $httpsPort * Port for secure requests. - * @param string $cert_filename + * @param string $certFilename * Filename of the SSL certificate. - * @param string $key_filename + * @param string $keyFilename * Filename of the private key for the SSL certificate. * @param array $env * A list of environment variables for the Traefik container. * @param bool $tls * Whether TLS should be supported. - * @param bool $dns_challenge + * @param bool $dnsChallenge * If TLS is supported, it uses the http challenge by default. Set to TRUE * to use the DNS challenge. - * @param string $dns_challenge_provider + * @param string $dnsChallengeProvider * If DNS challenge should be used, a provider is required. For a list of - * supported providers: - * @see https://doc.traefik.io/traefik/https/acme/#providers - * @param string $dns_challenge_resolver + * supported providers. + * @param string $dnsChallengeResolver * In some scenarios, the DNS resolver needs to be defined in order to * prevent local or shadow DNS servers being used. + * + * @see https://doc.traefik.io/traefik/https/acme/#providers */ public function __construct( string $name, string $domain = 'docker.localhost', - int $http_port = 8000, - int $https_port = 8443, - string $cert_filename = '', - string $key_filename = '', + int $httpPort = 8000, + int $httpsPort = 8443, + string $certFilename = '', + string $keyFilename = '', array $env = [], bool $tls = FALSE, - bool $dns_challenge = FALSE, - string $dns_challenge_provider = '', - string $dns_challenge_resolver = '' + bool $dnsChallenge = FALSE, + string $dnsChallengeProvider = '', + string $dnsChallengeResolver = '' ) { $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; + $this->httpPort = $httpPort; + $this->httpsPort = $httpsPort; + $this->certFilename = $certFilename; + $this->keyFilename = $keyFilename; $this->env = $env; $this->tls = $tls; - $this->dns_challenge = $dns_challenge; - $this->dns_challenge_provider = $dns_challenge_provider; - $this->dns_challenge_resolver = $dns_challenge_resolver; + $this->dnsChallenge = $dnsChallenge; + $this->dnsChallengeProvider = $dnsChallengeProvider; + $this->dnsChallengeResolver = $dnsChallengeResolver; } /** - * @param bool $addon_portainer + * Sets the flag for the Portainer addon. + * + * @param bool $addonPortainer + * The flag. */ - public function setAddonPortainer(bool $addon_portainer): void { - $this->addon_portainer = $addon_portainer; + public function setAddonPortainer(bool $addonPortainer): void { + $this->addonPortainer = $addonPortainer; } /** - * @param string $hub_token + * Sets the hub token. + * + * @param string $hubToken + * The hub token. */ - public function setHubToken(string $hub_token): void { - $this->hub_token = $hub_token; + public function setHubToken(string $hubToken): void { + $this->hubToken = $hubToken; } /** @@ -200,10 +231,15 @@ class Traefik { } /** + * Updates or deletes the file. + * * @param string $filename + * The file name. * @param string $content + * The file content. If empty, the file will be deleted, if it exists. * * @return bool + * TRUE, if an update was necessary, FALSE otherwise. */ private function updateFile(string $filename, string $content): bool { if ($content === '') { @@ -221,7 +257,10 @@ class Traefik { } /** + * Gets the default config. + * * @return array + * The default config. */ private function defaultDockerCompose(): array { $config = [ @@ -246,7 +285,7 @@ class Traefik { 'traefik-public', ], 'ports' => [ - $this->http_port . ':80', + $this->httpPort . ':80', ], 'labels' => [ 'traefik.enable=true', @@ -263,14 +302,14 @@ class Traefik { ], 'networks' => [ 'traefik-public' => [ - 'external' => true, + 'external' => TRUE, ], ], ]; if (!empty($this->env)) { $config['services']['traefik']['environment'] = $this->env; } - if ($this->addon_portainer) { + if ($this->addonPortainer) { $config['services']['portainer'] = [ 'container_name' => 'portainer', 'image' => 'portainer/portainer-ce:2.17.1', @@ -297,7 +336,7 @@ class Traefik { ], ]; } - if ($this->hub_token) { + if ($this->hubToken) { $config['services']['traefik']['command'][] = '--experimental.hub=true'; $config['services']['traefik']['command'][] = '--hub.tls.insecure=true'; $config['services']['traefik']['command'][] = '--metrics.prometheus.addrouterslabels=true'; @@ -308,7 +347,7 @@ class Traefik { 'image' => 'ghcr.io/traefik/hub-agent-traefik:v1.0.1', 'command' => [ 'run', - '--hub.token=' . $this->hub_token, + '--hub.token=' . $this->hubToken, '--auth-server.advertise-url=http://hub-agent', '--traefik.host=traefik', '--traefik.tls.insecure=true', @@ -329,20 +368,20 @@ class Traefik { $config['services']['traefik']['command'][] = '--entrypoints.websecure.address=:443'; $config['services']['traefik']['command'][] = '--entrypoints.web.http.redirections.entrypoint.to=websecure'; $config['services']['traefik']['command'][] = '--certificatesresolvers.lakedrops.acme.email=admin@' . $this->domain; - $config['services']['traefik']['ports'][] = $this->https_port . ':443'; + $config['services']['traefik']['ports'][] = $this->httpsPort . ':443'; $config['services']['traefik']['labels'][] = 'traefik.http.routers.traefik.tls=true'; $config['services']['traefik']['labels'][] = 'traefik.http.routers.traefik.tls.certresolver=lakedrops'; - if ($this->addon_portainer) { + if ($this->addonPortainer) { $config['services']['portainer']['labels'][] = 'traefik.http.routers.frontend.tls=true'; $config['services']['portainer']['labels'][] = 'traefik.http.routers.frontend.tls.certresolver=lakedrops'; $config['services']['portainer']['labels'][] = 'traefik.http.routers.edge.tls=true'; $config['services']['portainer']['labels'][] = 'traefik.http.routers.edge.tls.certresolver=lakedrops'; } - if ($this->dns_challenge && $this->dns_challenge_provider !== '') { + if ($this->dnsChallenge && $this->dnsChallengeProvider !== '') { $config['services']['traefik']['command'][] = '--certificatesresolvers.lakedrops.acme.dnschallenge=true'; - $config['services']['traefik']['command'][] = '--certificatesresolvers.lakedrops.acme.dnschallenge.provider=' . $this->dns_challenge_provider; - if ($this->dns_challenge_resolver !== '') { - $config['services']['traefik']['command'][] = '--certificatesresolvers.lakedrops.acme.dnschallenge.resolvers=' . $this->dns_challenge_resolver; + $config['services']['traefik']['command'][] = '--certificatesresolvers.lakedrops.acme.dnschallenge.provider=' . $this->dnsChallengeProvider; + if ($this->dnsChallengeResolver !== '') { + $config['services']['traefik']['command'][] = '--certificatesresolvers.lakedrops.acme.dnschallenge.resolvers=' . $this->dnsChallengeResolver; } } else { @@ -354,16 +393,19 @@ class Traefik { } /** + * Get the default certificate config. + * * @return string + * The default certificate config. */ private function defaultCertificatesConfig(): string { - if ($this->cert_filename === '' || $this->key_filename === '') { + if ($this->certFilename === '' || $this->keyFilename === '') { return ''; } return <<<EOF [[tls.certificates]] - certFile = "/certs/$this->cert_filename" - keyFile = "/certs/$this->key_filename" + certFile = "/certs/$this->certFilename" + keyFile = "/certs/$this->keyFilename" EOF; } diff --git a/composer.json b/composer.json index ac4348f565b603cb880b2e13ba0bacc14072c04d..e05091a46a836bbc2875b862d69c9fda3bc15328 100644 --- a/composer.json +++ b/composer.json @@ -1,57 +1,51 @@ { - "name": "lakedrops/docker-traefik", - "description": "Library to prepare local Traefik Docker container which is used by e.g. Docker4Drupal.", - "type": "library", - "keywords": ["Drupal", "Development", "Docker"], - "homepage": "https://gitlab.lakedrops.com/composer/library/docker-traefik", - "license": "GPL-2.0-or-later", - "authors": [ - { - "name": "Jürgen Haas", - "email": "juergen.haas@lakedrops.com", - "homepage": "https://www.lakedrops.com", - "role": "Drupal Expert" - }, - { - "name": "Daniel Speicher", - "email": "daniel.speicher@lakedrops.com", - "homepage": "https://www.lakedrops.com", - "role": "Drupal Expert" - }, - { - "name": "Richard Papp", - "email": "richard.papp@lakedrops.com", - "homepage": "https://www.lakedrops.com", - "role": "Drupal Expert" - } - ], - "support": { - "issues": "https://gitlab.lakedrops.com/composer/library/docker-traefik/issues", - "source": "https://gitlab.lakedrops.com/composer/library/docker-traefik/tree/main", - "docs": "https://devops-tools.docs.lakedrops.com/composer/library/docker-traefik/" + "name": "lakedrops/docker-traefik", + "description": "Library to prepare local Traefik Docker container which is used by e.g. Docker4Drupal.", + "type": "library", + "keywords": [ + "Drupal", + "Development", + "Docker" + ], + "homepage": "https://gitlab.lakedrops.com/composer/library/docker-traefik", + "license": "GPL-2.0-or-later", + "authors": [ + { + "name": "Jürgen Haas", + "email": "juergen.haas@lakedrops.com", + "homepage": "https://www.lakedrops.com", + "role": "Drupal Expert" }, - "require": { - "php": ">=7.4", - "symfony/filesystem": "^3.4||^4.4||^5.0||^6.0", - "symfony/yaml": "^3.4||^4.4||^5.0||^6.0" + { + "name": "Daniel Speicher", + "email": "daniel.speicher@lakedrops.com", + "homepage": "https://www.lakedrops.com", + "role": "Drupal Expert" }, - "require-dev": { - "composer/composer": "^1||^2", - "drupal/coder": "^8.3", - "phpunit/phpunit": "^9.5", - "roave/security-advisories": "dev-master", - "squizlabs/php_codesniffer": "^3.7" - }, - "minimum-stability": "dev", - "prefer-stable": true, - "autoload": { - "psr-4": { - "LakeDrops\\DockerTraefik\\": "" - } - }, - "config": { - "allow-plugins": { - "dealerdirect/phpcodesniffer-composer-installer": true - } + { + "name": "Richard Papp", + "email": "richard.papp@lakedrops.com", + "homepage": "https://www.lakedrops.com", + "role": "Drupal Expert" + } + ], + "support": { + "issues": "https://gitlab.lakedrops.com/composer/library/docker-traefik/issues", + "source": "https://gitlab.lakedrops.com/composer/library/docker-traefik/tree/main", + "docs": "https://devops-tools.docs.lakedrops.com/composer/library/docker-traefik/" + }, + "require": { + "php": ">=8.1", + "symfony/filesystem": "*", + "symfony/yaml": "*" + }, + "require-dev": { + "composer/composer": "^2", + "roave/security-advisories": "dev-latest" + }, + "autoload": { + "psr-4": { + "LakeDrops\\DockerTraefik\\": "" } + } }