Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HMRC-495 Add DevContainer Support #2058

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,15 @@ jobs:
BUNDLE_RETRY: "3"
RAILS_ENV: test
DATABASE_URL: "postgresql://postgres:postgres@localhost:5432/tariff_test"
REDIS_URL: "redis://localhost:6379/"
FRONTEND_REDIS_URL: "redis://localhost:6379/"
ELASTICSEARCH_URL: "http://localhost:9200"
- image: cimg/postgres:13.6
environment:
POSTGRES_USER: postgres
POSTGRES_DB: tariff_test
POSTGRES_PASSWORD: postgres
- image: cimg/redis:6.2
environment:
REDIS_URL: "redis://localhost:6379/"
- image: opensearchproject/opensearch:2
environment:
cluster.name: elasticsearch
Expand Down
5 changes: 5 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Make sure RUBY_VERSION matches the Ruby version in .ruby-version
ARG RUBY_VERSION=3.3.6
FROM ruby:$RUBY_VERSION-alpine3.20

RUN apk add --update build-base docker gcompat git gpg postgresql-dev postgresql-client pre-commit python3 openssh shared-mime-info tzdata gnupg
19 changes: 19 additions & 0 deletions .devcontainer/compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: "trade_tariff_backend"

services:
rails:
build:
context: ..
dockerfile: .devcontainer/Dockerfile

volumes:
- ../..:/workspaces:cached

# Overrides default command so things don't shut down after the process ends.
command: sleep infinity

# Uncomment the next line to use a non-root user for all processes.
# user: vscode

# Use "forwardPorts" in **devcontainer.json** to forward an app port locally.
# (Adding the "ports" property to this file will not forward from a Codespace.)
25 changes: 25 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// For format details, see https://containers.dev/implementors/json_reference/.
// For config options, see the README at: https://github.com/devcontainers/templates/tree/main/src/ruby
{
"name": "trade_tariff_backend",
"dockerComposeFile": "compose.yaml",
"service": "rails",
"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",
// Features to add to the dev container. More info: https://containers.dev/features.
"features": {
"ghcr.io/cirolosapio/devcontainers-features/alpine-ohmyzsh:0": {},
// "ghcr.io/gvatsal60/dev-container-features/pre-commit:1": {}
},
"containerEnv": {},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
"forwardPorts": [
3000
],
// Configure tool-specific properties.
// "customizations": {},
// Uncomment to connect as root instead. More info: https://containers.dev/implementors/json_reference/#remoteUser.
// "remoteUser": "root",
// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": "bin/bundle install",
"postStartCommand": "bin/dev"
}
7 changes: 4 additions & 3 deletions .env.development
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ ENABLE_ADMIN=true
ENVIRONMENT=development
EXCESS_QUERY_THRESHOLD=1000
EXCHANGE_RATE_ACCESS_KEY=flibble
FRONTEND_HOST=http://localhost:3001
FRONTEND_HOST=http://host.docker.internal:3001
GREEN_LANES_NOTIFY_MEASURE_UPDATES=false
[email protected]
LEGACY_SEARCH_ANCESTORS_ENABLED=true
MERGED_XI_UK_DATABASES=true
NESTED_SET_SUBHEADINGS=true
PERMUTATIONS=true
PGHOST=localhost
PGHOST=host.docker.internal
PORT=3000
REPORTING_CDN_HOST=https://reporting.trade-tariff.service.gov.uk
SOFT_DELETES_MISSING=true
Expand All @@ -28,8 +28,9 @@ SYNONYM_REFERENCE_ANALYZER=/usr/share/opensearch/config/synonyms_all.txt
[email protected]
TARIFF_IGNORE_PRESENCE_ERRORS=1
[email protected]
TARIFF_QUERY_SEARCH_PARSER_URL=http://127.0.0.1:5000/api/search/
TARIFF_QUERY_SEARCH_PARSER_URL=http://host.docker.internal:5000/api/search/
TARIFF_SUPPORT_EMAIL=Online Trade Tariff Support <[email protected]>
XE_API_PASSWORD=pass
XE_API_URL=url
XE_API_USERNAME=user
REDIS_URL=redis://host.docker.internal:6379
9 changes: 6 additions & 3 deletions .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ ENABLE_ADMIN=true
ENVIRONMENT=test
EXCESS_QUERY_THRESHOLD=200 # so high because commodities_controller_spec with --seed=4780
EXCHANGE_RATE_ACCESS_KEY=flibble
FRONTEND_HOST=http://localhost:3001
FRONTEND_HOST=http://host.docker.internal:3001
GREEN_LANES_NOTIFY_MEASURE_UPDATES=false
[email protected]
HMRC_API_HOST=http://example.com
Expand All @@ -27,7 +27,7 @@ MERGED_XI_UK_DATABASES=true
NORMALISED_MEASURE_UNITS=true
PATCH_BROKEN_TARIC_DOWNLOADS=false
PERMUTATIONS=true
PGHOST=localhost
PGHOST=host.docker.internal
REPORTING_CDN_HOST=http://reporting.example.com
REPORTING_ENABLED=true
SLACK_CHANNEL="#yyyyyyyy"
Expand All @@ -40,7 +40,7 @@ [email protected]
TARIFF_IGNORE_PRESENCE_ERRORS=0
[email protected]
TARIFF_MEASURES_LOGGER=0
TARIFF_QUERY_SEARCH_PARSER_URL=http://localhost:5000/api/search/
TARIFF_QUERY_SEARCH_PARSER_URL=http://host.docker.internal:5000/api/search/
[email protected]
[email protected]
TARIFF_SYNC_HOST=http://example.com
Expand All @@ -49,3 +49,6 @@ TARIFF_SYNC_USERNAME=username
XE_API_PASSWORD=pass
XE_API_URL=https://example.com
XE_API_USERNAME=user
DB_USER=postgres
REDIS_URL=redis://host.docker.internal:6379
ELASTICSEARCH_URL=http://host.docker.internal:9200
11 changes: 0 additions & 11 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,6 @@ repos:
- id: check-yaml
exclude: ^db/|config/sidekiq.yml

- repo: https://github.com/igorshubovych/markdownlint-cli
rev: v0.38.0
hooks:
- id: markdownlint-docker
args:
- "--fix"
- "--ignore"
- terraform
- "--ignore"
- db/

- repo: https://github.com/koalaman/shellcheck-precommit
rev: v0.10.0
hooks:
Expand Down
34 changes: 30 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,42 @@ Projects using the Trade Tariff (TT) API:

> Make sure you install and enable all pre-commit hooks https://pre-commit.com/

### Dependencies
Before setup you will need your backing services:

- Ruby [v3.2](https://github.com/trade-tariff/trade-tariff-frontend/blob/main/.ruby-version#L1)
- Postgresql v13
- OpenSearch v2
- Redis

These can be configured by running `docker-compose up` or by manual installation.
These can be configured by following the instructions here:

### Setup
https://docs.trade-tariff.service.gov.uk/manual/get-started.html#6-set-up-ott-docker

### DevContainers (recommended)

This application supports devcontainers - i.e running the application and your development tools inside Docker.

These instructions apply to VSCode, but the DevContainer should work with any tooling:

Once backing services are running inside Docker:

* Clone this repo to your local machine.
* Ensure you have the [DevContainer extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers) installed in VSCode.
* Open the folder in VSCode
* Run "Dev Containers: Open Folder in Container... "
* Once the container has booted, inside VSCode's terminal, run `bin/setup`
* Once complete, start the service with `bin/dev`

If commiting / pushing from inside your devcontainer, you may need to share your SSH configuration with your container. Instructions on this can be found [here](https://code.visualstudio.com/remote/advancedcontainers/sharing-git-credentials).

Should you *not* want to use DevContainers, you can setup this up manually:

### Manual development (not recommended)

#### Dependencies

- Ruby [v3.3](https://github.com/trade-tariff/trade-tariff-frontend/blob/main/.ruby-version#L1)

#### Setup

1. Clone this repo
2. Install the correct ruby version according to the `.ruby-version` - eg using
Expand Down
4 changes: 2 additions & 2 deletions app/lib/trade_tariff_backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def bulk_search_api_enabled?
end

def opensearch_host
ENV.fetch('ELASTICSEARCH_URL', 'http://localhost:9200')
ENV.fetch('ELASTICSEARCH_URL', 'http://host.docker.internal:9200')
end

def xe_api_url
Expand Down Expand Up @@ -246,7 +246,7 @@ def revision

def frontend_redis
@frontend_redis ||= begin
url = ENV.fetch('FRONTEND_REDIS_URL', 'redis://localhost:6379')
url = ENV.fetch('FRONTEND_REDIS_URL', 'redis://host.docker.internal:6379')
db = Rails.env.test? ? 1 : 0

Redis.new(url:, db:)
Expand Down
8 changes: 4 additions & 4 deletions bin/setup
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ FileUtils.chdir APP_ROOT do
system("bundle check") || system!("bundle install")

puts "\n== Creating database =="
system! "bin/rake db:create"
system! "bin/rails db:create"

if ARGV[0]
puts "\n== Importing database dump =="
system! "psql -h localhost tariff_development < #{ARGV[0]}"
else
puts "\n== Loading database structure =="
system! "bin/rake db:structure:load"
system! "bin/rails db:structure:load"

puts "\n== Loading seed data =="
system! "bin/rake db:seed"
system! "bin/rails db:seed"
end

puts "\n== Preparing test database =="
system! "bin/rake db:test:prepare"
system! "bin/rails db:test:prepare"

puts "\n== Removing old logs and tempfiles =="
system! "bin/rails log:clear tmp:clear"
Expand Down
7 changes: 5 additions & 2 deletions config/database.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
default: &default
adapter: postgresql
adapter: postgres
encoding: utf8
pool: 12
# pool: 12
search_path: <%= "#{ENV.fetch("SERVICE", "uk")}, public" %>

development:
<<: *default
host: host.docker.internal
user: postgres
database: tariff_development

test:
<<: *default
user: <%= ENV.fetch("DB_USER", "postgres") %>
database: tariff_test

production:
Expand Down
2 changes: 1 addition & 1 deletion config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def debug(progname = nil, &block)
config.action_mailer.perform_caching = false

# Mailcatcher configuration.
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
config.action_mailer.default_url_options = { host: 'host.docker.internal', port: 3000 }
config.action_mailer.delivery_method = :letter_opener

# Print deprecation notices to the Rails logger.
Expand Down