You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

202 lines
4.4 KiB

package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"os/exec"
"regexp"
"strconv"
"strings"
"github.com/fatih/color"
)
type command struct {
node string
hc string
}
type healthCheck struct {
Name string `json:"name"`
PodSpecs []struct {
Type string `json:"type"`
TaskSpecs []struct {
HealthCheckSpec struct {
Command string `json:"command"`
} `json:"health-check-spec"`
} `json:"task-specs"`
} `json:"pod-specs"`
}
type serviceIPs struct {
Name string `json:"name"`
Statuses []struct {
ContainerStatus struct {
NetworkInfos []struct {
IPAddresses []struct {
IPAddress string `json:"ip_address"`
} `json:"ip_addresses"`
} `json:"network_infos"`
} `json:"container_status"`
} `json:"statuses"`
}
func getHealthCheck() healthCheck {
cmd := exec.Command("dcos", "kubernetes", "config", "target")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
h := healthCheck{}
if err := json.Unmarshal([]byte(out.String()), &h); err != nil {
panic(err)
}
return h
}
func getServiceIPs() []serviceIPs {
cmd := exec.Command("dcos", "task", "--json")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
s := []serviceIPs{}
if err := json.Unmarshal([]byte(out.String()), &s); err != nil {
panic(err)
}
// get rid of kubernetes pod
//for i, n := range s {
// if n.Name == "kubernetes" {
// s = append(s[:i], s[i+1:]...)
// }
//}
return s
}
func addAPIServer(n string, ips []serviceIPs) string {
for _, i := range ips {
if strings.Contains(i.Name, n) {
return i.Statuses[0].ContainerStatus.NetworkInfos[0].IPAddresses[0].IPAddress
}
}
return ""
}
func stripHealthCheck(n string, hcs healthCheck) string {
for _, j := range hcs.PodSpecs {
if strings.Contains(n, j.Type) {
if len(j.TaskSpecs) != 0 {
return j.TaskSpecs[0].HealthCheckSpec.Command
}
return ""
}
}
return ""
}
func getPodInstanceIndex(n string) (int, error) {
re := regexp.MustCompile("[0-9]")
index := re.FindString(n)
i, err := strconv.Atoi(index)
return i, err
}
func buildCommands(hcs healthCheck, ips []serviceIPs) []command {
commands := []command{}
for _, s := range ips {
if strings.Contains(s.Name, "apiserver") {
index, err := getPodInstanceIndex(s.Name)
if err != nil {
log.Fatal("failed to get pod index for build command: ", err)
}
c := strings.Replace(stripHealthCheck(s.Name, hcs), "$POD_INSTANCE_INDEX", strconv.Itoa(index), -1)
commands = append(commands, command{node: s.Name, hc: c})
} else if strings.Contains(s.Name, "etcd") {
index, err := getPodInstanceIndex(s.Name)
if err != nil {
log.Fatal("failed to get pod index for build command: ", err)
}
c := strings.Replace(stripHealthCheck(s.Name, hcs), "$POD_INSTANCE_INDEX", strconv.Itoa(index), -1)
c = strings.Replace(c, "$ETCD_LISTEN_CLIENT_PORT", "2379", -1)
commands = append(commands, command{node: s.Name, hc: c})
//https://etcd-$POD_INSTANCE_INDEX-peer.{{FRAMEWORK_NAME}}.mesos:$ETCD_LISTEN_CLIENT_PORT/health
} else {
//commands = append(commands, command{node: s.Name, hc: stripHealthCheck(s.Name, hcs)})
}
}
return commands
}
func main() {
//var wg sync.WaitGroup
hcs := getHealthCheck()
ips := getServiceIPs()
//for _, i := range hcs.PodSpecs {
// fmt.Println(i)
//}
//fmt.Println("\n\n\n")
//fmt.Printf("%+v\n", hcs)
//fmt.Println("\n\n\n")
//fmt.Printf("%+v\n", ips)
//fmt.Println("\n\n\n")
hosts := buildCommands(hcs, ips)
//for i, j := range hosts {
// fmt.Printf("%d %+v\n", i, j)
//}
//fmt.Println(hosts)
//time.Sleep(time.Second * 5)
for _, n := range hosts {
//wg.Add(1)
//go func(c command) {
//defer wg.Done()
// s := strings.Split(n.hc, " ")
//if len(s) != 3 {
// log.Printf("could not be split: %v", s)
// return
//}
//for i, j := range s {
// fmt.Println(i, j)
//}
cmd := exec.Command(
"dcos",
"task",
"exec",
n.node,
n.hc,
//s[0],
//s[1],
//s[2],
//s[3],
//s[4],
//s[5],
//s[6],
//s[7],
//s[8],
)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
log.Printf("failed for %v with:", n, err)
return
}
if strings.Contains(out.String(), "ok") || strings.Contains(out.String(), "true") {
color.Green("%s", n.node)
} else {
color.Red(fmt.Sprintf("%s: \t%q\n", n.node, out.String()))
}
//}(n)
}
// wg.Wait()
}