summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick White <git@njw.name>2019-11-12 12:51:58 +0000
committerNick White <git@njw.name>2019-11-12 12:51:58 +0000
commit9aa1a692d0709b419dded5009dc2cf78dd0759c2 (patch)
tree3413a7d148b347cd273cd0dfdf4c88af26ae5634
parent1ebb21bfd9523291b3cc6f59103204942c5b9400 (diff)
Add spotme command to start appropriate spot instances
-rw-r--r--aws.go18
-rw-r--r--cmd/spotme/main.go53
2 files changed, 71 insertions, 0 deletions
diff --git a/aws.go b/aws.go
index 4aea082..73f3b2f 100644
--- a/aws.go
+++ b/aws.go
@@ -349,3 +349,21 @@ func (a *AwsConn) GetInstanceDetails() ([]InstanceDetails, error) {
})
return details, err
}
+
+func (a *AwsConn) StartInstances(n int) error {
+ _, err := a.ec2svc.RequestSpotInstances(&ec2.RequestSpotInstancesInput{
+ InstanceCount: aws.Int64(int64(n)),
+ LaunchSpecification: &ec2.RequestSpotLaunchSpecification{
+ IamInstanceProfile: &ec2.IamInstanceProfileSpecification{
+ Arn: aws.String("arn:aws:iam::557852942063:instance-profile/pipeliner"),
+ },
+ ImageId: aws.String("ami-02cd15d68d4ca2865"),
+ InstanceType: aws.String("m5.large"),
+ SecurityGroupIds: []*string{
+ aws.String("sg-0be8a3ab89e7136b9"),
+ },
+ },
+ Type: aws.String("one-time"),
+ })
+ return err
+}
diff --git a/cmd/spotme/main.go b/cmd/spotme/main.go
new file mode 100644
index 0000000..99404cb
--- /dev/null
+++ b/cmd/spotme/main.go
@@ -0,0 +1,53 @@
+package main
+
+import (
+ "flag"
+ "fmt"
+ "log"
+
+ "rescribe.xyz/bookpipeline"
+)
+
+const usage = `Usage: spotme [-n num]
+
+Create new spot instances for the book pipeline.
+`
+
+type SpotPipeliner interface {
+ Init() error
+ StartInstances(n int) error
+}
+
+// NullWriter is used so non-verbose logging may be discarded
+type NullWriter bool
+
+func (w NullWriter) Write(p []byte) (n int, err error) {
+ return len(p), nil
+}
+
+func main() {
+ num := flag.Int("n", 1, "number of instances to start")
+ flag.Usage = func() {
+ fmt.Fprintf(flag.CommandLine.Output(), usage)
+ flag.PrintDefaults()
+ }
+ flag.Parse()
+
+ var verboselog *log.Logger
+ var n NullWriter
+ verboselog = log.New(n, "", 0)
+
+ var conn SpotPipeliner
+ conn = &bookpipeline.AwsConn{Region: "eu-west-2", Logger: verboselog}
+ err := conn.Init()
+ if err != nil {
+ log.Fatalln("Failed to set up cloud connection:", err)
+ }
+
+ log.Println("Starting spot instances")
+ err = conn.StartInstances(*num)
+ if err != nil {
+ log.Fatalln("Failed to start a spot instance:", err)
+ }
+ log.Println("Spot instance request sent successfully")
+}