From ed8785618942145451b556fa8ee0f17219e824cf Mon Sep 17 00:00:00 2001 From: Technocoder <8334328+Techno-coder@users.noreply.github.com> Date: Fri, 14 Feb 2020 20:28:34 +1300 Subject: [PATCH 1/7] Update README and add Dockerfile --- Dockerfile | 46 ++++++++++++++++++++++++++++++++ README.rdoc | 77 +++++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 115 insertions(+), 8 deletions(-) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..da2225aa --- /dev/null +++ b/Dockerfile @@ -0,0 +1,46 @@ +# Build invocation: +# May need to be executed with --memory-swap -1 or else out of memory error +# docker build --memory-swap -1 . -t nztrain:latest + +# Execution invocation: +# Privileged mode is required for control groups +# Must be invoked with '--network host' for port exposure +# docker --privileged --network host -it nztrain:latest +# to detach + +FROM drecom/ubuntu-ruby:2.3.8 +RUN apt-get update && apt-get install -y sudo git libpq-dev software-properties-common +# Services cannot be started within Docker build environment +RUN printf "#!/bin/sh\nexit 0" > /usr/sbin/policy-rc.d + +WORKDIR /nztrain +COPY Gemfile* ./ +COPY script ./script +RUN bash script/install/maxmind.bash + +# Bundle installation is invoked in advance to avoid +# prompt at installation script +RUN bundle install --deployment + +RUN RAILS_ENV=development DATABASE_USERNAME=root DATABASE=nztrain \ + TEST_DATABASE=nztraintest APP_NAME=nztrain USER=root \ + APP_USER=root UNICORN_PORT= REDIS_HOST=localhost REDIS_PORT=6379 \ + REDIS_PASS=@/etc/redis/redis.conf REDIS_INSTALL=true \ + SERVER_NAME=_ bash script/install/config.bash --defaults + +COPY config ./config +# Avoid updating as database servers are not live yet +RUN yes | update=false bash script/install.bash + +COPY . . +RUN service postgresql start; \ + service redis start; \ + bash script/update.bash; \ + rm /var/run/redis/redis.pid; \ + service postgresql stop + +CMD service redis start; \ + service postgresql start; \ + bundle exec rails server -d; \ + bundle exec rake qless:work + diff --git a/README.rdoc b/README.rdoc index 50abc467..f3563f29 100644 --- a/README.rdoc +++ b/README.rdoc @@ -1,15 +1,76 @@ == NZTrain -Status: {Build Status}[http://travis-ci.org/NZOI/nztrain] {Coverage Status}[https://coveralls.io/r/NZOI/nztrain] {}[https://codeclimate.com/github/NZOI/nztrain] -Workflow: {WIP Issues}[http://waffle.io/NZOI/nztrain] +{Build Status}[http://travis-ci.org/NZOI/nztrain] {Coverage Status}[https://coveralls.io/r/NZOI/nztrain] {}[https://codeclimate.com/github/NZOI/nztrain] -=== Installation -Run `script/install.bash` to install dependencies. +=== Docker Installation +This requires the Docker engine. -`script/update.bash` will pull changes from origin and recompile various things. This includes assets - assuming that it is for production purposes. +First, build the image: -Both scripts depends on `script/install.cfg`, which is built using `script/install/config.bash` automatically. If `script/install.cfg` is incomplete (eg. new configuration is added), `script/install.bash` and `script/update.bash` will prompt for the new configurations required. + $ docker build --memory-swap -1 . -t nztrain:latest + +Then, execute the image: + + $ docker --privileged --network host -it nztrain:latest + +The website will be served over port +3000+. + +=== Manual Installation +This requires Ubuntu Xenial (16.04). + +Invoke the installation script within this repository: + + $ git clone https://github.com/NZOI/nztrain.git + $ cd nztrain + $ ./script/install.bash + +Select all default configuration options by pressing enter repeatedly. + +You will be prompted to select an option to install Ruby. Choose to install +rbenv+ (the fourth option). Then, restart the session and invoke the installation script again: + + $ bash + $ ./script/install.bash + +You will be prompted to select an option to install Ruby *again*. Choose to install either +RVM+ or +rbenv+. All prompts after this are package installation prompts. You may wish to quit the installation script and invoke it with +yes+. + + $ yes | ./script/install.bash + +Finally, run the website: + + $ bundle exec rails server + $ bundle exec rake qless:work + +Each of these commands must be kept alive. This can be achieved by running each command in a separate terminal session. Otherwise, you may wish to daemonize (run in the background) the commands: + + $ bundle exec rails server -d + +The website will be served over port +3000+. + +=== System User +This setups a system user for the website. + +First open a Ruby console: + + $ bundle exec rails console + +Then, create a new user with these commands: + + user = User.find(0) + Role.find_by_name("superadmin").users.push(user) + user.confirm! + require 'io/console' + user.reset_password!(STDIN.getpass("Password: "), STDIN.getpass("Confirm: ")) + +You will be prompted to enter a password twice. The invocation should return true if the user creation was successful. Otherwise, the password may be too short. + +=== Updating +The update script will recompile and pull changes from origin: + + $ cd nztrain + $ ./script/update.bash + +Both the installation and update script depends on the configuration script: +script/install.cfg+ which is built using +script/install/config.bash+ automatically. If +script/install.cfg+ is incomplete (for example, a new configuration is added) then new configurations will be prompted. === Development Tools -- `spring` instead of `bundle exec` to keep a background copy of rails running, and avoid startup time. -- `unicorn` starts of a development server (instead of Webrick). +- Use +spring+ instead of bundle to keep a background copy of Rails running and avoid startup time. +- +unicorn+ starts a development server (instead of Webrick). From 88f636605a69f6aebd5f43a471d063383e037799 Mon Sep 17 00:00:00 2001 From: Technocoder <8334328+Techno-coder@users.noreply.github.com> Date: Fri, 14 Feb 2020 21:12:00 +1300 Subject: [PATCH 2/7] Update Dockerfile to set locale to en_US.UTF-8 --- Dockerfile | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index da2225aa..19b78263 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,7 +9,14 @@ # to detach FROM drecom/ubuntu-ruby:2.3.8 -RUN apt-get update && apt-get install -y sudo git libpq-dev software-properties-common +RUN apt-get update && apt-get install -y sudo git libpq-dev software-properties-common locales + +# Replace Japanese locale with English +RUN echo 'en_US.UTF-8 UTF-8' > /etc/locale.gen && locale-gen +ENV LANG en_US.UTF-8 +ENV LANGUAGE en_US:en +ENV LC_ALL en_US.UTF-8 + # Services cannot be started within Docker build environment RUN printf "#!/bin/sh\nexit 0" > /usr/sbin/policy-rc.d From 68a6402879847b5fa69d1523ea97c6de6c7e6fc8 Mon Sep 17 00:00:00 2001 From: Technocoder <8334328+Techno-coder@users.noreply.github.com> Date: Sat, 15 Feb 2020 00:09:37 +1300 Subject: [PATCH 3/7] Fix and add Docker execution and inspection commands --- README.rdoc | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/README.rdoc b/README.rdoc index f3563f29..9038b5a1 100644 --- a/README.rdoc +++ b/README.rdoc @@ -4,16 +4,24 @@ === Docker Installation This requires the Docker engine. -First, build the image: +First build the image: $ docker build --memory-swap -1 . -t nztrain:latest -Then, execute the image: +Then execute the image: - $ docker --privileged --network host -it nztrain:latest + $ docker run --privileged --network host -it nztrain:latest The website will be served over port +3000+. +To inspect the running container identify the container name: + + $ docker ps + +Then execute an interactive bash shell in the container: + + $ docker exec -it /bin/bash + === Manual Installation This requires Ubuntu Xenial (16.04). From 358b6f2b02d66cfbbf2679cd82f101b0b16eb4f6 Mon Sep 17 00:00:00 2001 From: Technocoder <8334328+Techno-coder@users.noreply.github.com> Date: Sat, 15 Feb 2020 14:34:29 +1300 Subject: [PATCH 4/7] Replace base image with inbuilt Ruby --- Dockerfile | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/Dockerfile b/Dockerfile index 19b78263..cb77f7e4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,25 +5,34 @@ # Execution invocation: # Privileged mode is required for control groups # Must be invoked with '--network host' for port exposure -# docker --privileged --network host -it nztrain:latest +# docker run --privileged --network host -it nztrain:latest # to detach -FROM drecom/ubuntu-ruby:2.3.8 -RUN apt-get update && apt-get install -y sudo git libpq-dev software-properties-common locales - -# Replace Japanese locale with English -RUN echo 'en_US.UTF-8 UTF-8' > /etc/locale.gen && locale-gen -ENV LANG en_US.UTF-8 -ENV LANGUAGE en_US:en -ENV LC_ALL en_US.UTF-8 +FROM ubuntu:xenial # Services cannot be started within Docker build environment RUN printf "#!/bin/sh\nexit 0" > /usr/sbin/policy-rc.d +RUN apt-get update && apt-get install -y sudo git \ + libpq-dev software-properties-common curl + +# Install RVM +RUN gpg --keyserver hkp://keys.gnupg.net --recv-keys \ + 409B6B1796C275462A1703113804BB82D39DC0E3 \ + 7D2BAF1CF37B13E2069D6956105BD0E739499BDB +RUN curl -L https://get.rvm.io | bash -s stable +ENV PATH="/usr/local/rvm/bin/:${PATH}" +SHELL ["/bin/bash", "-l", "-c"] + +# Install Ruby 2.3.8 +RUN rvm requirements +RUN rvm install 2.3.8 +RUN gem install bundler --no-ri --no-rdoc WORKDIR /nztrain COPY Gemfile* ./ COPY script ./script RUN bash script/install/maxmind.bash +RUN yes | bash script/install/imagemagick.bash # Bundle installation is invoked in advance to avoid # prompt at installation script From 3ac4e4e92c369c45a0657db427361ac672793197 Mon Sep 17 00:00:00 2001 From: Technocoder <8334328+Techno-coder@users.noreply.github.com> Date: Sat, 15 Feb 2020 18:25:16 +1300 Subject: [PATCH 5/7] Move bundle installation after system dependencies --- Dockerfile | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/Dockerfile b/Dockerfile index cb77f7e4..230c830d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,7 +13,14 @@ FROM ubuntu:xenial # Services cannot be started within Docker build environment RUN printf "#!/bin/sh\nexit 0" > /usr/sbin/policy-rc.d RUN apt-get update && apt-get install -y sudo git \ - libpq-dev software-properties-common curl + libpq-dev software-properties-common curl locales + +# Set locales for database +ENV LANG en_US.UTF-8 +ENV LANGUAGE en_US:en +ENV LC_ALL en_US.UTF-8 +RUN echo 'en_US.UTF-8 UTF-8' > /etc/locale.gen && locale-gen +RUN update-locale LANG=en_US.UTF-8 # Install RVM RUN gpg --keyserver hkp://keys.gnupg.net --recv-keys \ @@ -29,25 +36,23 @@ RUN rvm install 2.3.8 RUN gem install bundler --no-ri --no-rdoc WORKDIR /nztrain -COPY Gemfile* ./ COPY script ./script RUN bash script/install/maxmind.bash RUN yes | bash script/install/imagemagick.bash -# Bundle installation is invoked in advance to avoid -# prompt at installation script -RUN bundle install --deployment - +COPY config ./config RUN RAILS_ENV=development DATABASE_USERNAME=root DATABASE=nztrain \ TEST_DATABASE=nztraintest APP_NAME=nztrain USER=root \ APP_USER=root UNICORN_PORT= REDIS_HOST=localhost REDIS_PORT=6379 \ REDIS_PASS=@/etc/redis/redis.conf REDIS_INSTALL=true \ SERVER_NAME=_ bash script/install/config.bash --defaults - -COPY config ./config # Avoid updating as database servers are not live yet RUN yes | update=false bash script/install.bash +COPY Gemfile* ./ +# Bundle installation is invoked in advance to avoid prompt +RUN bundle install --deployment + COPY . . RUN service postgresql start; \ service redis start; \ From abea83aaca314863120f8a56ade7cf7c2f04e3c6 Mon Sep 17 00:00:00 2001 From: Technocoder <8334328+Techno-coder@users.noreply.github.com> Date: Sat, 15 Feb 2020 18:35:19 +1300 Subject: [PATCH 6/7] Add actions workflow and cache Docker images --- .github/workflows/main.yml | 35 +++++++++++++++++++++++++++++++++++ .travis.yml | 21 --------------------- Gemfile | 3 ++- Gemfile.lock | 15 ++++----------- spec/spec_helper.rb | 7 +++++-- 5 files changed, 46 insertions(+), 35 deletions(-) create mode 100644 .github/workflows/main.yml delete mode 100644 .travis.yml diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 00000000..79909878 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,35 @@ +name: Docker Build +on: [push, pull_request] + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: whoan/docker-build-with-cache-action@v3 + with: + # Secrets are specified in the repository settings + # A Docker account is required for registry caching + username: "${{ secrets.DOCKER_USERNAME }}" + password: "${{ secrets.DOCKER_PASSWORD }}" + image_name: "${{ secrets.DOCKER_USERNAME }}/nztrain" + + - name: Run tests + run: > + docker run --privileged --network host --name container + "${{ secrets.DOCKER_USERNAME }}/nztrain:latest" + /bin/bash -l -c " + service redis start; + service postgresql start; + bundle exec rake db:test:load; + bundle exec rspec" + + - name: Copy coverage data + run: docker cp container:/nztrain/coverage/lcov/nztrain.lcov . + + - name: Coveralls + uses: coverallsapp/github-action@master + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + path-to-lcov: ./nztrain.lcov + diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 33e14ee1..00000000 --- a/.travis.yml +++ /dev/null @@ -1,21 +0,0 @@ -language: ruby -rvm: - - "2.3.8" -services: - - redis-server - - postgresql - -# uncomment this line if your project needs to run something other than `rake`: -#before_script: # for Selenium RSpec tests -# - "export DISPLAY=:99.0" -# - "sh -e /etc/init.d/xvfb start" - -before_install: - - bash script/install/maxmind.bash - -before_script: - - RAILS_ENV=test DATABASE_USERNAME=postgres DATABASE= TEST_DATABASE=nztraintest REDIS_INSTALL=false REDIS_PASS= SCHEDULE_BACKUPS=0 ISOLATE_ROOT=/ ISOLATE_CGROUPS=false ISOLATE_BRANCH=nztrain.travis.ci bash script/install/config.bash --defaults - - AUTOCONFIRM=true script/install.bash --skip-update - - bundle exec rake db:test:load -script: bundle exec rspec - diff --git a/Gemfile b/Gemfile index 5cbdc895..c4f1f7a8 100644 --- a/Gemfile +++ b/Gemfile @@ -71,7 +71,8 @@ gem 'sinatra' # Monitoring gem 'newrelic_rpm' -gem 'coveralls', require: false +gem 'simplecov', require: false +gem 'simplecov-lcov', require: false group :development do gem 'better_errors' diff --git a/Gemfile.lock b/Gemfile.lock index b082ec3d..8dae45a1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -127,12 +127,6 @@ GEM country_select (3.1.1) countries (~> 2.0) sort_alphabetical (~> 1.0) - coveralls (0.8.21) - json (>= 1.8, < 3) - simplecov (~> 0.14.1) - term-ansicolor (~> 1.3) - thor (~> 0.19.4) - tins (~> 1.6) crass (1.0.5) daemons (1.3.1) debug_inspector (0.0.3) @@ -348,6 +342,7 @@ GEM json (>= 1.8, < 3) simplecov-html (~> 0.10.0) simplecov-html (0.10.2) + simplecov-lcov (0.7.0) sinatra (1.4.8) rack (~> 1.5) rack-protection (~> 1.4) @@ -377,8 +372,6 @@ GEM request_store (~> 1.0.3) strong_attributes (~> 0.0.2) superfish-rails (1.6.0.1) - term-ansicolor (1.6.0) - tins (~> 1.0) therubyracer (0.12.3) libv8 (~> 3.16.14.15) ref @@ -389,7 +382,6 @@ GEM thor (0.19.4) thread_safe (0.3.6) tilt (2.0.8) - tins (1.16.3) ttfunk (1.5.1) tzinfo (0.3.53) uglifier (4.1.2) @@ -431,7 +423,6 @@ DEPENDENCIES connection_pool countries country_select - coveralls devise (~> 3.2.2) facebox-rails factory_girl @@ -477,6 +468,8 @@ DEPENDENCIES sass-rails simple-navigation (= 3.11.0) simple_form (= 3.0.1) + simplecov + simplecov-lcov sinatra spring squeel @@ -491,4 +484,4 @@ DEPENDENCIES yui-compressor BUNDLED WITH - 1.16.1 + 1.16.2 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 85b242de..3f268933 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,5 +1,8 @@ -require 'coveralls' -Coveralls.wear!('rails') +require 'simplecov' +require 'simplecov-lcov' +SimpleCov::Formatter::LcovFormatter.config.report_with_single_file = true +SimpleCov.formatter = SimpleCov::Formatter::LcovFormatter +SimpleCov.start 'rails' # This file is copied to spec/ when you run 'rails generate rspec:install' ENV["RAILS_ENV"] ||= 'test' From ca828548b722f67b1eb8dbd7543a1d9e3903f5d7 Mon Sep 17 00:00:00 2001 From: Technocoder <8334328+Techno-coder@users.noreply.github.com> Date: Sat, 15 Feb 2020 18:42:49 +1300 Subject: [PATCH 7/7] Remove build event on pull request --- .github/workflows/main.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 79909878..51cbfaaa 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,5 +1,5 @@ -name: Docker Build -on: [push, pull_request] +name: Build +on: [push] jobs: build: