Skip to content

Commit 55a7445

Browse files
committed
Add example build of Hubot Slack Docker container image using Ansible.
1 parent ac0a59a commit 55a7445

File tree

7 files changed

+213
-0
lines changed

7 files changed

+213
-0
lines changed

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ env:
1818
distro: ubuntu1604
1919
test_idempotence: false
2020

21+
- playbook: docker-hubot.yml
22+
distro: ubuntu1604
23+
test_idempotence: false
24+
2125
- playbook: drupal.yml
2226
distro: ubuntu1604
2327

docker-hubot/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Docker Example - Hubot Slack bot container image built with Ansible
2+
3+
This is an example lightweight Docker image build using Ansible which builds a usable Hubot chat bot that works with Slack. The example is explained in more detail in Chapter 13 of [Ansible for DevOps](https://www.ansiblefordevops.com/).
4+
5+
## Get a Slack API Token for your bot
6+
7+
Follow the instructions in Slack's guide, [Slack Developer Kit for Hubot](https://slackapi.github.io/hubot-slack/), and get an API token for a bot attached to a new App installed in the workspace of a Slack organization.
8+
9+
## Build the Hubot container image
10+
11+
Make sure you have Docker installed on the workstation or VM where this playbook is located, then enter the command:
12+
13+
$ ansible-playbook main.yml
14+
15+
This will generate a Docker image named `a4d/hubot-slack`, which you should see listed when you run `docker images`.
16+
17+
## Run the Hubot container image
18+
19+
To run the image, run the command below, replacing `TOKEN_HERE` with the API token your bot uses from Slack:
20+
21+
$ docker run -d --name "hubot-slack" -e HUBOT_
22+
SLACK_TOKEN=TOKEN_HERE a4d/hubot-slack
23+
24+
The container should be running in the background. You can inspect the logs from the bot using `docker logs hubot-slack`, and you can kill and remove the container with `docker rm -f hubot-slack`.
25+
26+
## About the Author
27+
28+
This project was created by [Jeff Geerling](https://www.jeffgeerling.com/) as an example for [Ansible for DevOps](https://www.ansiblefordevops.com/).

docker-hubot/main.yml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
- hosts: localhost
3+
connection: local
4+
gather_facts: no
5+
6+
vars:
7+
base_image: node:8
8+
container_name: hubot_slack
9+
image_namespace: a4d
10+
image_name: hubot-slack
11+
12+
pre_tasks:
13+
- name: Make the latest version of the base image available locally.
14+
docker_image:
15+
name: '{{ base_image }}'
16+
force: yes
17+
18+
- name: Create the Docker container.
19+
docker_container:
20+
image: '{{ base_image }}'
21+
name: '{{ container_name }}'
22+
command: sleep infinity
23+
24+
- name: Add the newly created container to the inventory.
25+
add_host:
26+
hostname: '{{ container_name }}'
27+
ansible_connection: docker
28+
29+
roles:
30+
- name: hubot-slack
31+
delegate_to: '{{ container_name }}'
32+
33+
post_tasks:
34+
- name: Clean up the container.
35+
shell: >
36+
apt-get remove --purge -y python &&
37+
rm -rf /var/lib/apt/lists/*
38+
delegate_to: '{{ container_name }}'
39+
args:
40+
warn: no
41+
42+
- name: Commit the container.
43+
command: >
44+
docker commit
45+
-c 'USER hubot'
46+
-c 'WORKDIR "/home/hubot"'
47+
-c 'CMD ["bin/hubot", "--adapter", "slack"]'
48+
-c 'VOLUME ["/home/hubot/scripts"]'
49+
{{ container_name }} {{ image_namespace }}/{{ image_name }}:latest
50+
51+
- name: Remove the container.
52+
docker_container:
53+
name: '{{ container_name }}'
54+
state: absent
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
hubot_owner: Ansible for DevOps
3+
hubot_name: a4dbot
4+
hubot_description: Ansible for DevOps test bot.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
galaxy_info:
2+
author: your name
3+
description: your description
4+
company: your company (optional)
5+
6+
# If the issue tracker for your role is not on github, uncomment the
7+
# next line and provide a value
8+
# issue_tracker_url: http://example.com/issue/tracker
9+
10+
# Some suggested licenses:
11+
# - BSD (default)
12+
# - MIT
13+
# - GPLv2
14+
# - GPLv3
15+
# - Apache
16+
# - CC-BY
17+
license: license (GPLv2, CC-BY, etc)
18+
19+
min_ansible_version: 1.2
20+
21+
# If this a Container Enabled role, provide the minimum Ansible Container version.
22+
# min_ansible_container_version:
23+
24+
# Optionally specify the branch Galaxy will use when accessing the GitHub
25+
# repo for this role. During role install, if no tags are available,
26+
# Galaxy will use this branch. During import Galaxy will access files on
27+
# this branch. If Travis integration is configured, only notifications for this
28+
# branch will be accepted. Otherwise, in all cases, the repo's default branch
29+
# (usually master) will be used.
30+
#github_branch:
31+
32+
#
33+
# platforms is a list of platforms, and each platform has a name and a list of versions.
34+
#
35+
# platforms:
36+
# - name: Fedora
37+
# versions:
38+
# - all
39+
# - 25
40+
# - name: SomePlatform
41+
# versions:
42+
# - all
43+
# - 1.0
44+
# - 7
45+
# - 99.99
46+
47+
galaxy_tags: []
48+
# List tags for your role here, one per line. A tag is a keyword that describes
49+
# and categorizes the role. Users find roles by searching for tags. Be sure to
50+
# remove the '[]' above, if you add tags to this list.
51+
#
52+
# NOTE: A tag is limited to a single word comprised of alphanumeric characters.
53+
# Maximum 20 tags per role.
54+
55+
dependencies: []
56+
# List your role dependencies here, one per line. Be sure to remove the '[]' above,
57+
# if you add dependencies to this list.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
- name: Install dependencies.
3+
package:
4+
name: sudo
5+
state: present
6+
7+
- name: Ensure hubot user exists.
8+
user:
9+
name: hubot
10+
create_home: yes
11+
12+
- name: Install required Node.js packages.
13+
npm:
14+
name: "{{ item }}"
15+
state: present
16+
global: yes
17+
with_items:
18+
- yo
19+
- generator-hubot
20+
21+
- name: Generate hubot.
22+
command: >
23+
yo hubot
24+
--owner="{{ hubot_owner }}"
25+
--name="{{ hubot_name }}"
26+
--description="{{ hubot_description }}"
27+
--adapter=slack
28+
--defaults
29+
chdir=/home/hubot
30+
become: yes
31+
become_user: hubot
32+
33+
- name: Remove certain scripts from external-scripts.json.
34+
lineinfile:
35+
path: /home/hubot/external-scripts.json
36+
regexp: "{{ item }}"
37+
state: absent
38+
with_items:
39+
- 'redis-brain'
40+
- 'heroku'
41+
become: yes
42+
become_user: hubot
43+
44+
- name: Remove the hubot-scripts.json file.
45+
file:
46+
path: /home/hubot/hubot-scripts.json
47+
state: absent

tests/docker-hubot.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
- hosts: all
3+
4+
vars:
5+
pip_install_packages:
6+
# See: https://github.com/ansible/ansible/issues/35612
7+
- name: docker
8+
version: "2.7.0"
9+
10+
pre_tasks:
11+
- name: Update the apt cache.
12+
apt: update_cache=yes cache_valid_time=600
13+
14+
roles:
15+
- geerlingguy.docker
16+
- geerlingguy.pip
17+
18+
# Docker Hubot test.
19+
- import_playbook: ../docker-hubot/main.yml

0 commit comments

Comments
 (0)