Last active
December 9, 2015 11:12
-
-
Save marcy-terui/3b4acef77afb5744fd14 to your computer and use it in GitHub Desktop.
HubotにOpsWorksのDeploymentを監視させてSlackに流す ref: http://qiita.com/Marcy/items/ae46c336b9956d2bf8d0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Version": "2012-10-17", | |
"Statement": [ | |
{ | |
"Effect": "Allow", | |
"Action": [ | |
"opsworks:Describe*" | |
], | |
"Resource": [ | |
"*" | |
] | |
} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Description | |
# OpsWorks deployment notify | |
# | |
# Configuration: | |
# None | |
# | |
# Commands: | |
# None | |
# | |
# Author: | |
# Masashi Terui | |
cron = require('cron').CronJob | |
AWS = require('aws-sdk'); | |
opsworks = new AWS.OpsWorks({region: 'us-east-1'}); | |
channel = '#your-channel' | |
module.exports = (robot) -> | |
new cron '*/10 * * * * *', () => | |
before = Math.floor(new Date().getTime() / 1000) - 10 | |
opsworks.describeStacks {}, (err, stacks) -> | |
if err | |
console.log(err, err.stack) | |
else | |
for stack in stacks.Stacks | |
opsworks.describeDeployments {StackId: stack.StackId}, (err, deploys) -> | |
if err | |
console.log(err, err.stack) | |
else | |
for deploy in deploys.Deployments | |
created_at = Math.floor(new Date(deploy.CreatedAt).getTime() / 1000) | |
if deploy.CompletedAt == "" | |
completed_at = 0 | |
else | |
completed_at = Math.floor(new Date(deploy.CompletedAt).getTime() / 1000) | |
if before <= created_at | |
data = deploy | |
opsworks.describeInstances {InstanceIds: data.InstanceIds}, (err, instances) -> | |
if err | |
console.log(err, err.stack) | |
else | |
ins_list = [] | |
for instance in instances.Instances | |
ins_list.push "- #{instance.Hostname} (#{instance.PrivateIp})" | |
msg = " | |
:opsworks: 開始しました \n | |
```\n | |
ID: #{data.DeploymentId} \n | |
Command: #{data.Command.Name} \n | |
Comment: #{data.Comment} \n | |
[Instances] \n#{ins_list.join('\n')} \n | |
```\n | |
" | |
robot.send {room: channel}, msg | |
if before <= completed_at | |
data = deploy | |
opsworks.describeInstances {InstanceIds: data.InstanceIds}, (err, instances) -> | |
if err | |
console.log(err, err.stack) | |
else | |
ins_list = [] | |
for instance in instances.Instances | |
ins_list.push "- #{instance.Hostname} (#{instance.PrivateIp})" | |
emoji = ':ok:' | |
emoji = ':ng:' if deploy.Status == "failed" | |
msg = " | |
:opsworks: 終了しました #{emoji} \n | |
```\n | |
ID: #{data.DeploymentId} \n | |
Status : #{data.Status} \n | |
Command: #{data.Command.Name} \n | |
Comment: #{data.Comment} \n | |
[Instances] \n#{ins_list.join('\n')} \n | |
```\n | |
" | |
robot.send {room: channel}, msg | |
, null, true, "Asia/Tokyo" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment