From e501f50fbd0c71803b592a5e27ec95b033f1da18 Mon Sep 17 00:00:00 2001
From: Matthew Baggett <matthew@baggett.me>
Date: Thu, 1 Sep 2022 16:31:45 +0200
Subject: [PATCH] Working on CTRL+C and everything.

---
 .gitignore          |  3 ++-
 Dockerfile.mariadb  | 15 ++++--------
 Dockerfile.postgres | 11 +++------
 Dockerfile.traptest |  6 +++++
 docker-compose.yml  |  3 +++
 start.sh            | 10 +++++---
 traptest.sh         | 58 ++++++---------------------------------------
 7 files changed, 32 insertions(+), 74 deletions(-)
 create mode 100644 Dockerfile.traptest

diff --git a/.gitignore b/.gitignore
index bca31cc..bfdae9e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,5 @@
 .php-cs-fixer.cache
 .idea
 /vendor/
-.minio
\ No newline at end of file
+.minio
+/test.yml
\ No newline at end of file
diff --git a/Dockerfile.mariadb b/Dockerfile.mariadb
index 7e9c739..f42bd93 100644
--- a/Dockerfile.mariadb
+++ b/Dockerfile.mariadb
@@ -50,24 +50,17 @@ COPY sync-push.runit /etc/service/sync-push/run
 VOLUME /dumps
 WORKDIR /sync
 COPY composer.* /sync/
+RUN composer install
 COPY syncer /sync/syncer
 COPY sync /sync/sync
 COPY start.sh /sync/start.sh
-RUN composer install
 ENV PATH="/sync:${PATH}"
 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 ["/bin/bash", "/sync/start.sh"]
+ENTRYPOINT ["/sync/start.sh"]
+CMD []
 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 []
+STOPSIGNAL SIGTERM
diff --git a/Dockerfile.postgres b/Dockerfile.postgres
index 69c15c3..3ad1d4d 100644
--- a/Dockerfile.postgres
+++ b/Dockerfile.postgres
@@ -54,22 +54,17 @@ COPY sync-push.runit /etc/service/sync-push/run
 VOLUME /dumps
 WORKDIR /sync
 COPY composer.* /sync/
+RUN composer install
 COPY syncer /sync/syncer
 COPY sync /sync/sync
 COPY start.sh /sync/start.sh
 COPY postgres_healthcheck.sh /usr/local/bin/postgres_healthcheck
-RUN composer install
 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"]
+CMD []
 HEALTHCHECK --start-period=30s \
     CMD /usr/local/bin/postgres_healthcheck
+STOPSIGNAL SIGTERM
 
-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/Dockerfile.traptest b/Dockerfile.traptest
new file mode 100644
index 0000000..daf830e
--- /dev/null
+++ b/Dockerfile.traptest
@@ -0,0 +1,6 @@
+FROM ubuntu:jammy AS mariadb-traptest
+COPY traptest.sh /usr/local/bin/traptest
+RUN chmod +x /usr/local/bin/traptest
+ENTRYPOINT ["/usr/local/bin/traptest"]
+CMD []
+STOPSIGNAL SIGINT
\ No newline at end of file
diff --git a/docker-compose.yml b/docker-compose.yml
index b484c81..de1f277 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -26,6 +26,7 @@ services:
     build:
       context: .
       dockerfile: Dockerfile.postgres
+      target: postgres
       args:
         PGSQL_VERSION: 14
     environment:
@@ -49,6 +50,7 @@ services:
     build:
       context: .
       dockerfile: Dockerfile.mariadb
+      target: mariadb
       args:
         MARIADB_VERSION: 10.9
     environment: &mariadb
@@ -74,6 +76,7 @@ services:
     build:
       context: .
       dockerfile: Dockerfile.mariadb
+      target: mariadb
       args:
         MARIADB_VERSION: 10.3
     environment:
diff --git a/start.sh b/start.sh
index df0b6cf..c1878bc 100755
--- a/start.sh
+++ b/start.sh
@@ -8,16 +8,20 @@ chmod +x /etc/service/*/run
 
 # Define shutdown + cleanup procedure
 cleanup() {
+    echo ""
+    echo "SIGTERM called!"
     echo "Container stop requested, running final dump + cleanup"
     /sync/sync --push
     echo "Good bye!"
+    exit 0
 }
 
 # Trap SIGTERM
 echo "Setting SIGTERM trap"
-trap 'cleanup' EXIT SIGINT
-trap 'echo SIGQUIT' SIGQUIT
+trap 'cleanup' SIGTERM
 
 # Start Runit.
 echo "Starting Runit."
-runsvdir -P /etc/service
\ No newline at end of file
+exec runsvdir -P /etc/service &
+
+sleep infinity & wait
\ No newline at end of file
diff --git a/traptest.sh b/traptest.sh
index ec805d5..9c6f795 100755
--- a/traptest.sh
+++ b/traptest.sh
@@ -1,55 +1,11 @@
 #!/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 'echo "Caught EXIT"' EXIT
+trap 'echo "Caught SIGINT"' SIGINT
+trap 'echo "Caught SIGTERM"' SIGTERM
+trap 'echo "Caught SIGKILL"' SIGKILL
+trap 'echo "Caught 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
+echo "Sleeping 600 seconds."
+sleep 600 & wait
\ No newline at end of file