SlackからHubot経由で 、サーバのシェルを実行してみよう!

f:id:hiraoy:20150430173353j:plain

こんにちは、久しぶりの登場です。おにぎりエンジニアの平尾です。
今日はおにぎりを、2つ食べました。

今回もSlackネタで書いていきます。

お題は

「SlackからHubot経由で、サーバのシェルを実行してみよう!」

Slackで特定の投稿をした時に、特定のサーバのシェルを実行してみるって感じです。

Slackとは?

これについては、以下におもしろい記事がありました。 tech.sanwasystem.com

Sanwa Systems Tech Blogかい!!!

Hubotとは?

  • GitHub社が開発しNode.jsで書かれている。
  • チャット(Slackなど※1)で動作させる事ができる。
  • ボットに特定のメッセージを送ると、そのメッセージに応じた処理を行える。

※1 adapterで変更できる ChatWorkなどもできるみたい。

ではSlackの設定をします

Slack Home「https://{YOUR_COMPANY}.slack.com/home」

左メニューの 「Integrations」

Integrationsページの検索で「Hubot」を入力し、「add」

f:id:hiraoy:20150428163238p:plain

次にボットの名前を聞かれるので、今回は「test_bot」を入力し「add Hubot Integrations」

f:id:hiraoy:20150428163510p:plain

なんと設定はこれだけ
(defaultでは、generalにtest_botがjoinされます。他のchannelでもbotを動かした場合inviteしてください)

f:id:hiraoy:20150428164149p:plain

このページのAPI_TOKENをメモしておきしましょう。

次に、投稿した時にシェルを実行するサーバ側の設定です。

以下rootで実行

EPELのリポジトリを追加(EPELパッケージを導入し、標準パッケージに含まれないパッケージをyumでインストールできるようにする。)

rpm -ivh http://ftp.riken.jp/Linux/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm

npm(Node.js)とRedis(Node.jsのRedisクライアントモジュールで使用)をインストール

yum install npm redis

Redisを起動する

/etc/init.d/redis start

Redis 自動起動設定

chkconfig redis on

hubot yo generator-hubot をインストール
(hubot は yo(Yeoman ジェネレーター)でインストールするようになっています)

npm install -g coffee-script hubot
npm install -g yo generator-hubot

公式のドキュメント
hubot/docs at v2.9.3 · hubotio/hubot · GitHub

以下Hubot実行ユーザ(今回はhogeユーザ)

cd ~
#bot用ディレクトリを作成する
mkdir mybot
cd mybot

#bot を作成する
yo hubot

以下入力を求められます。 Bot adapter で Slack を入力。他は適当で

Owner: (User <user@example.com>) 
Bot name: (test) 
Description: (A simple helpful robot for your Company)
Bot adapter: (campfire) slack #ここ

coffee スクリプトを書いてみる(動くか確認用)

vim scripts/hoge.coffee
module.exports = (robot) ->
  robot.hear /hello/, (msg) ->
    msg.send msg.random [
        'hello',
        'what?'
    ]

hello とコメントしたら、hello か what? が返ってくるスクリプト

ではHubot を起動してみましょう

HUBOT_SLACK_TOKEN=<API_TOKEN> ./bin/hubot --adapter slack 

API_TOKENはslackで hubot を設定した時にメモったものです

general チャンネルで
test_bot: hello
を投稿します

f:id:hiraoy:20150428183258p:plain

動きましたね~

では本題の実行するシェルを書きます。

mkdir scripts/shell
vim  scripts/shell/test.sh
#!/bin/sh
pwd
whoami

シェルを実行するcoffee スクリプトを書きます。

vim scripts/slack.coffee
module.exports = (robot) ->
  robot.respond /test_shell/, (msg) ->
    @exec = require('child_process').exec
    command = "sh /home/hoge/mybot/scripts/shell/test.sh"
    msg.send "Command: #{command}"
    @exec command, (error, stdout, stderr) ->
      msg.send error if error?
      msg.send stdout if stdout?
      msg.send stderr if stderr?

test_shell をbot に向け投稿した場合
sh /home/hoge/mybot/scripts/shell/test.sh
を実行し結果をコンソールに返すスクリプト

では Hubot を、再起動して投稿してみます

f:id:hiraoy:20150428215514p:plain

結果は、Hubotのrootディレクトリ と Hubotを実行しているユーザが返ってきました。

そうなんです!
投稿してシェルが実行できるって事は、Slackからなんでもできちゃいますね~

DBの値をSlackに返したり
deploy したりとか色々な可能性がありますね

お試しあれ

今回のおすすめおにぎりは「もち麦入りおにぎり 枝豆と塩昆布」

噛んだ時に、もち麦がプチプチしていておもしろい食感ですね!
要チェック!

http://natural.lawson.co.jp/recommend/commodity/00001/006707.html