Change findEndpoint to return errors for Scanlines (#7390)

This commit is contained in:
Harshavardhana 2019-03-25 01:01:02 -07:00 committed by Nitish Tiwari
parent 22b4fe0a51
commit 0a44e70177

View file

@ -19,6 +19,7 @@ package main
import ( import (
"bufio" "bufio"
"crypto/tls" "crypto/tls"
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
@ -45,23 +46,23 @@ func getStartTime() time.Time {
di, err := os.Stat("/proc/1") di, err := os.Stat("/proc/1")
if err != nil { if err != nil {
// Cant stat proc dir successfully, exit with error // Cant stat proc dir successfully, exit with error
log.Fatal(err.Error()) log.Fatalln(err)
} }
return di.ModTime() return di.ModTime()
} }
// Returns the ip:port of the Minio process // Returns the ip:port of the Minio process
// running in the container // running in the container
func findEndpoint() string { func findEndpoint() (string, error) {
cmd := exec.Command("netstat", "-ntlp") cmd := exec.Command("netstat", "-ntlp")
stdout, err := cmd.StdoutPipe() stdout, err := cmd.StdoutPipe()
if err != nil { if err != nil {
// error getting stdout pipe // error getting stdout pipe
log.Fatal(err.Error()) return "", err
} }
if err = cmd.Start(); err != nil { if err = cmd.Start(); err != nil {
// error starting the command // error executing the command.
log.Fatal(err.Error()) return "", err
} }
// split netstat output in rows // split netstat output in rows
scanner := bufio.NewScanner(stdout) scanner := bufio.NewScanner(stdout)
@ -85,20 +86,21 @@ func findEndpoint() string {
} }
// wait for cmd to complete before return // wait for cmd to complete before return
if err = cmd.Wait(); err != nil { if err = cmd.Wait(); err != nil {
// command failed to run return "", err
log.Fatal(err.Error())
} }
// return joint address and port // return joint address and port
return strings.Join([]string{addr, port}, ":") return strings.Join([]string{addr, port}, ":"), nil
} }
} }
if err = scanner.Err(); err != nil {
return "", err
}
if err = cmd.Wait(); err != nil { if err = cmd.Wait(); err != nil {
// command failed to run // command failed to run
log.Fatal(err.Error()) return "", err
} }
// minio process not found, exit with error // minio process not found, exit with error
os.Exit(1) return "", errors.New("no minio process found")
return ""
} }
func main() { func main() {
@ -110,16 +112,16 @@ func main() {
// Refer: https://github.com/moby/moby/pull/28938#issuecomment-301753272 // Refer: https://github.com/moby/moby/pull/28938#issuecomment-301753272
if (time.Now().Sub(startTime) / time.Second) < initGraceTime { if (time.Now().Sub(startTime) / time.Second) > initGraceTime {
os.Exit(0) endPoint, err := findEndpoint()
} else { if err != nil {
endPoint := findEndpoint() log.Fatalln(err)
}
u, err := url.Parse(fmt.Sprintf("http://%s%s", endPoint, healthPath)) u, err := url.Parse(fmt.Sprintf("http://%s%s", endPoint, healthPath))
if err != nil { if err != nil {
// Could not parse URL successfully // Could not parse URL successfully
log.Fatal(err.Error()) log.Fatalln(err)
} }
// Minio server may be using self-signed or CA certificates. To avoid // Minio server may be using self-signed or CA certificates. To avoid
// making Docker setup complicated, we skip verifying certificates here. // making Docker setup complicated, we skip verifying certificates here.
// This is because, following request tests for health status within // This is because, following request tests for health status within
@ -132,7 +134,7 @@ func main() {
resp, err := client.Get(u.String()) resp, err := client.Get(u.String())
if err != nil { if err != nil {
// GET failed exit // GET failed exit
log.Fatal(err.Error()) log.Fatalln(err)
} }
if resp.StatusCode == http.StatusOK { if resp.StatusCode == http.StatusOK {
// Drain any response. // Drain any response.
@ -145,7 +147,7 @@ func main() {
// Drain any response. // Drain any response.
xhttp.DrainBody(resp.Body) xhttp.DrainBody(resp.Body)
// GET failed exit // GET failed exit
log.Fatal(err.Error()) log.Fatalln(err)
} }
bodyString := string(bodyBytes) bodyString := string(bodyBytes)
// Drain any response. // Drain any response.
@ -157,7 +159,7 @@ func main() {
resp, err = client.Get(u.String()) resp, err = client.Get(u.String())
if err != nil { if err != nil {
// GET failed exit // GET failed exit
log.Fatal(err.Error()) log.Fatalln(err)
} }
if resp.StatusCode == http.StatusOK { if resp.StatusCode == http.StatusOK {
// Drain any response. // Drain any response.
@ -168,8 +170,9 @@ func main() {
// Drain any response. // Drain any response.
xhttp.DrainBody(resp.Body) xhttp.DrainBody(resp.Body)
} }
// Execution reaching here means none of
// the success cases were satisfied
os.Exit(1)
} }
// Execution reaching here means none of os.Exit(0)
// the success cases were satisfied
os.Exit(1)
} }