From 950dd29d08d7435507ce68f850360bb08594ed20 Mon Sep 17 00:00:00 2001 From: Eric Stiens Date: Fri, 7 Oct 2022 00:58:36 -0500 Subject: [PATCH] add some deployment stuff --- .dockerignore | 16 +++ Dockerfile | 135 ++++++++++++++++++++++++ Gemfile | 2 - Gemfile.lock | 3 - Procfile | 1 - Procfile.dev | 1 + app.json | 3 - config/database.yml | 7 +- fly.toml | 51 +++++++++ lib/tasks/2022/get_team_match_info.rake | 2 - lib/tasks/fly.rake | 37 +++++++ package.json | 11 -- yarn.lock | 129 ---------------------- 13 files changed, 246 insertions(+), 152 deletions(-) create mode 100644 .dockerignore create mode 100755 Dockerfile delete mode 100644 app.json create mode 100644 fly.toml create mode 100644 lib/tasks/fly.rake delete mode 100644 package.json delete mode 100644 yarn.lock diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..0e34b06 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,16 @@ +.git +tmp +!tmp/pids +log +public/assets +public/packs +.bundle + +db/*.sqlite3 +db/*.sqlite3-* + +storage +config/master.key +config/credentials/*.key + +node_modules diff --git a/Dockerfile b/Dockerfile new file mode 100755 index 0000000..892d7f2 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,135 @@ +# syntax = docker/dockerfile:experimental + +# Dockerfile used to build a deployable image for a Rails application. +# Adjust as required. +# +# Common adjustments you may need to make over time: +# * Modify version numbers for Ruby, Bundler, and other products. +# * Add library packages needed at build time for your gems, node modules. +# * Add deployment packages needed by your application +# * Add (often fake) secrets needed to compile your assets + +####################################################################### + +# Learn more about the chosen Ruby stack, Fullstaq Ruby, here: +# https://github.com/evilmartians/fullstaq-ruby-docker. +# +# We recommend using the highest patch level for better security and +# performance. + +ARG RUBY_VERSION=3.1.1 +ARG VARIANT=jemalloc-slim +FROM quay.io/evl.ms/fullstaq-ruby:${RUBY_VERSION}-${VARIANT} as base + +LABEL fly_launch_runtime="rails" + +ARG NODE_VERSION=18.9.0 +ARG BUNDLER_VERSION=2.3.7 + +ARG RAILS_ENV=production +ENV RAILS_ENV=${RAILS_ENV} + +ENV RAILS_SERVE_STATIC_FILES true +ENV RAILS_LOG_TO_STDOUT true + +ARG BUNDLE_WITHOUT=development:test +ARG BUNDLE_PATH=vendor/bundle +ENV BUNDLE_PATH ${BUNDLE_PATH} +ENV BUNDLE_WITHOUT ${BUNDLE_WITHOUT} + +RUN mkdir /app +WORKDIR /app +RUN mkdir -p tmp/pids + +RUN curl https://get.volta.sh | bash +ENV VOLTA_HOME /root/.volta +ENV PATH $VOLTA_HOME/bin:/usr/local/bin:$PATH +RUN volta install node@${NODE_VERSION} yarn + +####################################################################### + +# install packages only needed at build time + +FROM base as build_deps + +ARG BUILD_PACKAGES="git build-essential libpq-dev wget vim curl gzip xz-utils libsqlite3-dev" +ENV BUILD_PACKAGES ${BUILD_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 ${BUILD_PACKAGES} \ + && rm -rf /var/lib/apt/lists /var/cache/apt/archives + +####################################################################### + +# install gems + +FROM build_deps as gems + +RUN gem update --system --no-document && \ + gem install -N bundler -v ${BUNDLER_VERSION} + +COPY Gemfile* ./ +RUN bundle install && rm -rf vendor/bundle/ruby/*/cache + +####################################################################### + +# install node modules + +FROM build_deps as node_modules + +COPY package*json ./ +COPY yarn.* ./ +RUN yarn install + +####################################################################### + +# install deployment packages + +FROM base + +ARG DEPLOY_PACKAGES="postgresql-client file vim curl gzip libsqlite3-0" +ENV DEPLOY_PACKAGES=${DEPLOY_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 \ + ${DEPLOY_PACKAGES} \ + && rm -rf /var/lib/apt/lists /var/cache/apt/archives + +# copy installed gems +COPY --from=gems /app /app +COPY --from=gems /usr/lib/fullstaq-ruby/versions /usr/lib/fullstaq-ruby/versions +COPY --from=gems /usr/local/bundle /usr/local/bundle + +# copy installed node modules +COPY --from=node_modules /app/node_modules /app/node_modules + +####################################################################### + +# Deploy your application +COPY . . + +# Adjust binstubs to run on Linux and set current working directory +RUN chmod +x /app/bin/* && \ + sed -i 's/ruby.exe/ruby/' /app/bin/* && \ + sed -i '/^#!/aDir.chdir File.expand_path("..", __dir__)' /app/bin/* + +# The following enable assets to precompile on the build server. Adjust +# as necessary. If no combination works for you, see: +# https://fly.io/docs/rails/getting-started/existing/#access-to-environment-variables-at-build-time +ENV SECRET_KEY_BASE 1 +# ENV AWS_ACCESS_KEY_ID=1 +# ENV AWS_SECRET_ACCESS_KEY=1 + +# Run build task defined in lib/tasks/fly.rake +ARG BUILD_COMMAND="bin/rails fly:build" +RUN ${BUILD_COMMAND} + +# Default server start instructions. Generally Overridden by fly.toml. +ENV PORT 8080 +ARG SERVER_COMMAND="bin/rails fly:server" +ENV SERVER_COMMAND ${SERVER_COMMAND} +CMD ${SERVER_COMMAND} diff --git a/Gemfile b/Gemfile index a33b996..de14456 100644 --- a/Gemfile +++ b/Gemfile @@ -19,8 +19,6 @@ gem 'rack-attack' gem 'rack-cors', require: 'rack/cors' gem 'redis-rails' -gem 'cssbundling-rails' - group :production do gem 'scout_apm' end diff --git a/Gemfile.lock b/Gemfile.lock index 8230cd6..bab41da 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -86,8 +86,6 @@ GEM coderay (1.1.3) concurrent-ruby (1.1.10) crass (1.0.6) - cssbundling-rails (1.1.1) - railties (>= 6.0.0) dead_end (4.0.0) debug_inspector (1.1.0) derailed_benchmarks (2.1.2) @@ -300,7 +298,6 @@ DEPENDENCIES binding_of_caller chronic clockwork - cssbundling-rails derailed_benchmarks foundation-rails haml-rails diff --git a/Procfile b/Procfile index 7f3a461..c2c566e 100644 --- a/Procfile +++ b/Procfile @@ -1,2 +1 @@ web: bundle exec puma -C config/puma.rb -clock: bundle exec clockwork lib/new_scraper_clock.rb diff --git a/Procfile.dev b/Procfile.dev index 0c4b735..7dc3efc 100644 --- a/Procfile.dev +++ b/Procfile.dev @@ -1,2 +1,3 @@ web: bin/rails server -p 3000 css: yarn build:css --watch +clock: bundle exec clockwork lib/new_scraper_clock.rb diff --git a/app.json b/app.json deleted file mode 100644 index df26ab2..0000000 --- a/app.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "stack": "heroku-16" -} diff --git a/config/database.yml b/config/database.yml index 0ff6553..cc1c3dd 100644 --- a/config/database.yml +++ b/config/database.yml @@ -13,5 +13,10 @@ test: host: localhost production: - url: <%= ENV['DATABASE_URL'] %> + adapter: postgresql + encoding: unicode + database: <%= ENV['DATABASE_NAME'] %> + user: <%= ENV['DATABASE_USER'] || 'postgres' %> + host: <%= ENV['DATABASE_HOST'] %> + password: <%= ENV['DATABASE_PASSWORD'] %> pool: <%= ENV['DB_POOL'] || ENV['RAILS_MAX_THREADS'] || 5 %> diff --git a/fly.toml b/fly.toml new file mode 100644 index 0000000..e4a933c --- /dev/null +++ b/fly.toml @@ -0,0 +1,51 @@ +# fly.toml file generated for world-cup-json-2022 on 2022-10-07T00:53:06-05:00 + +app = "world-cup-json-2022" +kill_signal = "SIGINT" +kill_timeout = 5 +processes = [] + +[build] + [build.args] + BUILD_COMMAND = "bin/rails fly:build" + SERVER_COMMAND = "bin/rails fly:server" + +[deploy] + release_command = "bin/rails fly:release" + +[env] + PORT = "8080" + +[experimental] + allowed_public_ports = [] + auto_rollback = true + +[[services]] + http_checks = [] + internal_port = 8080 + processes = ["app"] + protocol = "tcp" + script_checks = [] + [services.concurrency] + hard_limit = 25 + soft_limit = 20 + type = "connections" + + [[services.ports]] + force_https = true + handlers = ["http"] + port = 80 + + [[services.ports]] + handlers = ["tls", "http"] + port = 443 + + [[services.tcp_checks]] + grace_period = "1s" + interval = "15s" + restart_limit = 0 + timeout = "2s" + +[[statics]] + guest_path = "/app/public" + url_prefix = "/" diff --git a/lib/tasks/2022/get_team_match_info.rake b/lib/tasks/2022/get_team_match_info.rake index 7076a10..8c64e56 100644 --- a/lib/tasks/2022/get_team_match_info.rake +++ b/lib/tasks/2022/get_team_match_info.rake @@ -1,5 +1,3 @@ -require 'HTTParty' - BASE_URL = 'https://api.fifa.com/api/v3/calendar/matches'.freeze BASE_PARAMS = '?from=2022-11-19T00%3A00%3A00Z&to=2022-12-31T23%3A59%3A59Z&language=en&count=500&idCompetition=17'.freeze diff --git a/lib/tasks/fly.rake b/lib/tasks/fly.rake new file mode 100644 index 0000000..9ae5beb --- /dev/null +++ b/lib/tasks/fly.rake @@ -0,0 +1,37 @@ +# commands used to deploy a Rails application +namespace :fly do + # BUILD step: + # - changes to the filesystem made here DO get deployed + # - NO access to secrets, volumes, databases + # - Failures here prevent deployment + task :build => 'assets:precompile' + + # RELEASE step: + # - changes to the filesystem made here are DISCARDED + # - full access to secrets, databases + # - failures here prevent deployment + task :release => 'db:migrate' + + # SERVER step: + # - changes to the filesystem made here are deployed + # - full access to secrets, databases + # - failures here result in VM being stated, shutdown, and rolled back + # to last successful deploy (if any). + task :server => :swapfile do + sh 'bin/rails server' + end + + # optional SWAPFILE task: + # - adjust fallocate size as needed + # - performance critical applications should scale memory to the + # point where swap is rarely used. 'fly scale help' for details. + # - disable by removing dependency on the :server task, thus: + # task :server do + task :swapfile do + sh 'fallocate -l 512M /swapfile' + sh 'chmod 0600 /swapfile' + sh 'mkswap /swapfile' + sh 'echo 10 > /proc/sys/vm/swappiness' + sh 'swapon /swapfile' + end +end diff --git a/package.json b/package.json deleted file mode 100644 index e03e3cb..0000000 --- a/package.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "name": "app", - "private": "true", - "dependencies": { - "bulma": "^0.9.4", - "sass": "^1.55.0" - }, - "scripts": { - "build:css": "sass ./app/assets/stylesheets/application.bulma.scss:./app/assets/builds/application.css --no-source-map --load-path=node_modules" - } -} diff --git a/yarn.lock b/yarn.lock deleted file mode 100644 index 3994a20..0000000 --- a/yarn.lock +++ /dev/null @@ -1,129 +0,0 @@ -# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. -# yarn lockfile v1 - - -anymatch@~3.1.2: - version "3.1.2" - resolved "https://registry.yarnpkg.com/anymatch/-/anymatch-3.1.2.tgz#c0557c096af32f106198f4f4e2a383537e378716" - integrity sha512-P43ePfOAIupkguHUycrc4qJ9kz8ZiuOUijaETwX7THt0Y/GNK7v0aa8rY816xWjZ7rJdA5XdMcpVFTKMq+RvWg== - dependencies: - normalize-path "^3.0.0" - picomatch "^2.0.4" - -binary-extensions@^2.0.0: - version "2.2.0" - resolved "https://registry.yarnpkg.com/binary-extensions/-/binary-extensions-2.2.0.tgz#75f502eeaf9ffde42fc98829645be4ea76bd9e2d" - integrity sha512-jDctJ/IVQbZoJykoeHbhXpOlNBqGNcwXJKJog42E5HDPUwQTSdjCHdihjj0DlnheQ7blbT6dHOafNAiS8ooQKA== - -braces@~3.0.2: - version "3.0.2" - resolved "https://registry.yarnpkg.com/braces/-/braces-3.0.2.tgz#3454e1a462ee8d599e236df336cd9ea4f8afe107" - integrity sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A== - dependencies: - fill-range "^7.0.1" - -bulma@^0.9.4: - version "0.9.4" - resolved "https://registry.yarnpkg.com/bulma/-/bulma-0.9.4.tgz#0ca8aeb1847a34264768dba26a064c8be72674a1" - integrity sha512-86FlT5+1GrsgKbPLRRY7cGDg8fsJiP/jzTqXXVqiUZZ2aZT8uemEOHlU1CDU+TxklPEZ11HZNNWclRBBecP4CQ== - -"chokidar@>=3.0.0 <4.0.0": - version "3.5.3" - resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-3.5.3.tgz#1cf37c8707b932bd1af1ae22c0432e2acd1903bd" - integrity sha512-Dr3sfKRP6oTcjf2JmUmFJfeVMvXBdegxB0iVQ5eb2V10uFJUCAS8OByZdVAyVb8xXNz3GjjTgj9kLWsZTqE6kw== - dependencies: - anymatch "~3.1.2" - braces "~3.0.2" - glob-parent "~5.1.2" - is-binary-path "~2.1.0" - is-glob "~4.0.1" - normalize-path "~3.0.0" - readdirp "~3.6.0" - optionalDependencies: - fsevents "~2.3.2" - -fill-range@^7.0.1: - version "7.0.1" - resolved "https://registry.yarnpkg.com/fill-range/-/fill-range-7.0.1.tgz#1919a6a7c75fe38b2c7c77e5198535da9acdda40" - integrity sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ== - dependencies: - to-regex-range "^5.0.1" - -fsevents@~2.3.2: - version "2.3.2" - resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-2.3.2.tgz#8a526f78b8fdf4623b709e0b975c52c24c02fd1a" - integrity sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA== - -glob-parent@~5.1.2: - version "5.1.2" - resolved "https://registry.yarnpkg.com/glob-parent/-/glob-parent-5.1.2.tgz#869832c58034fe68a4093c17dc15e8340d8401c4" - integrity sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow== - dependencies: - is-glob "^4.0.1" - -immutable@^4.0.0: - version "4.1.0" - resolved "https://registry.yarnpkg.com/immutable/-/immutable-4.1.0.tgz#f795787f0db780183307b9eb2091fcac1f6fafef" - integrity sha512-oNkuqVTA8jqG1Q6c+UglTOD1xhC1BtjKI7XkCXRkZHrN5m18/XsnUp8Q89GkQO/z+0WjonSvl0FLhDYftp46nQ== - -is-binary-path@~2.1.0: - version "2.1.0" - resolved "https://registry.yarnpkg.com/is-binary-path/-/is-binary-path-2.1.0.tgz#ea1f7f3b80f064236e83470f86c09c254fb45b09" - integrity sha512-ZMERYes6pDydyuGidse7OsHxtbI7WVeUEozgR/g7rd0xUimYNlvZRE/K2MgZTjWy725IfelLeVcEM97mmtRGXw== - dependencies: - binary-extensions "^2.0.0" - -is-extglob@^2.1.1: - version "2.1.1" - resolved "https://registry.yarnpkg.com/is-extglob/-/is-extglob-2.1.1.tgz#a88c02535791f02ed37c76a1b9ea9773c833f8c2" - integrity sha512-SbKbANkN603Vi4jEZv49LeVJMn4yGwsbzZworEoyEiutsN3nJYdbO36zfhGJ6QEDpOZIFkDtnq5JRxmvl3jsoQ== - -is-glob@^4.0.1, is-glob@~4.0.1: - version "4.0.3" - resolved "https://registry.yarnpkg.com/is-glob/-/is-glob-4.0.3.tgz#64f61e42cbbb2eec2071a9dac0b28ba1e65d5084" - integrity sha512-xelSayHH36ZgE7ZWhli7pW34hNbNl8Ojv5KVmkJD4hBdD3th8Tfk9vYasLM+mXWOZhFkgZfxhLSnrwRr4elSSg== - dependencies: - is-extglob "^2.1.1" - -is-number@^7.0.0: - version "7.0.0" - resolved "https://registry.yarnpkg.com/is-number/-/is-number-7.0.0.tgz#7535345b896734d5f80c4d06c50955527a14f12b" - integrity sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng== - -normalize-path@^3.0.0, normalize-path@~3.0.0: - version "3.0.0" - resolved "https://registry.yarnpkg.com/normalize-path/-/normalize-path-3.0.0.tgz#0dcd69ff23a1c9b11fd0978316644a0388216a65" - integrity sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA== - -picomatch@^2.0.4, picomatch@^2.2.1: - version "2.3.1" - resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.3.1.tgz#3ba3833733646d9d3e4995946c1365a67fb07a42" - integrity sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA== - -readdirp@~3.6.0: - version "3.6.0" - resolved "https://registry.yarnpkg.com/readdirp/-/readdirp-3.6.0.tgz#74a370bd857116e245b29cc97340cd431a02a6c7" - integrity sha512-hOS089on8RduqdbhvQ5Z37A0ESjsqz6qnRcffsMU3495FuTdqSm+7bhJ29JvIOsBDEEnan5DPu9t3To9VRlMzA== - dependencies: - picomatch "^2.2.1" - -sass@^1.55.0: - version "1.55.0" - resolved "https://registry.yarnpkg.com/sass/-/sass-1.55.0.tgz#0c4d3c293cfe8f8a2e8d3b666e1cf1bff8065d1c" - integrity sha512-Pk+PMy7OGLs9WaxZGJMn7S96dvlyVBwwtToX895WmCpAOr5YiJYEUJfiJidMuKb613z2xNWcXCHEuOvjZbqC6A== - dependencies: - chokidar ">=3.0.0 <4.0.0" - immutable "^4.0.0" - source-map-js ">=0.6.2 <2.0.0" - -"source-map-js@>=0.6.2 <2.0.0": - version "1.0.2" - resolved "https://registry.yarnpkg.com/source-map-js/-/source-map-js-1.0.2.tgz#adbc361d9c62df380125e7f161f71c826f1e490c" - integrity sha512-R0XvVJ9WusLiqTCEiGCmICCMplcCkIwwR11mOSD9CR5u+IXYdiseeEuXCVAjS54zqwkLcPNnmU4OeJ6tUrWhDw== - -to-regex-range@^5.0.1: - version "5.0.1" - resolved "https://registry.yarnpkg.com/to-regex-range/-/to-regex-range-5.0.1.tgz#1648c44aae7c8d988a326018ed72f5b4dd0392e4" - integrity sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ== - dependencies: - is-number "^7.0.0"