SHIBUYA 3%

(元在宅が)渋谷で働くエンジニアの備忘録的memo & 雑記 - ココロはいつもSHIBUYAに在り

SlackとHubotを連携させてherokuにデプロイする

ChatOpsという運用スタイルをちょっとずつ導入していきたいと思い、
まずはその準備として、環境構築からチャットツール(ココではSlack)に
いろいろ投稿できるようにするところまで。

※いずれ
Pull Requestしたら通知したり、CIでテスト終了後のステータスを通知したり・・
node.jsとCoffeeScriptはあとで追々勉強しよう。

環境構築

とりあえずローカルはMacで構築

NodeとRedisをインストール

$ brew install node redis

HubotとCoffee Scriptをnpmでインストール

$ npm install -g hubot coffee-script

redis起動

LaunchRocket使うと楽なので私はコレを使います。

LaunchRocket
https://github.com/jimbojsb/launchrocket

もちろん直接起動コマンドを叩いても問題ありません。

redis-server

bot作成

$ hubot --create slackbot
$ cd slackbot

動作確認

bin/hubot
Hubot> hubot ping
Hubot> PONG
Hubot> hubot time
Hubot> Server time is: Wed Oct 08 2014 01:06:59 GMT+0900 (JST)
Hubot> hubot echo ニーハオ
Hubot> ニーハオ

SlackとHubotを連携させる

参照:https://github.com/tinyspeck/hubot-slack

$ npm install hubot-slack --save

Herokuにあげるので実行コマンドをProcfileに記載

web: bin/hubot --adapter slack

いろいろ考えた結果とりあえずRedis不要・・・

$ vim hubot-scripts.json

redis-brain.coffeeの記述を削除

GithubにPush

$ git init
$ git add -A
$ git commit -m "first commit"
$ git remote add origin https://github.com/tomoya-k31/slackbot
$ git push origin master

おまけ

git push -u origin master とすると次回から git push だけで勝手に origin master で push してくれる。

URL:/hubot/testにリクエストがあった時に、Slackに「hello there!」と書き込まれる

$ vim scripts/hook-slack.coffee

以下を記述

module.exports = (robot) ->
  robot.router.get "/hubot/test",  (req,  res) ->
    robot.messageRoom '#general', 'hello there'
    console.log "Send [Hello There!]"
    res.send 'OK'

HerokuへPushする前にローカルで動作確認

以下、SlackのIntegrationsに表示されるtoken/team/botnameを記載

$ export PORT=18080
$ export HUBOT_SLACK_TOKEN=xxxxxxxxxx
$ export HUBOT_SLACK_TEAM=xxxxxxxxxxx
$ export HUBOT_SLACK_BOTNAME=xxxxxxx
$ bin/hubot --adapter slack

ブラウザからリクエスト
http://localhost:18080/hubot/test

コマンドで

$ curl -XGET http://localhost:18080/hubot/test

Heroku

登録どうぞ

URL: https://www.heroku.com/

Heroku command toolのインストール

アカウント作成

$ brew install heroku-toolbelt

認証鍵をHeroku用に生成

$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/tomo/.ssh/id_rsa): /Users/tomo/.ssh/heroku_rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/tomo/.ssh/heroku_rsa.
Your public key has been saved in /Users/tomo/.ssh/heroku_rsa.pub.
The key fingerprint is:
ca:e1:3b:59:96:75:e0:1b:c2:a3:18:3d:87:72:52:48 tomo@tomo-pc.local
The key's randomart image is:
+--[ RSA 2048]----+
|   .E.           |
.....
....
...
..
.
.
.
+-----------------+

コマンドからログイン

$ heroku login

http://ikezawa-ayaka.blogspot.jp/2012/04/heroku.html

herokuにApps作成

$ heroku create <<--アプリ名-->>
(git)-[master][~/slackbot]
Creating <<--アプリ名-->>... done, stack is cedar
http://<<--アプリ名-->>.herokuapp.com/ | git@heroku.com:<<--アプリ名-->>.git
Git remote heroku added
....
...
..
.
.

※注意

Heroku全体で同一のアプリケーション名は使えないため、すでに別の誰かが登録済みの場合は(Name is already taken)と表示されます。まーお決まりです。

環境変数の設定

以下、SlackのIntegrationsに表示されるtoken/team/botnameを記載

$ heroku config:add HUBOT_SLACK_TOKEN=xxxxxxxxxx
$ heroku config:add HUBOT_SLACK_TEAM=xxxxxxxxxxx
$ heroku config:add HUBOT_SLACK_BOTNAME=xxxxxxx

herokuへデプロイする

$ git push heroku master

※エラー

Identity added: /Users/tomo/.ssh/id_rsa (/Users/tomo/.ssh/id_rsa)
Permission denied (publickey).
fatal: Could not read from remote repository.

あれれ?ん?id_rsaが呼ばれてる??

$ vim ~/.ssh/config

以下を追記

Host heroku.com
    HostName        heroku.com
    IdentityFile    ~/.ssh/heroku_rsa
    IdentitiesOnly  yes

もう一度

$ git push heroku master
Initializing repository, done.
Counting objects: 24, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (21/21), done.
Writing objects: 100% (24/24), 11.01 KiB | 0 bytes/s, done.
Total 24 (delta 0), reused 0 (delta 0)

-----> Node.js app detected
....
...
..
.
.
.
 * [new branch]      master -> master

できた!!

ワーカプロセス数の設定

$ heroku ps:scale web=1

Heroku上のアプリのテスト

以下のURLをブラウザで開く http://<<--アプリ名-->>.herokuapp.com/hubot/test

SlackにHubotのURLを追加して完了