Skip to content

Commit

Permalink
Prepared v0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
yoheimuta committed Apr 22, 2015
0 parents commit 20b7dc6
Show file tree
Hide file tree
Showing 30 changed files with 1,254 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.DS_Store*
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# hubot-aws

Hubot masters aws commands

### Commands

```
hubot autoscaling create --name=[group_name] --launch_name=[launch_configuration_name] - Create an AutoScaling Group
hubot autoscaling create --name=[group_name] --launch_name=[launch_configuration_name] --dry-run - Try creating an AutoScaling Group
hubot autoscaling delete --group_name=[group_name] - Delete the AutoScaling Group
hubot autoscaling delete --group_name=[group_name] --force - Delete the AutoScaling Group with live instances
hubot autoscaling launch create --name=[launch_configuration_name] - Create an AutoScaling LaunchConfiguration
hubot autoscaling launch create --name=[launch_configuration_name] --dry-run - Try creating an AutoScaling LaunchConfiguration
hubot autoscaling launch delete --name=[launch_configuration_name] - Delete the AutoScaling LaunchConfiguration
hubot autoscaling launch ls - Displays all AutoScaling LaunchConfigurations
hubot autoscaling launch ls --name=[launch_configuration_name] - Details an Autoscaling LaunchConfiguration
hubot autoscaling ls - Displays all AutoScaling Groups
hubot autoscaling ls --name=[group_name] - Details an Autoscaling Group
hubot autoscaling notification delete --group_name=[group_name] --arn=[topic_arn] - Delete the AutoScaling Notificatoin
hubot autoscaling notification ls - Displays all AutoScaling NotificationConfigurations
hubot autoscaling notification ls --group_name=[group_name] - Details an Autoscaling NotificationConfiguration
hubot autoscaling notification put --group_name=[group_name] - Put an AutoScaling Notifications
hubot autoscaling notification put --group_name=[group_name] --dry-run - Try putting an AutoScaling Notifications
hubot autoscaling policy delete --group_name=[group_name] --policy_name=[policy_name] - Delete the AutoScaling Policy
hubot autoscaling policy ls - Displays all AutoScaling Policies
hubot autoscaling policy ls --group_name=[group_name] - Details an Autoscaling Policy
hubot autoscaling policy put --add --group_name=[group_name] - Put an AutoScaling ScaleOut Policy
hubot autoscaling policy put --add --group_name=[group_name] --dry-run - Try putting an AutoScaling ScaleOut Policy
hubot autoscaling policy put --remove --group_name=[group_name] - Put an AutoScaling ScaleIn Policy
hubot autoscaling policy put --remove --group_name=[group_name] --dry-run - Try putting an AutoScaling ScaleIn Policy
hubot autoscaling update --json=[json] - Update the AutoScaling Group
hubot autoscaling update --name=[name] --desired=[desired] - Update DesiredCapacity the AutoScaling Group
hubot autoscaling update --name=[name] --max=[max] - Update MaxSize of the AutoScaling Group
hubot autoscaling update --name=[name] --min=[min] - Update MinSize of the AutoScaling Group
hubot cloudwatch alarm delete --name=[alarm_name] - Delete the Alarm
hubot cloudwatch alarm ls - Displays all Alarms
hubot cloudwatch alarm ls --name=[alarm_name] - Details an Alarm
hubot ec2 ls - Displays all Instances
hubot ec2 ls --instance_id=[instance_id] - Details an Instance
hubot ec2 run - Run an Instance
hubot ec2 run --dry-run - Try running an Instance
hubot ec2 sg create --vpc_id=[vpc_id] --group_name=[group_name] --desc=[desc] - Create a SecurityGroup
hubot ec2 sg create --vpc_id=[vpc_id] --group_name=[group_name] --desc=[desc] --dry-run - Try creating a SecurityGroup
hubot ec2 sg delete --group_id=[group_id] - Delete the SecurityGroup
hubot ec2 sg ls - Desplays all SecurityGroups
hubot ec2 spot ls - Displays all SpotInstances
hubot ec2 terminate --instance_id=[instance_id] - Terminate the Instance
hubot s3 ls - Displays all S3 buckets
hubot s3 ls --bucket_name=[bucket-name] - Displays all objects
hubot s3 ls --bucket_name=[bucket-name] --prefix=[prefix] - Displays all objects with prefix
hubot s3 ls --bucket_name=[bucket-name] --prefix=[prefix] --marker=[marker] - Displays all objects with prefix from marker
```
14 changes: 14 additions & 0 deletions auth.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
canAccess: (robot, user) ->
return true if process.env.HUBOT_AWS_DEBUG

if robot.auth.isAdmin(user)
return true

role = process.env.HUBOT_AWS_CAN_ACCESS_ROLE
if role && robot.auth.hasRole(user, role)
return true
else
return false
}

8 changes: 8 additions & 0 deletions aws.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
aws: ->
aws = require 'aws-sdk'
aws.config.accessKeyId = process.env.HUBOT_AWS_ACCESS_KEY_ID
aws.config.secretAccessKey = process.env.HUBOT_AWS_SECRET_ACCESS_KEY
aws.config.region = process.env.HUBOT_AWS_REGION
return aws
}
10 changes: 10 additions & 0 deletions index.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
fs = require 'fs'
path = require 'path'

module.exports = (robot) ->
scripts_path = path.resolve __dirname, 'scripts'
if fs.existsSync scripts_path
for category_file in fs.readdirSync(scripts_path)
category_path = path.resolve scripts_path, category_file
if fs.existsSync category_path
robot.loadFile category_path, file for file in fs.readdirSync(category_path)
33 changes: 33 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "hubot-aws",
"version": "0.0.1",
"description": "Hubot masters aws commands",
"repository": {
"type": "git",
"url": "ssh://git@github.com/yoheimuta/hubot-aws.git"
},
"main": "index.coffee",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"aws-sdk": "2.1.23",
"tsv": "0.2.0",
"async": "0.9.0",
"underscore": "1.8.3",
"cson": "3.0.1",
"moment": "2.10.2"
},
"keywords": [
"aws",
"hubot",
"hubot aws",
"hubot-aws",
"aws-cli"
],
"bugs": {
"url": "https://github.com/yoheimuta/hubot-aws/issues"
},
"author": "yoheimuta <yoheimuta@gmail.com>",
"license": "MIT"
}
39 changes: 39 additions & 0 deletions scripts/autoscaling/create_group.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Description:
# Create autoscaling group
#
# Commands:
# hubot autoscaling create --name=[group_name] --launch_name=[launch_configuration_name] --dry-run - Try creating an AutoScaling Group
# hubot autoscaling create --name=[group_name] --launch_name=[launch_configuration_name] - Create an AutoScaling Group

fs = require 'fs'
cson = require 'cson'
util = require 'util'

module.exports = (robot) ->
robot.respond /autoscaling create --name=(.*) --launch_name=(.*?)(| --dry-run)$/i, (msg) ->
unless require('../../auth.coffee').canAccess(robot, msg.envelope.user)
msg.send "You cannot access this feature. Please contact with admin"
return

name = msg.match[1].trim()
conf = msg.match[2].trim()
dry_run = if msg.match[3] then true else false

msg.send "Requesting name=#{name}, launch_name=#{conf}, dry-run=#{dry_run}..."

params = cson.parseCSONFile process.env.HUBOT_AWS_AS_GROUP_CONFIG
params.AutoScalingGroupName = name
params.LaunchConfigurationName = conf

if dry_run
msg.send util.inspect(params, false, null)
return

aws = require('../../aws.coffee').aws()
autoscaling = new aws.AutoScaling({apiVersion: '2011-01-01'})

autoscaling.createAutoScalingGroup params, (err, res)->
if err
msg.send "Error: #{err}"
else
msg.send util.inspect(res, false, null)
44 changes: 44 additions & 0 deletions scripts/autoscaling/create_launch.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Description:
# Create autoscaling launch configurations
#
# Commands:
# hubot autoscaling launch create --name=[launch_configuration_name] --dry-run - Try creating an AutoScaling LaunchConfiguration
# hubot autoscaling launch create --name=[launch_configuration_name] - Create an AutoScaling LaunchConfiguration

fs = require 'fs'
cson = require 'cson'
util = require 'util'

module.exports = (robot) ->
robot.respond /autoscaling launch create --name=(.*?)(| --dry-run)$/i, (msg) ->
unless require('../../auth.coffee').canAccess(robot, msg.envelope.user)
msg.send "You cannot access this feature. Please contact with admin"
return

name = msg.match[1].trim()
dry_run = if msg.match[2] then true else false

msg.send "Requesting name=#{name}, dry-run=#{dry_run}..."

launch_configuration_path = process.env.HUBOT_AWS_AS_LAUNCH_CONF_CONFIG
params = cson.parseCSONFile launch_configuration_path

params.LaunchConfigurationName = name

userdata_path = process.env.HUBOT_AWS_AS_LAUNCH_CONF_USERDATA_PATH
if fs.existsSync userdata_path
init_file = fs.readFileSync userdata_path, 'utf-8'
params.UserData = new Buffer(init_file).toString('base64')

if dry_run
msg.send util.inspect(params, false, null)
return

aws = require('../../aws.coffee').aws()
autoscaling = new aws.AutoScaling({apiVersion: '2011-01-01'})

autoscaling.createLaunchConfiguration params, (err, res)->
if err
msg.send "Error: #{err}"
else
msg.send util.inspect(res, false, null)
28 changes: 28 additions & 0 deletions scripts/autoscaling/delete_group.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Description:
# Delete autoscaling group
#
# Commands:
# hubot autoscaling delete --group_name=[group_name] - Delete the AutoScaling Group
# hubot autoscaling delete --group_name=[group_name] --force - Delete the AutoScaling Group with live instances

util = require 'util'

module.exports = (robot) ->
robot.respond /autoscaling delete --group_name=(.*?)(| .*)$/i, (msg) ->
unless require('../../auth.coffee').canAccess(robot, msg.envelope.user)
msg.send "You cannot access this feature. Please contact with admin"
return

name = msg.match[1].trim() || ''
force = if msg.match[2].trim() == '--force' then true else false

msg.send "Requesting #{name} #{force}..."

aws = require('../../aws.coffee').aws()
autoscaling = new aws.AutoScaling({apiVersion: '2011-01-01'})

autoscaling.deleteAutoScalingGroup { AutoScalingGroupName: name, ForceDelete: force }, (err, res)->
if err
msg.send "Error: #{err}"
else
msg.send util.inspect(res, false, null)
29 changes: 29 additions & 0 deletions scripts/autoscaling/delete_launch.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Description:
# Delete autoscaling launch configurations
#
# Commands:
# hubot autoscaling launch delete --name=[launch_configuration_name] - Delete the AutoScaling LaunchConfiguration

fs = require 'fs'
cson = require 'cson'
util = require 'util'

module.exports = (robot) ->
robot.respond /autoscaling launch delete --name=(.*)$/i, (msg) ->
unless require('../../auth.coffee').canAccess(robot, msg.envelope.user)
msg.send "You cannot access this feature. Please contact with admin"
return

name = msg.match[1].trim() || ''

msg.send "Requesting #{name}..."

aws = require('../../aws.coffee').aws()
autoscaling = new aws.AutoScaling({apiVersion: '2011-01-01'})

autoscaling.deleteLaunchConfiguration { LaunchConfigurationName: name }, (err, res)->
if err
msg.send "Error: #{err}"
else
msg.send util.inspect(res, false, null)

27 changes: 27 additions & 0 deletions scripts/autoscaling/delete_notification.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Description:
# Delete an autoscaling notification
#
# Commands:
# hubot autoscaling notification delete --group_name=[group_name] --arn=[topic_arn] - Delete the AutoScaling Notificatoin

util = require 'util'

module.exports = (robot) ->
robot.respond /autoscaling notification delete --group_name=(.*) --arn=(.*)$/i, (msg) ->
unless require('../../auth.coffee').canAccess(robot, msg.envelope.user)
msg.send "You cannot access this feature. Please contact with admin"
return

group_name = msg.match[1].trim()
topic_arn = msg.match[2].trim()

msg.send "Requesting group_name=#{group_name} topic_arn=#{topic_arn}..."

aws = require('../../aws.coffee').aws()
autoscaling = new aws.AutoScaling({apiVersion: '2011-01-01'})

autoscaling.deletePolicy { AutoScalingGroupName: group_name, TopicARN: topic_arn}, (err, res)->
if err
msg.send "Error: #{err}"
else
msg.send util.inspect(res, false, null)
28 changes: 28 additions & 0 deletions scripts/autoscaling/delete_policy.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Description:
# Delete an autoscaling policy
#
# Commands:
# hubot autoscaling policy delete --group_name=[group_name] --policy_name=[policy_name] - Delete the AutoScaling Policy

util = require 'util'

module.exports = (robot) ->
robot.respond /autoscaling policy delete --group_name=(.*) --policy_name=(.*)$/i, (msg) ->
unless require('../../auth.coffee').canAccess(robot, msg.envelope.user)
msg.send "You cannot access this feature. Please contact with admin"
return

group_name = msg.match[1].trim()
policy_name = msg.match[2].trim()

msg.send "Requesting #{group_name} #{policy_name}..."

aws = require('../../aws.coffee').aws()
autoscaling = new aws.AutoScaling({apiVersion: '2011-01-01'})

autoscaling.deletePolicy { AutoScalingGroupName: group_name, PolicyName: policy_name}, (err, res)->
if err
msg.send "Error: #{err}"
else
msg.send util.inspect(res, false, null)

55 changes: 55 additions & 0 deletions scripts/autoscaling/ls_group.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Description:
# List autoscaling group
#
# Commands:
# hubot autoscaling ls - Displays all AutoScaling Groups
# hubot autoscaling ls --name=[group_name] - Details an Autoscaling Group

moment = require 'moment'
util = require 'util'

module.exports = (robot) ->
robot.respond /autoscaling ls($| --name=)(.*)$/i, (msg) ->
arg_name = msg.match[2].trim() || ''

msg.send "Fetching #{arg_name}..."

aws = require('../../aws.coffee').aws()
autoscaling = new aws.AutoScaling({apiVersion: '2011-01-01'})

autoscaling.describeAutoScalingGroups (if arg_name then { AutoScalingGroupNames: [arg_name] } else null), (err, res)->
if err
msg.send "Error: #{err}"
else
if arg_name
msg.send util.inspect(res, false, null)
else
msg.send "time\tcurrent_size\tdesired_size\tmin_size\tmax_size\taz\telb\tconf\tname"
msg.send "\ttag.Key\ttag.Value"
msg.send Array(130).join('-')
msg.send Array(130).join('-')

messages = []

res.AutoScalingGroups.sort (a, b) ->
moment(a.CreatedTime) - moment(b.CreatedTime)

for group in res.AutoScalingGroups
time = moment(group.CreatedTime).format('YYYY-MM-DD HH:mm:ssZ')
name = group.AutoScalingGroupName
conf = group.LaunchConfigurationName
az = group.AvailabilityZones.join ","
elb = group.LoadBalancerNames.join ","
min_size = group.MinSize
max_size = group.MaxSize
desired_size = group.DesiredCapacity
current_size = group.Instances.length

messages.push("#{time}\t#{current_size}\t#{desired_size}\t#{min_size}\t#{max_size}\t#{az}\t#{elb}\t#{conf}\t#{name}")

for tag in group.Tags
messages.push("\t#{tag.Key}\t#{tag.Value}")

message = messages.join "\n"
message ||= '[None]'
msg.send message
Loading

0 comments on commit 20b7dc6

Please sign in to comment.