1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ruby2.2.3でKandan+Hubot

Posted at

今話題のHubotを導入し、日々の業務に少しの癒し(リス)と業務効率化を実現したいなと考えていましたが、導入には大きな障害がありました。
現在の業務ではインターネットとは完全に隔離された特殊なネットワーク環境で行うことが多いので、Hubot導入事例によくあるHipChatやSlackは使えませんでした。

そこで今回はOSSチャットアプリ「Kandan」にHubotを導入する方法をご紹介したいと思います。

対象

  • Hubotを手軽に試してみたい人
  • ローカル環境でHubotを導入したい人
  • リスと戯れたい人

環境

  • OS: CentOS 6.7
  • Ruby: 2.2.3
  • MySQL: 5.1.73
  • Kandan: v1.2
  • Hubot: 2.4.7

事前準備

  • 必要なパッケージのインストール
$ sudo yum -y install epel-release
$ sudo yum -y install gcc zlib-devel openssl-devel git gcc-c++ libxml2-devel libxslt-devel nodejs npm
  • Ruby 2.2.3のインストール
$ cd /usr/local/src
$ sudo wget https://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.3.tar.gz
$ sudo tar xvf ruby-2.2.3.tar.gz 
$ cd ruby-2.2.3
$ sudo ./configure
$ sudo make
$ sudo make install
$ sudo /usr/local/bin/gem install bundler
  • MySQL 5.1.73のインストール
$ sudo yum -y install mysql mysql-server mysql-devel MySQL-python 
$ sudo chkconfig mysqld on
$ sudo service mysqld start
$ sudo mysql_secure_installation
  Enter current password for root (enter for none): 
  Set root password? [Y/n] Y
  New password: hoge1234
  Re-enter new password: hoge1234
  Remove anonymous users? [Y/n] Y
  Disallow root login remotely? [Y/n] n
  Remove test database and access to it? [Y/n] Y
  Reload privilege tables now? [Y/n] Y

Kandan v1.2のインストール

  • git clone
$ git clone https://github.com/kandanapp/kandan.git
$ cd kandan
  • Gemfileの編集
  • 今回はDBにMySQLを使用するのでmysql2のコメントアウトを外す
  • 同様にpgをコメントアウト
  • バージョン明記しないとbundle install時に怒られるので追記
$ vim Gemfile
Gemfile
source 'https://rubygems.org'

# Core gems
gem 'rails', '~> 3.2.21'

# Database adapters
# gem 'pg'

# Uncomment next line when using MySQL database
gem 'mysql2', '0.3.20'

# 後略
  • Gemfile.lockの削除
  • 残しておくとエラーが発生するので・・・
$ rm Gemfile.lock
  • bundle install
$ bundle install --without development test --path vendor/bundle
  • databaseのconfig設定
$ vim config/database.yml
config/database.yml
production:
  adapter: mysql2
  host: localhost
  database: kandan_production
  pool: 5
  timeout: 5000
  username: root
  password: hoge1234
  • DBの作成
$ RAILS_ENV=production bundle exec rake db:create db:migrate kandan:bootstrap
  • プリコンパイルの実行
$ vim config/environments/production.rb
config/environments/production.rb
Kandan::Application.configure do
  # Settings specified here will take precedence over those in config/application.rb

  # Code is not reloaded between requests
  config.cache_classes = true

  # Full error reports are disabled and caching is turned on
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true

  # Disable Rails's static asset server (Apache or nginx will already do this)
  config.serve_static_assets = true

# 後略
$ RAILS_ENV=production bundle exec rake assets:precompile
app/controllers/apis_controller.rb
class ApisController < ApplicationController
  before_filter :authenticate_user_from_token!
  before_filter :authenticate_user!
    
  def active_users
app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  protect_from_forgery
  
  skip_before_filter :verify_authenticity_token, if: -> {request.format.json?}
  before_filter :force_approved_account
  before_filter :redirect_suspended_account
app/controllers/channels_controller.rb
class ChannelsController < ApplicationController
  before_filter :authenticate_user_from_token!
  before_filter :authenticate_user!
  before_filter :find_channel_by_name, :only => :show
  load_and_authorize_resource
config/routes.rb
    :sessions => "sessions"
  }
  devise_scope :user do

    # these are allow to access with auth_token
    get "/active_users" => "apis#active_users"
    get "/me" => "apis#me"

    resources :channels do
      resources :activities
      resources :attachments
    end

    authenticated :user do
      root :to => "main#index"

      get '/search' => "main#search"

      resources :users, :only => [:index, :show]

      get "/users/edit" =>"main#users_edit"
  • Kandanの起動
  • デフォルトだと3000ポートで起動
$ bundle exec thin start -p 3000 -e production

Hubot 2.4.7

事前準備

  • Kandan側でHubot用のアカウントとアクセストークンを作成する
$ RAILS_ENV=production bundle exec rake kandan:boot_hubot
$ RAILS_ENV=production bundle exec rake kandan:hubot_access_key
Your hubot access key is アクセストークン

インストール

$ git clone https://github.com/github/hubot.git
$ cd hubot
$ git checkout v2.4.7
$ npm install
$ make package
$ cd hubot

Kandan用Hubotアダプタのインストール

  • git clone
$ git clone https://github.com/kandanapp/hubot-kandan.git node_modules/hubot-kandan
  • npm install
$ npm install faye ntwitter
  • hubot-kandanのバージョンを1.0から1.0.0に変更
$ vim node_modules/hubot-kandan/package.json
node_modules/hubot-kandan/package.json
{
  "name":        "hubot-kandan",
  "version":     "1.0.0",
# 後略
  • hubot-scripts.jsonの編集
  • "redis-brain.coffee"の削除
$ vim hubot-scripts.json
["tweet.coffee", "shipit.coffee"]
  • 環境変数の設定
$ vim ~/.bashrc
~/.bashrc
# Kandan Host Name
export HUBOT_KANDAN_HOST=localhost
# Kandan Port
export HUBOT_KANDAN_PORT=3000
# Hubot Kandan Token
export HUBOT_KANDAN_TOKEN=アクセストークン
# Hubot Log Level
export HUBOT_LOG_LEVEL=debug
# Hubot Port
export PORT=3001
$ source ~/.bashrc
  • Hubotの起動
  • external-scripts.jsonやhubot-scripts.jsonがある階層で実行
$ ./bin/hubot -a kandan
  • Kandan上でHubotがconnectされる
  • @hubot ship it!とメッセージした際にリスの画像が表示されれば成功
  • そのままで使えるコマンドが少ないので、色々とチューニングが必要です

参考にさせていただいた記事

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?