From 5faebaf4a032e31f09d2478326ccdeb2d5b31564 Mon Sep 17 00:00:00 2001
From: jurgenhaas <juergen@paragon-es.de>
Date: Mon, 2 May 2022 15:01:11 +0200
Subject: [PATCH] gitlab-ci-cd/drupal#10 - add support for crontabs in NG
 context

---
 src/Handler.php                   | 23 +++++++++++++++++++++--
 templates/crontabs/template.twig  |  3 +++
 templates/docker-compose.yml.twig | 29 +++++++++++++++++++++++++++++
 3 files changed, 53 insertions(+), 2 deletions(-)
 create mode 100644 templates/crontabs/template.twig

diff --git a/src/Handler.php b/src/Handler.php
index c0cb86a..d8747ed 100644
--- a/src/Handler.php
+++ b/src/Handler.php
@@ -153,6 +153,9 @@ class Handler extends BaseHandler {
         'enable' => 0,
       ],
       'backstop' => $this->backstopDefaults(),
+      'crontabs' => [
+        'www-data' => [],
+      ]
     ];
 
     if ($this->isCiContext() || $this->isLocalDevMode()) {
@@ -297,7 +300,13 @@ class Handler extends BaseHandler {
       $filename = $this->config->render($template, $template);
       $file = $def['dest'] . '/' . $filename;
       if (($overwrite && empty($def['add2git'])) || !$fs->exists($file)) {
-        $rendered = $this->config->render($filename, file_get_contents($pluginRoot . '/templates/' . ($def['source'] ?? '') . $template . '.twig'));
+        $source = isset($def['source']) && is_file($pluginRoot . '/templates/' . $def['source']) ?
+          $pluginRoot . '/templates/' . $def['source'] :
+          $pluginRoot . '/templates/' . ($def['source'] ?? '') . $template . '.twig';
+        if (isset($def['options'])) {
+          $this->config->setValue('loopoptions', $def['options'], FALSE);
+        }
+        $rendered = $this->config->render($filename, file_get_contents($source));
         $extraOptions = $this->config->readValue($filename);
         if (!empty($def['add2yaml']) && $extraOptions !== NULL) {
           $yaml = Yaml::parse($rendered);
@@ -409,7 +418,7 @@ class Handler extends BaseHandler {
    *   List of files.
    */
   protected function getFiles(string $projectRoot, string $webRoot, string $settingsPath): array {
-    return [
+    $files = [
       'settings.docker.php' => [
         'dest' => $projectRoot . '/' . $settingsPath,
         'link' => $webRoot . '/sites/default',
@@ -461,6 +470,16 @@ class Handler extends BaseHandler {
         'dest' => $projectRoot . '/tests',
       ],
     ];
+    if (getenv('LAKEDROPS_BUILD_NG') === 'yes') {
+      foreach ($this->config->readValue('crontabs') ?? [] as $user => $tasks) {
+        $files[$user] = [
+          'source' => 'crontabs/template.twig',
+          'dest' => $projectRoot . '/crontabs',
+          'options' => $tasks,
+        ];
+      }
+    }
+    return $files;
   }
 
   /**
diff --git a/templates/crontabs/template.twig b/templates/crontabs/template.twig
new file mode 100644
index 0000000..2f08daa
--- /dev/null
+++ b/templates/crontabs/template.twig
@@ -0,0 +1,3 @@
+{% for line in loopoptions|default([]) %}
+  {{ line|raw }}
+{% endfor %}
diff --git a/templates/docker-compose.yml.twig b/templates/docker-compose.yml.twig
index 8d78cb4..764eb89 100644
--- a/templates/docker-compose.yml.twig
+++ b/templates/docker-compose.yml.twig
@@ -93,6 +93,34 @@ services:
 {% endif %}
     extra_hosts:
       - '{{ projectdomain }}:{{ docker0.ip }}'
+{% if drupal.live|default(0) %}
+
+  cron:
+    image: 'wodby/drupal-php:{{ php.version }}'
+    restart: unless-stopped
+    environment:
+      PHP_SENDMAIL_PATH: /usr/sbin/sendmail -t -i -S mailhog:1025
+      DB_HOST: mariadb
+      DB_USER: drupal
+      DB_PASSWORD: drupal
+      DB_NAME: drupal
+      DB_DRIVER: mysql
+      COMPOSER_ALLOW_SUPERUSER: 1
+{% if php.ssh and not drupal.live|default(0) %}
+      SSH_AUTH_SOCK: /ssh-agent
+{% endif %}
+    volumes:
+      - {{ projectroot }}:/var/www/html
+      - {{ projectroot }}/drush:/etc/drush
+      - {{ projectrootfiles }}:/data/default
+      - {{ projectroot }}/crontabs:/etc/crontabs
+{% if php.ssh and not drupal.live|default(0) %}
+      - {{ php.ssh_auth_sock }}:/ssh-agent
+{% endif %}
+    extra_hosts:
+      - '{{ projectdomain }}:{{ docker0.ip }}'
+{% endif %}
+    command: sudo -E crond -f -d 0
 
   {{ webserver.type }}:
     image: {% if webserver.type == 'nginx' %}'wodby/drupal-nginx:{{ webserver.version|default("1.21") }}'{% elseif webserver.type == 'apache' %}'wodby/apache:2.4'{% endif %}
@@ -134,6 +162,7 @@ services:
       traefik.http.routers.{{ webserver.type }}-{{ projectname }}.middlewares: {{ webserver.type }}-{{ projectname }}-auth@docker
 {% endif %}
       traefik.http.routers.{{ webserver.type }}-{{ projectname }}.rule: Host(`{{ projectdomain }}`){% for domain in extradomains|default([]) %} || Host(`{{ domain }}`){% endfor %}
+
 {% if drupal.live|default(0) %}
       traefik.http.routers.{{ webserver.type }}-{{ projectname }}.tls: 'true'
       traefik.http.routers.{{ webserver.type }}-{{ projectname }}.tls.certresolver: lakedrops
-- 
GitLab