Ruby on Jets is a Rails-like serverless framework. https://rubyonjets.com/docs/
It is assumed that you are using AWS Lambda.
It is very convenient to be able to manage Lambda settings with config/application.rb
like Rails.
Since you can start puma locally like Rails with a single command, it is very easy to build an environment even though it is serverless.
From now on, I will write about building and deploying the development environment in the docker environment of Ruby On Jets.
This is Part 2.
docker 19.03.13
docker-compose 1.27.4
ruby 2.5.7
Ruby on Jets 2.0.6
PostgreSQL 11.6
FROM ruby:2.5.7
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash - &&\
apt-get update -qq &&\
apt-get install -y build-essential nodejs postgresql-client graphviz task-japanese fonts-ipafont fonts-noto-cjk &&\
apt-get -y install rsync &&\
apt-get -y install zip
ENV LANG ja_JP.UTF-8
ENV TZ=Asia/Tokyo
RUN curl -sL https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip -o awscliv2.zip | bash - &&\
unzip awscliv2.zip &&\
./aws/install
WORKDIR /usr/src/app
COPY Gemfile .
COPY Gemfile.lock .
RUN bundle install
CMD ["/bin/sh"]
I'm using rsync and awscli. I'm sorry if there is excess or deficiency. ..
version: '3'
volumes:
bundle:
postgres:
services:
app:
build: .
ports:
- 3000:3000
volumes:
- bundle:/usr/local/bundle
- .:/usr/src/app:delegated
- ~/.aws:/root/.aws
depends_on:
- postgres
links:
- postgres
stdin_open: true
tty: true
env_file:
- .env.container
command: /bin/sh -c "bundle exec jets server --host '0.0.0.0' --port '3000'"
postgres:
image: postgres:11.6-alpine
ports:
- 5432:5432
volumes:
- postgres:/var/lib/postgresql/data
Since you can start puma with jets server
, it runs when the container starts.
The environment variable management file is explicitly named .env.container.
The reason is that I wanted to make a clear distinction between the environment variables in the docker container and the environment variables in the application.
Also, what was the demon here was where to have the aws credential.
This time I mounted the credential file with volume.
It seems that it is not recommended. .. In CI, I think there is no problem if the encrypted key is decrypted on the pipeline.
Please note that this method is based on the assumption that the deployment user's credentials are used for local ~/.aws.
What this means is that you will be deploying using the user information that already exists in ~/.aws, so you will be giving that user the IAM policy required for deployment.
If it is not convenient, create a new deployment user and mount the credential file in the app container.
app:
volumes:
- <Path with credentials>:/root/.aws
For the time being, this is all you need.
source 'https://rubygems.org'
git_source(:github) do |repo| "https://github.com/#{repo}.git" end
gem 'jets'
Create an empty Gemfile.lock and .env.container.
$ touch Gemfile.lock
$ touch .env.container
$ docker-compose build
$ docker-compose run --rm app jets new . --database=postgresql --mode=api
I hit the above command referring to the following.
https://rubyonjets.com/reference/jets-new/
https://rubyonjets.com/docs/new-modes/
This time we will create it in api mode.
You will be asked if you want to overwrite the Gemfile here.
Enter y. Please customize it later.
conflict Gemfile
Overwrite /usr/src/app/Gemfile? (enter "h" for help) [Ynaqdhm]
#y press enter
Now that the Gemfile has been updated, do bundle install again.
$ docker-compose run --rm app bundle install
Describe the environment variables in .env.container. This time, we will describe the DB information.
DB_NAME=hoge
DB_HOST=fuga
DB_USER=hogehoge
DB_PASS=fugafuga
POSTGRES_HOST_AUTH_METHOD=trust
Execute db: create.
$ docker-compose run --rm app bundle exec jets db:create
Create a template with scaffold for the time being.
$ docker-compose run --rm app bundle exec jets g scaffold post title:string
This will be rejected in the future unless you explicitly pass the options `check_default_type: false` or call `allow_incompatible_default_type!` in your code
You can silence deprecations warning by setting the environment variable THOR_SILENCE_DEPRECATION.
invoke active_record
create db/migrate/20200927064200_create_posts.rb
create app/models/post.rb
invoke resource_route
route resources :posts
Deprecation warning: Expected string default value for '--test-framework'; got false (boolean).
This will be rejected in the future unless you explicitly pass the options `check_default_type: false` or call `allow_incompatible_default_type!` in your code
You can silence deprecations warning by setting the environment variable THOR_SILENCE_DEPRECATION.
invoke scaffold_controller
create app/controllers/posts_controller.rb
Execute db: migrate.
$ docker-compose run --rm app bundle exec jets db:migrate
$ docker-compose up
This will run the jets server and bring up the local server.
If you can check the browser at http://0.0.0.0:3000, it's OK.
This time, we were able to confirm that the application server started up locally, creating an environment where we could develop.
In the second part, I would like to write about how to create a stag, prod environment, etc. from the IAM policy for deployment.
Recommended Posts