Skip to content

Commit

Permalink
fly deploy setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Fine committed Aug 12, 2022
1 parent 985761e commit 47eca27
Show file tree
Hide file tree
Showing 12 changed files with 339 additions and 97 deletions.
107 changes: 90 additions & 17 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,22 +1,95 @@
FROM ruby:3.1.1
# syntax = docker/dockerfile:experimental
ARG RUBY_VERSION=3.1.1
ARG VARIANT=jemalloc-slim
FROM quay.io/evl.ms/fullstaq-ruby:${RUBY_VERSION}-${VARIANT} as base

RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN apt-get update -y
RUN apt-get install -y \
libqt5webkit5-dev \
build-essential \
qtbase5-dev \
qtchooser \
qt5-qmake \
qtbase5-dev-tools \
xvfb
ARG NODE_VERSION=16
ARG BUNDLER_VERSION=2.3.9

ENV app /app
ENV BUNDLE_PATH /gems
ENV GEM_HOME /gems
ARG RAILS_ENV=production
ENV RAILS_ENV=${RAILS_ENV}

COPY Gemfile* $app/
ENV RAILS_SERVE_STATIC_FILES true
ENV RAILS_LOG_TO_STDOUT true

ENV PATH="$PATH:$BUNDLE_PATH/bin"
ARG BUNDLE_WITHOUT=development:test
ARG BUNDLE_PATH=vendor/bundle
ENV BUNDLE_PATH ${BUNDLE_PATH}
ENV BUNDLE_WITHOUT ${BUNDLE_WITHOUT}

COPY . $app/
RUN mkdir -p /app/spec/dummy/tmp/pids
WORKDIR /app
# RUN touch tmp/pids/.keep
RUN touch /app/spec/dummy/tmp/pids/server.pid

SHELL ["/bin/bash", "-c"]

RUN curl https://get.volta.sh | bash

ENV BASH_ENV ~/.bashrc
ENV VOLTA_HOME /root/.volta
ENV PATH $VOLTA_HOME/bin:/usr/local/bin:$PATH

RUN volta install node@${NODE_VERSION} && volta install yarn

FROM base as build_deps

ARG DEV_PACKAGES="git build-essential libpq-dev wget vim curl gzip xz-utils libsqlite3-dev imagemagick libmagickcore-dev libmagickwand-dev"
ENV DEV_PACKAGES ${DEV_PACKAGES}

RUN --mount=type=cache,id=dev-apt-cache,sharing=locked,target=/var/cache/apt \
--mount=type=cache,id=dev-apt-lib,sharing=locked,target=/var/lib/apt \
apt-get update -qq && \
apt-get install --no-install-recommends -y ${DEV_PACKAGES} \
&& rm -rf /var/lib/apt/lists /var/cache/apt/archives

FROM build_deps as gems

RUN gem install -N bundler -v ${BUNDLER_VERSION}

COPY Gemfile* ./
COPY fae.gemspec* ./
RUN bundle install && rm -rf vendor/bundle/ruby/*/cache

FROM build_deps as node_modules

COPY package*json ./
COPY yarn.* ./

RUN if [ -f "yarn.lock" ]; then \
yarn install; \
elif [ -f "package-lock.json" ]; then \
npm install; \
else \
mkdir node_modules; \
fi

FROM base

ARG PROD_PACKAGES="postgresql-client file vim curl gzip libsqlite3-0 imagemagick libmagickcore-dev libmagickwand-dev"
ENV PROD_PACKAGES=${PROD_PACKAGES}

RUN --mount=type=cache,id=prod-apt-cache,sharing=locked,target=/var/cache/apt \
--mount=type=cache,id=prod-apt-lib,sharing=locked,target=/var/lib/apt \
apt-get update -qq && \
apt-get install --no-install-recommends -y \
${PROD_PACKAGES} \
&& rm -rf /var/lib/apt/lists /var/cache/apt/archives

COPY --from=gems /app /app
COPY --from=node_modules /app/node_modules /app/node_modules

ENV SECRET_KEY_BASE 1

COPY . .

WORKDIR /app/spec/dummy

RUN bundle exec rails assets:precompile
# RUN cd spec/dummy && bundle exec rails assets:precompile

ENV PORT 8080

ARG SERVER_COMMAND="bundle exec puma -C config/puma.rb"
ENV SERVER_COMMAND ${SERVER_COMMAND}
CMD ${SERVER_COMMAND}
22 changes: 22 additions & 0 deletions Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM ruby:3.1.1

RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN apt-get update -y
RUN apt-get install -y \
libqt5webkit5-dev \
build-essential \
qtbase5-dev \
qtchooser \
qt5-qmake \
qtbase5-dev-tools \
xvfb

ENV app /app
ENV BUNDLE_PATH /gems
ENV GEM_HOME /gems

COPY Gemfile* $app/

ENV PATH="$PATH:$BUNDLE_PATH/bin"

COPY . $app/
5 changes: 4 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,7 @@ gem 'capistrano', '~> 3.1'
gem 'capistrano-rails', git: 'https://github.com/wearefine/rails'
gem 'capistrano-rvm'

gem 'mysql2'
gem 'pg'
gem "puma", "~> 5.0"

gem "fog-aws"
28 changes: 26 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,29 @@ GEM
digest (3.1.0)
erubi (1.10.0)
eventmachine (1.2.7)
excon (0.92.4)
execjs (2.8.1)
factory_bot (4.8.2)
activesupport (>= 3.0.0)
factory_bot_rails (4.8.2)
factory_bot (~> 4.8.2)
railties (>= 3.0.0)
ffi (1.15.5)
fog-aws (3.14.0)
fog-core (~> 2.1)
fog-json (~> 1.1)
fog-xml (~> 0.1)
fog-core (2.3.0)
builder
excon (~> 0.71)
formatador (>= 0.2, < 2.0)
mime-types
fog-json (1.2.0)
fog-core
multi_json (~> 1.10)
fog-xml (0.1.4)
fog-core
nokogiri (>= 1.5.11, < 2.0.0)
formatador (1.1.0)
globalid (1.0.0)
activesupport (>= 5.0)
Expand Down Expand Up @@ -238,10 +254,13 @@ GEM
marcel (1.0.2)
matrix (0.4.2)
method_source (1.0.0)
mime-types (3.4.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2022.0105)
mini_magick (4.11.0)
mini_mime (1.1.2)
minitest (5.15.0)
mysql2 (0.5.3)
multi_json (1.15.0)
nenv (0.3.0)
net-imap (0.2.3)
digest
Expand All @@ -268,10 +287,13 @@ GEM
nenv (~> 0.1)
shellany (~> 0.0)
orm_adapter (0.5.0)
pg (1.4.3)
pry (0.14.1)
coderay (~> 1.1)
method_source (~> 1.0)
public_suffix (4.0.6)
puma (5.6.4)
nio4r (~> 2.0)
racc (1.6.0)
rack (2.2.3)
rack-test (1.1.0)
Expand Down Expand Up @@ -422,9 +444,11 @@ DEPENDENCIES
database_cleaner
factory_bot_rails (~> 4.8.2)
fae-rails!
fog-aws
guard-rspec
mysql2
pg
pry
puma (~> 5.0)
rails (~> 7.0.2)
rails-controller-testing
rails-perftest
Expand Down
1 change: 1 addition & 0 deletions app/models/fae/change.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Change < ActiveRecord::Base
serialize :updated_attributes

class << self

# writing current_user to thread for thread safety
def current_user=(user)
Thread.current[:current_user] = user
Expand Down
43 changes: 41 additions & 2 deletions config/initializers/carrierwave.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,42 @@
CarrierWave.configure do |config|
config.cache_dir = "#{Rails.root}/tmp/fae/uploads"
CarrierWave.configure do |config|
config.cache_dir = "#{Rails.root}/tmp/fae/uploads"

if Rails.env.production? || Rails.env.remote_development?
config.fog_provider = 'fog/aws'
config.fog_credentials = {
provider: 'AWS',
aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'],
aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
region: 'us-west-2'
}
config.storage = :fog
else
config.storage = :file
end

if Rails.env.production?
config.asset_host = 'https://s3.us-west-2.amazonaws.com/fae-engine-test-fly'
config.fog_directory = 'fae-engine-test-fly'
elsif Rails.env.remote_development?

end

# uncomment to deliver production assets to the local CMS
# if Rails.env.development?
# config.asset_host = ''
# config.fog_directory = ''
# end

# uncomment to upload to bucket/cdn in local dev
# if Rails.env.development?
# config.fog_provider = 'fog/aws'
# config.fog_credentials = {
# provider: 'AWS',
# aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'],
# aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
# region: 'us-west-2'
# }
# config.storage = :fog
# end

end
43 changes: 43 additions & 0 deletions config/puma.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Puma can serve each request in a thread from an internal thread pool.
# The `threads` method setting takes two numbers: a minimum and maximum.
# Any libraries that use thread pools should be configured to match
# the maximum value specified for Puma. Default is set to 5 threads for minimum
# and maximum; this matches the default thread size of Active Record.
#
max_threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
min_threads_count = ENV.fetch("RAILS_MIN_THREADS") { max_threads_count }
threads min_threads_count, max_threads_count

# Specifies the `worker_timeout` threshold that Puma will use to wait before
# terminating a worker in development environments.
#
worker_timeout 3600 if ENV.fetch("RAILS_ENV", "development") == "development"

# Specifies the `port` that Puma will listen on to receive requests; default is 3000.
#
port ENV.fetch("PORT") { 3000 }

# Specifies the `environment` that Puma will run in.
#
environment ENV.fetch("RAILS_ENV") { "development" }

# Specifies the `pidfile` that Puma will use.
pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" }

# Specifies the number of `workers` to boot in clustered mode.
# Workers are forked web server processes. If using threads and workers together
# the concurrency of the application would be max `threads` * `workers`.
# Workers do not work on JRuby or Windows (both of which do not support
# processes).
#
workers ENV.fetch("WEB_CONCURRENCY") { 4 }

# Use the `preload_app!` method when specifying a `workers` number.
# This directive tells Puma to first boot the application and load code
# before forking the application. This takes advantage of Copy On Write
# process behavior so workers use less memory.
#
preload_app!

# Allow puma to be restarted by `bin/rails restart` command.
plugin :tmp_restart
39 changes: 0 additions & 39 deletions docker-compose.override.yml.postgres

This file was deleted.

12 changes: 10 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
app:
stdin_open: true
tty: true
build: .
build:
context: .
dockerfile: ./Dockerfile.dev
working_dir: /app/spec/dummy
command: /bin/bash -c "bundle check || bundle install && bundle exec rails s -p 3000 -P /tmp/puma.pid -b 0.0.0.0"
volumes:
Expand All @@ -21,7 +23,13 @@
- FINE_NETLIFY_API_TOKEN=test
- FINE_NETLIFY_API_USER=test
db:
image: mariadb:10.2
image: postgres
volumes:
- ./tmp/db:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: password
ports:
- "5432:5432"
environment:
- MYSQL_ROOT_PASSWORD=
- MYSQL_ALLOW_EMPTY_PASSWORD='yes'
Expand Down
Loading

0 comments on commit 47eca27

Please sign in to comment.