From 47997f27ad1707b27596062ffca7980fff1b315c Mon Sep 17 00:00:00 2001 From: f Date: Mon, 19 Jul 2021 19:00:12 -0300 Subject: [PATCH] Deploy --- Dockerfile | 110 +++++++++++++++++++++++++++++++++++ Procfile | 6 ++ entrypoint.sh | 11 ++++ monit.conf | 27 +++++++++ rubygems-platform-musl.patch | 14 +++++ 5 files changed, 168 insertions(+) create mode 100644 Dockerfile create mode 100644 Procfile create mode 100644 entrypoint.sh create mode 100644 monit.conf create mode 100644 rubygems-platform-musl.patch diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..49297ce --- /dev/null +++ b/Dockerfile @@ -0,0 +1,110 @@ +# Este Dockerfile está armado pensando en una compilación lanzada desde +# el mismo repositorio de trabajo. Cuando tengamos CI/CD algunas cosas +# como el tarball van a tener que cambiar porque ya vamos a haber hecho +# un clone/pull limpio. +FROM alpine:3.13.5 AS build +MAINTAINER "f " + +ARG RAILS_MASTER_KEY +ARG BRANCH + +# Un entorno base +ENV BRANCH=$BRANCH +ENV SECRET_KEY_BASE solo_es_necesaria_para_correr_rake +ENV RAILS_ENV production +ENV RAILS_MASTER_KEY=$RAILS_MASTER_KEY + +RUN apk add --no-cache libxslt libxml2 tzdata ruby ruby-bundler ruby-json ruby-bigdecimal ruby-rake postgresql-libs + +RUN test "2.7.3" = `ruby -e 'puts RUBY_VERSION'` + +# https://github.com/rubygems/rubygems/issues/2918 +# https://gitlab.alpinelinux.org/alpine/aports/issues/10808 +RUN apk add --no-cache patch +COPY ./rubygems-platform-musl.patch /tmp/ +RUN cd /usr/lib/ruby/2.7.0 && patch -Np 0 -i /tmp/rubygems-platform-musl.patch + +# Agregar el usuario +RUN addgroup -g 82 -S www-data +RUN adduser -s /bin/sh -G www-data -h /home/app -D app +RUN install -dm750 -o app -g www-data /home/app/ectomobile +RUN gem install --no-document bundler + +# Empezamos con la usuaria app +USER app +# Vamos a trabajar dentro de este directorio +WORKDIR /home/app/ectomobile + +# Copiamos solo el Gemfile para poder instalar las gemas necesarias +COPY --chown=app:www-data ./Gemfile . +COPY --chown=app:www-data ./Gemfile.lock . +RUN bundle config set no-cache 'true' +RUN bundle install --path=./vendor --without='test development' +# Vaciar la caché +RUN rm vendor/ruby/2.7.0/cache/*.gem + +# Copiar el repositorio git +COPY --chown=app:www-data ./.git/ ./.git/ +# Hacer un clon limpio del repositorio en lugar de copiar todos los +# archivos +RUN cd .. && git clone ectomobile checkout +RUN cd ../checkout && git checkout $BRANCH + +WORKDIR /home/app/checkout +# Traer las gemas: +RUN rm -rf ./vendor +RUN mv ../ectomobile/vendor ./vendor +RUN mv ../ectomobile/.bundle ./.bundle + +# Instalar secretos +COPY --chown=app:root ./config/credentials.yml.enc ./config/ + +# Eliminar la necesidad de un runtime JS en producción, porque los +# assets ya están pre-compilados. +RUN sed -re "/(sassc|uglifier|bootstrap|coffee-rails)/d" -i Gemfile +RUN bundle clean +RUN rm -rf ./node_modules ./tmp/cache ./.git ./test ./doc +# Eliminar archivos innecesarios +USER root +RUN apk add --no-cache findutils +RUN find /home/app/checkout/vendor/ruby/2.7.0 -maxdepth 3 -type d -name test -o -name spec -o -name rubocop | xargs -r rm -rf + +# Contenedor final +FROM sutty/monit:latest +ENV RAILS_ENV production + +# Instalar las dependencias, separamos la librería de base de datos para +# poder reutilizar este primer paso desde otros contenedores +RUN apk add --no-cache libxslt libxml2 tzdata ruby ruby-bundler ruby-json ruby-bigdecimal ruby-rake ruby-irb +RUN apk add --no-cache postgresql-libs + +# Chequear que la versión de ruby sea la correcta +RUN test "2.7.3" = `ruby -e 'puts RUBY_VERSION'` + +# https://github.com/rubygems/rubygems/issues/2918 +# https://gitlab.alpinelinux.org/alpine/aports/issues/10808 +COPY ./rubygems-platform-musl.patch /tmp/ +RUN apk add --no-cache patch && cd /usr/lib/ruby/2.7.0 && patch -Np 0 -i /tmp/rubygems-platform-musl.patch && apk del patch + +# Instalar foreman para poder correr los servicios +RUN gem install --no-document --no-user-install bundler foreman + +# Agregar el grupo del servidor web y la usuaria +RUN addgroup -g 82 -S www-data +RUN adduser -s /bin/sh -G www-data -h /srv/http -D app + +# Convertirse en app para instalar +USER app +COPY --from=build --chown=app:www-data /home/app/checkout /srv/http +COPY --chown=app:www-data ./.git/ ./.git/ + +# Volver a root para cerrar la compilación +USER root +# Instalar la configuración de monit +RUN install -m 640 -o root -g root /srv/http/monit.conf /etc/monit.d/ectomobile.conf +RUN apk add --no-cache daemonize ruby-webrick +RUN install -m 755 /srv/http/entrypoint.sh /usr/local/bin/ectomobile + +# El puerto de puma +EXPOSE 3000 +EXPOSE 9394 diff --git a/Procfile b/Procfile new file mode 100644 index 0000000..ed9d48a --- /dev/null +++ b/Procfile @@ -0,0 +1,6 @@ +migrate: bundle exec rake db:prepare db:seed +ectomobile: 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 diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..48a769d --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,11 @@ +#!/bin/sh +set -e + +case $1 in + start) + su app -c "cd /srv/http && foreman start migrate" + daemonize -c /srv/http -u app /usr/bin/foreman start ectomobile + ;; + stop) cat /srv/http/tmp/pids/server.pid | xargs kill ;; + blazer*) cd /srv/http && foreman start $1 ;; +esac diff --git a/monit.conf b/monit.conf new file mode 100644 index 0000000..0e21c8b --- /dev/null +++ b/monit.conf @@ -0,0 +1,27 @@ +check process ectomobile with pidfile /srv/http/tmp/pids/server.pid + start program = "/usr/local/bin/ectomobile start" + stop program = "/usr/local/bin/ectomobile stop" + +check program blazer_5m + with path "/usr/local/bin/ectomobile blazer_5m" + as uid "app" and gid "www-data" + every 5 cycles + if status != 0 then alert + +check program blazer_1h + with path "/usr/local/bin/ectomobile blazer_1h" + as uid "app" and gid "www-data" + every 60 cycles + if status != 0 then alert + +check program blazer_1d + with path "/usr/local/bin/ectomobile blazer_1d" + as uid "app" and gid "www-data" + every 1440 cycles + if status != 0 then alert + +check program blazer + with path "/usr/local/bin/ectomobile blazer" + as uid "app" and gid "www-data" + every 61 cycles + if status != 0 then alert diff --git a/rubygems-platform-musl.patch b/rubygems-platform-musl.patch new file mode 100644 index 0000000..d6db223 --- /dev/null +++ b/rubygems-platform-musl.patch @@ -0,0 +1,14 @@ +--- rubygems.rb.orig ++++ rubygems.rb +@@ -764,10 +764,7 @@ + def self.platforms + @platforms ||= [] + if @platforms.empty? +- # XXX: Patched to avoid installing platform-specific gems with binaries +- # linked against glibc. +- @platforms = [Gem::Platform::RUBY] +- #@platforms = [Gem::Platform::RUBY, Gem::Platform.local] ++ @platforms = [Gem::Platform::RUBY, Gem::Platform.local] + end + @platforms + end