diff --git a/.dockerignore b/.dockerignore index b2decdb69..fe69a29dd 100644 --- a/.dockerignore +++ b/.dockerignore @@ -41,3 +41,7 @@ Capfile config/deploy config/deploy.rb Gemfile.capistrano* + +Dockerfile +Dockerfile-dev +docker-compose.yml diff --git a/.gitignore b/.gitignore index 185b2f74c..ec1dff9ef 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ public/system public/uploads storage vendor/bundle +swagger # no configuration config/*.yml diff --git a/Dockerfile b/Dockerfile index e8f6a4c00..967de0e47 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,52 +1,75 @@ -FROM ruby:2.7 - -RUN supercronicUrl=https://github.com/aptible/supercronic/releases/download/v0.1.3/supercronic-linux-amd64 && \ - supercronicBin=/usr/local/bin/supercronic && \ - supercronicSha1sum=96960ba3207756bb01e6892c978264e5362e117e && \ - curl -fsSL -o "$supercronicBin" "$supercronicUrl" && \ - echo "$supercronicSha1sum $supercronicBin" | sha1sum -c - && \ - chmod +x "$supercronicBin" +FROM ruby:2.7 as base ENV PORT=3000 \ SMTP_SERVER_PORT=2525 \ - RAILS_ENV=production \ RAILS_LOG_TO_STDOUT=true \ RAILS_SERVE_STATIC_FILES=true WORKDIR /usr/src/app -COPY . ./ - # install dependencies and generate crontab -RUN buildDeps='libmagic-dev' && \ +RUN buildDeps='libmagic-dev nodejs chromium' && \ + export DEBIAN_FRONTEND=noninteractive && \ apt-get update && \ apt-get install --no-install-recommends -y $buildDeps && \ echo 'gem: --no-document' >> ~/.gemrc && \ gem install bundler && \ - bundle config build.nokogiri "--use-system-libraries" && \ - bundle install --deployment --without development test -j 4 && \ - apt-get purge -y --auto-remove $buildDeps && \ - rm -Rf /var/lib/apt/lists/* /var/cache/apt/* ~/.gemrc ~/.bundle && \ - \ - bundle exec whenever >crontab + bundle config build.nokogiri "--use-system-libraries" -# compile assets with temporary mysql server -RUN export DATABASE_URL=mysql2://localhost/temp?encoding=utf8 && \ - export SECRET_KEY_BASE=thisisnotimportantnow && \ - export DEBIAN_FRONTEND=noninteractive && \ - apt-get update && \ - apt-get install -y mariadb-server nodejs && \ - /etc/init.d/mariadb start && \ - mariadb -e "CREATE DATABASE temp" && \ - cp config/app_config.yml.SAMPLE config/app_config.yml && \ + +COPY bin bin +COPY plugins plugins +COPY Gemfile Gemfile.lock docker-entrypoint.sh ./ + +# Development +FROM base as development +ENV RAILS_ENV=development \ + CHROMIUM_FLAGS=--no-sandbox + +RUN bundle install + +COPY . ./ + +# generate api spec file using nulldb adapter +RUN cp config/database.yml.NULLDB_SAMPLE config/database.yml && \ + RAILS_ENV=test DB_ADAPTER=nulldb bundle exec rake rswag:specs:swaggerize && \ + rm config/database.yml + +ENTRYPOINT ["./docker-entrypoint.sh"] +CMD ["./proc-start", "web"] + + +# Production + +FROM base as production + +ENV RAILS_ENV=production + +RUN supercronicUrl=https://github.com/aptible/supercronic/releases/download/v0.1.3/supercronic-linux-amd64 && \ + supercronicBin=/usr/local/bin/supercronic && \ + supercronicSha1sum=96960ba3207756bb01e6892c978264e5362e117e && \ + curl -fsSL -o "$supercronicBin" "$supercronicUrl" && \ + echo "$supercronicSha1sum $supercronicBin" | sha1sum -c - && \ + chmod +x "$supercronicBin" + +RUN bundle config set deployment 'true' && \ + bundle install --without development test + +COPY . ./ +COPY --from=development /usr/src/app/swagger/v1/swagger.yaml /usr/src/app/swagger/v1/swagger.yaml + +# copy sample configs +RUN cp config/app_config.yml.SAMPLE config/app_config.yml && \ cp config/database.yml.MySQL_SAMPLE config/database.yml && \ - cp config/storage.yml.SAMPLE config/storage.yml && \ - bundle exec rake db:setup assets:precompile && \ - rm -Rf tmp/* && \ - /etc/init.d/mariadb stop && \ - rm -Rf /run/mysqld /tmp/* /var/tmp/* /var/lib/mysql /var/log/mysql* && \ - apt-get purge -y --auto-remove mariadb-server && \ - rm -Rf /var/lib/apt/lists/* /var/cache/apt/* + cp config/storage.yml.SAMPLE config/storage.yml + +# precompile assets +RUN SECRET_KEY_BASE=42 bundle exec rake assets:precompile + +# Cleanup +RUN apt-get purge -y --auto-remove $buildDeps && \ + rm -Rf tmp/* /var/lib/apt/lists/* /var/cache/apt/* ~/.gemrc ~/.bundle && \ + bundle exec whenever >crontab # Make relevant dirs and files writable for app user RUN mkdir -p tmp storage && \ @@ -61,6 +84,6 @@ EXPOSE 3000 VOLUME /usr/src/app/storage -# cleanup, and by default start web process from Procfile +# by default start web process from Procfile ENTRYPOINT ["./docker-entrypoint.sh"] CMD ["./proc-start", "web"] diff --git a/Dockerfile-dev b/Dockerfile-dev deleted file mode 100644 index 37dce5f6b..000000000 --- a/Dockerfile-dev +++ /dev/null @@ -1,29 +0,0 @@ -FROM ruby:2.7 - -# Install dependencies -RUN deps='libmagic-dev chromium nodejs' && \ - apt-get update && \ - apt-get install --no-install-recommends -y $deps && \ - rm -Rf /var/lib/apt/lists/* /var/cache/apt/* - -ENV PORT=3000 \ - SMTP_SERVER_PORT=2525 \ - RAILS_ENV=development \ - RAILS_LOG_TO_STDOUT=true \ - RAILS_SERVE_STATIC_FILES=true \ - \ - CHROMIUM_FLAGS=--no-sandbox \ - \ - BUNDLE_PATH=/usr/local/bundle \ - BUNDLE_APP_CONFIG=/usr/local/bundle/config - -WORKDIR /app - -RUN gem install bundler -RUN bundle config build.nokogiri "--use-system-libraries" - -EXPOSE 3000 - -# cleanup, and by default start web process from Procfile -ENTRYPOINT ["./docker-entrypoint.sh"] -CMD ["./proc-start", "web"] diff --git a/Gemfile b/Gemfile index 97422021a..5483f0c28 100644 --- a/Gemfile +++ b/Gemfile @@ -125,3 +125,5 @@ group :test do # api gem 'rswag-specs' end + +gem "activerecord-nulldb-adapter", "~> 0.9.0" diff --git a/Gemfile.lock b/Gemfile.lock index c66901cfd..9aa50e788 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -128,6 +128,8 @@ GEM activerecord (7.0.4) activemodel (= 7.0.4) activesupport (= 7.0.4) + activerecord-nulldb-adapter (0.9.0) + activerecord (>= 5.2.0, < 7.1) activestorage (7.0.4) actionpack (= 7.0.4) activejob (= 7.0.4) @@ -605,6 +607,7 @@ PLATFORMS DEPENDENCIES active_model_serializers (~> 0.10.0) + activerecord-nulldb-adapter (~> 0.9.0) acts_as_tree acts_as_versioned! apparition diff --git a/app/lib/foodsoft_config.rb b/app/lib/foodsoft_config.rb index c7dda5906..c1fc575c9 100644 --- a/app/lib/foodsoft_config.rb +++ b/app/lib/foodsoft_config.rb @@ -116,7 +116,7 @@ def select_multifoodcoop(foodcoop) # @param key [String, Symbol] # @return [Object] Value of the key. def [](key) - if RailsSettings::CachedSettings.table_exists? && allowed_key?(key) + if ActiveRecord::Base.connected? && RailsSettings::CachedSettings.table_exists? && allowed_key?(key) value = RailsSettings::CachedSettings["foodcoop.#{scope}.#{key}"] value = config[key] if value.nil? value diff --git a/config/application.rb b/config/application.rb index 696d66477..3ed7d4ee9 100644 --- a/config/application.rb +++ b/config/application.rb @@ -58,10 +58,6 @@ class Application < Rails::Application # Version of your assets, change this if you want to expire all your assets config.assets.version = '1.0' - # It would be nice not to enable database connection when precompiling assets, - # but i18n-js requires initialization, that's why it's on. - config.assets.initialize_on_precompile = true - # Load legacy scripts from vendor config.assets.precompile += ['vendor/assets/javascripts/*.js'] diff --git a/config/database.yml.NULLDB_SAMPLE b/config/database.yml.NULLDB_SAMPLE new file mode 100644 index 000000000..a758382c6 --- /dev/null +++ b/config/database.yml.NULLDB_SAMPLE @@ -0,0 +1,2 @@ +test: + adapter: nulldb diff --git a/docker-compose-dev.yml b/docker-compose-dev.yml index b0a325db4..b272265df 100644 --- a/docker-compose-dev.yml +++ b/docker-compose-dev.yml @@ -10,12 +10,12 @@ services: foodsoft_worker: build: context: . - dockerfile: Dockerfile-dev + target: development platform: linux/x86_64 command: ./proc-start worker volumes: - bundle:/usr/local/bundle - - .:/app + - .:/usr/src/app environment: - DATABASE_URL=mysql2://root:secret@mariadb/development?encoding=utf8mb4 - REDIS_URL=redis://redis:6379