ChatOps with Mattermost and Hubot

Ahmet Atalay
5 min readOct 7, 2017

One of the most important duty of DevOps guy is creating an automation of tasks if they are repetitive and executed manually. So, running tasks manually can be waste of time. While you are automating or operating, you can ssh to your server, and run scripts. This can be sometimes risky. For example, some important folders or files can be deleted( rm -rf blabla…) or services, ports can be shut down.

At this point, a while ago, we faced ChatOps paradigm in a DevOps world. In a ChatOps paradigm, while you are chatting with your team-mates in a chat application, you can execute tasks for automation or operation. While you are executing these tasks in a chat application, your other team-mates can see which tasks executed or what logs of the running tasks printed. So, each people in chat channel quickly can take an action. You can see the below ChatOps diagram;

Resource: http://nordicapis.com/12-frameworks-to-build-chatops-bots/

According to above diagram, developers in a Chat app( Slack, HipChat, Mattermost,Rocket.chat etc.) can execute commands.Chat apps send commands to bot with an outgoing integration. Bot parses command, and run the scripts for neccessary operational tasks. Then, bot sends the results(logs) to the Chat app’s channel with an incoming integration.

Now, let’s deep dive into implementation. I use Mattermost as a Chat app. In this blog, I will explain setup of Hubot and its infrastructure in a docker, and integration with Mattermost.

Firstly, Let’s create an instance with an AmazonLinuxAMI, ssh into there and install docker / docker-compose.

ssh -i myDevOps.pem ec2-user@35.12.55.29
sudo yum update -y
# Install docker
sudo yum install -y docker
# Start docker service
sudo service docker start
# Bind docker user to the ec2-u
sudo usermod -a -G docker ec2-user
# Test docker info with sudo
sudo docker info
# Exit instance
exit
# Ssh again to the instance
ssh -i myDevOps.pem ec2-user@35.12.55.29
# Test docker info without sudo
docker info
# Install docker-compose
sudo pip install docker-compose
#Test docker-compose
docker-compose

You can create an incoming integration in a Mattermost as below.

Created Incoming URL will be written to Hubot Dockerfile.

You can create an outgoing integration in a Mattermost as below.

Created outgoing token will be written to Hubot Dockerfile. And, Callback URL will be the public ip of EC2 Instance.

You can clone following repository of Hubot and its infrastructure into instance https://github.com/onedaywillcome1/ChatOps.git

cd ~
git clone https://github.com/onedaywillcome1/ChatOps.git
cd ~/ChatOps/docker/infra
# Execute infra.sh
./infra.sh

External volumes are created for Hubot and Redis. These volumes are essential for data persistency.

cd ~/ChatOps
docker volume create --name hubot_home
docker volume create --name redis_data
docker volume create --name redis_etc

You can open Dockerfile in a Robo folder with a vim an edit as below.

cd ~/ChatOps/docker/robo
vim Dockerfile
#Following parts are modified#The outgoing token is written while creating outgoing-integration in a Mattermost.
ENV MATTERMOST_TOKEN <Mattermost_Outgoing_Token>
# URL is written while creating incoming-integration in a Mattermost.
ENV MATTERMOST_INCOME_URL <Mattermost_Incoming_URL>
#Redis URL is set with public ip of EC2 instance with 6379 port.
redis://35.12.55.29:6379

Docker containers are created from Hubot and Redis docker images with “Docker-Compose”

cd ~/ChatOps/docker
docker-compose build
docker-compose up -d

You can see the Hubot and Redis is up running with a Docker ps command.

You need to install following hubot plugin to execute shell scripts with hubot in a mattermost:
https://github.com/coderofsalvation/hubot-script-shellcmd

Enter to hubot docker container and install plugin

docker exec -it hubot-robo bash
[hubot@71fad547eaab ~]$
npm install hubot-script-shellcmd
[hubot@71fad547eaab ~]$ cp -R node_modules/hubot-script-shellcmd/bash .
#Sample hubot-shellcmd scripts are in the folder below.You can add your specific scripts in here.
[hubot@71fad547eaab ~]$ ls -ltr bash/handlers/
total 12
-rwxrwxr-x 1 hubot hubot 168 Apr 7 08:37 update
-rwxrwxr-x 1 hubot hubot 67 Apr 7 08:37 helloworld
-rwxrwxr-x 1 hubot hubot 140 Apr 7 08:48 rungradle
[hubot@71fad547eaab ~]$ cat bash/handlers/helloworld
#!/bin/bash
echo "Hello.."
exit 0

Stop and start docker-compose

cd ~/ChatOps/docker
docker-compose stop
docker-compose start

Now, you can run script with Hubot from mattermost.

Meanwhile, you can clone your Git repository which contains your deployment scripts into Hubot Docker container and execute your java or shell deployment scripts. On the other hand, you can install python,ruby,groovy, or etc. environments into Dockerfile of Infra and build your images again to create your environment.

Lastly, to prevent any developer’s action in a Hubot, you need a role mechanism. You need to install this plugin and can read this blog to create role mechanism in a Hubot.

If you liked this blog, you can like:) See you until next blog.

--

--