-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
301 additions
and
160 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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"time" | ||
) | ||
|
||
func fmtDuration(duration time.Duration) string { | ||
result := []string{} | ||
started := false | ||
if duration > 7*24*time.Hour || started { | ||
started = true | ||
weeks := int(duration.Hours() / 24 / 7) | ||
duration %= 7 * 24 * time.Hour | ||
result = append(result, fmt.Sprintf("%2dw", weeks)) | ||
} | ||
if duration > 24*time.Hour || started { | ||
started = true | ||
days := int(duration.Hours() / 24) | ||
duration %= 24 * time.Hour | ||
result = append(result, fmt.Sprintf("%2dd", days)) | ||
} | ||
if duration > time.Hour || started { | ||
started = true | ||
hours := int(duration.Hours()) | ||
duration %= time.Hour | ||
result = append(result, fmt.Sprintf("%2dh", hours)) | ||
} | ||
if duration > time.Minute || started { | ||
started = true | ||
minutes := int(duration.Minutes()) | ||
duration %= time.Minute | ||
result = append(result, fmt.Sprintf("%2dm", minutes)) | ||
} | ||
seconds := int(duration.Seconds()) | ||
result = append(result, fmt.Sprintf("%2ds", seconds)) | ||
return strings.TrimSpace(strings.Join(result, "")) | ||
} |
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,94 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"sort" | ||
"time" | ||
|
||
"github.com/stripe/aws-go/gen/ec2" | ||
) | ||
|
||
type Instance struct { | ||
InstanceID, PrivateIP, State 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.State.Name, | ||
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.DescribeInstancesResult) []*Instance { | ||
out := []*Instance{} | ||
for _, r := range in.Reservations { | ||
for _, oi := range r.Instances { | ||
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() } |
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.