-
Notifications
You must be signed in to change notification settings - Fork 1
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
Magnus Kulke
committed
Jul 3, 2019
1 parent
a7cb630
commit 4d4aa67
Showing
1 changed file
with
47 additions
and
47 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 |
---|---|---|
@@ -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) | ||
} |