Closed9

Rails+LiteFSを試す

たがみだいきたがみだいき
$ fly launch

にプロンプトが出てyを押すとFlyioの画面に飛ぶのでそこでNameなどをuniqueなものにする必要がある

たがみだいきたがみだいき

bundle install のときに pg gempg_config が見つからないというエラーで止まったので以下のように対応

$ brew install libpg
$ bundle config build.pg --with-pg-config="$(brew --prefix libpq)/bin/pg_config"

これでとおった

たがみだいきたがみだいき

fly deploy 後に fly apps open するとRailsの見慣れたエラーが出た。
以下のDockerfileとfly.tomlでやってるけど fly deploy するとDB内が空になる?よう。

Dockerfile
# syntax = docker/dockerfile:1

# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
ARG RUBY_VERSION=3.3.0
FROM ruby:$RUBY_VERSION-slim as base

LABEL fly_launch_runtime="rails"

# 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 libpq-dev pkg-config

# Install application gems
COPY --link 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 --link . .

# 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, configure litefs
COPY --from=flyio/litefs:0.4.0 /usr/local/bin/litefs /usr/local/bin/litefs
COPY --link config/litefs.yml /etc/litefs.yml

# Install packages needed for deployment
RUN apt-get update -qq && \
    apt-get install --no-install-recommends -y ca-certificates curl fuse3 libsqlite3-0 postgresql-client sudo && \
    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 && \
    chown -R 1000:1000 db log storage tmp

# Authorize rails user to launch litefs
COPY <<-"EOF" /etc/sudoers.d/rails
rails ALL=(root) /usr/local/bin/litefs
EOF

# Deployment options
ENV PORT="3001"

# Entrypoint sets up the container.
ENTRYPOINT ["/rails/bin/docker-entrypoint"]

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD ["./bin/rails", "server"]
fly.toml
# fly.toml app configuration file generated for list-sample-litefs on 2024-02-06T20:50:04+09:00
#
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
#

app = 'list-sample-litefs'
primary_region = 'nrt'
console_command = '/rails/bin/rails console'

[build]

[http_service]
  internal_port = 3000
  force_https = true
  auto_stop_machines = false
  auto_start_machines = true
  min_machines_running = 0
  processes = ['app']

[checks]
  [checks.status]
    port = 3000
    type = 'http'
    interval = '10s'
    timeout = '2s'
    grace_period = '5s'
    method = 'GET'
    path = '/up'
    protocol = 'http'
    tls_skip_verify = false

    [checks.status.headers]
      X-Forwarded-Proto = 'https'

[[vm]]
  cpu_kind = 'shared'
  cpus = 1
  memory_mb = 1024

[[statics]]
  guest_path = '/rails/public'
  url_prefix = '/'
[deploy]
  release_command = "./bin/rails db:prepare"

SQLite3::SQLException: no such table: names (ActiveRecord::StatementInvalid) というエラーが出たので以下のように対応

$ flyctl ssh console -s
(該当VM選択)
# ./bin/rails db:prepare

fly.toml のrelease_commandで指定しているけど何故かうまく動かない。
そもそも2つのVMで db:prepare を動かさないと動かない。LiteFSが使えていない?

たがみだいきたがみだいき

fly launch 時にPostgreSQLを使うようになっていたのでDatabaseNoを選択する

以下コマンド実行時に以下の文言が現れた

$ bin/rails generate dockerfile --litefs
...
     execute  flyctl consul attach
Secrets are staged for the first deployment

こっちの記事に flyctl consul attach を実行してって書いてあったけどさっきまではこの文言はなかった気がするので fly launch 時の設定がミスっていたようだ
https://community.fly.io/t/v2-migration-gone-wrong/13884/3

たがみだいきたがみだいき

無事チュートリアルのとおりに動いた!よかった。
machineの1つを日本、もう1つはロンドンにしてそれぞれsshすると日本のときはスムーズに動くけどロンドンのときは少しラグっていたのは距離を感じられてよかった。
flyioは日本リージョンがあるのがいいところなので使っていきたい

このスクラップは3ヶ月前にクローズされました