零からの

ゆるーく綴るブログ

Ruby on Jets 環境構築メモ

Ruby on Jetsを検証することになったので、まずは環境構築までをやってみた。

Docker環境の構築

Dockerfileとdocker-compose.ymlをそれぞれ以下のように作成する。

FROM ruby:2.5-alpine

ENV APP_HOME=/app
WORKDIR $APP_HOME
version: '3'
services:
  app:
    build:
      context: .
      dockerfile: ./docker/ruby/Dockerfile
    volumes:
      - .:/app

Dockerイメージをビルドし、作成したイメージからコンテナを実行する。 rubyのバージョンが確認できる。

docker-compose build
docker-compose run --rm app ruby -v
ruby 2.5.7p206 (2019-10-01 revision 67816) [x86_64-linux-musl]

Gemfile作成

Bundlerを初期化し、Gemfileを作成する。

docker-compose run --rm app bundle init

Jetsのインストール

Gemfileを更新する。

# frozen_string_literal: true

source "https://rubygems.org"

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

gem 'jets', '~> 2.3', '>= 2.3.11'

jetsをインストールするためDockerfileを修正する。

FROM ruby:2.5-alpine

ENV APP_HOME=/app
WORKDIR $APP_HOME

COPY ./Gemfile* $APP_HOME
RUN bundle install
docker-compose build

An error occurred while installing nokogiri (1.10.7), and Bundler cannot continue.
Make sure that `gem install nokogiri -v '1.10.7' --source 'https://rubygems.org/'` succeeds before bundling.

alpineイメージの場合、nokogiriのインストールで失敗する(らしい)のでDockerfileを修正する。

FROM ruby:2.5-alpine

RUN apk update && \
    apk add --no-cache build-base libxml2-dev libxslt-dev

ENV APP_HOME=/app
WORKDIR $APP_HOME

COPY ./Gemfile $APP_HOME
RUN bundle install

Dockerイメージをビルドし、作成したイメージからコンテナを実行する。 jetsのバージョンが確認できる。

docker-compose build
docker-compose run --rm app bundle exec jets -v
2.3.11

DockerのNode公式イメージを利用してServerless Frameworkをインストールする

Serverless Frameworkを試すにあたりDockerfileを作成したがちょっとハマったのでメモ。

まず最初に

公式サイトを参考に以下のDockerfileを作成した。

FROM node:13.5-alpine
RUN npm install -g serverless

実行すると以下のように"update check failed"となる。 update checkだから問題なさそうだが、root権限での実行は微妙。

npm WARN deprecated superagent@3.8.3: Please note that v5.0.1+ of superagent removes User-Agent header by default, therefore you may need to add it yourself (e.g. GitHub blocks requests without a User-Agent header).  This notice will go away with v5.0.2+ once it is released.
/usr/local/bin/serverless -> /usr/local/lib/node_modules/serverless/bin/serverless.js
/usr/local/bin/slss -> /usr/local/lib/node_modules/serverless/bin/serverless.js
/usr/local/bin/sls -> /usr/local/lib/node_modules/serverless/bin/serverless.js

> serverless@1.60.5 postinstall /usr/local/lib/node_modules/serverless
> node ./scripts/postinstall.js


   ┌───────────────────────────────────────────────────┐
   │                                                   │
   │   Serverless Framework successfully installed!    │
   │                                                   │
   │   To start your first project run “serverless”.   │
   │                                                   │
   └───────────────────────────────────────────────────┘


┌───────────────────────────────────────────────────┐
│          serverless update check failed           │
│        Try running with sudo or get access        │
│       to the local update config store via        │
│ sudo chown -R $USER:$(id -gn $USER) /root/.config │
└───────────────────────────────────────────────────┘
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@2.1.2 (node_modules/serverless/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.1.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

+ serverless@1.60.5
added 519 packages from 334 contributors in 29.894s

次に試したこと

nodeユーザーにしてインストールを行った。

FROM node:13.5-alpine
USER node
RUN npm install -g serverless

すると、Permissionエラーが発生した。

npm WARN deprecated superagent@3.8.3: Please note that v5.0.1+ of superagent removes User-Agent header by default, therefore you may need to add it yourself (e.g. GitHub blocks requests without a User-Agent header).  This notice will go away with v5.0.2+ once it is released.
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules
npm ERR! code EACCES
npm ERR! syscall access
npm ERR! path /usr/local/lib/node_modules
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR!  [Error: EACCES: permission denied, access '/usr/local/lib/node_modules'] {
npm ERR!   stack: "Error: EACCES: permission denied, access '/usr/local/lib/node_modules'",
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/usr/local/lib/node_modules'
npm ERR! }
npm ERR!
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR!
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/node/.npm/_logs/2020-01-05T13_39_33_482Z-debug.log

最終的に

こちらを参考にして解消した。

グローバルインストール用にNPM_CONFIG_PREFIXを変更するのがキモ。

FROM node:13.5-alpine
RUN apk update

USER node
ENV NPM_CONFIG_PREFIX /home/node/.npm-global
RUN npm install -g serverless

root実行時に表示された"update check failed"が表示されなくなった。

npm WARN deprecated superagent@3.8.3: Please note that v5.0.1+ of superagent removes User-Agent header by default, therefore you may need to add it yourself (e.g. GitHub blocks requests without a User-Agent header).  This notice will go away with v5.0.2+ once it is released.
/home/node/.npm-global/bin/serverless -> /home/node/.npm-global/lib/node_modules/serverless/bin/serverless.js
/home/node/.npm-global/bin/slss -> /home/node/.npm-global/lib/node_modules/serverless/bin/serverless.js
/home/node/.npm-global/bin/sls -> /home/node/.npm-global/lib/node_modules/serverless/bin/serverless.js

> serverless@1.60.5 postinstall /home/node/.npm-global/lib/node_modules/serverless
> node ./scripts/postinstall.js


   ┌───────────────────────────────────────────────────┐
   │                                                   │
   │   Serverless Framework successfully installed!    │
   │                                                   │
   │   To start your first project run “serverless”.   │
   │                                                   │
   └───────────────────────────────────────────────────┘

npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@2.1.2 (node_modules/serverless/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.1.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

+ serverless@1.60.5
added 519 packages from 334 contributors in 25.324s

続・最終的に

上記のままだと、インストールは完了するがPATHが通っていないため実行できない。

下記のようにしてPATHを通す。

FROM node:13.5-alpine
RUN apk update

USER node
ENV NPM_CONFIG_PREFIX /home/node/.npm-global
RUN npm install -g serverless
ENV PATH $PATH:/home/node/.npm-global/bin

SAM(Serverless Application Model)を試す

概要

LambdaとAPI Gatewayと使って何か作るのであれば、SAM(Serverless Application Model)を使うと楽にできるらしいので試してみた。

準備

SAM CLIをインストールする。 MacなのでHomebrewを使用する。

brew tap aws/tap
brew install aws-sam-cli

以下のコマンドを実行してバージョン情報が表示されればOK

sam --version

samで使用できるコマンドは以下の通り

Usage: sam [OPTIONS] COMMAND [ARGS]...

  AWS Serverless Application Model (SAM) CLI

  The AWS Serverless Application Model extends AWS CloudFormation to provide
  a simplified way of defining the Amazon API Gateway APIs, AWS Lambda
  functions, and Amazon DynamoDB tables needed by your serverless
  application. You can find more in-depth guide about the SAM specification
  here: https://github.com/awslabs/serverless-application-model.

Options:
  --debug    Turn on debug logging to print debug message generated by SAM
             CLI.
  --version  Show the version and exit.
  --info
  --help     Show this message and exit.

Commands:
  init      Init an AWS SAM application.
  validate  Validate an AWS SAM template.
  build     Build your Lambda function code
  local     Run your Serverless application locally for quick development &...
  package   Package an AWS SAM application.
  deploy    Deploy an AWS SAM application.
  logs      Fetch logs for a function
  publish   Publish a packaged AWS SAM template to the AWS Serverless
            Application Repository.

アプリケーション作成

アプリケーションを作成する。

sam init --name start-sam --runtime go1.x

treeコマンドを実行すると、以下が確認できる。

.
├── Makefile
├── README.md
├── hello-world
│   ├── main.go
│   └── main_test.go
└── template.yaml

1 directory, 5 files

READMEが作成されるので、それを参照しながら進める。

開発

以下のコマンドを実行するとlocalhostで確認が可能になる。

sam local start-api

デプロイ

※事前にAWS CLIのインストールとクレデンシャルの設定が必要

CloudFormationを実行してデプロイできる。

sam deploy --guided

Prod/Stageの2つのステージが作成され、それぞれで実行できるところまで確認した。

Present Perfect

I've been studying the present perfect.

Present perfect is an action from past to present.

It almost seems too simple.

Present perfect uses 'for' and 'since', but doesn't use 'ago'.

'For' is duration.

ex) I've been in Tokyo for two years.

'Since' is from particular point to present.

ex) I've been in Tokyo since January.

'Ago' uses simple past.

ex) I lived in Tokyo two years ago.

そのお尻の痛み、肛門周囲膿瘍かも!?

平成最期の夏がとても残念なものとなりました(泣)

トドメを刺した?急性胃腸炎

8/3

仕事中、夕方に倦怠感と寒気を覚える。 電車内が寒く、ひとり鳥肌を立てながら帰宅する。

帰宅後、熱を測ると39度オーバー...
すぐに寝る。

8/4

朝に下痢をしたのと腹部が痛いので病院へ行き、急性胃腸炎の診断を受ける。

8/7まで熱が下がらず、仕事を休む。

突然の違和感

8/8

朝に平熱であることを確認し出社。
ただ、夕方には頭がボーッとするので帰宅後熱を測ると38度。

腹痛は特になかったので頓服を服用する。

8/9

朝から微熱。
熱だけで特に異変はないからと出社したものの椅子に座るとお尻が痛い。

座るたびに「イテテ」と言っている気がする。

肛門は体の中心?

8/10

肛門は体の中心かと思うくらい、何をしていてもお尻が痛い。

立っていても、座っていても、寝ていても。
くしゃみの場合はズキーーンとくる。

8/11〜15で夏休みの予定だったが、仕事できる状態じゃないので休むことに。

土曜、山の日!?まじかよ...

8/11

痛みは相変わらず続いたため、近くの病院を探して行くことに。

普段であれば歩いて10分もかからない場所ですが、 歩く時も痛いため、ゆっくり15分ほどかけて病院へ。

5分ほど前に着いたのでまだ開いていないのかと思ったら、 入り口に「本日休診」の文字...

病院に電話しても自動音声。 そんなバカな!?と思ってスマホのカレンダーを見ると山の日の文字。

「祝日じゃん...」

暑い中、また15分かけて家へ帰りました...

クッションを持って病院へ

8/13

地獄の土日を乗り越えた月曜日。

朝一番で近所の病院へ飛び込みました。

座るのも辛いため、ふかふかのクッションを持って、 のそのそと診察室に入ってきた男を見て「ヤバいのがきたな...」と医師の方はきっと思われたことでしょう。

切開の激痛

ベッドに「く」の字の状態で横になって診察してもらうと、 「かなり膿んでるね。切開しましょう!」と言われる。

切開にあたり麻酔を打つとのことでしたが、これがまぁ痛い。 深い部分まで針が刺さってくる感覚。

そして、膿を切っている最中も麻酔したのかと思うくらいの激痛。

暴れないように看護師さんに押さえつけられ、痛みをこらえるように深呼吸するもかなり荒め... さらに、直腸の様子も見るためにカメラも入れられ、早く終われと只々願うばかりでした。

診察の結果は「肛門周囲膿瘍」
切開箇所から大量の膿が出たらしく、久々に大きいのを見たと言われる。
そして、この後「痔瘻」へジョブチェンジする可能性が高いことも告げられる。

ガーゼを切った箇所に挿入され、抗生物質の点滴をしてもらい帰宅。
お尻に痛みはあるものの、通院前と違って全然歩けるようになりました。

しばらくは通院

翌日の朝は大量の膿と血がパンツから布団まで汚していて軽くパニックでしたがずっと続いていた熱も平熱になり、2日もすると状態もかなりよくなりました。

改めて通院し、消毒を受けましたが、様子見のためしばらくは週1回通院となりました。
膿が抜け、瘻管の状況を見た上で今後の治療方針が決まるとのこと。

現在も通院中。
9月中には治療を終えたい...

「Webを支える技術」を読んで

普段、URIの設計や実装をWebアプリケーションフレームワークに任せっきりの人も多いかもしれませんが、 URIの仕様を正しく理解することは、使いやすいWebサービスとWeb APIへの第一歩です。 *1

上記の通り、あまり意識せずに実装されている方もいるかと思います。

私がまさにそんな状況でした。

知らなくてもWebサービスは提供できるかもしれない。

だけど、それが使いやすいWebサービスなのだろうか。

そんな思いから、基本を押さえるためにこの本を読んでみようと思いました。

構成

本書は「Webとは何か」から始まり

  • Webの歴史
  • REST
  • URI
  • HTTP
  • 認証
  • HTML

など、Webについて一通りのことが凝縮されています。

リソース設計の重要性

また、第5章では「Webサービスの設計」として郵便番号検索サービスを例に実践的なアプローチが説明されています。

前述の通り、私はURIの設計を含めたリソース設計をあまり意識出来ていなかったため、

本書を通じてリソース設計の重要さに触れることができました。


最初に書いた通りですが、同じような状況の方がいたら一度読んでみてはいかがでしょうか。

Webを支える技術 -HTTP、URI、HTML、そしてREST (WEB+DB PRESS plus)

Webを支える技術 -HTTP、URI、HTML、そしてREST (WEB+DB PRESS plus)

*1:第4章 URIの仕様