Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added command of ec2 tag [ls|create|delete] #33

Merged
merged 1 commit into from
Oct 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,19 @@ hubot autoscaling update --name=[name] --min=[min] --dry-run - Try updating MinS
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 ami create - Create an ami.
hubot ec2 ami deregister --ami_id=[ami_id] - Deregister an ami
hubot ec2 ami deregister --ami_id=[ami_id] --dry-run - Try deregistering an ami
hubot ec2 ami create --instance_id=*** - Create an ami.
hubot ec2 ami deregister --ami_id=[ami_id] - Deregisters the specified AMI
hubot ec2 ami ls - Desplays all AMI(Images)
hubot ec2 ami ls --owner=[owner] - Desplays owner's AMI(Images)
hubot ec2 ls - Displays all Instances
hubot ec2 ls --instance_id=[instance_id] - Details an Instance
hubot ec2 run - Run an Instance
hubot ec2 sg create --group_name=[group_name] --desc=[desc] --vpc_id=[vpc_id] - Create 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 tag create --resource_id=*** --tag_key=*** --tag_value=*** - Create a tag.
hubot ec2 tag delete --resource_id=*** - Deletes the specified set of tags
hubot ec2 tag ls - Desplays all tags
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
Expand Down
2 changes: 1 addition & 1 deletion scripts/ec2/create_ami.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# HUBOT_AWS_EC2_CREATE_AMI_CONFIG: [optional] Path to csonfile to be performs service operation based on. Required config_path argument or this.
#
# Commands:
# hubot ec2 ami create - Create an ami.
# hubot ec2 ami create --instance_id=*** - Create an ami.
#
# Notes:
# --instance_id=*** : [required] Target instance id.
Expand Down
76 changes: 76 additions & 0 deletions scripts/ec2/create_tags.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Description:
# Adds or overwrites one tag for the specified Amazon EC2 resource or resources
#
# Commands:
# hubot ec2 tag create --resource_id=*** --tag_key=*** --tag_value=*** - Create a tag.
#
# Notes:
# --resource_id=*** : [required] The IDs of one resource to tag. For example, ami-1a2b3c4d.
# --tag_key=*** : [required] The key of the tag.
# --tag_value=*** : [required] The value of the tag.
# --dry-run : [optional] Checks whether the api request is right. Recommend to set before applying to real asset.

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

getArgParams = (arg) ->
dry_run = if arg.match(/--dry-run/) then true else false

resource_id_capture = /--resource_id=(.*?)( |$)/.exec(arg)
resource_id = if resource_id_capture then resource_id_capture[1] else null

tag_key_capture = /--tag_key=(.*?)( |$)/.exec(arg)
tag_key = if tag_key_capture then tag_key_capture[1] else null

tag_value_capture = /--tag_value=(.*?)( |$)/.exec(arg)
tag_value = if tag_value_capture then tag_value_capture[1] else null

return {dry_run: dry_run, resource_id: resource_id, tag_key: tag_key, tag_value: tag_value}

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

arg_params = getArgParams(msg.match[1])

resource_id = arg_params.resource_id
tag_key = arg_params.tag_key
tag_value = arg_params.tag_value
dry_run = arg_params.dry_run

unless resource_id
msg.send "resource_id option is required"
return

unless tag_key
msg.send "tag_key option is required"
return

unless tag_value
msg.send "tag_value option is required"
return

msg.send "Requesting resource_id=#{resource_id}, tag_key=#{tag_key}, tag_value=#{tag_value}, dry-run=#{dry_run}..."

params =
Resources: [resource_id]
Tags: [
Key: tag_key, Value: tag_value
]

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

aws = require('../../aws.coffee').aws()
ec2 = new aws.EC2({apiVersion: '2014-10-01'})

ec2.createTags params, (err, res) ->
if err
msg.send "Error: #{err}"
else
msg.send "Success to create a tag"
msg.send util.inspect(res, false, null)
65 changes: 65 additions & 0 deletions scripts/ec2/delete_tags.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Description:
# Deletes the specified set of tags from the specified set of resources
#
# Commands:
# hubot ec2 tag delete --resource_id=*** - Deletes the specified set of tags
#
# Notes:
# --resource_id=*** : [required] The IDs of one resource to tag. For example, ami-1a2b3c4d.
# --tag_key=*** : [optional] The key of the tag.
# --tag_value=*** : [optional] The value of the tag.
# --dry-run : [optional] Checks whether the api request is right. Recommend to set before applying to real asset.

util = require 'util'

getArgParams = (arg) ->
dry_run = if arg.match(/--dry-run/) then true else false

resource_id_capture = /--resource_id=(.*?)( |$)/.exec(arg)
resource_id = if resource_id_capture then resource_id_capture[1] else null

tag_key_capture = /--tag_key=(.*?)( |$)/.exec(arg)
tag_key = if tag_key_capture then tag_key_capture[1] else null

tag_value_capture = /--tag_value=(.*?)( |$)/.exec(arg)
tag_value = if tag_value_capture then tag_value_capture[1] else null

return {dry_run: dry_run, resource_id: resource_id, tag_key: tag_key, tag_value: tag_value}

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

arg_params = getArgParams(msg.match[1])

resource_id = arg_params.resource_id
tag_key = arg_params.tag_key
tag_value = arg_params.tag_value
dry_run = arg_params.dry_run

unless resource_id
msg.send "resource_id option is required"
return

msg.send "Deleting a tag: resource_id=#{resource_id}, tag_key=#{tag_key}, tag_value=#{tag_value}, dry-run=#{dry_run}..."

params =
Resources: [resource_id]
params.Tags = [ Key: tag_key ] if tag_key
params.Tags[0].Value = tag_value if params.Tags && 0 < params.Tags.length && tag_value

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

aws = require('../../aws.coffee').aws()
ec2 = new aws.EC2({apiVersion: '2014-10-01'})

ec2.deleteTags params, (err, res) ->
if err
msg.send "Error: #{err}"
else
msg.send "Success to delete tags"
msg.send util.inspect(res, false, null)
9 changes: 6 additions & 3 deletions scripts/ec2/deregister_ami.coffee
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# Description:
# Deregister ec2 ami
# Deregisters the specified AMI. After you deregister an AMI, it can't be used to launch new instances.
#
# Commands:
# hubot ec2 ami deregister --ami_id=[ami_id] --dry-run - Try deregistering an ami
# hubot ec2 ami deregister --ami_id=[ami_id] - Deregister an ami
# hubot ec2 ami deregister --ami_id=[ami_id] - Deregisters the specified AMI
#
# Notes:
# --ami_id=*** : [required] The ID of the AMI.
# --dry-run : [optional] Checks whether the api request is right. Recommend to set before applying to real asset.

util = require 'util'

Expand Down
6 changes: 4 additions & 2 deletions scripts/ec2/ls_ami.coffee
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# Description:
# List ec2 ami info
# Describes one or more of the images (AMIs, AKIs, and ARIs) available to you
#
# Commands:
# hubot ec2 ami ls - Desplays all AMI(Images)
# hubot ec2 ami ls --owner=[owner] - Desplays owner's AMI(Images)
#
# Notes:
# --owner=*** : [optional] Filters the images by the owner. Specify an AWS account ID, amazon, aws-marketplace self. Omitting this option returns all images for which you have launch permissions.

moment = require 'moment'
tsv = require 'tsv'
Expand Down
79 changes: 79 additions & 0 deletions scripts/ec2/ls_tags.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Description:
# Describes one or more of the tags for your EC2 resources
#
# Commands:
# hubot ec2 tag ls - Desplays all tags
#
# Notes:
# --resource_type=*** : [optional] Filters the tags by the resource type.
# --resource_id=*** : [optional] Filters the tags by the resource ID.
# --key=*** : [optional] Filters the tags by the tag key.
# --value=*** : [optional] Filters the tags by the tab value.
# --max_results=*** : [optional] The maximum number of results to return for the request in a single page. This value can be between 5 and 1000.

moment = require 'moment'
tsv = require 'tsv'

getArgParams = (arg) ->
resource_type_capture = /--resource_type=(.*?)( |$)/.exec(arg)
resource_type = if resource_type_capture then resource_type_capture[1] else null

resource_id_capture = /--resource_id=(.*?)( |$)/.exec(arg)
resource_id = if resource_id_capture then resource_id_capture[1] else null

key_capture = /--key=(.*?)( |$)/.exec(arg)
key = if key_capture then key_capture[1] else null

value_capture = /--value=(.*?)( |$)/.exec(arg)
value = if value_capture then value_capture[1] else null

max_results_capture = /--max_results=(.*?)( |$)/.exec(arg)
max_results = if max_results_capture then max_results_capture[1] else null

return {resource_type: resource_type, resource_id: resource_id, key: key, value: value, max_results: max_results}

module.exports = (robot) ->
robot.respond /ec2 tag ls(.*)$/i, (msg) ->
msg.send "Fetching ..."

aws = require('../../aws.coffee').aws()
ec2 = new aws.EC2({apiVersion: '2014-10-01'})

arg_params = getArgParams(msg.match[1])

resource_type = arg_params.resource_type
resource_id = arg_params.resource_id
key = arg_params.key
value = arg_params.value
max_results = arg_params.max_results

filters = []
filters.push({Name: "resource-type", Values:[resource_type]}) if resource_type
filters.push({Name: "resource-id", Values:[resource_id]}) if resource_id
filters.push({Name: "key", Values:[key]}) if key
filters.push({Name: "value", Values:[value]}) if value

params = {}
params.Filters = filters if 0 < filters.length
params.MaxResults = max_results if max_results

ec2.describeTags params, (err, res) ->
if err
msg.send "Error: #{err}"
else
messages = []
for data in res.Tags

messages.push({
resource_type : data.ResourceType
resource_id : data.ResourceId
key : data.Key
value : data.Value
})

messages.sort (a, b) ->
if a.resource_type < b.resource_type then return -1
if b.resource_type < a.resource_type then return 1
return 0
message = tsv.stringify(messages) || '[None]'
msg.send message