ラベル Mattermost の投稿を表示しています。 すべての投稿を表示
ラベル Mattermost の投稿を表示しています。 すべての投稿を表示

2017年3月10日金曜日

Mattermost で Hubot を連携してみた

概要

前回 Mattermost のインストールと動作確認までやりました
今回は Mattermost と Hubot を連携させてチャットオプスの第一歩を踏み出したいと思います

環境

  • CentOS 6.7 Final
  • Mattermost 2.0.0
  • npm 2.14.12
  • node 4.2.5
  • Hubot 2.18.0

Hubot インストール

まずは Hubot をインストールします
Hubot は npm でインストールできるのであらかじめ node と npm をインストールしておいてください

  • npm install -g yo generator-hubot --allow-root

また、Hubot の動作には保存領域として Redis が必要になるので Redis をインストールしておいてください

  • yum -y install redis

hubot-mattermost アダプタのインストール

hubot-mattermost アダプタを使うことで Mattermost と Hubot の連携を行います
適当な作業ディレクトリに移動してアダプタをインストールしつつ bot を作成します

  • chmod g+rwx /root /path/to/work
  • chmod -R g+rwx /root/.npm
  • cd /path/to/work
  • yo hubot --adapter mattermost

いきなり権限をいろいろと変更しています
これは YEOMAN が動作するために行っているのですが、 yo コマンドのトラブルシューティングとして権限周りのエラーが多発するみたいです
自分の場合は「作業ディレクトリ」と「npm 用のキャッシュディレクトリ」あたりの権限を変更しましたが、この辺は環境によってパスが変わってくるので臨機応変に対応してください
yo hubot を実行して Try & Error で解決していく感じになると思います

とりあえず問題なく hubot-mattermost アダプタ入りの bot が作成されれば OK です

                     _____________________________
                    /                             \
   //\              |      Extracting input for    |
  ////\    _____    |   self-replication process   |
 //////\  /_____\   \                             /
 ======= |[^_/\_]|   /----------------------------
  |   | _|___@@__|__
  +===+/  ///     \_\
   | |_\ /// HUBOT/\\
   |___/\//      /  \\
         \      /   +---+
          \____/    |   |
           | //|    +===+
            \//      |xx|

? Owner User <user@example.com>
? Bot name mattermost2
? Description A simple helpful robot for your Company
   create bin/hubot
   create bin/hubot.cmd
   create Procfile
   create README.md
   create external-scripts.json
   create hubot-scripts.json
   create .gitignore
   create package.json
   create scripts/example.coffee
   create .editorconfig
                     _____________________________
 _____              /                             \
 \    \             |   Self-replication process   |
 |    |    _____    |          complete...         |
 |__\\|   /_____\   \     Good luck with that.    /
   |//+  |[^_/\_]|   /----------------------------
  |   | _|___@@__|__
  +===+/  ///     \_\
     | |_\ /// HUBOT/\\
   |___/\//      /  \\
         \      /   +---+
          \____/    |   |
           | //|    +===+
            \//      |xx|

hubot-maps@0.0.2 node_modules/hubot-maps

hubot-help@0.1.3 node_modules/hubot-help

hubot-shipit@0.2.0 node_modules/hubot-shipit

hubot-google-images@0.2.6 node_modules/hubot-google-images

hubot-diagnostics@0.0.1 node_modules/hubot-diagnostics

hubot-rules@0.1.1 node_modules/hubot-rules

hubot-google-translate@0.2.0 node_modules/hubot-google-translate

hubot-pugme@0.1.0 node_modules/hubot-pugme

hubot-heroku-keepalive@1.0.2 node_modules/hubot-heroku-keepalive

hubot-redis-brain@0.0.3 node_modules/hubot-redis-brain
mqq redis@0.8.4

hubot-mattermost@1.0.7 node_modules/hubot-mattermost
mqq parent-require@1.0.0

hubot-scripts@2.16.2 node_modules/hubot-scripts
mqq redis@0.8.4

hubot@2.18.0 node_modules/hubot
tqq optparse@1.0.4

tqq async@0.9.2
tqq log@1.4.0
tqq scoped-http-client@0.11.0
tqq coffee-script@1.6.3
tqq chalk@1.1.1 (supports-color@2.0.0, escape-string-regexp@1.0.5, has-ansi@2.0.0, strip-ansi@3.0.1, ansi-styles@2.2.0)
tqq connect-multiparty@1.2.5 (qs@2.2.5, on-finished@2.1.1, type-is@1.5.7, multiparty@3.3.2)
mqq express@3.21.2 (commander@2.6.0, escape-html@1.0.2, cookie@0.1.3, merge-descriptors@1.0.0, cookie-signature@1.0.6, utils-merge@1.0.0, content-type@1.0.1, vary@1.0.1, methods@1.1.2, range-parser@1.0.3, etag@1.7.0, parseurl@1.3.1, basi
c-auth@1.0.3, fresh@0.3.0, content-disposition@0.5.0, depd@1.0.1, debug@2.2.0, proxy-addr@1.0.10, send@0.13.0, mkdirp@0.5.1, connect@2.30.2)

Mattermost の Incomming Webhooks を有効にする

Mattermost の UI 上で設定します
左上のオプションメニューから以下の項目を有効にします

  • System Console -> Enable Incoming Webhooks: -> true -> [Save]

mm_enable_incoming.png

Incoming Webhook を有効にしたら、チャネルに対して Incoming Webook を追加します

  • Account Setting -> Integrations -> Incoming Webhooks -> Add a new incoming webhook -> Town Square -> [Add]

mm_setting_incoming.png

ここで表示された Incoming 用の URL をメモしておきます

Mattermost の Outgoing Webhooks を有効にする

Mattermost の UI 上で設定します
左上のオプションメニューから以下の項目を有効にします

  • System Console -> Enable Outgoing Webhooks: -> true -> [Save]

mm_enable_outgoing.png

Outgoing Webhook を有効にしたら、チャネルに対して Outgoing Webook を追加します

  • Account Setting -> Integrations -> Outgoing Webhooks
    • -> Channel -> Town Square
    • -> Callback URLs: -> http://localhost:8080/hubot/incoming -> [Add]

mm_setting_outgoing.png

設定した URL と取得できた Token をメモしておきます

その他 Mattermost の設定

以下も設定しておきましょう
簡単に説明すると作成した bot のアイコンを上書きする設定や名前を上書きする設定を有効にしています

  • System Console
    • -> Enable Slash Commands: -> true
    • -> Enable Integrations for Admin Only: -> false
    • -> Enable Overriding Usernames from Webhooks and Slash Commands: -> true
    • -> Enable Overriding Icon from Webhooks and Slash Commands: -> true
      • -> [Save]

必要な環境変数を設定して hubot を起動

それでは Hubot を起動していきます
取得した各種 Webhook URL と Token を使って環境変数を設定し hubot を起動します
今回 hubot は Mattermost と同じホスト上で動作しているのでホスト名は localhost を使っていますが、hubot が別サーバにある場合は Mattermost 側のサーバ名を適切に設定してください

  • cd /path/to/work
  • export MATTERMOST_ENDPOINT=”/hubot/incoming”
  • export MATTERMOST_INCOME_URL=”http://localhost:8065/hooks/9d8y79i6yigh8bg1pximy1w3sh”
  • export MATTERMOST_TOKEN=”kap36u7ejtdo8cwbeag8fuhtxw”
  • export MATTERMOST_ICON_URL=”https://s3-eu-west-1.amazonaws.com/renanvicente/toy13.png”
  • export MATTERMOST_HUBOT_USERNAME=”matterbot”
  • bin/hubot -a mattermost -n hubot

簡単に各種環境変数の説明をすると

  • MATTERMOST_ENDPOINT・・・Outgoing Webhook の設定で取得できた URI を入力します、ここはホスト名部分は省略して記載します
  • MATTERMOST_INCOME_URL・・・Incoming Webhook の設定で取得できた URL を入力します
  • MATTERMOST_TOKEN・・・Outgoing Webhook の設定で取得できた Token を入力します
  • MATTERMOST_ICON_URL・・・ bot のアイコンがある URL を指定します
  • MATTERMOST_HUBOT_USERNAME・・・ Mattermost 上で表示される bot の名前を入力します

環境変数を指定したら起動するだけです
今回は -a でアダプタを指定して、-n で bot を呼び出すコマンド名を指定します
先程、環境変数で指定した bot の名前とは異なるので注意してください

起動後エラーがでないことを確認しましょう

動作確認

ここまでくればあとは Mattermost 上で bot を呼び出せば応答してくれるはずです
Town Square に戻って hubot help と入力してみましょう
bot が応答してコマンドのヘルプを表示してくれるはずです

mm_check_bot_action.png

最後に

紹介は以上です

はまりそうなポイントは

  • yo コマンドで bot 作成する部分
  • Incoming, Outgoing Webhook を設定後にそれらの情報を環境変数に設定して hubot を起動する部分
  • Mattermost <--> Hubot 間の ACL の設定

あたりかなと思います

hubot は特にエラーを吐いてくれないので環境変数の値を変更して再起動して試すみたいな感じで動作確認を進めました

あと、hubot の起動はフォアグランド起動なので、バックグランドで起動できるような起動スクリプトも本格的に運用するなら必要かなと思います

参考サイト

CentOS6 に Mattermost をインストール

概要

“Mattermost”:http://www.mattermost.org/ は Slack 互換のオープンソースなチャットツールです
今回は CentOS6 のシングル構成のサーバにインストールしてみました

環境

  • CentOS 6.7 Final
  • Mattermost 2.0.0
  • PostgreSQL 9.4.6

インストール

Mattermost は DB に PostgreSQL か MySQL を選択することができます
今回は PostgreSQL を使って動作させてみたいと思います

PostgreSQL のインストール

root で作業を行います

  • yum install http://yum.postgresql.org/9.4/redhat/rhel-6-x86_64/pgdg-redhat94-9.4-1.noarch.rpm
  • yum install postgresql94-server postgresql94-contrib
  • service postgresql-9.4 initdb
  • chkconfig postgresql-9.4 on

PostgreSQL 用の設定ファイルを編集

root で作業を行います

  • vi /var/lib/pgsql/9.4/data/postgresql.conf

#listen_addresses = 'localhost' の部分を必要であれば '*' に変更してください
今回はシングル公正なのでそのままでいきます

  • vi /var/lib/pgsql/9.4/data/pg_hba.conf

PostgreSQL で localhost にアクセスするときにパスワード認証ができるように修正します
以下の diff になるように修正してください

82c82
< host    all             all             127.0.0.1/32            trust
---
> host    all             all             127.0.0.1/32            ident
  • service postgresql-9.4 start

修正できたら PostgreSQL を起動します

Mattermost 用のデータベースの作成

インストールした PostgreSQL にデータベースを作成します

  • sudo -i -u postgres
  • psql

ここでプロンプトが psql になります
データベースとそのデータベースにアクセスするユーザを作成します

  • postgres=# CREATE DATABASE mattermost;
  • postgres=# CREATE USER mmuser WITH PASSWORD ‘mmuser_password’;
  • postgres=# GRANT ALL PRIVILEGES ON DATABASE mattermost to mmuser;

完了したら \q でプロンプトを抜けましょう
psql ユーザもログアウトして OK です

Mattermost のインストール

root で作業を行います

  • cd /var/tmp
  • wget https://github.com/mattermost/platform/releases/download/v2.0.0/mattermost.tar.gz
  • tar zvxf mattermost.tar.gz
  • sudo mv mattermost /opt
  • sudo mkdir -p /opt/mattermost/data
  • sudo useradd -r mattermost -U
  • sudo chown -R mattermost:mattermost /opt/mattermost
  • sudo chmod -R g+w /opt/mattermost
  • cd /opt/mattermost/config
  • sudo vi config.json
    • DriverName": "mysql" -> DriverName": "postgres"
    • "DataSource": "mmuser:mostest@tcp(dockerhost:3306)/mattermost_test?charset=utf8mb4,utf8" -> "DataSource": "postgres://mmuser:mmuser_password@localhost:5432/mattermost?sslmode=disable&connect_timeout=10"

コマンドを並べていますが Mattermost をダウンロードして解凍後、/opt 配下にデプロイしています
デプロイ後必要な設定ファイルを編集して PostgreSQL を使えるようにします

Mattermost の起動

デプロイできたら起動します
起動スクリプトがないので、直接バイナリを実行します

  • cd /opt/mattermost/bin
  • ./platform

これでエラーなく起動すれば OK です
http://servername:8065/ にアクセスして使用するための設定を行います

動作確認

アクセスするとまず E メールアドレスを登録する必要があります
mm_registration_email.png

E メールアドレスを入力して問題なければ、correct を選択して次に進みます
mm_confirm_email.png

チーム名を作成します
このチーム名がチャットルームの名前にもなります
決まったら次に進みます
mm_registration_team.png

チャットルームの URL を決定します
デフォルトではチーム名がそのまま URL になります
問題なければ次に進みます
mm_registration_url.png

登録したメールアドレスに紐づくユーザ名を決定します
ルールにしたがって適当に決めてください
mm_registration_username.png

登録したユーザのパスワードを決定します
mm_registration_password.png

これで初期設定は完了です
あとはチャットルームに移動するのでこれでチャットを始めることができます
mm_start_chat.png

Emoticon もデフォルトでいろいろ使えることができます

最後に

Mattermost をインストールして使えるところまでやってみました
今回は起動スクリプトで実行していないので、停止する場合は Ctrl + c で終了してください
せっかくなので、Hubot とかのボットとも連携してみたいなと思っています

参考サイト