Created
June 2, 2016 11:43
-
-
Save JensRantil/deac041b78465989f55c9a43c90a4ef1 to your computer and use it in GitHub Desktop.
Script that runs in the background and tells you when ElasticSearch migration/cluster has stabilised. Requires the "say" command that exists on MaxOSX.
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" | |
"log" | |
"net/http" | |
"os" | |
"os/exec" | |
"time" | |
) | |
func say(s string) { | |
exec.Command("say", s).Run() | |
} | |
type Health struct { | |
Status string `json:"status"` | |
RelocatingShards int `json:"relocating_shards"` | |
InitializingShards int `json:"initializing_shards"` | |
} | |
func (h Health) String() string { | |
return fmt.Sprintf("{Status: %s, RelocatingShards: %d, InitializingShards: %d}", h.Status, h.RelocatingShards, h.InitializingShards) | |
} | |
func getHealth(ip string) Health { | |
s := &Health{} | |
resp, err := http.Get(fmt.Sprintf("http://%s:9200/_cluster/health?pretty", ip)) | |
if err != nil { | |
log.Fatalln(err) | |
} | |
defer resp.Body.Close() | |
d := json.NewDecoder(resp.Body) | |
err = d.Decode(s) | |
if err != nil { | |
log.Fatalln(err) | |
} | |
return *s | |
} | |
func main() { | |
if len(os.Args) != 2 { | |
log.Fatalln("Usage: ./tell-me-when-elasticsearch-is-done <ip>") | |
} | |
ip := os.Args[1] | |
say("Starting to poll elastic search. I'll let you know when status is red or green.") | |
for { | |
health := getHealth(ip) | |
log.Println(health) | |
toSay := "Elastic search " | |
switch health.Status { | |
case "red": | |
toSay = "status is red" | |
case "green": | |
toSay = "status is green" | |
} | |
if health.RelocatingShards > 0 { | |
if toSay != "" { | |
toSay += " and it " | |
} | |
toSay += "is still relocating" | |
} | |
if health.InitializingShards > 0 { | |
if toSay != "" { | |
toSay += " and it " | |
} | |
toSay += "is still initializing" | |
} | |
say(toSay) | |
time.Sleep(5 * time.Minute) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment