3
2

More than 5 years have passed since last update.

EC2でslack hubotを動かす

Posted at

初EC2を立てるまで
でせっかく立てたので早速hubotを動かしたいと思います。

nodeインストール

nodebrewをインストール

$ wget git.io/nodebrew
$ perl nodebrew setup
Fetching nodebrew...
Installed nodebrew in $HOME/.nodebrew

========================================
Export a path to nodebrew:

export PATH=$HOME/.nodebrew/current/bin:$PATH
========================================

パス通す

export PATH=$HOME/.nodebrew/current/bin:$PATH」を追加する。EC2起動時にもパスを有効にするために設定。

$ vi ~/.bashrc
$ cat ~/.bashrc 
# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
    . /etc/bashrc
fi

# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=

# User specific aliases and functions
export PATH=$HOME/.nodebrew/current/bin:$PATH

今使えないので、exportもしとく。

$ export PATH=$HOME/.nodebrew/current/bin:$PATH

無事に入りました。

$ nodebrew -v
nodebrew 0.9.8

Usage:
    nodebrew help                         Show this message
    nodebrew install <version>            Download and install <version> (compile from source)
    nodebrew install-binary <version>     Download and install <version> (binary file)
    nodebrew uninstall <version>          Uninstall <version>
    nodebrew use <version>                Use <version>
    nodebrew list                         List installed versions
    nodebrew ls                           Alias for `list`
    nodebrew ls-remote                    List remote versions
    nodebrew ls-all                       List remote and installed versions
    nodebrew alias <key> <value>          Set alias
    nodebrew unalias <key>                Remove alias
    nodebrew clean <version> | all        Remove source file
    nodebrew selfupdate                   Update nodebrew
    nodebrew migrate-package <version>    Install global NPM packages contained in <version> to current version
    nodebrew exec <version> -- <command>  Execute <command> using specified <version>

Example:
    # install from binary
    nodebrew install-binary v0.10.22

    # use a specific version number
    nodebrew use v0.10.22

    # io.js
    nodebrew install-binary io@v1.0.0
    nodebrew use io@v1.0.0

nodeインストール

stableで安定版が入ります。

$ nodebrew install-binary stable
Fetching: https://nodejs.org/dist/v8.9.4/node-v8.9.4-linux-x64.tar.gz
######################################################################## 100.0%
Installed successfully
$ nodebrew use stable
use v8.9.4
$ node -v
v8.9.4
$ npm -v
5.6.0

hubot使うため各種インストール

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

ひとまずこれで動きます。

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

ただしこのままだと、インスタンス起動時に自動起動してくれません。

slack hubotを永続で動かす

foreverインストール

$ npm install -g forever

bin/hubot書き換え

↓コメントアウトする
exec node_modules/.bin/hubot --name "slackhubot" "$@"

↓追加する
forever start -w -c coffee node_modules/.bin/hubot --adapter slack

実行&確認

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

forever listで確認できます。

$ forever list
info:    Forever processes running
data:        uid  command script                                  forever pid  id logfile                          uptime     
data:    [0] gJet coffee  node_modules/.bin/hubot --adapter slack 8748    8758    /home/ec2-user/.forever/gJet.log 0:0:0:5.56  

停止

uidを指定することで停止できます。

$ forever stop gJet
info:    Forever stopped process:
    uid  command script                                  forever pid  id logfile                          uptime       
[0] gJet coffee  node_modules/.bin/hubot --adapter slack 8748    8758    /home/ec2-user/.forever/gJet.log 0:0:1:19.310 
$ forever list
info:    No forever processes running

起動スクリプト

起動スクリプトも作っておきます

$ vi start_slackhubot.sh
$ cat start_slackhubot.sh 
#!/bin/bash

DIR=<hubotのあるディレクトリ>
cd $DIR
HUBOT_SLACK_TOKEN=<HUBOT_SLACK_TOKEN> ./bin/hubot --adapter slack

実行

$ chmod u+x start_slackhubot.sh 
$ ./start_slackhubot.sh 
$ forever list
info:    Forever processes running
data:        uid  command script                                  forever pid  id logfile                          uptime      
data:    [0] Lz2q coffee  node_modules/.bin/hubot --adapter slack 8935    8945    /home/ec2-user/.forever/Lz2q.log 0:0:0:4.635 

良さげ。

EC2起動時に自動起動する設定

cronに設定にします。

$ crontab -e
no crontab for ec2-user - using an empty one
crontab: installing new crontab
$ crontab -l
@reboot /home/ec2-user/start_slackhubot.sh > ~/slackhubot.log 2>&1

rebootして動くか確認。

$ sudo reboot

ログインして確認するとまさかのやつ

$ cat slackhubot.log 
./bin/hubot: line 5: npm: command not found

bin/hubotの4行目に以下を追加。

export PATH="$HOME/.nodebrew/current/bin:$PATH"

再度reboot後、見事動きました。

$ forever list
info:    Forever processes running
data:        uid  command script                                  forever pid  id logfile                          uptime       
data:    [0] TxT1 coffee  node_modules/.bin/hubot --adapter slack 3181    3191    /home/ec2-user/.forever/TxT1.log 0:0:1:59.748 

今回は以上です。

3
2
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
3
2