From 8b020b9719b1af4b24ad3fcdff04608091609ead Mon Sep 17 00:00:00 2001 From: Joe Germuska Date: Wed, 11 May 2022 15:32:18 -0500 Subject: [PATCH 1/3] Initial adjustments to make it easier to develop Klaxon using Docker. Move the docker-compose-override.yml file to some other name so that it isn't used automatically for production deployments; create a dev.Dockerfile which mounts the repository in the image, so that docker image builds aren't required each time code changes; start updating DOCKER.md to explain process; remove obsolete docker_example directory. --- DOCKER.md | 28 ++++++++++++++++++-------- db/schema.rb | 10 +++++----- dev.Dockerfile | 28 ++++++++++++++++++++++++++ docker-compose-dev.yml | 24 ++++++++++++++++++++++ docker-compose.override.yml | 11 ----------- docker_example/README.md | 33 ------------------------------- docker_example/docker-compose.yml | 23 --------------------- docker_example/env_local.list | 25 ----------------------- 8 files changed, 77 insertions(+), 105 deletions(-) create mode 100644 dev.Dockerfile create mode 100644 docker-compose-dev.yml delete mode 100644 docker-compose.override.yml delete mode 100644 docker_example/README.md delete mode 100644 docker_example/docker-compose.yml delete mode 100644 docker_example/env_local.list diff --git a/DOCKER.md b/DOCKER.md index efb069e1..22f2e58a 100644 --- a/DOCKER.md +++ b/DOCKER.md @@ -1,23 +1,35 @@ # Running Klaxon with Docker -## Development Quickstart +## Developing Klaxon using Docker + +This assumes you already have Docker installed on your system. If you haven't done that yet, visit https://docs.docker.com/get-docker/ 1. Run the following commands: ``` -docker-compose up -open http://localhost:3000 +mkdir .docker_data +docker compose -f docker-compose.yml -f docker-compose-dev.yml up ``` -2. Enter 'admin@news.org' in the email window. It should redirect you to a page that says: "Email Sent". +The first time you do this may take a while as Docker downloads files and builds the container. + +2. In a browser, open http://localhost:3000 + +3. Enter 'admin@news.org' in the email field. It should redirect you to a page that says: "Email Sent". + +4. In the console find where it says "Go to Dashboard ( ... )" and copy and paste the link into the browser. + +5. You'll now be logged in. The page should say "Watch Your First Item". + +## Deploying Klaxon using Docker -3. In the console find where it says "Go to Dashboard ( ... )" and copy and paste the link into the browser. +TK: explain deployment methods, including elaborating environment variables below. -4. You'll now be logged in. The page should say "Watch Your First Item". +### Expected environment variables -## Expected environmental variables +Klaxon needs certain environment variables to be able to run. For development (see above), these are defined in files in this repository, but for production, the environment variable values are often things which should not be in version control, so if you're deploying using Docker, you'll need to make sure these are set correctly. -Klaxon needs certain environmental variables to be able to run. One way to accomplish this in Docker is with an [env file](https://docs.docker.com/engine/reference/commandline/run/#set-environment-variables-e-env-env-file). Below is a template for setting one up. +One way to accomplish this in Docker is with an [env file](https://docs.docker.com/engine/reference/commandline/run/#set-environment-variables-e-env-env-file). Below is a template for setting one up. ```sh DATABASE_URL= diff --git a/db/schema.rb b/db/schema.rb index c8b20819..71ee5905 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -2,11 +2,11 @@ # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. # -# Note that this schema.rb definition is the authoritative source for your -# database schema. If you need to create the application database on another -# system, you should be using db:schema:load, not running all the migrations -# from scratch. The latter is a flawed and unsustainable approach (the more migrations -# you'll amass, the slower it'll run and the greater likelihood for issues). +# This file is the source Rails uses to define your schema when running `rails +# db:schema:load`. When creating a new database, `rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. # # It's strongly recommended that you check this file into your version control system. diff --git a/dev.Dockerfile b/dev.Dockerfile new file mode 100644 index 00000000..d024cc4a --- /dev/null +++ b/dev.Dockerfile @@ -0,0 +1,28 @@ +FROM ruby:2.7.2 + +# throw errors if Gemfile has been modified since Gemfile.lock +RUN bundle config --global frozen 1 + +RUN mkdir -p /usr/src/app +WORKDIR /usr/src/app + +COPY Gemfile /usr/src/app/ +COPY Gemfile.lock /usr/src/app/ +RUN bundle install + +COPY . /usr/src/app + +EXPOSE 3000 + +# install s6overlay so that we can run cron inside this container as well. +ADD https://github.com/just-containers/s6-overlay/releases/download/v1.21.8.0/s6-overlay-amd64.tar.gz /tmp/ +RUN tar xzf /tmp/s6-overlay-amd64.tar.gz -C / \ + && mkdir /config \ + && apt-get update \ + && apt-get install -y cron \ + && rm -rf /var/lib/apt/lists/* + +COPY rootfs/ / +VOLUME ["/config"] + +ENTRYPOINT ["/init"] diff --git a/docker-compose-dev.yml b/docker-compose-dev.yml new file mode 100644 index 00000000..e9a82dd0 --- /dev/null +++ b/docker-compose-dev.yml @@ -0,0 +1,24 @@ +version: '2.2' + +services: + database: + environment: + POSTGRES_PASSWORD: PASSWORD + POSTGRES_DB: klaxon + POSTGRES_USER: postgres + volumes: + - ./.docker_data:/var/lib/postgresql + + app: + ports: ["3000:3000"] + environment: + RACK_ENV: "development" + RAILS_ENV: "development" + DATABASE_URL: "postgres://postgres:PASSWORD@database/klaxon" + SECRET_KEY_BASE: "secret_key_base" + ADMIN_EMAILS: "admin@news.org" + build: + context: . + dockerfile: dev.Dockerfile + volumes: + - .:/usr/src/app diff --git a/docker-compose.override.yml b/docker-compose.override.yml deleted file mode 100644 index 0cac7bc8..00000000 --- a/docker-compose.override.yml +++ /dev/null @@ -1,11 +0,0 @@ -version: '2.2' - -services: - app: - ports: ["3000:3000"] - environment: - RACK_ENV: "development" - RAILS_ENV: "development" - DATABASE_URL: "postgres://postgres@database/klaxon" - SECRET_KEY_BASE: "secret_key_base" - ADMIN_EMAILS: "admin@news.org" diff --git a/docker_example/README.md b/docker_example/README.md deleted file mode 100644 index 77adee3a..00000000 --- a/docker_example/README.md +++ /dev/null @@ -1,33 +0,0 @@ -## Modify environment variables - -edit env_local.list (found in the docker_example directory) and use your own values for things like database_url, admin_email, etc. - - -## Build the image from The Marshall Project's github repo (using the develop branch) - -`docker build --network host -t klaxon https://github.com/themarshallproject/klaxon.git#develop` - - -## Run it on your local machine as a daemon - -### Docker Run - -cd into the docker_example directory in this repo and run the following. You only need to be in this directory because we used a relative path to the env_local.list file. - -`docker run -p 8885:3000 --name klaxon_local --env-file ./env_local.list --restart unless-stopped -d klaxon` - -### Docker Compose - -to use docker-compose modify all values in docker.compose.yml to your liking and run the following command in the docker_example directory. - -`docker-compose up` - -Important values to change in docker-compose.yml: - -- db -> environment -> POSTGRES_PASSWORD -- app -> ports - -## View the app - -Go to your browser and hit `127.0.0.1:8885` . - diff --git a/docker_example/docker-compose.yml b/docker_example/docker-compose.yml deleted file mode 100644 index 17b96437..00000000 --- a/docker_example/docker-compose.yml +++ /dev/null @@ -1,23 +0,0 @@ -version: '2.2' - -services: - db: - image: "postgres:13.3-alpine" - environment: - - POSTGRES_USER=klaxon - - POSTGRES_PASSWORD=secret-psql-password - - POSTGRES_DB=klaxon - volumes: - - ./db:/var/lib/postgresql - app: - links: - - db - depends_on: - - db - image: themarshallproject/klaxon - env_file: - - env_local.list - ports: - - change_me:3000 - # Example: - # - 8885:3000 diff --git a/docker_example/env_local.list b/docker_example/env_local.list deleted file mode 100644 index 8aca50d8..00000000 --- a/docker_example/env_local.list +++ /dev/null @@ -1,25 +0,0 @@ -#We set the postgres url/IP addres here and include username and password in the url -DATABASE_URL=postgres://klaxon:PASSWORD@db/klaxon -ADMIN_EMAILS=someuser@companyname.com -RAILS_ENV=production -# We're using the same production secret_key_base value used in non-docker version of the app -SECRET_KEY_BASE=secret_key_value_here - -# postfix doesn't need to be installed for this to work. -SMTP_PROVIDER=POSTFIX -# set this to the value of your own smpt server -POSTFIX_ADDRESS=smtp-xxx.companyname.com -POSTFIX_PORT=25 -POSTFIX_DOMAIN=companyname.com -# set this if smtp server required authentication -POSTFIX_USERNAME=someuser@companyname.com -POSTFIX_PASSWORD=secret_postfix_pass_here -# Choose alternative FROM adress -MAILER_FROM_ADDRESS=noreply@companyname.com - -#We have this set to false because we are handling ssl redirect in our nginx vhost. Leaving it true causes "too many redirects" error. If running on your local machine, this should also be set to false because you probably don't have an ssl cert set up for localhost. -KLAXON_FORCE_SSL=false -KLAXON_COMPILE_ASSETS=true - -#Uncomment to allow only the listed emails -#APPROVED_USER_DOMAINS=someuser@companyname.com From 7abdbe924310da544e4af355c5d8ec69f170ed83 Mon Sep 17 00:00:00 2001 From: Alexandra Andreiu Date: Thu, 26 May 2022 15:39:32 -0500 Subject: [PATCH 2/3] work in progress --- app/controllers/application_controller.rb | 2 +- app/views/change_mailer/page.html.erb | 8 ++++++-- app/views/layouts/_nav.html.erb | 7 ++++++- docker-compose-dev.yml | 1 + spec/mailers/previews/change_mailer_preview.rb | 6 ++++++ spec/mailers/previews/user_mailer_preview.rb | 8 ++++++-- 6 files changed, 26 insertions(+), 6 deletions(-) create mode 100644 spec/mailers/previews/change_mailer_preview.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 29f94802..1b3ac75f 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,6 +1,6 @@ class ApplicationController < ActionController::Base protect_from_forgery with: :exception - + skip_before_action :verify_authenticity_token helper_method :authorize def authorize unless current_user.present? diff --git a/app/views/change_mailer/page.html.erb b/app/views/change_mailer/page.html.erb index 77ec2f18..c736c4fb 100644 --- a/app/views/change_mailer/page.html.erb +++ b/app/views/change_mailer/page.html.erb @@ -34,9 +34,13 @@
- Klaxon + <% if ENV['KLAXON_INSTANCE_NAME'].present? -%> + Klaxon @ <%= ENV['KLAXON_INSTANCE_NAME'] %> + <% else -%> + Klaxon + <% end -%>
-

Something Changed

+

Something Changed @ <%= ENV['KLAXON_INSTANCE_NAME'] %>

<%= @page.name %> changed. It has changed <%= @page.num_changes %> times since Klaxon started monitoring it on <%= @page.created_at.strftime("%A, %B %d, %Y") %>.

diff --git a/app/views/layouts/_nav.html.erb b/app/views/layouts/_nav.html.erb index b124a0ca..edaf76e6 100644 --- a/app/views/layouts/_nav.html.erb +++ b/app/views/layouts/_nav.html.erb @@ -9,7 +9,12 @@ - Klaxon + <% if ENV['KLAXON_INSTANCE_NAME'].present? -%> + Klaxon @ <%= ENV['KLAXON_INSTANCE_NAME'] %> + <% else -%> + Klaxon + <% end -%> +