Skip to content

Instantly share code, notes, and snippets.

@marcy-terui
Last active December 9, 2015 11:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcy-terui/3b4acef77afb5744fd14 to your computer and use it in GitHub Desktop.
Save marcy-terui/3b4acef77afb5744fd14 to your computer and use it in GitHub Desktop.
HubotにOpsWorksのDeploymentを監視させてSlackに流す ref: http://qiita.com/Marcy/items/ae46c336b9956d2bf8d0
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"opsworks:Describe*"
],
"Resource": [
"*"
]
}
]
}
# 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