No Description
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.

main.go 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "log"
  7. "os/exec"
  8. "regexp"
  9. "strconv"
  10. "strings"
  11. "github.com/fatih/color"
  12. )
  13. type command struct {
  14. node string
  15. hc string
  16. }
  17. type healthCheck struct {
  18. Name string `json:"name"`
  19. PodSpecs []struct {
  20. Type string `json:"type"`
  21. TaskSpecs []struct {
  22. HealthCheckSpec struct {
  23. Command string `json:"command"`
  24. } `json:"health-check-spec"`
  25. } `json:"task-specs"`
  26. } `json:"pod-specs"`
  27. }
  28. type serviceIPs struct {
  29. Name string `json:"name"`
  30. Statuses []struct {
  31. ContainerStatus struct {
  32. NetworkInfos []struct {
  33. IPAddresses []struct {
  34. IPAddress string `json:"ip_address"`
  35. } `json:"ip_addresses"`
  36. } `json:"network_infos"`
  37. } `json:"container_status"`
  38. } `json:"statuses"`
  39. }
  40. func getHealthCheck() healthCheck {
  41. cmd := exec.Command("dcos", "kubernetes", "config", "target")
  42. var out bytes.Buffer
  43. cmd.Stdout = &out
  44. err := cmd.Run()
  45. if err != nil {
  46. log.Fatal(err)
  47. }
  48. h := healthCheck{}
  49. if err := json.Unmarshal([]byte(out.String()), &h); err != nil {
  50. panic(err)
  51. }
  52. return h
  53. }
  54. func getServiceIPs() []serviceIPs {
  55. cmd := exec.Command("dcos", "task", "--json")
  56. var out bytes.Buffer
  57. cmd.Stdout = &out
  58. err := cmd.Run()
  59. if err != nil {
  60. log.Fatal(err)
  61. }
  62. s := []serviceIPs{}
  63. if err := json.Unmarshal([]byte(out.String()), &s); err != nil {
  64. panic(err)
  65. }
  66. // get rid of kubernetes pod
  67. //for i, n := range s {
  68. // if n.Name == "kubernetes" {
  69. // s = append(s[:i], s[i+1:]...)
  70. // }
  71. //}
  72. return s
  73. }
  74. func addAPIServer(n string, ips []serviceIPs) string {
  75. for _, i := range ips {
  76. if strings.Contains(i.Name, n) {
  77. return i.Statuses[0].ContainerStatus.NetworkInfos[0].IPAddresses[0].IPAddress
  78. }
  79. }
  80. return ""
  81. }
  82. func stripHealthCheck(n string, hcs healthCheck) string {
  83. for _, j := range hcs.PodSpecs {
  84. if strings.Contains(n, j.Type) {
  85. if len(j.TaskSpecs) != 0 {
  86. return j.TaskSpecs[0].HealthCheckSpec.Command
  87. }
  88. return ""
  89. }
  90. }
  91. return ""
  92. }
  93. func getPodInstanceIndex(n string) (int, error) {
  94. re := regexp.MustCompile("[0-9]")
  95. index := re.FindString(n)
  96. i, err := strconv.Atoi(index)
  97. return i, err
  98. }
  99. func buildCommands(hcs healthCheck, ips []serviceIPs) []command {
  100. commands := []command{}
  101. for _, s := range ips {
  102. if strings.Contains(s.Name, "apiserver") {
  103. index, err := getPodInstanceIndex(s.Name)
  104. if err != nil {
  105. log.Fatal("failed to get pod index for build command: ", err)
  106. }
  107. c := strings.Replace(stripHealthCheck(s.Name, hcs), "$POD_INSTANCE_INDEX", strconv.Itoa(index), -1)
  108. commands = append(commands, command{node: s.Name, hc: c})
  109. } else if strings.Contains(s.Name, "etcd") {
  110. index, err := getPodInstanceIndex(s.Name)
  111. if err != nil {
  112. log.Fatal("failed to get pod index for build command: ", err)
  113. }
  114. c := strings.Replace(stripHealthCheck(s.Name, hcs), "$POD_INSTANCE_INDEX", strconv.Itoa(index), -1)
  115. c = strings.Replace(c, "$ETCD_LISTEN_CLIENT_PORT", "2379", -1)
  116. commands = append(commands, command{node: s.Name, hc: c})
  117. //https://etcd-$POD_INSTANCE_INDEX-peer.{{FRAMEWORK_NAME}}.mesos:$ETCD_LISTEN_CLIENT_PORT/health
  118. } else {
  119. //commands = append(commands, command{node: s.Name, hc: stripHealthCheck(s.Name, hcs)})
  120. }
  121. }
  122. return commands
  123. }
  124. func main() {
  125. //var wg sync.WaitGroup
  126. hcs := getHealthCheck()
  127. ips := getServiceIPs()
  128. //for _, i := range hcs.PodSpecs {
  129. // fmt.Println(i)
  130. //}
  131. //fmt.Println("\n\n\n")
  132. //fmt.Printf("%+v\n", hcs)
  133. //fmt.Println("\n\n\n")
  134. //fmt.Printf("%+v\n", ips)
  135. //fmt.Println("\n\n\n")
  136. hosts := buildCommands(hcs, ips)
  137. //for i, j := range hosts {
  138. // fmt.Printf("%d %+v\n", i, j)
  139. //}
  140. //fmt.Println(hosts)
  141. //time.Sleep(time.Second * 5)
  142. for _, n := range hosts {
  143. //wg.Add(1)
  144. //go func(c command) {
  145. //defer wg.Done()
  146. // s := strings.Split(n.hc, " ")
  147. //if len(s) != 3 {
  148. // log.Printf("could not be split: %v", s)
  149. // return
  150. //}
  151. //for i, j := range s {
  152. // fmt.Println(i, j)
  153. //}
  154. cmd := exec.Command(
  155. "dcos",
  156. "task",
  157. "exec",
  158. n.node,
  159. n.hc,
  160. //s[0],
  161. //s[1],
  162. //s[2],
  163. //s[3],
  164. //s[4],
  165. //s[5],
  166. //s[6],
  167. //s[7],
  168. //s[8],
  169. )
  170. var out bytes.Buffer
  171. cmd.Stdout = &out
  172. err := cmd.Run()
  173. if err != nil {
  174. log.Printf("failed for %v with:", n, err)
  175. return
  176. }
  177. if strings.Contains(out.String(), "ok") || strings.Contains(out.String(), "true") {
  178. color.Green("%s", n.node)
  179. } else {
  180. color.Red(fmt.Sprintf("%s: \t%q\n", n.node, out.String()))
  181. }
  182. //}(n)
  183. }
  184. // wg.Wait()
  185. }