Skip to content

Commit

Permalink
gofmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Magnus Kulke committed Jul 3, 2019
1 parent a7cb630 commit 4d4aa67
Showing 1 changed file with 47 additions and 47 deletions.
94 changes: 47 additions & 47 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,63 +1,63 @@
package main

import (
"github.com/aws/aws-sdk-go/aws"
"encoding/json"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ecs"
"io/ioutil"
"encoding/json"
"net/http"
"time"
"log"
"io/ioutil"
"log"
"net/http"
"time"
)

type ECSMetadata struct {
Cluster string `json:"Cluster"`
ContainerInstanceArn string `json:"ContainerInstanceArn"`
Cluster string `json:"Cluster"`
ContainerInstanceArn string `json:"ContainerInstanceArn"`
}

func main() {
client := &http.Client{Timeout: time.Second * 10}
client := &http.Client{Timeout: time.Second * 10}

for {
time.Sleep(5 * time.Second)
response, err := client.Get("http://169.254.169.254/latest/meta-data/spot/termination-time")
if err != nil {
panic(err)
}

if response.StatusCode == 200 {
log.Print("Spot instance termination notice detected")
break
}
}
for {
time.Sleep(5 * time.Second)
response, err := client.Get("http://169.254.169.254/latest/meta-data/spot/termination-time")
if err != nil {
panic(err)
}

response, err := client.Get("http://localhost:51678/v1/metadata")
if err != nil {
panic(err);
}
buf, err := ioutil.ReadAll(response.Body)
if err != nil {
panic(err);
}
var ecsMetadata ECSMetadata
err = json.Unmarshal(buf, &ecsMetadata)
if err != nil {
panic(err);
}
if response.StatusCode == 200 {
log.Print("Spot instance termination notice detected")
break
}
}

response, err := client.Get("http://localhost:51678/v1/metadata")
if err != nil {
panic(err)
}
buf, err := ioutil.ReadAll(response.Body)
if err != nil {
panic(err)
}
var ecsMetadata ECSMetadata
err = json.Unmarshal(buf, &ecsMetadata)
if err != nil {
panic(err)
}
svc := ecs.New(session.New(&aws.Config{}))
status := "DRAINING"
state := &ecs.UpdateContainerInstancesStateInput{
Cluster: &ecsMetadata.Cluster,
ContainerInstances: []*string{&ecsMetadata.ContainerInstanceArn},
Status: &status,
}
log.Print("Putting instance in state DRAINING")
_, err = svc.UpdateContainerInstancesState(state)
if err != nil {
panic(err);
}
status := "DRAINING"
state := &ecs.UpdateContainerInstancesStateInput{
Cluster: &ecsMetadata.Cluster,
ContainerInstances: []*string{&ecsMetadata.ContainerInstanceArn},
Status: &status,
}
log.Print("Putting instance in state DRAINING")
_, err = svc.UpdateContainerInstancesState(state)
if err != nil {
panic(err)
}

log.Print("Sleeping for 120s until termination")
time.Sleep(120 * time.Second)
log.Print("Sleeping for 120s until termination")
time.Sleep(120 * time.Second)
}

0 comments on commit 4d4aa67

Please sign in to comment.