This repository has been archived by the owner on Jan 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from boxuk/amazon
Fix #24: Add an Amazon task to see instances infos
- Loading branch information
Showing
14 changed files
with
412 additions
and
37 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,5 +23,10 @@ | |
}, | ||
"changelog": { | ||
"gistId": "" | ||
}, | ||
"amazon": { | ||
"key": "", | ||
"secret": "", | ||
"region": "" | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
Configuration = require './configuration' | ||
_ = require('underscore')._ | ||
_.str = require 'underscore.string' | ||
_.mixin _.str.exports() | ||
|
||
Async = require 'async' | ||
|
||
Utils = require './utils' | ||
AwsHelpers = require './aws_helpers' | ||
|
||
amazon = (fallback, keyword, filter, filterValue) -> | ||
|
||
Route53 = new Configuration.Amazon.aws.Route53() | ||
Ec2 = new Configuration.Amazon.aws.EC2() | ||
Elb = new Configuration.Amazon.aws.ELB() | ||
|
||
params = {} | ||
|
||
if (keyword == 'instances') | ||
AwsHelpers.getAllRecordSets Route53, (err, recordSets) -> | ||
if (not recordSets?) | ||
Utils.fallback_printError fallback, err | ||
else | ||
dnsRoutePairs = mappingDnsWithRoute(recordSets) | ||
|
||
findInstancesBehindLoadBalancer Elb, dnsRoutePairs, (err, idInstances) -> | ||
if (not idInstances?) | ||
Utils.fallback_printError fallback, err | ||
else | ||
if (filter == 'with') | ||
display fallback, Ec2, params, dnsRoutePairs, idInstances, filterValue | ||
else | ||
display fallback, Ec2, params, dnsRoutePairs, idInstances, filter | ||
|
||
|
||
display = (fallback, Ec2, params, dnsRoutePairs, idInstances, filterValue) -> | ||
AwsHelpers.getInstancesByParams Ec2, params, (err, results) -> | ||
list = _.map results, (instance) -> | ||
|
||
tag = _.findWhere instance.Tags, {"Key": "Name"} | ||
security = instance.SecurityGroups[0] | ||
role = if instance.IamInstanceProfile? then " / Role: " + instance.IamInstanceProfile.Arn.split('/')[1] else "" | ||
route = _.findWhere dnsRoutePairs, {"Dns": instance.PublicDnsName} | ||
title = | ||
if (route?) | ||
route.Route | ||
else if instance.PublicDnsName | ||
instance.PublicDnsName | ||
else | ||
"No route or public dns" | ||
|
||
behinds = _.findWhere idInstances, {"Id": instance.InstanceId} | ||
lb = if behinds? and behinds.LoadBalancer? then " - behind: #{behinds.LoadBalancer.LoadBalancerName}" else "" | ||
|
||
{ | ||
title: title | ||
#TODO: Should remove the trailing dot | ||
url: "http://#{title}" | ||
infos: if tag? then tag.Value + ' / ' + instance.InstanceType else instance.InstanceType | ||
comments: if security? then "Security: #{security.GroupName}#{role}" | ||
status: instance.State.Name == 'running' | ||
tails: if behinds? then _.map _.pluck(behinds.Routes, 'Route'), (route) -> "via #{route}#{lb}" | ||
} | ||
|
||
Utils.fallback_printList fallback, list, (list) -> | ||
if (filterValue?) | ||
_.filter list, (o) -> | ||
stuff = o.title + o.infos + o.comments | ||
stuff += _.reduce o.tails, (memo, tail) -> | ||
memo.concat(tail) | ||
, [] | ||
|
||
_.str.include(stuff, filterValue) | ||
else | ||
list | ||
|
||
mappingDnsWithRoute = (recordSets) -> | ||
_.reduce recordSets, (memo, recordSet) -> | ||
if (not _.isEmpty(recordSet.ResourceRecords)) | ||
memo.concat _.map recordSet.ResourceRecords, (record) -> | ||
{ Dns: record.Value, Route: recordSet.Name } | ||
else | ||
memo.concat [ | ||
{ Dns: recordSet.AliasTarget.DNSName, Route: recordSet.Name } | ||
] | ||
, [] | ||
|
||
filterByUrl = (dnsRoutePairs, url) -> | ||
if (url?) | ||
_.filter dnsRoutePairs, (name) -> name.Route.indexOf(url) > -1 | ||
else | ||
dnsRoutePairs | ||
|
||
findInstancesBehindLoadBalancer = (Elb, routes, callback) -> | ||
names = _.reduce routes, (memo, dnsRoutePair) -> | ||
match = dnsRoutePair.Dns.match("^(.*lb)-") | ||
|
||
if match then memo.concat(match[1]) else memo | ||
, [] | ||
|
||
AwsHelpers.getLoadBalancersByNames Elb, _.uniq(names), (err, lbDescriptions) -> | ||
if (not lbDescriptions?) | ||
callback err | ||
else | ||
Async.reduce lbDescriptions, [], (memo, description, cb) -> | ||
cb null, memo.concat _.map description.Instances, (instance) -> | ||
{ | ||
Id: instance.InstanceId, | ||
LoadBalancer: description, | ||
Routes: _.where(routes, {"Dns": description.DNSName + "."}) | ||
} | ||
, (err, idInstances) -> | ||
callback null, idInstances | ||
|
||
module.exports = { | ||
name: "Amazon", | ||
description: "[ instances [ with -term- ] ] Display various informations about your Amazon infrastructure", | ||
action: amazon | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
Configuration = require './configuration' | ||
_ = require('underscore')._ | ||
|
||
Async = require 'async' | ||
|
||
prepareFilters = (name, value, sensitive) -> | ||
if (_.isString(value) or _.isArray(value)) | ||
if (sensitive) | ||
value = [ | ||
"\*" + value.toUpperCase() + "\*", | ||
"\*" + value.toLowerCase() + "\*" | ||
] | ||
else if (_.isString(value)) | ||
value = ["\*" + value + "\*"] | ||
|
||
{"Filters": [{ | ||
"Name": name, | ||
"Values": value | ||
}]} | ||
|
||
#TODO: More than 100 instances | ||
getInstancesByParams = (Ec2, params, callback) -> | ||
Ec2.describeInstances params, (err, data) -> | ||
if (not data?) | ||
callback(err) | ||
else | ||
callback null, _.reduceRight data.Reservations, (memo, reservation) -> | ||
memo.concat(reservation.Instances) | ||
, [] | ||
|
||
getAllRecordSets = (Route53, callback) -> | ||
Route53.listHostedZones {}, (err, hostedZones) -> | ||
if (not hostedZones?) | ||
callback(err) | ||
else | ||
Async.concat hostedZones.HostedZones, (hz, cb) -> | ||
getAllRecordSetsAcc Route53, hz, [], null, null, cb | ||
, callback | ||
|
||
getAllRecordSetsAcc = (Route53, hz, acc, recordName, recordType, callback) -> | ||
if (recordName? and recordType?) | ||
params = {"HostedZoneId": hz.Id, "StartRecordName": recordName, "StartRecordType": recordType} | ||
else | ||
params = {"HostedZoneId": hz.Id} | ||
|
||
Route53.listResourceRecordSets params, (err, records) -> | ||
if (not records?) | ||
callback(err, []) | ||
else | ||
all = acc.concat records.ResourceRecordSets | ||
|
||
if (records.IsTruncated) | ||
getAllRecordSetsAcc(Route53, hz, all, records.NextRecordName, records.NextRecordType, callback) | ||
else | ||
callback(null, all) | ||
|
||
getLoadBalancersByNames = (Elb, names, callback) -> | ||
params = {"LoadBalancerNames": names} | ||
|
||
Elb.describeLoadBalancers params, (err, loadBalancers) -> | ||
if (not loadBalancers?) | ||
callback err | ||
else | ||
callback null, loadBalancers.LoadBalancerDescriptions | ||
|
||
module.exports = { | ||
prepareFilters: prepareFilters, | ||
getInstancesByParams: getInstancesByParams, | ||
getAllRecordSets: getAllRecordSets, | ||
getLoadBalancersByNames: getLoadBalancersByNames | ||
} |
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
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
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
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
Oops, something went wrong.