Skip to content

Instantly share code, notes, and snippets.

@buty4649
Created October 9, 2014 15:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buty4649/d0b6aaee8d3cb4c9763b to your computer and use it in GitHub Desktop.
Save buty4649/d0b6aaee8d3cb4c9763b to your computer and use it in GitHub Desktop.
ITインフラ業務自動化現状確認会の資料

hubotを使ってIRCから便利にする

自己紹介

  • 高谷雄貴
  • 2014/02にペパボに入社
    • 前職ではSEとかSIerとか言われることをやっていた
    • EC系、ヘテムルのインフラを見てます
    • munin職人

最近、puppet業を始めました

のでそちらのお話しはあまりできません。。。

しかし、最近hubotなる おもちゃ 便利ツールを使い始めたのでこれを紹介したいと思います。

hubotとは

Github社が開発したチャットbotフレームワークです。

Node.jsで動いていて、スクリプトはcoffeescriptを使って書かれています。 (Javascriptでも動く)

hubotの構成

-----------------------------
|hubot                      |
|  [Robot]  <-> [brain] ----|-> [redis or file or ...]
|     ^                     |
|     |                     |
|     --------> [Adapter]---|-> [IRC or Slack or Twitter or ...]
----------------------------
```


* Robot
   * Hubot本体
   * メッセージを受け取って処理する
* brain
   * 外部ストレージと連携しデータの永続化を行う
* Adapter
   * チャットツールとRobotを接続する


### hubotのインストールと実行

#### Mac
```
$ brew install node
$ npm install -g coffee-script hubot
```

#### プロジェクト作成
```
$ hubot -c test
```

#### とりあえず実行
```
$ cd test
$ ./bin/hubot

Hubot>
Hubot> hubot ping
Hubot> PONG
```



### 構成

```
+ Procfile
+ README.md
+ bin
   +- hubot
   +- hubot.cmd
+ external-scripts.json
+ hubot-scripts.json
+ node_modules
+ package.json
+ scripts
   +- events.coffee
   +- google-images.coffee
   +- help.coffee
   +- httpd.coffee
   +- maps.coffee
   +- ping.coffee
   +- pugme.coffee
   +- roles.coffee
   +- rules.coffee
   +- storage.coffee
   +- translate.coffee
   +- youtube.coffee
```


Github社謹製スクリプト
https://github.com/github/hubot-scripts


### scriptの作成
scriptsディレクトリ配下にスクリプトを作成する

以下、ping.coffeeから抜粋

```
module.exports = (robot) ->
  robot.respond /PING$/i, (msg) ->
    msg.send "PONG"
```


`respond` を `hear` にするとすべての会話を対象とする



### 外部プログラムの呼び出し
```
     cmd = "#{httping} -c 5 -t 10 #{url} | tail -3"

     exec cmd, (err, stdout, stderr) ->
        if err
                msg.send "httping error:" + stderr
        else
                for line in stdout.split("\n")
                        msg.send line if line.length > 0
```

exec関数を使えば外部プログラムが呼び出せる



### 作った奴
* muninのリンク返す奴
* nagiosのリンク返す奴
* httpingする奴
* テストメールを送る奴(最近動かない)
* リマインドする奴

```
zipper>munin manage002
 ecbot>ξ*゚⊿゚)ξ< http://***/munin/manage/manage002.shop-pro.jp/index.html
zipper>nagios manage002
 ecbot>ξ*゚⊿゚)ξ< http://***/nagios/cgi-bin/status.cgi?host=manage002.shop-pro.jp
 ecbot>ξ*゚⊿゚)ξ< http://***/nagios/cgi-bin/status.cgi?host=manage002.shop-pro.lan
```


### もっと便利な使い方
#### 自動デプロイする
```
 tnmt>hubot deploy heteml puppet to production
hubot>tnmt started to deploy heteml puppet master to production
```


#### セキュリティアップデートを自動化する奴
参考: http://blog.glidenote.com/blog/2014/09/12/chatops-security-update/







### 参考資料
* [Hubot + CoffeeScript ではじめるやわらかプログラミング入門](https://gist.github.com/udzura/0cb2447c305c51670414)
* [GitHub社謹製! bot開発・実行フレームワーク「Hubot」](http://gihyo.jp/dev/serial/01/hubot/0002)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment