Created
October 4, 2015 06:01
-
-
Save orcaman/30c9c7882c1914b7d75a to your computer and use it in GitHub Desktop.
monitor deploy hash on aws
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
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"github.com/aws/aws-sdk-go/aws" | |
"github.com/aws/aws-sdk-go/service/ec2" | |
"io/ioutil" | |
"net" | |
"net/http" | |
"sync" | |
"time" | |
) | |
var wg sync.WaitGroup | |
func main() { | |
stats := make(map[string][]string) | |
svc := ec2.New(&aws.Config{Region: aws.String("us-east-1")}) | |
resp, err := svc.DescribeInstances(nil) | |
if err != nil { | |
panic(err) | |
} | |
for idx, _ := range resp.Reservations { | |
for _, inst := range resp.Reservations[idx].Instances { | |
for _, t := range inst.Tags { | |
if *t.Key == "Name" { | |
wg.Add(1) | |
stats[*t.Value] = appendHash(stats[*t.Value], *inst.PublicDnsName) | |
} | |
} | |
} | |
} | |
wg.Wait() | |
str, _ := json.Marshal(stats) | |
fmt.Println(string(str)) | |
} | |
func appendHash(hashes []string, publicDnsName string) []string { | |
defer wg.Done() | |
if hashes == nil { | |
hashes = []string{} | |
} | |
url := "http://" + publicDnsName + "/debug/vars" | |
fmt.Println("url: " + url) | |
req, _ := http.NewRequest("GET", url, nil) | |
tr := http.Transport{ | |
Dial: func(network, addr string) (net.Conn, error) { | |
timeout := 2 * time.Second | |
return net.DialTimeout(network, addr, timeout) | |
}, | |
} | |
tr.ResponseHeaderTimeout = 2 * time.Second | |
res, err := tr.RoundTrip(req) | |
if err != nil { | |
fmt.Println(err.Error()) | |
return hashes | |
} | |
str, err := ioutil.ReadAll(res.Body) | |
defer res.Body.Close() | |
if err != nil { | |
fmt.Println(err.Error()) | |
return hashes | |
} | |
var obj map[string]interface{} | |
json.Unmarshal(str, &obj) | |
if obj["DeployHashKey"] != nil { | |
hash := obj["DeployHashKey"].(string) | |
hashes = append(hashes, hash) | |
} | |
return hashes | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment