1 | Configuration = require './configuration'
|
2 | _ = require('underscore')._
|
3 | _.str = require 'underscore.string'
|
4 | _.mixin _.str.exports()
|
5 |
|
6 | Async = require 'async'
|
7 |
|
8 | Utils = require './utils'
|
9 | AwsHelpers = require './aws_helpers'
|
10 |
|
11 | amazon = (fallback, keyword, filter, filterValue) ->
|
12 |
|
13 | Route53 = new Configuration.Amazon.aws.Route53()
|
14 | Ec2 = new Configuration.Amazon.aws.EC2()
|
15 | Elb = new Configuration.Amazon.aws.ELB()
|
16 |
|
17 | params = {}
|
18 |
|
19 | if (keyword == 'instances')
|
20 | AwsHelpers.getAllRecordSets Route53, (err, recordSets) ->
|
21 | if (not recordSets? or (err? and _.isEmpty(recordSets)))
|
22 | Utils.fallback_printError fallback, err
|
23 | else
|
24 | dnsRoutePairs = mappingDnsWithRoute(recordSets)
|
25 |
|
26 | findInstancesBehindLoadBalancer Elb, dnsRoutePairs, (err, idInstances) ->
|
27 | if (not idInstances?)
|
28 | Utils.fallback_printError fallback, err
|
29 | else
|
30 | if (filter == 'with')
|
31 | display fallback, Ec2, params, dnsRoutePairs, idInstances, filterValue
|
32 | else
|
33 | display fallback, Ec2, params, dnsRoutePairs, idInstances, filter
|
34 |
|
35 |
|
36 | display = (fallback, Ec2, params, dnsRoutePairs, idInstances, filterValue) ->
|
37 | AwsHelpers.getInstancesByParams Ec2, params, (err, results) ->
|
38 | if (not results?)
|
39 | console.log err + results
|
40 | Utils.fallback_printError fallback, err
|
41 | else
|
42 | list = _.map results, (instance) ->
|
43 |
|
44 | tag = _.findWhere instance.Tags, {"Key": "Name"}
|
45 | security = instance.SecurityGroups[0]
|
46 | role = if instance.IamInstanceProfile? then " / Role: " + instance.IamInstanceProfile.Arn.split('/')[1] else ""
|
47 | route = _.findWhere dnsRoutePairs, {"Dns": instance.PublicDnsName}
|
48 | title =
|
49 | if (route?)
|
50 | route.Route
|
51 | else if instance.PublicDnsName
|
52 | instance.PublicDnsName
|
53 | else
|
54 | "No route or public dns"
|
55 |
|
56 | behinds = _.findWhere idInstances, {"Id": instance.InstanceId}
|
57 | lb = if behinds? and behinds.LoadBalancer? then " - behind: #{behinds.LoadBalancer.LoadBalancerName}" else ""
|
58 |
|
59 | {
|
60 | title: title
|
61 |
|
62 | url: if route? or instance.PublicDnsName then "http://#{title}"
|
63 | infos: if tag? then tag.Value + ' / ' + instance.InstanceType else instance.InstanceType
|
64 | comments: if security? then "Security: #{security.GroupName}#{role}"
|
65 | status: instance.State.Name == 'running'
|
66 | tails: if behinds? then _.map _.pluck(behinds.Routes, 'Route'), (route) -> "via #{route}#{lb}"
|
67 | }
|
68 |
|
69 | Utils.fallback_printList fallback, list, (list) ->
|
70 | if (filterValue?)
|
71 | _.filter list, (o) ->
|
72 | stuff = o.title + o.infos + o.comments
|
73 | stuff += _.reduce o.tails, (memo, tail) ->
|
74 | memo.concat(tail)
|
75 | , []
|
76 |
|
77 | _.str.include(stuff, filterValue)
|
78 | else
|
79 | list
|
80 |
|
81 | mappingDnsWithRoute = (recordSets) ->
|
82 | _.reduce recordSets, (memo, recordSet) ->
|
83 | if (recordSet? and not _.isEmpty(recordSet.ResourceRecords))
|
84 | memo.concat _.map recordSet.ResourceRecords, (record) ->
|
85 | { Dns: record.Value, Route: recordSet.Name }
|
86 | else if (recordSet?)
|
87 | memo.concat [
|
88 | { Dns: recordSet.AliasTarget.DNSName, Route: recordSet.Name }
|
89 | ]
|
90 | else
|
91 | memo
|
92 | , []
|
93 |
|
94 | findInstancesBehindLoadBalancer = (Elb, routes, callback) ->
|
95 | names = _.reduce routes, (memo, dnsRoutePair) ->
|
96 | match = dnsRoutePair.Dns.match("^(.*lb)-")
|
97 |
|
98 | if match then memo.concat(match[1]) else memo
|
99 | , []
|
100 |
|
101 | AwsHelpers.getLoadBalancersByNames Elb, _.uniq(names), (err, lbDescriptions) ->
|
102 | if (not lbDescriptions?)
|
103 | callback err
|
104 | else
|
105 | Async.reduce lbDescriptions.LoadBalancerDescriptions, [], (memo, description, cb) ->
|
106 | cb null, memo.concat _.map description.Instances, (instance) ->
|
107 | {
|
108 | Id: instance.InstanceId,
|
109 | LoadBalancer: description,
|
110 | Routes: _.where(routes, {"Dns": description.DNSName + "."})
|
111 | }
|
112 | , (err, idInstances) ->
|
113 | callback null, idInstances
|
114 |
|
115 | module.exports = {
|
116 | name: "Amazon",
|
117 | description: "[ instances [ with -term- ] ] Display various informations about your Amazon infrastructure",
|
118 | action: amazon
|
119 | }
|