-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathautomation-AwsGetInstanceInfo.yml
122 lines (108 loc) · 5.12 KB
/
automation-AwsGetInstanceInfo.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
args:
- default: true
description: AWS EC2 Instance ID
name: instanceId
- description: AWS EC2 Instance Name
name: instanceName
comment: Get AWS EC2 instance details
commonfields:
id: AwsGetInstanceInfo
version: -1
dependson:
must:
- get-instance-info
- get-sg-info
- get-ebs-volume-info
name: AwsGetInstanceInfo
runonce: false
script: |-
function markdownTableHeader(columnNames) {
var header="|";
var divider="|"
for (var idx=0;idx<columnNames.length;idx++) {
header += columnNames[idx] + "|";
divider+= "-|";
}
return header+"\n"+divider+"\n";
}
function formatSecurityGroupRule(id,name,dir,rule) {
var tmpSgTable="";
var tmpProtocol = (rule.ipProtocol == "-1") ? "All" : rule.ipProtocol.toUpperCase();
var tmpPort;
if (!rule.fromPort || rule.fromPort == "-1" || rule.fromPort == "0") {
tmpPort = "All";
} else {
tmpPort = (rule.fromPort == rule.toPort) ? rule.fromPort : rule.fromPort + "-" + rule.toPort;
}
var ipRanges = (Array.isArray(rule.ipRanges.item)) ? (rule.ipRanges.item) : new Array(rule.ipRanges.item);
for (var ipIdx=0;ipIdx<ipRanges.length;ipIdx++) {
tmpSgTable += "|" + name + " (" + id + ")|"+dir+"|"+tmpProtocol+"|"+tmpPort+"|" + ipRanges[ipIdx].cidrIp + "|\n";
}
return tmpSgTable;
}
var keysToPrint = ['privateIpAddress','instanceType','ipAddress','launchTime','privateDnsName','vpcId'];
var res = "";
var resCmd = executeCommand('get-instance-info', args);
if (resCmd[0].Contents.DescribeInstancesResponse.reservationSet.length===0) {
// instnce not found
return { ContentsFormat: formats.text, Type: entryTypes.error, Contents: 'Instance not found.' };
} else {
var instance = resCmd[0].Contents.DescribeInstancesResponse.reservationSet.item.instancesSet.item;
res += "---------\n### Instance ID " + instance.instanceId + " ("+ instance.instanceState.name + ")\n";
res += "\n#### Instance Information\n";
// print only relevant keys based on keysToPrint list, and only string values
var keys = Object.keys(instance);
var tmpKeys="|";
var tmpDivider="|";
var tmpValues="|";
for (var keysIdx=0; keysIdx<keys.length;keysIdx++) {
if (typeof instance[keys[keysIdx]] == "string" && keysToPrint.indexOf(keys[keysIdx])>=0) {
tmpKeys+=keys[keysIdx] + "|";
tmpDivider+="-|";
tmpValues+=instance[keys[keysIdx]] + "|";
}
}
res += tmpKeys + "\n" + tmpDivider + "\n" + tmpValues + "\n";
// print EBS volume data
res += "\n#### EBS Volumes\n";
var tmpVolTable=markdownTableHeader(['Volume ID','Size','Encrypted?','Snapshot ID']);
var volumes = instance.blockDeviceMapping.item;
volumes = (Array.isArray(volumes)) ? volumes : new Array(volumes);
for (var volIdx=0;volIdx<volumes.length;volIdx++) {
var resVolCmd = executeCommand('get-ebs-volume-info', {'volumeId' : volumes[volIdx].ebs.volumeId});
var volInfo = resVolCmd[0].Contents.DescribeVolumesResponse.volumeSet.item;
tmpVolTable += "|" + volInfo.volumeId + "|" + volInfo.size + "|" + volInfo.encrypted + "|" + volInfo.snapshotId + "|\n";
}
res += tmpVolTable;
// print security groups details (using get-sg-info command)
res += "\n#### Security Groups\n";
var tmpSgTable=markdownTableHeader(['Security Group','Direction','Protocol','Port','IP']);
var securityGroups = (Array.isArray(instance.networkInterfaceSet.item.groupSet.item)) ? instance.networkInterfaceSet.item.groupSet.item : new Array(instance.networkInterfaceSet.item.groupSet.item);
for (var sgIdx=0;sgIdx<securityGroups.length;sgIdx++) {
var resSgCmd = executeCommand('get-sg-info', {'sgId' : securityGroups[sgIdx].groupId});
var sg = resSgCmd[0].Contents.DescribeSecurityGroupsResponse.securityGroupInfo.item;
var inbound = (Array.isArray(sg.ipPermissions.item)) ? sg.ipPermissions.item : new Array(sg.ipPermissions.item);
for (var ruleIdx=0;ruleIdx<inbound.length;ruleIdx++) {
tmpSgTable += formatSecurityGroupRule(sg.groupId,sg.groupName,'Inbound',inbound[ruleIdx]);
}
var outbound = (Array.isArray(sg.ipPermissionsEgress.item)) ? sg.ipPermissionsEgress.item : new Array(sg.ipPermissionsEgress.item);
for (var ruleIdx=0;ruleIdx<outbound.length;ruleIdx++) {
tmpSgTable += formatSecurityGroupRule(sg.groupId,sg.groupName,'Outbound',outbound[ruleIdx]);
}
}
res += tmpSgTable;
// print instance tags
res += "\n#### Tags\n";
var tmpTagsTable = markdownTableHeader(['Key','Value']);
var tags = (Array.isArray(instance.tagSet.item)) ? instance.tagSet.item : new Array(instance.tagSet.item);
for (var tagIdx = 0; tagIdx<tags.length; tagIdx++) {
tmpTagsTable += "|" + tags[tagIdx].key + "|" + tags[tagIdx].value + "|\n";
}
res += tmpTagsTable;
return { ContentsFormat: formats.markdown, Type: entryTypes.note, Contents: res } ;
}
scripttarget: 0
system: true
tags:
- Amazon Web Services
type: javascript