github.com/Cloud-Foundations/Dominator@v0.3.4/cmd/subd/tests.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "net/http" 7 "os" 8 "strings" 9 10 "github.com/Cloud-Foundations/Dominator/lib/constants" 11 ) 12 13 func checkExternallyPatchable() (bool, error) { 14 resp, err := http.Get(constants.MetadataUrl + 15 constants.MetadataExternallyPatchable) 16 if err != nil { 17 return false, err 18 } 19 defer resp.Body.Close() 20 if resp.StatusCode != 200 { 21 return false, nil 22 } 23 if body, err := ioutil.ReadAll(resp.Body); err != nil { 24 return false, err 25 } else { 26 value := strings.TrimSpace(string(body)) 27 if value == "true" { 28 return true, nil 29 } else { 30 return false, nil 31 } 32 } 33 } 34 35 func runTestAndExit(test func() (bool, error)) { 36 if patchable, err := checkExternallyPatchable(); err != nil { 37 fmt.Fprintln(os.Stderr, err) 38 os.Exit(1) 39 } else if patchable { 40 os.Exit(0) 41 } 42 os.Exit(1) 43 }