summaryrefslogtreecommitdiff
path: root/bookpipeline/cmd/lspipeline/main.go
blob: 8794b382af2880e5fd3653f0e6948c97bc889896 (plain)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main

import (
	"errors"
	"flag"
	"fmt"
	"log"

	// TODO: abstract out the aws stuff into aws.go in due course
	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/ec2"
	//"github.com/aws/aws-sdk-go/service/s3"
	"github.com/aws/aws-sdk-go/service/sqs"
)

const usage = `Usage: lspipeline

Lists useful things related to the pipeline.

- Instances running
- Messages in each queue
- Books not completed (from S3 without a best file)
- Books completed (from S3 with a best file)
- Last 5 lines of bookpipeline logs from each running instance (with -v)
`

type instanceDetails struct {
	id, name, ip, spot, iType, state, launchTime string
}

type queueDetails struct {
	name, numAvailable, numInProgress string
}

func ec2getInstances(svc *ec2.EC2, instances chan instanceDetails) {
	err := svc.DescribeInstancesPages(&ec2.DescribeInstancesInput{}, parseInstances(instances))
	if err != nil {
		close(instances)
		log.Println("Error with ec2 DescribeInstancePages call:", err)
	}
}

func parseInstances(details chan instanceDetails) (func(*ec2.DescribeInstancesOutput, bool) bool) {
	return func(page *ec2.DescribeInstancesOutput, lastPage bool) bool {
		for _, r := range page.Reservations {
			for _, i := range r.Instances {
				var d instanceDetails

				for _, t := range i.Tags {
					if *t.Key == "Name" {
						d.name = *t.Value
					}
				}
				if i.PublicIpAddress != nil {
					d.ip = *i.PublicIpAddress
				}
				if i.SpotInstanceRequestId != nil {
					d.spot = *i.SpotInstanceRequestId
				}
				d.iType = *i.InstanceType
				d.id = *i.InstanceId
				d.launchTime = i.LaunchTime.String()
				d.state = *i.State.Name

				details <- d
			}
		}
		if lastPage {
			close(details)
		}
		return !lastPage
	}
}

func ec2GetQueueDetails(svc *sqs.SQS, qdetails chan queueDetails, qnames []string) {
	for _, q := range qnames {
		avail, inprog, err := getQueueDetails(q, svc)
		if err != nil {
			log.Println("Error getting queue details:", err)
		}
		var qd queueDetails
		qd.name = q
		qd.numAvailable = avail
		qd.numInProgress = inprog
		qdetails <- qd
	}
	close(qdetails)
}

func getQueueDetails(qname string, svc *sqs.SQS) (string, string, error) {
	result, err := svc.GetQueueUrl(&sqs.GetQueueUrlInput{
		QueueName: aws.String(qname),
	})
	if err != nil {
		return "", "", err
	}
	prequrl := *result.QueueUrl

	numAvailable := "ApproximateNumberOfMessages"
	numInProgress := "ApproximateNumberOfMessagesNotVisible"
	attrs, err := svc.GetQueueAttributes(&sqs.GetQueueAttributesInput{
		AttributeNames: []*string{&numAvailable, &numInProgress},
		QueueUrl: &prequrl,
	})
	if err != nil {
		return "", "", errors.New(fmt.Sprintf("Failed to get queue attributes: %s", err))
	}
	return *attrs.Attributes[numAvailable], *attrs.Attributes[numInProgress], nil
}

func main() {
	flag.Usage = func() {
		fmt.Fprintf(flag.CommandLine.Output(), usage)
		flag.PrintDefaults()
	}
	flag.Parse()

	sess, err := session.NewSession(&aws.Config{
		Region: aws.String("eu-west-2"),
	})
	if err != nil {
		log.Fatalln("Failed to set up aws session", err)
	}
	ec2svc := ec2.New(sess)
	//s3svc := s3.New(sess)
	sqssvc := sqs.New(sess)

	instances := make(chan instanceDetails, 100)
	queues := make(chan queueDetails)

	go ec2getInstances(ec2svc, instances)
	go ec2GetQueueDetails(sqssvc, queues, []string{"rescribepreprocess", "rescribeocr", "rescribeanalyse"})

	fmt.Println("# Instances")
	for i := range instances {
		fmt.Printf("ID: %s, Type: %s, LaunchTime: %s, State: %s", i.id, i.iType, i.launchTime, i.state)
		if i.name != "" {
			fmt.Printf(", Name: %s", i.name)
		}
		if i.ip != "" {
			fmt.Printf(", IP: %s", i.ip)
		}
		if i.spot != "" {
			fmt.Printf(", SpotRequest: %s", i.spot)
		}
		fmt.Printf("\n")
	}

	fmt.Println("\n# Queues")
	for i := range queues {
		fmt.Printf("%s: %s available, %s in progress\n", i.name, i.numAvailable, i.numInProgress)
	}

	// TODO: See remaining items in the usage statement
}