-
Notifications
You must be signed in to change notification settings - Fork 5
/
slack-command-handler.php
48 lines (40 loc) · 1.35 KB
/
slack-command-handler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
/**
* Slack slash command handler.
*
* Receives a payload from a slash command and calls an Ansible playbook. This
* should be placed in a web-accessible location and registered with Slack.
*
* @link https://api.slack.com/slash-commands
*/
define( 'SLACK_TOKEN', '' );
define( 'ANSIBLE_INVENTORY_PATH', '/srv/www/ansible' );
define( 'ANSIBLE_PLAYBOOK_PATH', '/srv/www/ansible' );
// Ensure this is a POST request with a valid token.
if ( empty( $_POST['token'] ) || SLACK_TOKEN !== $_POST['token'] ) {
exit( 1 );
}
// @todo Consider validating users here.
list( $environment, $branch ) = explode( ' ', $_POST['text'] );
// Validate the environment.
$environments = array( 'production', 'staging' );
if ( ! in_array( $environment, $environments ) ) {
exit( 'Invalid environment. Choose `production` or `staging`.' );
}
// Sanitize the branch to deploy.
if ( 'production' === $environment ) {
$branch = 'production';
} elseif ( empty( $branch ) ) {
$branch = 'staging';
}
// Connection details are managed in Ansible inventory.
$command = sprintf(
'sudo -H -u deploy /usr/bin/ansible-playbook -i %1$s/%2$s%3$s %4$s/deploy.yml',
rtrim( ANSIBLE_INVENTORY_PATH, '/' )
$environment,
'staging' === $environment ? ' -c local' : '',
rtrim( ANSIBLE_PLAYBOOK_PATH, '/' )
);
set_time_limit( 0 );
exec( escapeshellcmd( $command ), $output );
exit( 0 );