Skip to content

Commit

Permalink
Create a "watch" mode (jump @)
Browse files Browse the repository at this point in the history
  • Loading branch information
pwaller committed Dec 20, 2014
1 parent 2303242 commit 0f1d540
Showing 1 changed file with 76 additions and 11 deletions.
87 changes: 76 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package main
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"syscall"
"time"

"github.com/olekukonko/tablewriter"

Expand Down Expand Up @@ -44,7 +47,7 @@ func ShowInstances(instances []*Instance) {
table.Render()
}

func UserSelectInstance(max int) int {
func GetInstanceFromUser(max int) int {
s := bufio.NewScanner(os.Stdin)
if !s.Scan() {
// User closed stdin before we read anything
Expand All @@ -65,7 +68,7 @@ func UserSelectInstance(max int) int {
}

func InvokeSSH(instance *Instance) {
log.Println("Sending you to:", instance)
fmt.Fprintln(os.Stderr, "Connecting to", instance.Name())

args := []string{"/usr/bin/ssh"}
// Enable the user to specify arguments to the left and right of the host.
Expand All @@ -80,25 +83,87 @@ func InvokeSSH(instance *Instance) {
}
}

func main() {
func CursorUp(n int) {
fmt.Fprint(os.Stderr, "[", n, "F")
}
func ClearToEndOfScreen() {
fmt.Fprint(os.Stderr, "[", "J")
}

if os.Getenv("SSH_AUTH_SOCK") == "" {
fmt.Fprintln(os.Stderr, "Warning: agent forwarding not enabled")
}
func JumpTo(client *ec2.EC2) {

creds := aws.IAMCreds()
c := ec2.New(creds, AWS_REGION, nil)
ec2Instances, err := c.DescribeInstances(&ec2.DescribeInstancesRequest{})
ec2Instances, err := client.DescribeInstances(&ec2.DescribeInstancesRequest{})
if err != nil {
log.Fatal("DescribeInstances error:", err)
}

// Do this after querying the AWS endpoint (otherwise vulnerable to MITM.)
ConfigureHTTP()
ConfigureHTTP(false)

instances := InstancesFromEC2Result(ec2Instances)
ShowInstances(instances)

n := UserSelectInstance(len(instances))
n := GetInstanceFromUser(len(instances))

// +1 to account for final newline.
CursorUp(len(instances) + N_TABLE_DECORATIONS + 1)
ClearToEndOfScreen()

InvokeSSH(instances[n])
}

func Watch(client *ec2.EC2) {
creds := aws.IAMCreds()
c := ec2.New(creds, AWS_REGION, nil)

finish := make(chan struct{})
go func() {
defer close(finish)
// Await stdin closure
io.Copy(ioutil.Discard, os.Stdin)
}()

for {
queryStart := time.Now()
ConfigureHTTP(true)

ec2Instances, err := c.DescribeInstances(&ec2.DescribeInstancesRequest{})
if err != nil {
log.Fatal("DescribeInstances error:", err)
}

ConfigureHTTP(false)

instances := InstancesFromEC2Result(ec2Instances)
ShowInstances(instances)

queryDuration := time.Since(queryStart)

select {
case <-time.After(1*time.Second - queryDuration):
case <-finish:
return
}
CursorUp(len(instances) + N_TABLE_DECORATIONS)
}

}

const N_TABLE_DECORATIONS = 4

func main() {
if os.Getenv("SSH_AUTH_SOCK") == "" {
fmt.Fprintln(os.Stderr, "Warning: agent forwarding not enabled")
}

// Pull requests welcome: code to generalise this to other clouds.
creds := aws.IAMCreds()
client := ec2.New(creds, AWS_REGION, nil)

if len(os.Args) > 1 && os.Args[1] == "@" {
Watch(client)
return
}

JumpTo(client)
}

0 comments on commit 0f1d540

Please sign in to comment.