LoginSignup
12
12

More than 5 years have passed since last update.

Vagrant + CentOS + Hubot + HipChat + cron

Last updated at Posted at 2015-12-31

タイトルどおりの組み合わせで、botを動かしたい

事前準備

Vagrant, CentOS

# 古いVagrant情報を消したいとき
# rm -r ~/.vagrant.d

$ mkdir ~/Vagrant/centos-6.7-min
$ cd    ~/Vagrant/centos-6.7-min/

$ vagrant init bento/centos-6.7 --minimal
$ vagrant up
$ vagrant status

# 追加されたBoxを確認
$ vagrant box list

# SSH接続
$ vagrant ssh

# 停止
$ vagrant halt

EPEL 追加

$ sudo yum install epel-release -y

redis インストール, 起動, 自動起動

$ sudo yum install redis  --enablerepo=epel -y

$ redis-server -v
Redis server version 2.4.10 (00000000:0)

$ sudo service   redis restart
$ sudo chkconfig redis on

node.js, npm インストール

  • inherits ~ node.jsの継承をサポートするモジュールutil
# いったんインストール
$ sudo yum install nodejs --enablerepo=epel -y
$ sudo yum install npm    --enablerepo=epel -y
$ sudo npm install -g inherits

$ npm -v
1.3.6
$ node -v
v0.10.36

# n package インストール
$ sudo npm install -g n

# 利用できる node.js バージョンの一覧表示
$ sudo n ls

# Stableの確認
$ sudo n --stable
6.2.2

# Latestの確認
$ sudo n --latest
6.3.1

# Stableをインストール
$ sudo n stable

# n が管理するのは /usr/local/bin/node のほう
$ sudo ln -sf /usr/local/bin/node /usr/bin/node

$ node -v
v6.2.2


# npmのアップデート
$ sudo npm update -g npm
# npm@3.10.5 /usr/local/lib/node_modules/npm と表示されたので、こっちにリンク
$ sudo ln -sf /usr/local/lib/node_modules/npm/cli.js /usr/bin/npm

$ npm -v
3.10.5

CoffeeScript, hubot インストール

  • yo, yeoman ~ ひな型生成ツール
$ sudo npm install -g coffee-script
$ sudo npm install -g hubot

$ sudo npm install -g yo
$ sudo npm install -g generator-hubot

$ coffee -v
CoffeeScript version 1.10.0

$ hubot -v
2.19.0

Botをテンプレートから作成

mkdir -p ~/bot/tachikoma
cd       ~/bot/tachikoma/

# プロジェクト作成
yo hubot

# Bot adapterは hipchat

# Start bot
$ ./bin/hubot

# ping
tachikoma> tachikoma ping

PONG

hubot + hipchat 連携

# 追加モジュール
$ sudo yum install libicu-devel
$ sudo npm install hubot-hipchat --save


# hipchat用 環境変数

## bot用アカウント
$ export HUBOT_HIPCHAT_JID=12345_12345678@chat.hipchat.com
$ export HUBOT_HIPCHAT_PASSWORD=xxxxxxxxxx

## Group admin > API > token … なくても動くようだ
# $ export HUBOT_HIPCHAT_TOKEN=xxxxxxxxx

$ ./bin/hubot -a hipchat

Hubotをデーモン化させる

  • forever ~ node.jsスクリプトをデーモン化するツール
$ sudo npm install -g forever 
start.sh
#!/bin/bash

BOT_HOME="/path/to/bot"
BOT_ID="xxxxxxxxxx@chat.hipchat.com"
BOT_PW="xxxxxxxxxx"

export HUBOT_HIPCHAT_JID=${BOT_ID}
export HUBOT_HIPCHAT_PASSWORD=${BOT_PW}
export HUBOT_HIPCHAT_JOIN_PUBLIC_ROOMS=false
export HUBOT_HIPCHAT_ROOMS=12345_example@conf.hipchat.com
export HUBOT_LOG_LEVEL=debug
export NODE_PATH=$NODE_PATH:$BOT_HOME/node_modules:~/node_modules

# ${BOT_HOME}/bin/hubot --adapter hipchat

# foreverでhubotをデーモンで起動
forever start -c coffee node_modules/.bin/hubot -a hipchat
# 実行中スクリプトの表示
$ forever list

# 実行中スクリプトの停止や再起動
$ forever restart ${Forever uid}
$ forever stop    ${Forever uid}
$ forever stopall

cron 定期実行

# 追加モジュール
$ sudo npm install cron --save
$ sudo npm install time --save

error This version of node/NAN/v8 requires a C++11 compiler … ってエラーがでたら、こちらを参照 => http://qiita.com/zoeponta/items/a2e109304729ffa23d81

cron.coffee
roomId = '12345_example@conf.hipchat.com'

cronJob = require('cron').CronJob

module.exports = (robot) ->

  # method
  send = (ROOM_ID, MESSAGE) ->
    robot.send {room: ROOM_ID}, MESSAGE

  # *(sec) *(min) *(hour) *(day) *(month) *(day of the week)
  new cronJob
    cronTime: '0 59 8 * * 1-5'
    onTick: -> send "#{roomId}", "@here standup! (standup)"
    start: true
    timeZone: 'Asia/Singapore'

参考

12
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
12