From e5a40d941c4de424260842b287a141aa0c38a17c Mon Sep 17 00:00:00 2001
From: f <f@sutty.nl>
Date: Tue, 1 Mar 2022 10:03:53 -0300
Subject: [PATCH] rails base container

---
 .woodpecker.yml |  6 +++---
 Dockerfile      | 37 ++++++++++++++++++-------------------
 Procfile        |  8 ++++++++
 entrypoint.sh   | 40 ++++++++++++++++++++++++++++++++++++++++
 monit.conf      | 30 ++++++++++++++++++++++++++++++
 5 files changed, 99 insertions(+), 22 deletions(-)
 create mode 100644 Procfile
 create mode 100755 entrypoint.sh
 create mode 100644 monit.conf

diff --git a/.woodpecker.yml b/.woodpecker.yml
index a30873e..4d8908b 100644
--- a/.woodpecker.yml
+++ b/.woodpecker.yml
@@ -2,7 +2,7 @@ pipeline:
   publish:
     image: plugins/docker
     registry: registry.nulo.in
-    repo: registry.nulo.in/sutty/ruby
+    repo: registry.nulo.in/sutty/rails
     tags:
     - ${ALPINE_VERSION}-${RUBY_VERSION}.${RUBY_PATCH}
     - latest
@@ -10,11 +10,11 @@ pipeline:
     - RUBY_VERSION=${RUBY_VERSION}
     - RUBY_PATCH=${RUBY_PATCH}
     - ALPINE_VERSION=${ALPINE_VERSION}
-    - BASE_IMAGE=registry.nulo.in/sutty/monit
+    - BASE_IMAGE=registry.nulo.in/sutty/ruby
     username: sutty
     secrets:
     - docker_password
-    cache_from: registry.nulo.in/sutty/ruby:${ALPINE_VERSION}-${RUBY_VERSION}.${RUBY_PATCH}
+    cache_from: registry.nulo.in/sutty/rails:${ALPINE_VERSION}-${RUBY_VERSION}.${RUBY_PATCH}
     when:
       branch: antifascista
       event: push
diff --git a/Dockerfile b/Dockerfile
index 18deb4d..42b0efa 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,24 +1,23 @@
+ARG BASE_IMAGE=registry.nulo.in/sutty/ruby
 ARG ALPINE_VERSION=3.13.6
-ARG BASE_IMAGE=alpine
-ARG EMAIL=monit@sutty.nl
-FROM ${BASE_IMAGE}:${ALPINE_VERSION} as build
 ARG RUBY_VERSION=2.7
 ARG RUBY_PATCH=5
-ENV EMAIL ${EMAIL:-monit@sutty.nl}
-
-RUN apk add --no-cache ruby~${RUBY_VERSION}.${RUBY_PATCH} patch
-
-# Patch rubygems to install x86_64-linux-musl gems
-COPY ./rubygems-platform-musl.patch /tmp/
-RUN cd /usr/lib/ruby/${RUBY_VERSION}.0 && patch -Np 0 -i /tmp/rubygems-platform-musl.patch || true
-
-FROM ${BASE_IMAGE}:${ALPINE_VERSION}
+ARG EMAIL=monit@sutty.nl
+FROM ${BASE_IMAGE}:${ALPINE_VERSION}-${RUBY_VERSION}.${RUBY_PATCH}
 ARG ALPINE_VERSION
-MAINTAINER "f <f@sutty.nl>"
-ARG RUBY_VERSION
-ARG RUBY_PATCH
+MAINTAINER f <f@sutty.nl>
+ENV RAILS_ENV production
 
-# Install ruby and extracted gems and copy patched rubygems
-RUN apk add --no-cache tzdata ruby~${RUBY_VERSION}.${RUBY_PATCH} && rm /usr/lib/ruby/${RUBY_VERSION}.0/rubygems.rb
-RUN apk add --no-cache ruby-json ruby-bigdecimal ruby-rake ruby-bundler ruby-irb ruby-io-console ruby-etc
-COPY --from=build /usr/lib/ruby/${RUBY_VERSION}.0/rubygems.rb /usr/lib/ruby/${RUBY_VERSION}.0/
+RUN addgroup -g 82 -S www-data
+RUN adduser -s /bin/sh -G www-data -h /srv -D rails
+RUN install -dm 750 -o rails -g www-data /srv
+
+RUN apk add --no-cache jemalloc daemonize
+
+COPY ./monit.conf /etc/monit.d/rails.conf
+COPY ./Procfile /etc/Procfile
+COPY ./entrypoint.sh /usr/local/bin/rails
+
+VOLUME /srv
+EXPOSE 3000
+EXPOSE 9394
diff --git a/Procfile b/Procfile
new file mode 100644
index 0000000..f948c78
--- /dev/null
+++ b/Procfile
@@ -0,0 +1,8 @@
+install: bundle install --path ./vendor --without="test development" --no-cache --clean
+migrate: bundle exec rake db:prepare db:seed
+rails: bundle exec puma config.ru
+blazer_5m: bundle exec rake blazer:run_checks SCHEDULE="5 minutes"
+blazer_1h: bundle exec rake blazer:run_checks SCHEDULE="1 hour"
+blazer_1d: bundle exec rake blazer:run_checks SCHEDULE="1 day"
+blazer: bundle exec rake blazer:send_failing_checks
+prometheus: bundle exec prometheus_exporter -b 0.0.0.0 --prefix "lunar_"
diff --git a/entrypoint.sh b/entrypoint.sh
new file mode 100755
index 0000000..49c1c24
--- /dev/null
+++ b/entrypoint.sh
@@ -0,0 +1,40 @@
+#!/bin/sh
+set -e
+
+case $1 in
+  start)
+    su app -c "cd /srv && foreman run -f /etc/Procfile install"
+    su app -c "cd /srv && foreman run -f /etc/Procfile migrate"
+    daemonize -c /srv -u app /usr/bin/foreman start -f /etc/Procfile rails
+
+    ;;
+  stop)
+    cat /srv/tmp/puma.pid | xargs -r kill
+
+    ;;
+  reload)
+    su app -c "cd /srv && foreman run -f /etc/Procfile install"
+    cat /srv/tmp/puma.pid | xargs -r kill -USR2
+
+    ;;
+  prometheus)
+    rm -f /tmp/prometheus.pid
+
+    daemonize -c /srv \
+              -p /tmp/prometheus.pid \
+              -l /tmp/prometheus.pid \
+              -u app \
+              /usr/bin/foreman start -f /etc/Procfile prometheus
+    ;;
+
+  prometheus-stop)
+    cat /tmp/prometheus.pid | xargs -r kill
+
+    ;;
+
+  blazer)
+    test -z "$2" || schedule="_${2}"
+    su app -c "cd /srv && foreman run -f /etc/Procfile blazer${schedule}"
+
+    ;;
+esac
diff --git a/monit.conf b/monit.conf
new file mode 100644
index 0000000..babc39b
--- /dev/null
+++ b/monit.conf
@@ -0,0 +1,30 @@
+# Rails
+check process rails with pidfile /srv/tmp/puma.pid
+  start program = "/usr/local/bin/rails start"
+  stop program = "/usr/local/bin/rails stop"
+
+# Prometheus
+check process prometheus with pidfile /tmp/prometheus.pid
+  start program = "/usr/local/bin/rails prometheus"
+  stop program = "/usr/local/bin/rails prometheus-stop"
+
+# Blazer checks
+check program blazer_5m
+  with path "/usr/local/bin/rails blazer 5m"
+  every 5 cycles
+  if status != 0 then alert
+
+check program blazer_1h
+  with path "/usr/local/bin/rails blazer 1h"
+  every 60 cycles
+  if status != 0 then alert
+
+check program blazer_1d
+  with path "/usr/local/bin/rails blazer 1d"
+  every 1440 cycles
+  if status != 0 then alert
+
+check program blazer
+  with path "/usr/local/bin/rails blazer"
+  every 61 cycles
+  if status != 0 then alert
