Deploying Hubot to Windows using Desired State Configuration

ChatOps is a term used to describe bringing development or operations (DEVOPS) work that is already being carried out manually by sys-admins/developers/ etc into a centralised work chat such as Zulip, Slack, Mattermost etc. This is made a lot easier by using bots such as Hubot and Errbot. The aim is to have single or multiple teams in that one channel and then bringing tools into the channel so everyone can automate, collaborate and see how automation is used to solve those general day to day DEVOPS tasks. Not only are you able to provide quick access to automation tools, but also the chat/channel will provide you with a history log of who carried out such tasks.

ChatOps can be implemented in pretty much anyway you envisage with the use of tools or scripts that you expose to the chat/schannels using a chat bot. Users in the chat/channel can talk to the bot and have it take actions on their behalf, some examples of this may be:

Checking the status of a Windows Service.

Running a playbook using Ansible Tower or its open-source upstream version AWX on a Linux Server by invoking the Web API

Querying a server to see how much disk space is available.

Deploying an application from GitHub

Bots can also be a great way to expose functionality to low-privileged users such as help desk staff, without having to create web interfaces or forms or grant them any unnecessary permissions.

If you want to read more into the idea and concept of ChatOps, then you can find more information by reading Sean Regan’s blog here.

A popular tool-set for ChatOps is Slack as the chat client, and GitHub’s infamous open-source automation robot Hubot as the chat bot. In this post we will use Slack and Hubot together, but rather than running on a Linux server, we will be installing it on a Windows machine which will allow us to integrate it easily with PowerShell and the hundreds of PowerShell scripts we already use on a daily basis. Of course this is slightly trickier than on Linux due to dependency issues as well as the ability to get it to run as a service so that it can start automatically at boot etc.

So I created a PowerShell module that will handle these tricky bits for you. The module will handle the installation of the dependencies, the setting up of the Windows Service as well as the installation of the chat bot with only a little manual intervention. The module uses PowerShell’s Desired State Configuration to configure the system. You can read up on this here. The module can be installed from the PowerShell Gallery here. You can also find the source code in my GitHub Repository here.

Requirements

A Windows Machine with PowerShell 5.0+. For this tutorial I will be using a Windows 2012 R2 Standard machine with GUI.

Administrative access in your Slack group to create a Hubot integration

Create a Slack Integration for Hubot

Click Browse Apps.

Click View App Directory. This will take you to your slack domain in your web browser in the apps section.

Search for Hubot andchoose Install.

Provide a Hubot username – this will be the name of your bot. For this blog the bot will be called autobot.

Click Add Hubot Integration.

After the integration has been added, you will be provided an API Token, something like xoxb-XXXXX-XXXXXX. We will need this later so note it down.

Additionally, you can customise your bots icon and add channels at this screen.

Installing the InstallHubot PowerShell Module

Install the InstallHubot module by using the following code:

Install-Module -Name InstallHubot -Force

Running the DSC Configuration

Once the module has been successfully, you first need to import the module, this can be done by running

Import-Module -Name InstallHubot -Force

We then need to create our DCS configuration file. You need to make copy of the example file, which can be found here. The only section we need to edit is the below, I recommend using either Windows PowerShell ISE or Visual Studio Code with the PowerShell extension to create and edit this file, due to being able to run the script from an integrated terminal.

The Slack API Key you got earlier from creating the Hubot integration with Slack should be entered instead of xoxb-XXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXX.

The HubotAdapter setting should stay as slack.

The Role setting should stay as Hubot.

The HubotBotName setting can be set to anything you wish except from Hubot.

The NodeName setting is the name of the computer you wish to run this configuration against. I have only tested this against localhost, so it may not work against a remote computer. Feel free to test and let me know.

The HubotBotPath setting can be anywhere you like.

Once you have finalised the above settings, you need to run the script to create the DSC Configuration which will be called Hubot as well as create the $configData array which stores the settings.

The next step is to apply the DSC Configuration which can be done by running the below commands from within an admin PowerShell session:

The server will then ask to be rebooted once it is between 50% and 75% complete. Once you have rebooted the machine, the DSC Configuration will continue for a few minutes. You can track its progress by running Get-DscConfigurationStatus, this will fail with an error whilst the configuration is still running. Once finished, it will return without an error.

You can double check that this is then setup by checking that the Hubot_BotName service is running from within services.msc as well messaging your bot from within slack with @bot-username echo test. This should then return test in the same chat/channel.

Want to know more?

Keep up on my posts, I’ll have a few more coming up on this topic. Also check out my GitHub profile for other things I’m working on.