From bb78c0ad99bbd552f31d785b5c44e6fa642e241b Mon Sep 17 00:00:00 2001
From: Matthew Baggett <matthew@baggett.me>
Date: Thu, 1 Sep 2022 15:28:13 +0200
Subject: [PATCH] Fix: Push on Trapped Container Termination, Postgres
 Healthcheck & Typos

---
 Dockerfile.mariadb                            | 21 ++++--
 Dockerfile.postgres                           | 23 ++++--
 bake.hcl                                      | 12 +++
 composer.lock                                 | 75 +++++++++++--------
 mysql.runit                                   |  6 +-
 ...res_healthcheck => postgres_healthcheck.sh |  0
 start.sh                                      | 12 +++
 sync-pull.runit                               |  2 +-
 traptest.sh                                   | 55 ++++++++++++++
 9 files changed, 157 insertions(+), 49 deletions(-)
 rename postgres_healthcheck => postgres_healthcheck.sh (100%)
 create mode 100755 traptest.sh

diff --git a/Dockerfile.mariadb b/Dockerfile.mariadb
index 6ad2d5f..7e9c739 100644
--- a/Dockerfile.mariadb
+++ b/Dockerfile.mariadb
@@ -1,5 +1,5 @@
 ARG MARIADB_VERSION=10.9
-FROM mariadb:$MARIADB_VERSION
+FROM mariadb:$MARIADB_VERSION AS mariadb
 SHELL ["/bin/bash", "-o", "pipefail", "-c"]
 ENV DEBIAN_FRONTEND="teletype" \
     TERM=xterm-256color \
@@ -55,8 +55,19 @@ COPY sync /sync/sync
 COPY start.sh /sync/start.sh
 RUN composer install
 ENV PATH="/sync:${PATH}"
-RUN chmod +x /sync/sync /etc/service/*/run
+RUN ln -s /sync/vendor/bin/wait-for-mariadb /usr/local/bin/wait-for-database && \
+    chmod +x /sync/sync /etc/service/*/run
 ENTRYPOINT ["/bin/bash", "/sync/start.sh"]
-#CMD ["start.sh"]
-HEALTHCHECK --start-period=30s CMD /usr/local/bin/healthcheck.sh --connect
-RUN ln -s /sync/vendor/bin/wait-for-mysql /usr/local/bin/wait-for-database
\ No newline at end of file
+CMD ["/bin/bash", "/sync/start.sh"]
+HEALTHCHECK --start-period=30s \
+    CMD /usr/local/bin/healthcheck.sh \
+        --defaults-extra-file=/etc/healthcheck.cnf \
+        --connect
+
+FROM mariadb AS mariadb-traptest
+COPY traptest.sh /usr/local/bin/traptest
+#STOPSIGNAL SIGINT
+RUN chmod +x /usr/local/bin/traptest
+#CMD ["/usr/local/bin/traptest"]
+ENTRYPOINT ["/usr/local/bin/traptest"]
+#ENTRYPOINT []
diff --git a/Dockerfile.postgres b/Dockerfile.postgres
index b63bfd8..69c15c3 100644
--- a/Dockerfile.postgres
+++ b/Dockerfile.postgres
@@ -1,5 +1,5 @@
 ARG PGSQL_VERSION
-FROM postgres:$PGSQL_VERSION-alpine
+FROM postgres:$PGSQL_VERSION-alpine AS postgres
 RUN apk add --no-cache runit && \
     apk --no-cache --repository https://dl-cdn.alpinelinux.org/alpine/edge/main add \
       icu-libs \
@@ -57,10 +57,19 @@ COPY composer.* /sync/
 COPY syncer /sync/syncer
 COPY sync /sync/sync
 COPY start.sh /sync/start.sh
-ENV PATH="/sync:${PATH}"
-ENTRYPOINT ["start.sh"]
-#CMD ["start.sh"]
+COPY postgres_healthcheck.sh /usr/local/bin/postgres_healthcheck
 RUN composer install
-COPY postgres_healthcheck /sync/postgres_healthcheck
-HEALTHCHECK --start-period=30s CMD /sync/postgres_healthcheck
-RUN ln -s /sync/vendor/bin/wait-for-postgresql /usr/local/bin/wait-for-database
+ENV PATH="/sync:${PATH}"
+RUN ln -s /sync/vendor/bin/wait-for-postgresql /usr/local/bin/wait-for-database && \
+    chmod +x /sync/sync /etc/service/*/run /usr/local/bin/postgres_healthcheck
+ENTRYPOINT ["/bin/bash", "/sync/start.sh"]
+STOPSIGNAL SIGINT
+CMD ["/bin/bash", "/sync/start.sh"]
+HEALTHCHECK --start-period=30s \
+    CMD /usr/local/bin/postgres_healthcheck
+
+FROM postgres AS postgres-traptest
+COPY traptest.sh /usr/local/bin/traptest
+RUN chmod +x /usr/local/bin/traptest
+CMD []
+ENTRYPOINT ["/usr/local/bin/traptest"]
diff --git a/bake.hcl b/bake.hcl
index 80d01ec..9ee6b4e 100644
--- a/bake.hcl
+++ b/bake.hcl
@@ -26,6 +26,7 @@ target "postgres-14" {
   args = {
     PGSQL_VERSION = 14
   }
+  target = "postgres"
 }
 
 target "postgres-13" {
@@ -36,6 +37,7 @@ target "postgres-13" {
   args = {
     PGSQL_VERSION = 13
   }
+  target = "postgres"
 }
 
 target "postgres-12" {
@@ -46,6 +48,7 @@ target "postgres-12" {
   args = {
     PGSQL_VERSION = 12
   }
+  target = "postgres"
 }
 
 target "postgres-11" {
@@ -56,6 +59,7 @@ target "postgres-11" {
   args = {
     PGSQL_VERSION = 11
   }
+  target = "postgres"
 }
 
 target "postgres-10" {
@@ -66,6 +70,7 @@ target "postgres-10" {
   args = {
     PGSQL_VERSION = 10
   }
+  target = "postgres"
 }
 
 target "mariadb-10-9" {
@@ -79,6 +84,7 @@ target "mariadb-10-9" {
   args = {
     MARIADB_VERSION=10.9
   }
+  target = "mariadb"
 }
 target "mariadb-10-8" {
   context = "."
@@ -88,6 +94,7 @@ target "mariadb-10-8" {
   args = {
     MARIADB_VERSION=10.8
   }
+  target = "mariadb"
 }
 target "mariadb-10-7" {
   context = "."
@@ -97,6 +104,7 @@ target "mariadb-10-7" {
   args = {
     MARIADB_VERSION=10.7
   }
+  target = "mariadb"
 }
 target "mariadb-10-6" {
   context = "."
@@ -106,6 +114,7 @@ target "mariadb-10-6" {
   args = {
     MARIADB_VERSION=10.6
   }
+  target = "mariadb"
 }
 target "mariadb-10-5" {
   context = "."
@@ -115,6 +124,7 @@ target "mariadb-10-5" {
   args = {
     MARIADB_VERSION=10.5
   }
+  target = "mariadb"
 }
 target "mariadb-10-4" {
   context = "."
@@ -124,6 +134,7 @@ target "mariadb-10-4" {
   args = {
     MARIADB_VERSION=10.4
   }
+  target = "mariadb"
 }
 target "mariadb-10-3" {
   context = "."
@@ -133,4 +144,5 @@ target "mariadb-10-3" {
   args = {
     MARIADB_VERSION=10.3
   }
+  target = "mariadb"
 }
\ No newline at end of file
diff --git a/composer.lock b/composer.lock
index c9d9a7d..28ba774 100644
--- a/composer.lock
+++ b/composer.lock
@@ -108,16 +108,16 @@
         },
         {
             "name": "aws/aws-sdk-php",
-            "version": "3.234.4",
+            "version": "3.234.7",
             "source": {
                 "type": "git",
                 "url": "https://github.com/aws/aws-sdk-php.git",
-                "reference": "8d56ddb99632200273bb933cbf82b758ab9cde2a"
+                "reference": "325931ead99e794657f88393c5b7cdf56723aadb"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/8d56ddb99632200273bb933cbf82b758ab9cde2a",
-                "reference": "8d56ddb99632200273bb933cbf82b758ab9cde2a",
+                "url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/325931ead99e794657f88393c5b7cdf56723aadb",
+                "reference": "325931ead99e794657f88393c5b7cdf56723aadb",
                 "shasum": ""
             },
             "require": {
@@ -194,9 +194,9 @@
             "support": {
                 "forum": "https://forums.aws.amazon.com/forum.jspa?forumID=80",
                 "issues": "https://github.com/aws/aws-sdk-php/issues",
-                "source": "https://github.com/aws/aws-sdk-php/tree/3.234.4"
+                "source": "https://github.com/aws/aws-sdk-php/tree/3.234.7"
             },
-            "time": "2022-08-26T18:20:48+00:00"
+            "time": "2022-08-31T18:25:01+00:00"
         },
         {
             "name": "bramus/ansi-php",
@@ -290,16 +290,16 @@
         },
         {
             "name": "guzzlehttp/guzzle",
-            "version": "7.4.5",
+            "version": "7.5.0",
             "source": {
                 "type": "git",
                 "url": "https://github.com/guzzle/guzzle.git",
-                "reference": "1dd98b0564cb3f6bd16ce683cb755f94c10fbd82"
+                "reference": "b50a2a1251152e43f6a37f0fa053e730a67d25ba"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/guzzle/guzzle/zipball/1dd98b0564cb3f6bd16ce683cb755f94c10fbd82",
-                "reference": "1dd98b0564cb3f6bd16ce683cb755f94c10fbd82",
+                "url": "https://api.github.com/repos/guzzle/guzzle/zipball/b50a2a1251152e43f6a37f0fa053e730a67d25ba",
+                "reference": "b50a2a1251152e43f6a37f0fa053e730a67d25ba",
                 "shasum": ""
             },
             "require": {
@@ -314,10 +314,10 @@
                 "psr/http-client-implementation": "1.0"
             },
             "require-dev": {
-                "bamarni/composer-bin-plugin": "^1.4.1",
+                "bamarni/composer-bin-plugin": "^1.8.1",
                 "ext-curl": "*",
                 "php-http/client-integration-tests": "^3.0",
-                "phpunit/phpunit": "^8.5.5 || ^9.3.5",
+                "phpunit/phpunit": "^8.5.29 || ^9.5.23",
                 "psr/log": "^1.1 || ^2.0 || ^3.0"
             },
             "suggest": {
@@ -327,8 +327,12 @@
             },
             "type": "library",
             "extra": {
+                "bamarni-bin": {
+                    "bin-links": true,
+                    "forward-command": false
+                },
                 "branch-alias": {
-                    "dev-master": "7.4-dev"
+                    "dev-master": "7.5-dev"
                 }
             },
             "autoload": {
@@ -394,7 +398,7 @@
             ],
             "support": {
                 "issues": "https://github.com/guzzle/guzzle/issues",
-                "source": "https://github.com/guzzle/guzzle/tree/7.4.5"
+                "source": "https://github.com/guzzle/guzzle/tree/7.5.0"
             },
             "funding": [
                 {
@@ -410,20 +414,20 @@
                     "type": "tidelift"
                 }
             ],
-            "time": "2022-06-20T22:16:13+00:00"
+            "time": "2022-08-28T15:39:27+00:00"
         },
         {
             "name": "guzzlehttp/promises",
-            "version": "1.5.1",
+            "version": "1.5.2",
             "source": {
                 "type": "git",
                 "url": "https://github.com/guzzle/promises.git",
-                "reference": "fe752aedc9fd8fcca3fe7ad05d419d32998a06da"
+                "reference": "b94b2807d85443f9719887892882d0329d1e2598"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/guzzle/promises/zipball/fe752aedc9fd8fcca3fe7ad05d419d32998a06da",
-                "reference": "fe752aedc9fd8fcca3fe7ad05d419d32998a06da",
+                "url": "https://api.github.com/repos/guzzle/promises/zipball/b94b2807d85443f9719887892882d0329d1e2598",
+                "reference": "b94b2807d85443f9719887892882d0329d1e2598",
                 "shasum": ""
             },
             "require": {
@@ -478,7 +482,7 @@
             ],
             "support": {
                 "issues": "https://github.com/guzzle/promises/issues",
-                "source": "https://github.com/guzzle/promises/tree/1.5.1"
+                "source": "https://github.com/guzzle/promises/tree/1.5.2"
             },
             "funding": [
                 {
@@ -494,20 +498,20 @@
                     "type": "tidelift"
                 }
             ],
-            "time": "2021-10-22T20:56:57+00:00"
+            "time": "2022-08-28T14:55:35+00:00"
         },
         {
             "name": "guzzlehttp/psr7",
-            "version": "2.4.0",
+            "version": "2.4.1",
             "source": {
                 "type": "git",
                 "url": "https://github.com/guzzle/psr7.git",
-                "reference": "13388f00956b1503577598873fffb5ae994b5737"
+                "reference": "69568e4293f4fa993f3b0e51c9723e1e17c41379"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/guzzle/psr7/zipball/13388f00956b1503577598873fffb5ae994b5737",
-                "reference": "13388f00956b1503577598873fffb5ae994b5737",
+                "url": "https://api.github.com/repos/guzzle/psr7/zipball/69568e4293f4fa993f3b0e51c9723e1e17c41379",
+                "reference": "69568e4293f4fa993f3b0e51c9723e1e17c41379",
                 "shasum": ""
             },
             "require": {
@@ -521,15 +525,19 @@
                 "psr/http-message-implementation": "1.0"
             },
             "require-dev": {
-                "bamarni/composer-bin-plugin": "^1.4.1",
+                "bamarni/composer-bin-plugin": "^1.8.1",
                 "http-interop/http-factory-tests": "^0.9",
-                "phpunit/phpunit": "^8.5.8 || ^9.3.10"
+                "phpunit/phpunit": "^8.5.29 || ^9.5.23"
             },
             "suggest": {
                 "laminas/laminas-httphandlerrunner": "Emit PSR-7 responses"
             },
             "type": "library",
             "extra": {
+                "bamarni-bin": {
+                    "bin-links": true,
+                    "forward-command": false
+                },
                 "branch-alias": {
                     "dev-master": "2.4-dev"
                 }
@@ -593,7 +601,7 @@
             ],
             "support": {
                 "issues": "https://github.com/guzzle/psr7/issues",
-                "source": "https://github.com/guzzle/psr7/tree/2.4.0"
+                "source": "https://github.com/guzzle/psr7/tree/2.4.1"
             },
             "funding": [
                 {
@@ -609,7 +617,7 @@
                     "type": "tidelift"
                 }
             ],
-            "time": "2022-06-20T21:43:11+00:00"
+            "time": "2022-08-28T14:45:39+00:00"
         },
         {
             "name": "jimmiw/php-time-ago",
@@ -959,16 +967,17 @@
             "source": {
                 "type": "git",
                 "url": "https://github.com/matthewbaggett/wait-for-mysql.git",
-                "reference": "729ef9518a8df1c4685654ffda034e766047ae10"
+                "reference": "f7ba6c81b80ccf1296061fc23fc5b719aeac763d"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/matthewbaggett/wait-for-mysql/zipball/729ef9518a8df1c4685654ffda034e766047ae10",
-                "reference": "729ef9518a8df1c4685654ffda034e766047ae10",
+                "url": "https://api.github.com/repos/matthewbaggett/wait-for-mysql/zipball/f7ba6c81b80ccf1296061fc23fc5b719aeac763d",
+                "reference": "f7ba6c81b80ccf1296061fc23fc5b719aeac763d",
                 "shasum": ""
             },
             "default-branch": true,
             "bin": [
+                "wait-for-mariadb",
                 "wait-for-mysql",
                 "wait-for-postgresql"
             ],
@@ -987,7 +996,7 @@
                 "issues": "https://github.com/matthewbaggett/wait-for-mysql/issues",
                 "source": "https://github.com/matthewbaggett/wait-for-mysql/tree/main"
             },
-            "time": "2022-08-28T09:25:52+00:00"
+            "time": "2022-09-01T11:47:40+00:00"
         },
         {
             "name": "monolog/monolog",
diff --git a/mysql.runit b/mysql.runit
index 0881401..7c27b9a 100644
--- a/mysql.runit
+++ b/mysql.runit
@@ -1,9 +1,9 @@
 #!/bin/bash
 
 # Create /etc/my.cnf to allow internal utils to JustWork:tm:
-echo "[mariadb-client]" > /etc/my.cnf
-echo "user = $MARIADB_USER" >> /etc/my.cnf
-echo "password = $MARIADB_PASSWORD" >> /etc/my.cnf
+echo "[mariadb-client]" > /etc/healthcheck.cnf
+echo "user = $MARIADB_USER" >> /etc/healthcheck.cnf
+echo "password = $MARIADB_PASSWORD" >> /etc/healthcheck.cnf
 
 # Call upstreamed mariadb entrypoint.
 echo "Running docker-entrypoint"
diff --git a/postgres_healthcheck b/postgres_healthcheck.sh
similarity index 100%
rename from postgres_healthcheck
rename to postgres_healthcheck.sh
diff --git a/start.sh b/start.sh
index 9f66580..df0b6cf 100755
--- a/start.sh
+++ b/start.sh
@@ -6,6 +6,18 @@ dos2unix /etc/service/*/run
 # Fix permissions on run files
 chmod +x /etc/service/*/run
 
+# Define shutdown + cleanup procedure
+cleanup() {
+    echo "Container stop requested, running final dump + cleanup"
+    /sync/sync --push
+    echo "Good bye!"
+}
+
+# Trap SIGTERM
+echo "Setting SIGTERM trap"
+trap 'cleanup' EXIT SIGINT
+trap 'echo SIGQUIT' SIGQUIT
+
 # Start Runit.
 echo "Starting Runit."
 runsvdir -P /etc/service
\ No newline at end of file
diff --git a/sync-pull.runit b/sync-pull.runit
index 6d5743a..b2bec50 100644
--- a/sync-pull.runit
+++ b/sync-pull.runit
@@ -3,7 +3,7 @@
 sleep 10
 echo "Waiting for database before running sync-pull.."
 /usr/local/bin/wait-for-database
-echo "Database read, running sync-pull."
+echo "Database ready, running sync-pull."
 /sync/sync --pull
 
 sleep infinity
\ No newline at end of file
diff --git a/traptest.sh b/traptest.sh
new file mode 100755
index 0000000..ec805d5
--- /dev/null
+++ b/traptest.sh
@@ -0,0 +1,55 @@
+#!/bin/bash
+# traptest.sh
+# Define shutdown + cleanup procedure
+cleanup() {
+    echo "Container stop requested, running final dump + cleanup"
+    echo "Good bye!"
+}
+
+# Trap SIGTERM
+echo "Setting SIGTERM trap"
+trap 'cleanup' EXIT SIGINT
+trap 'echo SIGQUIT' SIGQUIT
+
+# Trap SIGTERM
+#echo "Setting SIGTERM trap"
+#trap 'echo "Hello!"' SIGABRT SIGALRM SIGBUS SIGCHLD SIGCONT SIGFPE SIGHUP SIGILL SIGINT SIGIO SIGPIPE SIGPROF SIGPWR SIGQUIT SIGSEGV SIGSTKFLT SIGSTOP SIGTSTP SIGSYS SIGTERM SIGTRAP SIGTTIN SIGTTOU SIGURG SIGUSR1 SIGUSR2 SIGVTALRM SIGXCPU SIGXFSZ SIGWINCH
+#trap 'cleanup' EXIT
+#trap 'echo  SIGABRT'  SIGABRT
+#trap 'echo  SIGALRM'  SIGALRM
+#trap 'echo  SIGBUS'  SIGBUS
+#trap 'echo  SIGCHLD'  SIGCHLD
+#trap 'echo  SIGCONT'  SIGCONT
+#trap 'echo  SIGFPE'  SIGFPE
+#trap 'echo  SIGHUP'  SIGHUP
+#trap 'echo  SIGILL'  SIGILL
+#trap 'echo  SIGINT'  SIGINT
+#trap 'echo  SIGIO'  SIGIO
+#trap 'echo  SIGPIPE'  SIGPIPE
+#trap 'echo  SIGPROF'  SIGPROF
+#trap 'echo  SIGPWR'  SIGPWR
+#trap 'echo  SIGQUIT'  SIGQUIT
+#trap 'echo  SIGSEGV'  SIGSEGV
+#trap 'echo  SIGSTKFLT'  SIGSTKFLT
+#trap 'echo  SIGSTOP'  SIGSTOP
+#trap 'echo  SIGTSTP'  SIGTSTP
+#trap 'echo  SIGSYS'  SIGSYS
+#trap 'echo  SIGTERM'  SIGTERM
+#trap 'echo  SIGTRAP'  SIGTRAP
+#trap 'echo  SIGTTIN'  SIGTTIN
+#trap 'echo  SIGTTOU'  SIGTTOU
+#trap 'echo  SIGURG'  SIGURG
+#trap 'echo  SIGUSR1'  SIGUSR1
+#trap 'echo  SIGUSR2'  SIGUSR2
+#trap 'echo  SIGVTALRM'  SIGVTALRM
+#trap 'echo  SIGXCPU'  SIGXCPU
+#trap 'echo  SIGXFSZ'  SIGXFSZ
+#trap 'echo  SIGWINCH'  SIGWINCH
+
+echo "pid is $$"
+
+while :			# This is the same as "while true".
+do
+  echo "Idling"
+  sleep 60	# This script is not really doing anything.
+done