From 199d86688cc1c71299d58f703da5b549ada3a321 Mon Sep 17 00:00:00 2001 From: f Date: Fri, 26 Jul 2019 20:57:11 -0300 Subject: [PATCH] WIP de deploy --- Dockerfile | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++ Gemfile.lock | 2 +- Procfile | 3 ++ monit.conf | 8 ++--- 4 files changed, 106 insertions(+), 7 deletions(-) create mode 100644 Dockerfile create mode 100644 Procfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..faa56722 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,100 @@ +# 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 sutty/sdk-ruby:latest as build +MAINTAINER "f " + +# Un entorno base +ENV NOKOGIRI_USE_SYSTEM_LIBRARIES=1 +ENV SECRET_KEY_BASE solo_es_necesaria_para_correr_rake +ENV RAILS_ENV production + +# Para compilar los assets en brotli +RUN apk add --no-cache brotli + +# Empezamos con la usuaria app creada por sdk-ruby +USER app +# Vamos a trabajar dentro de este directorio +WORKDIR /home/app/sutty + +# Copiamos solo el Gemfile para poder instalar las gemas necesarias +COPY --chown=app:www-data ./Gemfile . +COPY --chown=app:www-data ./Gemfile.lock . +# Instalar las gemas de producción +# XXX: No usamos la flag --production porque luego no nos deja +# desinstalar las gemas de los assets +RUN bundle install --path=./vendor --without test development +# Vaciar la caché +RUN rm vendor/ruby/2.5.0/cache/*.gem +# Limpiar las librerías nativas, esto ahorra más espacio y uso de +# memoria ya que no hay que cargar símbolos que no se van a usar. +RUN find vendor -name "*.so" | xargs -rn 1 strip --strip-unneeded + +# Copiar el repositorio git +COPY --chown=app:www-data ./.git/ ./.git/ +# Hacer un tarball de los archivos desde el repositorio +RUN git archive -o ../sutty.tar.gz HEAD + +# Extraer archivos necesarios para compilar los assets +RUN tar xvf ../sutty.tar.gz Rakefile config app yarn.lock package.json +# Instalar los paquetes JS +RUN yarn +# Pre-compilar los assets +RUN bundle exec rake assets:precompile +# Comprimirlos usando brotli +RUN find public/assets -type f | grep -v ".gz$" | xargs -r brotli -k -9 + +# Eliminar la necesidad de un runtime JS en producción, porque los +# assets ya están pre-compilados. +RUN sed -re "/(uglifier|bootstrap|coffee-rails)/d" -i Gemfile +RUN bundle clean + +# Contenedor final +FROM sutty/monit:latest + +# 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 +RUN apk add --no-cache sqlite-libs +# Necesitamos yarn para que Jekyll pueda generar los sitios +# XXX: Eliminarlo cuando extraigamos la generación de sitios del proceso +# principal +RUN apk add --no-cache yarn +# Instalar foreman para poder correr los servicios +RUN gem install --no-rdoc --no-ri --no-user-install foreman + +# Agregar el grupo del servidor web +RUN addgroup -g 82 -S www-data +# Agregar la usuaria +RUN adduser -s /bin/sh -G www-data -h /srv/http -D app + +# Convertirse en app para instalar +USER app +WORKDIR /srv/http + +# Traer los archivos y colocarlos donde van definitivamente +COPY --from=build --chown=app:www-data /home/app/sutty.tar.gz /tmp/ +# XXX: No vale la pena borrarlo porque sigue ocupando espacio en la capa +# anterior +RUN tar xf /tmp/sutty.tar.gz && rm /tmp/sutty.tar.gz + +# Traer los assets compilados y las gemas +COPY --from=build --chown=app:www-data /home/app/web/public/assets public/assets +COPY --from=build --chown=app:www-data /home/app/web/vendor vendor +COPY --from=build --chown=app:www-data /home/app/web/.bundle .bundle + +# Volver a root para cerrar la compilación +USER root + +# Instalar la configuración de monit y comprobarla +RUN install -m 640 -o root -g root ./monit.conf /etc/monit.d/sutty.conf +RUN monit -t + +# Mantener estos directorios! +VOLUME "/srv/http/_deploy" +VOLUME "/srv/http/_sites" +VOLUME "/srv/http/public" + +# El puerto de puma +EXPOSE 3000 diff --git a/Gemfile.lock b/Gemfile.lock index 7250ed95..9d611326 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -466,4 +466,4 @@ DEPENDENCIES web-console (>= 3.3.0) BUNDLED WITH - 1.17.3 + 2.0.2 diff --git a/Procfile b/Procfile new file mode 100644 index 00000000..367b027f --- /dev/null +++ b/Procfile @@ -0,0 +1,3 @@ +migrate: bundle exec rake db:migrate db:seed +sutty: bundle exec puma -d config.ru +sidekiq: bundle exec sidekiq -t 1 diff --git a/monit.conf b/monit.conf index 3d686349..211b03a6 100644 --- a/monit.conf +++ b/monit.conf @@ -1,7 +1,3 @@ -check process rails with pidfile /srv/http/tmp/puma.pid - start program = "/usr/bin/entrypoint rails" as uid app +check process sutty with pidfile /srv/http/tmp/puma.pid + start program = "/bin/sh -c 'cd /srv/http && foreman start migrate && foreman start sutty'" as uid app stop program = "/bin/sh -c 'cat /srv/http/tmp/puma.pid | xargs kill'" - -check process static with pidfile /tmp/darkhttpd.pid - start program = "/usr/bin/entrypoint darkhttpd" - stop program = "/bin/sh -c 'cat /tmp/darkhttpd.pid | xargs kill'"