Skip to content

Commit

Permalink
Merge pull request #125 from StackStorm/st2chatops_role
Browse files Browse the repository at this point in the history
Add st2chatops role
  • Loading branch information
humblearner authored Mar 21, 2017
2 parents 5f0977b + bb4e4b6 commit 1f7a1b6
Show file tree
Hide file tree
Showing 11 changed files with 303 additions and 3 deletions.
1 change: 1 addition & 0 deletions .kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ provisioner:
idempotency_test: true
extra_vars:
st2_pkg_repo: <%= ENV['ST2_REPO'] || 'stable' %>
hubot_token: <%= ENV['HUBOT_TOKEN'] %>

platforms:
# Ubuntu Trusty with Upstart
Expand Down
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Below is the list of variables you can redefine in your playbook to customize st
| **st2repo**
| `st2_pkg_repo` | `stable` | StackStorm PackageCloud repository to install. [`stable`](https://packagecloud.io/StackStorm/stable/), [`unstable`](https://packagecloud.io/StackStorm/unstable/), [`staging-stable`](https://packagecloud.io/StackStorm/staging-stable/), [`staging-unstable`](https://packagecloud.io/StackStorm/staging-unstable/)
| **st2**
| `st2_version` | `latest` | StackStorm version to install. Use latest `latest` to get automatic updates or pin it to numeric version like `2.1.1`.
| `st2_version` | `latest` | StackStorm version to install. Use latest `latest` to get automatic updates or pin it to numeric version like `2.2.0`.
| `st2_revision` | `1` | StackStorm revision to install. Used only with pinned `st2_version`.
| `st2_system_user` | `stanley` | System user from which st2 will execute local/remote shell actions.
| `st2_system_user_in_sudoers` | `yes`| Add `st2_system_user` to the sudoers (recommended for most `st2` features to work).
Expand All @@ -43,10 +43,15 @@ Below is the list of variables you can redefine in your playbook to customize st
| `st2_auth_password` | `testp` | Password used by StackStorm standalone authentication.
| `st2_save_credentials` | `yes` | Save credentials for local CLI in `/root/.st2/config` file.
| **st2mistral**
| `st2mistral_version` | `latest` | st2mistral version to install. Use latest `latest` to get automatic updates or pin it to numeric version like `2.1.1`.
| `st2mistral_version` | `latest` | st2mistral version to install. Use latest `latest` to get automatic updates or pin it to numeric version like `2.2.0`.
| `st2mistral_db` | `mistral` | PostgreSQL DB name for Mistral.
| `st2mistral_db_username` | `mistral` | PostgreSQL DB user for Mistral.
| `st2mistral_db_password` | `StackStorm` | PostgreSQL DB password for Mistral.
| **st2chatops**
| `st2chatops_st2_api_key` | | st2 API key to be updated in st2chatops.env using "st2 apikey create -k" in a task [**Required**]
| `st2chatops_hubot_adapter` | | Hubot Adapter to be used for st2chatops. Default is `shell`, but should be changed to one of the [`supported adapters`](`https://github.com/StackStorm/ansible-st2/blob/master/roles/st2chatops/vars/main.yml`).[**Required**]
| `st2chatops_config` | `{ }` | Based on adapter in `st2chatops_hubot_adapter`, provide hash for the adapter settings, to update [`st2chatops.env`](https://github.com/StackStorm/st2chatops/blob/master/st2chatops.env). For example, for `Slack` hubot adapter: `st2chatops_config: {"HUBOT_SLACK_TOKEN": "xoxb-CHANGE-ME-PLEASE"}`
| `st2chatops_version` | `latest` | st2chatops version to install. Use `latest` to get automatic updates or pin it to numeric version like `2.2.0`.

## Examples
Install latest `stable` StackStorm with all its components on local machine:
Expand All @@ -59,7 +64,7 @@ This is default behavior. If you don't want updates - consider pinning version-r

Install specific numeric version of st2 with pinned revision number as well:
```sh
ansible-playbook stackstorm.yml --extra-vars='st2_version=2.1.1 st2_revision=8'
ansible-playbook stackstorm.yml --extra-vars='st2_version=2.2.0 st2_revision=8'
```

## Installing behind a proxy.
Expand Down
17 changes: 17 additions & 0 deletions roles/st2chatops/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# st2chatops version to install. Use `latest` to get automatic updates or pin it to numeric version like `2.2.0`
st2chatops_version: latest

# Please provide ST2_API_KEY using "st2 apikey create -k"
st2chatops_st2_api_key: CHANGE-ME-PLEASE

# Hubot Adapter to be used for st2chatops.
# For supported adapters, please check: https://github.com/StackStorm/ansible-st2/blob/master/roles/st2chatops/vars/main.yml
#
# Example, for slack: st2chatops_hubot_adapter: slack
st2chatops_hubot_adapter: shell

# Hash to configure values for the adapter in "/opt/stackstorm/chatops/st2chatops.env"
# Original: https://github.com/StackStorm/st2chatops/blob/master/st2chatops.env
#
# Example, for slack: st2chatops_config: {"HUBOT_SLACK_TOKEN":"xoxb-CHANGE-ME-PLEASE"}
st2chatops_config: {}
6 changes: 6 additions & 0 deletions roles/st2chatops/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
- name: restart st2chatops
become: yes
service:
name: st2chatops
state: restarted
25 changes: 25 additions & 0 deletions roles/st2chatops/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
galaxy_info:
description: Install st2chatops
author: humblearner
company: StackStorm
license: Apache 2.0
min_ansible_version: 2.2
platforms:
- name: Ubuntu
versions:
- trusty
- xenial
- name: EL
versions:
- 6
- 7
categories:
- st2
- devops
- chatops
- automation
- hubot
dependencies:
- role: nodejs
- role: st2
115 changes: 115 additions & 0 deletions roles/st2chatops/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
---
- name: Assert st2chatops_hubot_adapter is specified
fail:
msg: >
'"st2chatops_hubot_adapter" must be one of the supported adapters in https://github.com/StackStorm/st2chatops/blob/master/st2chatops.env'
when: st2chatops_hubot_adapter not in supported_hubot_adapters

- name: Assert st2chatops_hubot_adapter "{{ st2chatops_hubot_adapter|upper }}" settings are specified
fail:
msg: '"st2chatops_config" hash cannot be empty for "{{ st2chatops_hubot_adapter|upper }}" hubot adapter.'
when: st2chatops_config.keys() == [] and st2chatops_hubot_adapter != "shell"

- name: Install latest st2chatops package
become: yes
package:
name: st2chatops
state: latest
when: st2chatops_version == "latest"
tags: [st2chatops, skip_ansible_lint]

- name: Install pinned st2chatops package
become: yes
package:
name: st2chatops={{ st2chatops_version }}
state: present
when: st2chatops_version != "latest"
tags: [st2chatops, skip_ansible_lint]

- name: Add st2_api_key in st2chatops.env
become: yes
replace:
dest: /opt/stackstorm/chatops/st2chatops.env
regexp: '(?<=ST2_API_KEY=)(.*)$'
replace: '{{ st2chatops_st2_api_key }}'
when: st2chatops_st2_api_key != 'CHANGE-ME-PLEASE'
register: task_st2_api_key
no_log: true
notify: restart st2chatops
tags: st2chatops

- name: Comment Username, Password and Auth URL, if API_KEY provided
become: yes
replace:
dest: /opt/stackstorm/chatops/st2chatops.env
regexp: '{{ item }}'
replace: '# \1'
with_items:
- '^(export ST2_AUTH_URL.*)'
- '^(export ST2_AUTH_USERNAME.*)'
- '^(export ST2_AUTH_PASSWORD.*)'
when: task_st2_api_key.changed
tags: [st2chatops, skip_ansible_lint]

- name: Check if any adapter is enabled
shell: "cat /opt/stackstorm/chatops/st2chatops.env | grep -e '^export HUBOT_ADAPTER=' "
ignore_errors: True
register: task_adapter_enabled
changed_when: False

- name: Set variable enabled_adapter
set_fact:
enabled_adapter: '{% set list_var = (task_adapter_enabled.stdout).split("=") %}{{ list_var[1] }}'
ignore_errors: True
changed_when: False

- name: Comment existing hubot adapters in "/opt/stackstorm/chatops/st2chatops.env"
become: yes
replace:
dest: /opt/stackstorm/chatops/st2chatops.env
regexp: '^(export HUBOT_ADAPTER=.*)'
replace: '# \1'
when: 'enabled_adapter != "{{ st2chatops_hubot_adapter }}" or enabled_adapter is not defined'
changed_when: no
tags: st2chatops

- name: Uncomment Hubot "{{ st2chatops_hubot_adapter|upper }}" adapter in "/opt/stackstorm/chatops/st2chatops.env"
become: yes
replace:
dest: /opt/stackstorm/chatops/st2chatops.env
regexp: '^# (export HUBOT_ADAPTER={{ st2chatops_hubot_adapter }})$'
replace: '\1'
changed_when: no
notify: restart st2chatops
tags: st2chatops

- name: Add entry for "SHELL" adapter
become: yes
lineinfile:
dest: /opt/stackstorm/chatops/st2chatops.env
line: export HUBOT_ADAPTER=shell
when: st2chatops_hubot_adapter == "shell"
notify: restart st2chatops
tags: st2chatops

- name: Configure "{{ st2chatops_hubot_adapter|upper }}" adapter setttings in "/opt/stackstorm/chatops/st2chatops.env"
become: yes
ini_file:
dest: /opt/stackstorm/chatops/st2chatops.env
section: null
option: 'export {{ _conf_option.key }}'
value: '{{ _conf_option.value }}'
no_extra_spaces: yes
with_dict: '{{ st2chatops_config }}'
loop_control:
loop_var: _conf_option
no_log: true
notify: restart st2chatops
tags: st2chatops

- name: Ensure st2chatops service is enabled and running
become: yes
service:
name: st2chatops
enabled: yes
tags: st2chatops
12 changes: 12 additions & 0 deletions roles/st2chatops/vars/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
supported_hubot_adapters:
- slack
- hipchat
- xmpp
- flowdock
- yammer
- spark
- irc
- mattermost
- matteruser
- shell
5 changes: 5 additions & 0 deletions roles/st2smoketests/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- name: restart st2chatops
become: yes
service:
name: st2chatops
state: restarted
21 changes: 21 additions & 0 deletions roles/st2smoketests/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,24 @@
changed_when: no
tags:
- smoke-tests

- name: Generate st2api key
command: st2 apikey create -k
environment:
ST2_AUTH_TOKEN: "{{ st2_token.stdout }}"
register: st2_api_key
changed_when: no
no_log: true
tags:
- smoke-tests

- name: st2chatops smoketests for "SLACK"
include: >
st2chatops_smoketests.yml
ST2_AUTH_TOKEN={{ st2_token.stdout }}
st2chatops_api_key={{ st2_api_key.stdout }}
st2chatops_hubot_adapter=slack
st2chatops_config={'HUBOT_SLACK_TOKEN':'{{ hubot_token }}'}
when: hubot_token is defined
tags:
- smoke-tests
92 changes: 92 additions & 0 deletions roles/st2smoketests/tasks/st2chatops_smoketests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
- name: Set variables and hubot adapter settings
set_fact:
st2chatops_api_key: "{{ st2chatops_api_key }}"
st2chatops_hubot_adapter: "{{ st2chatops_hubot_adapter }}"
st2chatops_config: "{{ st2chatops_config }}"
changed_when: no
no_log: true
tags:
- smoke-tests

- name: Install st2 pack from exchange
become: yes
environment:
ST2_AUTH_TOKEN: "{{ ST2_AUTH_TOKEN }}"
command: "st2 pack install st2"
changed_when: no
tags:
- smoke-tests

- name: Update st2_api_key in st2chatops.env
become: yes
replace:
dest: /opt/stackstorm/chatops/st2chatops.env
regexp: '(?<=ST2_API_KEY=)(.*)$'
replace: '{{ st2chatops_api_key }}'
no_log: true
changed_when: no
tags:
- smoke-tests

- name: Comment Username, Password and Auth URL, if API_KEY provided
become: yes
replace:
dest: /opt/stackstorm/chatops/st2chatops.env
regexp: '{{ item }}'
replace: '# \1'
with_items:
- '^(export ST2_AUTH_URL.*)'
- '^(export ST2_AUTH_USERNAME.*)'
- '^(export ST2_AUTH_PASSWORD.*)'
tags:
- smoke-tests

- name: Comment existing hubot adapters in "/opt/stackstorm/chatops/st2chatops.env"
become: yes
replace:
dest: /opt/stackstorm/chatops/st2chatops.env
regexp: '^(export HUBOT_ADAPTER=.*)'
replace: '# \1'
changed_when: no
tags:
- smoke-tests

- name: Update "{{ st2chatops_hubot_adapter|upper }}" adapter
become: yes
replace:
dest: /opt/stackstorm/chatops/st2chatops.env
regexp: '^# (export HUBOT_ADAPTER={{ st2chatops_hubot_adapter }})$'
replace: '\1'
changed_when: no
tags:
- smoke-tests

- name: Configure "{{ st2chatops_hubot_adapter|upper }}" settings
become: yes
ini_file:
dest: /opt/stackstorm/chatops/st2chatops.env
section: null
option: 'export {{ _conf_option.key }}'
value: '{{ _conf_option.value }}'
no_extra_spaces: yes
with_dict: '{{ st2chatops_config }}'
loop_control:
loop_var: _conf_option
no_log: true
notify: restart st2chatops
tags:
- smoke-tests

- meta: flush_handlers
tags:
- smoke-tests

- name: Verify st2chatops using bin/hubot
command: "timeout 10 bin/hubot"
args:
chdir: "/opt/stackstorm/chatops/"
register: test_output
failed_when: "'{{ st2chatops_hubot_adapter|title }} client now connected' not in test_output.stdout or 'DEBUG Added command: st2 list' not in test_output.stdout"
changed_when: no
tags:
- smoke-tests
1 change: 1 addition & 0 deletions stackstorm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
- nginx
- st2web
- nodejs
- st2chatops
- st2smoketests

0 comments on commit 1f7a1b6

Please sign in to comment.