forked from pwaller/jump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instance.go
98 lines (87 loc) · 2.09 KB
/
instance.go
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
package main
import (
"fmt"
"sort"
"time"
"github.com/aws/aws-sdk-go/service/ec2"
)
type Instance struct {
InstanceID, PrivateIP, PublicIP, State, VPCID string
Up time.Duration
Tags map[string]string
ICMPPing, SSHPing, HTTPPing, HTTPSPing <-chan PingResponse
}
func NewInstance(i *ec2.Instance) *Instance {
return &Instance{
*i.InstanceId, *i.PrivateIpAddress, *i.PublicIpAddress, *i.State.Name,
*i.VpcId,
time.Since(*i.LaunchTime),
TagMap(i.Tags),
ICMPPing(*i.PrivateIpAddress),
SSHPing(*i.PrivateIpAddress),
HTTPPing(*i.PrivateIpAddress),
HTTPSPing(*i.PrivateIpAddress),
}
}
func TagMap(ts []*ec2.Tag) map[string]string {
m := map[string]string{}
for _, t := range ts {
m[*t.Key] = *t.Value
}
return m
}
func (i *Instance) Name() string {
return i.Tags["Name"]
}
func (i *Instance) String() string {
return fmt.Sprint(i.InstanceID, " ", i.Name(), " ", i.PrivateIP)
}
func (i *Instance) PrettyState() string {
var (
s = ""
color = ""
)
switch i.State {
default:
s = "U"
case "running":
s = "R"
color = "32" // Green
case "rebooting":
s = "B"
color = "34" // Blue
case "pending":
s = "P"
color = "33" // Yellow
case "stopping":
s = "-"
color = "33" // Yellow
case "shutting-down":
s = "G"
color = "33" // Yellow
case "stopped":
s = "."
color = "31" // Red
case "terminated":
s = "T"
color = "31" // Red
}
return fmt.Sprint("[" + color + "m" + s + "[m")
}
func InstancesFromEC2Result(in *ec2.DescribeInstancesOutput) []*Instance {
out := []*Instance{}
for _, r := range in.Reservations {
for _, oi := range r.Instances {
if oi.PrivateIpAddress == nil || oi.PublicIpAddress == nil {
continue
}
out = append(out, NewInstance(oi))
}
}
sort.Sort(InstancesByName(out))
return out
}
type InstancesByName []*Instance
func (a InstancesByName) Len() int { return len(a) }
func (a InstancesByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a InstancesByName) Less(i, j int) bool { return a[i].Name() < a[j].Name() }