diff --git a/.travis.yml b/.travis.yml index fe03dbf..3616f8f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,6 +18,10 @@ env: distro: ubuntu1604 test_idempotence: false + - playbook: docker-hubot.yml + distro: ubuntu1604 + test_idempotence: false + - playbook: drupal.yml distro: ubuntu1604 diff --git a/docker-hubot/README.md b/docker-hubot/README.md new file mode 100644 index 0000000..d3fba60 --- /dev/null +++ b/docker-hubot/README.md @@ -0,0 +1,28 @@ +# Docker Example - Hubot Slack bot container image built with Ansible + +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/). + +## Get a Slack API Token for your bot + +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. + +## Build the Hubot container image + +Make sure you have Docker installed on the workstation or VM where this playbook is located, then enter the command: + + $ ansible-playbook main.yml + +This will generate a Docker image named `a4d/hubot-slack`, which you should see listed when you run `docker images`. + +## Run the Hubot container image + +To run the image, run the command below, replacing `TOKEN_HERE` with the API token your bot uses from Slack: + + $ docker run -d --name "hubot-slack" -e HUBOT_ +SLACK_TOKEN=TOKEN_HERE a4d/hubot-slack + +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`. + +## About the Author + +This project was created by [Jeff Geerling](https://www.jeffgeerling.com/) as an example for [Ansible for DevOps](https://www.ansiblefordevops.com/). diff --git a/docker-hubot/main.yml b/docker-hubot/main.yml new file mode 100644 index 0000000..abf6e45 --- /dev/null +++ b/docker-hubot/main.yml @@ -0,0 +1,54 @@ +--- +- hosts: localhost + connection: local + gather_facts: no + + vars: + base_image: node:8 + container_name: hubot_slack + image_namespace: a4d + image_name: hubot-slack + + pre_tasks: + - name: Make the latest version of the base image available locally. + docker_image: + name: '{{ base_image }}' + force: yes + + - name: Create the Docker container. + docker_container: + image: '{{ base_image }}' + name: '{{ container_name }}' + command: sleep infinity + + - name: Add the newly created container to the inventory. + add_host: + hostname: '{{ container_name }}' + ansible_connection: docker + + roles: + - name: hubot-slack + delegate_to: '{{ container_name }}' + + post_tasks: + - name: Clean up the container. + shell: > + apt-get remove --purge -y python && + rm -rf /var/lib/apt/lists/* + delegate_to: '{{ container_name }}' + args: + warn: no + + - name: Commit the container. + command: > + docker commit + -c 'USER hubot' + -c 'WORKDIR "/home/hubot"' + -c 'CMD ["bin/hubot", "--adapter", "slack"]' + -c 'VOLUME ["/home/hubot/scripts"]' + {{ container_name }} {{ image_namespace }}/{{ image_name }}:latest + + - name: Remove the container. + docker_container: + name: '{{ container_name }}' + state: absent diff --git a/docker-hubot/roles/hubot-slack/defaults/main.yml b/docker-hubot/roles/hubot-slack/defaults/main.yml new file mode 100644 index 0000000..7670353 --- /dev/null +++ b/docker-hubot/roles/hubot-slack/defaults/main.yml @@ -0,0 +1,4 @@ +--- +hubot_owner: Ansible for DevOps +hubot_name: a4dbot +hubot_description: Ansible for DevOps test bot. diff --git a/docker-hubot/roles/hubot-slack/meta/main.yml b/docker-hubot/roles/hubot-slack/meta/main.yml new file mode 100644 index 0000000..7223799 --- /dev/null +++ b/docker-hubot/roles/hubot-slack/meta/main.yml @@ -0,0 +1,57 @@ +galaxy_info: + author: your name + description: your description + company: your company (optional) + + # If the issue tracker for your role is not on github, uncomment the + # next line and provide a value + # issue_tracker_url: http://example.com/issue/tracker + + # Some suggested licenses: + # - BSD (default) + # - MIT + # - GPLv2 + # - GPLv3 + # - Apache + # - CC-BY + license: license (GPLv2, CC-BY, etc) + + min_ansible_version: 1.2 + + # If this a Container Enabled role, provide the minimum Ansible Container version. + # min_ansible_container_version: + + # Optionally specify the branch Galaxy will use when accessing the GitHub + # repo for this role. During role install, if no tags are available, + # Galaxy will use this branch. During import Galaxy will access files on + # this branch. If Travis integration is configured, only notifications for this + # branch will be accepted. Otherwise, in all cases, the repo's default branch + # (usually master) will be used. + #github_branch: + + # + # platforms is a list of platforms, and each platform has a name and a list of versions. + # + # platforms: + # - name: Fedora + # versions: + # - all + # - 25 + # - name: SomePlatform + # versions: + # - all + # - 1.0 + # - 7 + # - 99.99 + + galaxy_tags: [] + # List tags for your role here, one per line. A tag is a keyword that describes + # and categorizes the role. Users find roles by searching for tags. Be sure to + # remove the '[]' above, if you add tags to this list. + # + # NOTE: A tag is limited to a single word comprised of alphanumeric characters. + # Maximum 20 tags per role. + +dependencies: [] + # List your role dependencies here, one per line. Be sure to remove the '[]' above, + # if you add dependencies to this list. \ No newline at end of file diff --git a/docker-hubot/roles/hubot-slack/tasks/main.yml b/docker-hubot/roles/hubot-slack/tasks/main.yml new file mode 100644 index 0000000..7fc4196 --- /dev/null +++ b/docker-hubot/roles/hubot-slack/tasks/main.yml @@ -0,0 +1,47 @@ +--- +- name: Install dependencies. + package: + name: sudo + state: present + +- name: Ensure hubot user exists. + user: + name: hubot + create_home: yes + +- name: Install required Node.js packages. + npm: + name: "{{ item }}" + state: present + global: yes + with_items: + - yo + - generator-hubot + +- name: Generate hubot. + command: > + yo hubot + --owner="{{ hubot_owner }}" + --name="{{ hubot_name }}" + --description="{{ hubot_description }}" + --adapter=slack + --defaults + chdir=/home/hubot + become: yes + become_user: hubot + +- name: Remove certain scripts from external-scripts.json. + lineinfile: + path: /home/hubot/external-scripts.json + regexp: "{{ item }}" + state: absent + with_items: + - 'redis-brain' + - 'heroku' + become: yes + become_user: hubot + +- name: Remove the hubot-scripts.json file. + file: + path: /home/hubot/hubot-scripts.json + state: absent diff --git a/tests/docker-hubot.yml b/tests/docker-hubot.yml new file mode 100644 index 0000000..9ac6d77 --- /dev/null +++ b/tests/docker-hubot.yml @@ -0,0 +1,19 @@ +--- +- hosts: all + + vars: + pip_install_packages: + # See: https://github.com/ansible/ansible/issues/35612 + - name: docker + version: "2.7.0" + + pre_tasks: + - name: Update the apt cache. + apt: update_cache=yes cache_valid_time=600 + + roles: + - geerlingguy.docker + - geerlingguy.pip + +# Docker Hubot test. +- import_playbook: ../docker-hubot/main.yml