Wakerを動かす

github.com

認証まわりのセットアップ

Google Developer Consoleで適当なプロジェクトを作って、OAuth 2.0 クライアント IDを発行する。

f:id:winebarrel:20160915192114p:plain

  • 承認済みの JavaScript 生成元:
    • http://localhost:5000
  • 承認済みのリダイレクト URI:
    • http://localhost:5000/auth/google_oauth2/callback

.envファイルに認証情報を書いておく。

echo 'GOOGLE_CLIENT_ID=...' >> .env
echo 'GOOGLE_CLIENT_SECRET=...' >> .env
echo 'GOOGLE_DOMAIN=...' >> .env # If you restrict to use Google Apps doma

セットアップ

まず、bundle install

$ bundle install
Using rake 11.2.2
...
Bundle complete! 31 Gemfile dependencies, 113 gems now installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.

MySQLを起動。

$ mysql.server start
Starting MySQL
.. SUCCESS!

Redisも起動。

$ redis-server &
[1] 9337
...
[9337] 15 Sep 19:14:36.797 * The server is now ready to accept connections on port 6379

データベースをセットアップ。

]$ rake db:create
[DEPRECATION] `last_comment` is deprecated.  Please use `last_description` instead.
[DEPRECATION] `last_comment` is deprecated.  Please use `last_description` instead.
[DEPRECATION] `last_comment` is deprecated.  Please use `last_description` instead.
$ rake db:migrate
...
== 20160914063913 AddCommentIndex: migrated (0.0592s) =========================

サーバを起動。

$ bundle exec foreman start -f Procfile.docker
...
19:28:49 web.1                | * Listening on tcp://0.0.0.0:5000
19:28:49 web.1                | Use Ctrl-C to stop```

ユーザのセットアップ

http://localhost:5000/にアクセスすると、Googleアカウントで認証される。

f:id:winebarrel:20160915193024p:plain

ただし認証直後はユーザは非アクティブ。

f:id:winebarrel:20160915193150p:plain

なので、DBをいじってアクティベートする。

$ bundle exec rails runner 'User.first.update!(active: true)'

再度アクセスすると、トップ画面が表示される。

f:id:winebarrel:20160915193404p:plain

Wakerのセットアップ

Escalation Seriesを作る

f:id:winebarrel:20160915193552p:plain

Escalationを作る

f:id:winebarrel:20160915193708p:plain

Topicを作る

f:id:winebarrel:20160915194240p:plain

Notifier Providerを作る

今回はRailsLogger。

f:id:winebarrel:20160915193833p:plain

どういうProviderがあって、どういう設定値を必要としているかは、notifier_provider.rbを見るとよい。

Notifierを作る

今回はRailsLoggerなので、詳細な設定とユーザへのひも付けは不要。

f:id:winebarrel:20160916003738p:plain

動作確認

以下のようにcurlAPIをたたく。

$ curl -s -XPOST "localhost:5000/topics/1/mailgun.json" -d 'subject=foo&body-plain=bar'
{}

するとIncidentが作成されて

f:id:winebarrel:20160915195622p:plain

ログの方に(わかりにくいけど)

Notification: opened

と出力される。 default.text.erbをいじれば、もう少し詳細な情報を出力できる。

MailgunからIncidentを作る

Routeのとこで

f:id:winebarrel:20160915203340p:plain

こんな感じでStore and notify設定してMailgun宛てにメール投げれば、Incidentされる…はず。

IncidentのイベントをSlackに通知する

Incoming Webhookを作る

適当にIncoming Webhookを作る。

f:id:winebarrel:20160915202443p:plain

Notifier Providerを作る

f:id:winebarrel:20160915202730p:plain

Notifierを作る

f:id:winebarrel:20160916003907p:plain

動作確認

curlでIncidentを作ってみる。

 curl -XPOST "localhost:5000/topics/1/mailgun.json" -d 'subject=hello&body-plain=world'
{}

Slackに通知が来る。

f:id:winebarrel:20160915202941p:plain

hakoのoneshotを使う

hakoで、バッチ系の一発処理用のoneshotコマンドを使ってみる。

hakoのバージョンは>= hako-0.20.2(要Ruby 2.3)

$ ruby -v
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin15]

$ gem install hako
...
Fetching: hako-0.20.2.gem (100%)
Successfully installed hako-0.20.2
5 gems installed

まず、例のごとくECSクラスタとEC2インスタンスを準備する。

ecs-cli configure -r ap-northeast-1 -c hello-hako-oneshot
ecs-cli up \
  --keypair winebarrel \
  --capability-iam \
  --vpc vpc-... \
  --subnets subnet-... \
  --security-group sg-... \
  --instance-type m4.large

f:id:winebarrel:20160915210052p:plain

おけ。

ログ出力用CloudWatch LogsにLogGroupを作っておく。

aws logs create-log-group --log-group-name my-logs

hakoのyamlは以下のような感じ。

  • hello.yml
scheduler:
  type: ecs
  region: ap-northeast-1
  cluster: hello-hako-oneshot
  desired_count: 1
app:
  image: busybox
  memory: 128
  log_configuration:
    log_driver: awslogs
    options:
      awslogs-group: my-logs
      awslogs-region: ap-northeast-1
      awslogs-stream-prefix: example

実行してみる。

$ hako oneshot hello.yml echo hello
I, [2016-09-15T21:19:19.668005 #50187]  INFO -- : Registered task definition: arn:aws:ecs:ap-northeast-1:822997939312:task-definition/hello-oneshot:2
I, [2016-09-15T21:19:19.795313 #50187]  INFO -- : Started task: arn:aws:ecs:ap-northeast-1:822997939312:task/008875de-41d1-49ea-88c9-29ea1270384e
I, [2016-09-15T21:19:20.880933 #50187]  INFO -- : Container instance is arn:aws:ecs:ap-northeast-1:822997939312:container-instance/71b35bd6-6d06-486a-955a-6dd24b2ca1ec (ECS Instance - amazon-ecs-cli-setup-hello-hako-oneshot i-0a295d5b1ff678c4f)
I, [2016-09-15T21:19:24.158628 #50187]  INFO -- : Started at 2016-09-15 21:19:23 +0900
I, [2016-09-15T21:19:24.158756 #50187]  INFO -- : Stopped at 2016-09-15 21:19:23 +0900 (reason: Essential container in task exited)
I, [2016-09-15T21:19:24.158802 #50187]  INFO -- : Oneshot task finished
I, [2016-09-15T21:19:24.158840 #50187]  INFO -- : app has stopped with exit_code=0

ログを見てみる。

f:id:winebarrel:20160915212010p:plain

f:id:winebarrel:20160915212016p:plain

おけおけ。