-
Notifications
You must be signed in to change notification settings - Fork 43
/
ec2hostsfile.ngs
executable file
·54 lines (42 loc) · 2.29 KB
/
ec2hostsfile.ngs
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
#!/usr/bin/env ngs
# Builds a section of /etc/hosts file listing EC2 instances.
# Naming is based on "env" and "role" tags of the instances.
### sample output
### 172.XX.XX.XX staging-api-gateway # i-b2XXXXXX 5X.XXX.XXX.XXX api-gateway/66
### 172.XX.XX.XX prod-compositor-1 # i-83XXXXXX 5X.XXX.XXX.XXX compositor/978-1-7
### 172.XX.XX.XX prod-compositor-2 # i-84XXXXXX 1XX.XX.XX.XXX compositor/978-1-7
### 172.XX.XX.XX prod-vdr-manager-1 # i-90XXXXXX 5X.XX.XX.XXX vdr-manager-vpc/683-4
### 172.XX.XX.XX prod-vdr-manager-2 # i-97XXXXXX 5X.XX.XXX.XX vdr-manager-vpc/683-4
### 172.XX.XX.XX staging-store # i-79XXXXXX 5X.XX.XXX.XXX store/257
### 172.XX.XX.XX staging-dsp-manager # i-acXXXXXX 5X.X.XX.XXX dsp-manager/707-1-7
### ...
# For minimalistic output: NGS_table_ec2hosts='["PrivateIpAddress", "HostName"]' ./demo/ec2hostsfile.ngs
{
# TODO: sorting of the instances
F HostnameBase(i:Hash) "${i.Tags.env}-${i.Tags.role}"
instances = ``aws ec2 describe-instances --filters Name=instance-state-name,Values=running``
# Only look at instances with "env" and "role" tags
instances = instances.filter(F(i) try HostnameBase(i))
# stats - know which instances to suffix with sequential number and which not
# stats = instances.map(HostnameBase).Stats()
stats = Stats(instances / HostnameBase)
debug("Names stats: ${stats}")
# sequential numbering for instances with same "env" and "role" tags
numbering = Stats()
# Preparing the table
instances.each(F(i) {
hostname_base = HostnameBase(i)
i.HostName = if stats[hostname_base] == 1 hostname_base else {
numbering.push(hostname_base)
"${hostname_base}-${numbering[hostname_base]}"
}
i.Comment = '#'
i.Name = i.Tags.get('Name', '-')
})
config('table_ec2hosts', %[PrivateIpAddress HostName Comment InstanceId PublicIpAddress Name])
config('output_format', 'text')
config('output_title', false)
config('output_headers', false)
t = Table('ec2hosts', instances)
echo(t)
}