-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #126 from fly-apps/125-add-litestream-as-an-option
Add --litestream as an option
- Loading branch information
Showing
16 changed files
with
227 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
LITESTREAM_CONFIG = ENV["LITESTREAM_CONFIG"] || Rails.root.join("tmp/litestream.yml").to_s | ||
|
||
LITESTREAM_TEMPLATE = <<-EOF | ||
# This is the configuration file for litestream. | ||
# | ||
# For more details, see: https://litestream.io/reference/config/ | ||
# | ||
dbs: | ||
<%% for db in @dbs -%> | ||
- path: <%%= db %> | ||
replicas: | ||
- type: s3 | ||
endpoint: $AWS_ENDPOINT_URL_S3 | ||
bucket: $BUCKET_NAME | ||
path: storage/<%%= File.basename(db) %> | ||
access-key-id: $AWS_ACCESS_KEY_ID | ||
secret-access-key: $AWS_SECRET_ACCESS_KEY | ||
<%% end -%> | ||
EOF | ||
|
||
namespace :litestream do | ||
task prepare: "db:load_config" do | ||
require "erubi" | ||
|
||
@dbs = | ||
ActiveRecord::Base | ||
.configurations | ||
.configs_for(env_name: "production", include_hidden: true) | ||
.select { |config| [ "sqlite3", "litedb" ].include? config.adapter } | ||
.map(&:database) | ||
|
||
result = eval(Erubi::Engine.new(LITESTREAM_TEMPLATE).src) | ||
|
||
unless File.exist?(LITESTREAM_CONFIG) && File.read(LITESTREAM_CONFIG) == result | ||
File.write(LITESTREAM_CONFIG, result) | ||
end | ||
|
||
@dbs.each do |db| | ||
next if File.exist?(db) or !ENV["BUCKET_NAME"] | ||
system "litestream restore -config #{LITESTREAM_CONFIG} -if-replica-exists #{db}" | ||
exit $?.exitstatus unless $?.exitstatus == 0 | ||
end | ||
end | ||
|
||
task :run do | ||
require "shellwords" | ||
|
||
exec(*%w[bundle exec litestream replicate -config], | ||
LITESTREAM_CONFIG, "-exec", Shellwords.join(ARGV[1..-1])) | ||
end | ||
end | ||
|
||
namespace :db do | ||
task prepare: "litestream:prepare" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# syntax = docker/dockerfile:1 | ||
|
||
# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile | ||
ARG RUBY_VERSION=xxx | ||
FROM ruby:$RUBY_VERSION-slim AS base | ||
|
||
# Rails app lives here | ||
WORKDIR /rails | ||
|
||
# Set production environment | ||
ENV BUNDLE_DEPLOYMENT="1" \ | ||
BUNDLE_PATH="/usr/local/bundle" \ | ||
BUNDLE_WITHOUT="development:test" \ | ||
RAILS_ENV="production" | ||
|
||
# Update gems and bundler | ||
RUN gem update --system --no-document && \ | ||
gem install -N bundler | ||
|
||
|
||
# Throw-away build stage to reduce size of final image | ||
FROM base AS build | ||
|
||
# Install packages needed to build gems | ||
RUN apt-get update -qq && \ | ||
apt-get install --no-install-recommends -y build-essential pkg-config | ||
|
||
# Install application gems | ||
COPY Gemfile Gemfile.lock ./ | ||
RUN bundle install && \ | ||
bundle exec bootsnap precompile --gemfile && \ | ||
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git | ||
|
||
# Copy application code | ||
COPY . . | ||
|
||
# Precompile bootsnap code for faster boot times | ||
RUN bundle exec bootsnap precompile app/ lib/ | ||
|
||
# Precompiling assets for production without requiring secret RAILS_MASTER_KEY | ||
RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile | ||
|
||
|
||
# Final stage for app image | ||
FROM base | ||
|
||
# Install packages needed for deployment | ||
RUN apt-get update -qq && \ | ||
apt-get install --no-install-recommends -y curl libsqlite3-0 && \ | ||
rm -rf /var/lib/apt/lists /var/cache/apt/archives | ||
|
||
# Copy built artifacts: gems, application | ||
COPY --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}" | ||
COPY --from=build /rails /rails | ||
|
||
# Run and own only the runtime files as a non-root user for security | ||
RUN groupadd --system --gid 1000 rails && \ | ||
useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \ | ||
mkdir /data && \ | ||
chown -R 1000:1000 db log storage tmp /data | ||
USER 1000:1000 | ||
|
||
# Deployment options | ||
ENV DATABASE_URL="sqlite3:///data/production.sqlite3" | ||
|
||
# Entrypoint prepares the database. | ||
ENTRYPOINT ["/rails/bin/docker-entrypoint"] | ||
|
||
# Start the server by default, this can be overwritten at runtime | ||
EXPOSE 80 | ||
VOLUME /data | ||
CMD ["./bin/rake", "litestream:run", "./bin/thrust", "./bin/rails", "server"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/bin/bash -e | ||
|
||
# If running the rails server then create or migrate existing database | ||
if [ "${@: -5:1}" == "./bin/rake" ] && [ "${@: -4:1}" == litestream:run ] && [ "${@: -3:1}" == "./bin/thrust" ] && [ "${@: -2:1}" == "./bin/rails" ] && [ "${@: -1:1}" == "server" ]; then | ||
./bin/rails db:prepare | ||
fi | ||
|
||
exec "${@}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
|
||
[env] | ||
PORT = "8080" | ||
|
||
[processes] | ||
app = "./script/litestream ./bin/rails server" | ||
|
||
[mounts] | ||
source = "data" | ||
destination = "/data" | ||
auto_extend_size_threshold = 80 | ||
auto_extend_size_increment = "1GB" | ||
auto_extend_size_limit = "10GB" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ def app_setup | |
system "bundle add litestack" | ||
end | ||
|
||
def test_sqlite3 | ||
def test_litestack | ||
check_dockerfile | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "base" | ||
|
||
class TestLitestream < TestBase | ||
@rails_options = "--database=sqlite3" | ||
@generate_options = "--litestream" | ||
|
||
def app_setup | ||
FileUtils.touch "fly.toml" | ||
end | ||
|
||
def test_litestream | ||
check_dockerfile | ||
check_entrypoint | ||
check_toml | ||
end | ||
end |