Skip to content

Commit

Permalink
Return to AWS
Browse files Browse the repository at this point in the history
  • Loading branch information
Thrillberg committed Jan 11, 2025
1 parent a0c4302 commit 778916a
Show file tree
Hide file tree
Showing 21 changed files with 7,910 additions and 14,694 deletions.
8 changes: 8 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cypress/
node_modules/
vendor/
tmp/
log/
.git/
spec/
docker-compose.yml
4 changes: 3 additions & 1 deletion .github/workflows/cypress.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ jobs:
PGUSER: postgres
PGPASSWORD: postgres
RAILS_ENV: test
REDIS_URL: redis://localhost:6379/0
run: |
npm ci
yarn install
bin/rails db:setup
bin/webpack
- name: Cypress run
Expand All @@ -54,5 +55,6 @@ jobs:
PGUSER: postgres
PGPASSWORD: postgres
RAILS_ENV: test
REDIS_URL: redis://localhost:6379/0
with:
start: bundle exec rails server -e test -p 5002
62 changes: 62 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: Deploy to AWS

on:
push:
branches: [main]

permissions:
id-token: write
contents: read

jobs:
deploy:
name: Build, Push to ECR, and Deploy to Fargate
runs-on: ubuntu-latest

steps:
- name: Checkout Code
uses: actions/checkout@v3

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ACTION_ROLE }}
aws-region: us-east-1

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
with:
mask-password: 'true'

- name: Build, tag, and push image to Amazon ECR
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: imperial-app
IMAGE_TAG: ${{ github.sha }}
run: |
docker build -t $ECR_REPOSITORY:$IMAGE_TAG -f Dockerfile .
docker tag $ECR_REPOSITORY:$IMAGE_TAG $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT
- name: Download task definition
run: |
aws ecs describe-task-definition --task-definition imperial-app --query taskDefinition > task-definition.json
- name: Fill in the new image ID in the Amazon ECS task definition
id: task-def
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: task-definition.json
container-name: imperial
image: ${{ steps.build-image.outputs.image }}

- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v2
with:
task-definition: ${{ steps.task-def.outputs.task-definition }}
service: ${{ secrets.SERVICE_NAME }}
cluster: ${{ secrets.CLUSTER_NAME }}
wait-for-service-stability: true
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: yarn install
- name: Run eslint on changed files
uses: tj-actions/eslint-changed-files@v18
with:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ jobs:
PGUSER: postgres
PGPASSWORD: postgres
RAILS_ENV: test
REDIS_URL: redis://localhost:6379/0
run: |
bin/rails db:setup
- name: Run tests
Expand All @@ -53,6 +54,7 @@ jobs:
PGUSER: postgres
PGPASSWORD: postgres
RAILS_ENV: test
REDIS_URL: redis://localhost:6379/0
run: |
bundle exec standardrb
bundle exec rake
17 changes: 17 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM ruby:3.2.2-slim
WORKDIR /app
RUN apt-get update -qq && apt-get install -y \
libpq-dev \
nodejs \
npm \
yarn \
&& rm -rf /var/lib/apt/lists/*
COPY Gemfile Gemfile.lock ./
RUN bundle install --without development test
COPY package.json yarn.lock ./
RUN npm install -g yarn@1.22.21
RUN yarn install
COPY . .
RUN bundle exec ./bin/webpack
EXPOSE 80
CMD ["rails", "s", "-b", "0.0.0.0", "-p", "80"]
17 changes: 17 additions & 0 deletions Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM ruby:3.2.2-slim
WORKDIR /app
RUN apt-get update -qq && apt-get install -y \
libpq-dev \
nodejs \
npm \
yarn \
&& rm -rf /var/lib/apt/lists/*
COPY Gemfile Gemfile.lock ./
RUN bundle install
COPY package.json yarn.lock ./
RUN npm install -g yarn@1.22.21
RUN yarn install
COPY . .
RUN bundle exec ./bin/webpack
EXPOSE 3000
CMD ["rails", "s", "-b", "0.0.0.0"]
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ And in another terminal:
npm run cypress
```

## Docker commands for running locally

```
docker-compose up --build
```

## Local debugging

Production games will often present situations that are hard to reproduce locally and therefore get challening to debug. Therefore, we have implemented the ability to **export** and **import** games.
Expand Down
2 changes: 1 addition & 1 deletion app/models/game.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def log
end

def abandoned?
if actions.length > 0 && !force_ended_at
if actions.any? && !force_ended_at
return actions.order(:created_at).last.created_at < 7.days.ago
elsif actions.any? { |a| JSON.parse(a.data)["type"] == "rondel" } && !force_ended_at
return actions.order(:created_at).last.created_at < 30.days.ago
Expand Down
2 changes: 0 additions & 2 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ class Application < Rails::Application
config.middleware.use ActionDispatch::Session::CookieStore
# config.middleware.insert_after ActionDispatch::Static, Rack::Deflater

config.hosts << "imperial-staging.herokuapp.com"
config.hosts << "imperial-game.herokuapp.com"
config.hosts << "localhost"

config.autoloader = :classic
Expand Down
2 changes: 0 additions & 2 deletions config/cable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,3 @@ production:
adapter: redis
url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %>
channel_prefix: rails_server_production
ssl_params:
verify_mode: <%= OpenSSL::SSL::VERIFY_NONE %>
2 changes: 1 addition & 1 deletion config/credentials.yml.enc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sTk6GUI0CbEHxSKZ72/CoPW+mFPOPW1FZdsQ7hCIyS8ysD77TjNs4Xrzdx7SIe/RhVKzC4ms4EFNoEDWtOOWVsRkY0Udvc0q9O5Pabz3zayhbG7Mt/mcTcWENV1D9WkatkXLYuVqcBfHB9jyYGY2OGHOz0pKWOq8220PAvpElMtemLqryv1AZ7wUeH/SMjNZguOviETFPwuzv1BKzyZN4gJdEMRbQx1E60k92NSVTXwZdoLLuT1a4MV0QUVTtRJxy6jpoofiNQfStBBm/e+iq+VckWVZWZAhs3l+05fAT30xS9o67eP9CcOq2V6/TezlRfhDf7hhqK4MPqNfsC3fVt+oykxtjW5kAo0dBOyHVmt5F91i7vsZB/qRwRYq/YD/OQVC2AOJeAH0PubaWadl63Kj6uyHpb4QGnNG--P3hvyPLqGejrpUSA--t21niZCtyOmwUHiaZ7eT5w==
vwb77QWn4hYEykY6jcCYC47PLoutybau0WXalgxvqQLwxlXnk1u4f/X64XapK9mzUWKLH42s3fE/vkIIevTUVrSzl1ye6uM69LPyzyO/W4Yh6OumyAgnALNrwVj7VWwPMlWX5hH6x9wBUTOA4gLxVJCZTFTc8TkJ7nu4N2JviDtMwNREB59wbIV715eEoJfh2UIj7uikP4mhM03PML+tjt9D0oKcfVxKv7xmCinvww6QJ4espYzNxLnjUCGq0Rk6SZoSi/DqLHTCmxbdMyQiKnE0RDzOp41++jRfLk8U2P/04ihk0Y1T78mfp90FITx5KMNs+w0Au3vljceHMzQqByv/Lv0uuFgYtizSX3wbaaOZyhUV4T5E6v/G5LU6jfyh9u1bA11vhSfLNNFJBCwfEhxeEz3K3s0=--Oz0Z/7tQ/Dd+hE5m--AYVW+Ack+X5f/y7URMvlYw==
5 changes: 3 additions & 2 deletions config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ default: &default
# For details on connection pooling, see Rails configuration guide
# https://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
port: 5432

development:
<<: *default
database: rails_server_development
database: postgres
host: db

# The specified database role being used to connect to postgres.
# To create additional roles in postgres see `$ createuser --help`.
Expand Down Expand Up @@ -85,4 +87,3 @@ production:
username: <%= ENV['POSTGRES_USER'] %>
password: <%= ENV['POSTGRES_PASSWORD'] %>
host: <%= ENV['POSTGRES_HOST'] %>
port: 5432
7 changes: 3 additions & 4 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
# config.action_cable.allowed_request_origins = [ "http://example.com", /http:\/\/example.*/ ]

# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
config.force_ssl = true
config.force_ssl = false

# Include generic and useful information about system operation, but avoid logging too much
# information to avoid inadvertent exposure of personally identifiable information (PII).
Expand Down Expand Up @@ -101,7 +101,6 @@
config.active_record.dump_schema_after_migration = false

config.hosts << "www.playimperial.club"
config.hosts << "imperial-staging.herokuapp.com"
config.hosts << "staging.playimperial.club"
config.hosts << IPAddr.new("172.31.0.0/16")
config.hosts << "playimperial.club"
config.hosts << /\A172\.30\.\d+\.\d+\z/
end
2 changes: 1 addition & 1 deletion config/initializers/redis.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require "redis"

REDIS = Redis.new(ssl_params: {verify_mode: OpenSSL::SSL::VERIFY_NONE})
REDIS = Redis.new(url: ENV["REDIS_URL"] || "redis://redis:6379/0")
REDIS.set("online_users", [].to_json)
REDIS.set("users_observing_games", {}.to_json)
4 changes: 2 additions & 2 deletions config/initializers/sidekiq.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Sidekiq.configure_server do |config|
config.redis = {ssl_params: {verify_mode: OpenSSL::SSL::VERIFY_NONE}}
config.redis = {url: ENV["REDIS_URL"], namespace: "imperial_production"}
end

Sidekiq.configure_client do |config|
config.redis = {ssl_params: {verify_mode: OpenSSL::SSL::VERIFY_NONE}}
config.redis = {url: ENV["REDIS_URL"], namespace: "imperial_production"}
end
6 changes: 5 additions & 1 deletion config/puma.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@

# Specifies the `port` that Puma will listen on to receive requests; default is 3000.
#
port ENV.fetch("PORT", 3000)
if ENV.fetch("RAILS_ENV", "development") == "production"
port ENV.fetch("PORT", 80)
else
port ENV.fetch("PORT", 3000)
end

# Specifies the `environment` that Puma will run in.
#
Expand Down
11 changes: 5 additions & 6 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
enable_extension "pgcrypto"
enable_extension "plpgsql"

create_table "accounts", id: :uuid, default: -> { "public.gen_random_uuid()" }, force: :cascade do |t|
create_table "accounts", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
Expand All @@ -27,7 +27,7 @@
t.index ["reset_password_token"], name: "index_accounts_on_reset_password_token", unique: true
end

create_table "actions", id: :uuid, default: -> { "public.gen_random_uuid()" }, force: :cascade do |t|
create_table "actions", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.string "data"
t.uuid "game_id", null: false
t.datetime "created_at", null: false
Expand All @@ -37,7 +37,7 @@
t.index ["game_id"], name: "index_actions_on_game_id"
end

create_table "games", id: :uuid, default: -> { "public.gen_random_uuid()" }, force: :cascade do |t|
create_table "games", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.string "name"
t.uuid "host_id", null: false
t.datetime "created_at", null: false
Expand All @@ -60,7 +60,7 @@
t.index ["winner_id"], name: "index_games_on_winner_id"
end

create_table "players", id: :uuid, default: -> { "public.gen_random_uuid()" }, force: :cascade do |t|
create_table "players", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.uuid "user_id", null: false
t.uuid "game_id", null: false
t.datetime "created_at", null: false
Expand All @@ -78,15 +78,14 @@
t.index ["game_id"], name: "index_snapshots_on_game_id"
end

create_table "users", id: :uuid, default: -> { "public.gen_random_uuid()" }, force: :cascade do |t|
create_table "users", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.uuid "account_id"
t.datetime "anonymity_confirmed_at", precision: nil
t.boolean "turn_notifications_enabled", default: false
t.string "discord_id"
t.boolean "is_bot"
t.index ["account_id"], name: "index_users_on_account_id"
end

Expand Down
35 changes: 35 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
services:
app:
build:
context: .
dockerfile: Dockerfile.dev
platform: linux/amd64
depends_on:
- db
- redis
ports:
- "3000:3000"
volumes:
- .:/app
- /app/vendor
- /app/node_modules
environment:
DATABASE_URL: postgres://postgres:password@db:5432/postgres
RAILS_ENV: development
command: ["sh", "-c", "rm -f tmp/pids/server.pid && bin/rails db:migrate && rails s -b 0.0.0.0"]
db:
image: postgres:13
container_name: db
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
POSTGRES_DB: postgres
volumes:
- db-data:/var/lib/postgresql/data
ports:
- "5432:5432"
redis:
image: redis:alpine
volumes:
db-data:
driver: local
Loading

0 comments on commit 778916a

Please sign in to comment.