From 0bba5add810fb11693948dee1b0687bc20d1fd2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Busqu=C3=A9?= Date: Wed, 24 Feb 2021 15:40:02 +0100 Subject: [PATCH] Add docker-compose environment See solidusio/solidus#3947 for details on the decisions that have been taken. We are also decoupling the source of solidus & solidus_i18n dependencies, and providing more flexibility by allowing the specification of both repository & branch for each project (therefore allowing working with forks). --- .dockerdev/.psqlrc | 1 + .dockerdev/Dockerfile | 56 +++++++++++++++++++++++++++++++++++++ .gitignore | 1 + Gemfile | 28 +++++++++---------- bin/sandbox | 27 ++++++++++++++++++ docker-compose.yml | 64 +++++++++++++++++++++++++++++++++++++++++++ docs/development.md | 33 ++++++++++++++++++++-- spec/spec_helper.rb | 12 ++++++++ 8 files changed, 205 insertions(+), 17 deletions(-) create mode 100644 .dockerdev/.psqlrc create mode 100644 .dockerdev/Dockerfile create mode 100644 docker-compose.yml diff --git a/.dockerdev/.psqlrc b/.dockerdev/.psqlrc new file mode 100644 index 00000000..6a0fa9dc --- /dev/null +++ b/.dockerdev/.psqlrc @@ -0,0 +1 @@ +\set HISTFILE ~/history/psql_history diff --git a/.dockerdev/Dockerfile b/.dockerdev/Dockerfile new file mode 100644 index 00000000..3e7e9b64 --- /dev/null +++ b/.dockerdev/Dockerfile @@ -0,0 +1,56 @@ +ARG RUBY_VERSION +FROM ruby:$RUBY_VERSION-slim-buster + +ARG PG_VERSION +ARG MYSQL_VERSION +ARG NODE_VERSION +ARG BUNDLER_VERSION + +RUN apt-get update -qq \ + && DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \ + build-essential \ + gnupg2 \ + curl \ + git \ + imagemagick \ + libmariadb-dev \ + sqlite3 \ + libsqlite3-dev \ + chromium \ + && rm -rf /var/cache/apt/lists/* + +RUN curl -sSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - \ + && echo 'deb http://apt.postgresql.org/pub/repos/apt/ buster-pgdg main' $PG_VERSION > /etc/apt/sources.list.d/pgdg.list + +RUN apt-key adv --keyserver ha.pool.sks-keyservers.net --recv-keys 8C718D3B5072E1F5 \ + && echo "deb http://repo.mysql.com/apt/debian/ buster mysql-"$MYSQL_VERSION > /etc/apt/sources.list.d/mysql.list + +RUN curl -sSL https://deb.nodesource.com/setup_$NODE_VERSION.x | bash - + +RUN apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get -yq dist-upgrade && \ + DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \ + libpq-dev \ + postgresql-client-$PG_VERSION \ + mysql-client \ + nodejs \ + && rm -rf /var/lib/apt/lists/* + +ENV APP_USER=solidus_starter_frontend_user \ + LANG=C.UTF-8 \ + BUNDLE_JOBS=4 \ + BUNDLE_RETRY=3 +ENV GEM_HOME=/home/$APP_USER/gems +ENV APP_HOME=/home/$APP_USER/app +ENV PATH=$PATH:$GEM_HOME/bin + +RUN useradd -ms /bin/bash $APP_USER + +RUN gem update --system \ + && gem install bundler:$BUNDLER_VERSION \ + && chown -R $APP_USER:$(id -g $APP_USER) /home/$APP_USER/gems + +USER $APP_USER + +RUN mkdir -p /home/$APP_USER/history + +WORKDIR /home/$APP_USER/app diff --git a/.gitignore b/.gitignore index 2d8e06f4..22fd22c7 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,4 @@ spec/examples.txt .rvmrc .ruby-version .ruby-gemset +.env diff --git a/Gemfile b/Gemfile index f0473dfe..5fe3b0e5 100644 --- a/Gemfile +++ b/Gemfile @@ -3,26 +3,26 @@ source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } -branch = ENV.fetch('SOLIDUS_BRANCH', 'master') -gem 'solidus_core', github: 'solidusio/solidus', branch: branch -gem 'solidus_api', github: 'solidusio/solidus', branch: branch -gem 'solidus_backend', github: 'solidusio/solidus', branch: branch -gem 'solidus_sample', github: 'solidusio/solidus', branch: branch -gem 'solidus_i18n', github: 'solidusio/solidus_i18n', branch: branch +solidus_repo = ENV.fetch('SOLIDUS_REPO', 'solidusio/solidus') +solidus_branch = ENV.fetch('SOLIDUS_BRANCH', 'master') +solidus_i18n_repo = ENV.fetch('SOLIDUS_I18N_REPO', 'solidusio/solidus_i18n') +solidus_i18n_branch = ENV.fetch('SOLIDUS_I18N_BRANCH', 'master') +gem 'solidus_core', github: solidus_repo, branch: solidus_branch +gem 'solidus_api', github: solidus_repo, branch: solidus_branch +gem 'solidus_backend', github: solidus_repo, branch: solidus_branch +gem 'solidus_sample', github: solidus_repo, branch: solidus_branch +gem 'solidus_i18n', github: solidus_i18n_repo, branch: solidus_i18n_branch # Needed to help Bundler figure out how to resolve dependencies, # otherwise it takes forever to resolve them. # See https://github.com/bundler/bundler/issues/6677 gem 'rails', '>0.a' -case ENV['DB'] -when 'mysql' - gem 'mysql2' -when 'postgresql' - gem 'pg' -else - gem 'sqlite3' -end +gem 'mysql2' if ENV['DB'] == 'mysql' || ENV['DB_ALL'] + +gem 'pg' if ENV['DB'] == 'postgresql' || ENV['DB_ALL'] + +gem 'sqlite3' if !%w[mysql postgresql].include?(ENV['DB']) || ENV['DB_ALL'] gemspec diff --git a/bin/sandbox b/bin/sandbox index 8f822819..e1a2dc7b 100755 --- a/bin/sandbox +++ b/bin/sandbox @@ -5,9 +5,15 @@ set -e case "$DB" in postgres|postgresql) RAILSDB="postgresql" + HOST=${DB_POSTGRES_HOST:-${DB_HOST}} + USERNAME=$DB_USERNAME + PASSWORD=$DB_PASSWORD ;; mysql) RAILSDB="mysql" + HOST=${DB_MYSQL_HOST:-${DB_HOST}} + USERNAME=$DB_USERNAME + PASSWORD=$DB_PASSWORD ;; sqlite|'') RAILSDB="sqlite3" @@ -62,6 +68,27 @@ gem '$extension_name', path: '..' gem 'solidus_auth_devise' RUBY +replace_in_database_yml() { + if [ $RAILSDB = "postgresql" ]; then + sed -i.bck "/^ adapter:/a \ \ $1: $2" config/database.yml + elif [ $RAILSDB = "mysql" ]; then + sed -i.bck "s/^ $1:.*/\ \ $1: $2/" config/database.yml + fi + if [ -f config/database.yml.bck ]; then + rm -f config/database.yml.bck + fi +} + +if [ ${HOST} ]; then + replace_in_database_yml "host" $HOST +fi +if [ ${USERNAME} ]; then + replace_in_database_yml "username" $USERNAME +fi +if [ ${PASSWORD} ]; then + replace_in_database_yml "password" $PASSWORD +fi + unbundled bundle install --gemfile Gemfile unbundled bundle exec rake db:drop db:create diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..d78b9568 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,64 @@ +version: '3.7' + +services: + mysql: + image: mysql:8.0 + command: --default-authentication-plugin=mysql_native_password + environment: + MYSQL_ROOT_PASSWORD: password + volumes: + - mysql:/var/lib/mysql:cached + + postgres: + image: postgres:13.2 + environment: + POSTGRES_USER: root + POSTGRES_PASSWORD: password + POSTGRES_DB: solidus_starter_frontend_solidus_test + volumes: + - postgres:/var/lib/postgresql/data:cached + + app: + build: + context: .dockerdev + dockerfile: Dockerfile + args: + RUBY_VERSION: "2.7.2" + PG_VERSION: 13 + NODE_VERSION: 14 + MYSQL_VERSION: "8.0" + BUNDLER_VERSION: 2 + image: solidus_starter_frontend-0.1.0 + command: bash -c "(bundle check || bundle) && tail -f /dev/null" + environment: + CAPYBARA_JS_DRIVER: apparition_docker_friendly + DB_USERNAME: root + DB_PASSWORD: password + DB_ALL: "1" + DB_MYSQL_HOST: mysql + DB_POSTGRES_HOST: postgres + HISTFILE: "/home/solidus_starter_frontend_user/history/bash_history" + MYSQL_HISTFILE: "/home/solidus_starter_frontend_user/history/mysql_history" + # TODO: Remove repo/branch config when PR is merged + SOLIDUS_REPO: 'nebulab/solidus' + SOLIDUS_BRANCH: 'docker' + ports: + - "${SANDBOX_PORT:-3000}:${SANDBOX_PORT:-3000}" + volumes: + - .:/home/solidus_starter_frontend_user/app:delegated + - bundle:/home/solidus_starter_frontend_user/gems:cached + - history:/home/solidus_starter_frontend_user/history:cached + - .dockerdev/.psqlrc:/home/solidus_starter_frontend_user/.psqlrc:cached + tty: true + stdin_open: true + tmpfs: + - /tmp + depends_on: + - mysql + - postgres + +volumes: + bundle: + history: + postgres: + mysql: diff --git a/docs/development.md b/docs/development.md index 869f9b52..388ee100 100644 --- a/docs/development.md +++ b/docs/development.md @@ -3,9 +3,10 @@ This document aims to give some extra information for developers that are going to contribute to our `solidus_starter_frontend` component. ### Testing the extension -First bundle your dependencies, then run `bin/rake`. `bin/rake` will default to -building the dummy app if it does not exist, then it will run specs. The dummy -app can be regenerated by using `bin/rake extension:test_app`. +First bundle your dependencies (or use [docker](#docker-development)), then run +`bin/rake`. `bin/rake` will default to building the dummy app if it does not +exist, then it will run specs. The dummy app can be regenerated by using +`bin/rake extension:test_app`. ```shell bin/rake @@ -84,3 +85,29 @@ The tool internally works in this way: - remote GIT source is updated using the parameters provided by the config file; - compare process is executed and a hash for each file is calculated; - if they match the hashes saved in the configuration there are no differences. + +### Docker development + +If you are a docker user you can wake up the project as usual with: + +```bash +docker-compose up -d +``` + +Wait for all the dependencies to be installed. You can check progress with `docker-compose logs -f app`. + +Then you can dispatch commands to the `app` container: + +```bash +docker-compose exec app bin/rake +``` + +When running the [sandbox application](#running-the-sandbox), take into account +that you need to bind to `0.0.0.0`. By default, port `3000` is exposed but you +can changed it through `SANDBOX_PORT` environment variable: + +```bash +SANDBOX_PORT=4000 docker-compose up -d +docker-compose exec app bin/sandbox +docker-compose exec app bin/rails server --binding 0.0.0.0 --port 4000 +``` diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d8f09d92..06fb45ca 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -26,6 +26,18 @@ SolidusDevSupport::TestingSupport::Factories.load_for(SolidusStarterFrontend::Engine) +Capybara.register_driver :apparition_docker_friendly do |app| + opts = { + headless: true, + browser_options: [ + :no_sandbox, + :disable_gpu, + { disable_features: 'VizDisplayCompositor' } + ] + } + Capybara::Apparition::Driver.new(app, opts) +end + RSpec.configure do |config| config.infer_spec_type_from_file_location! config.use_transactional_fixtures = false