2019-02-20 11:12:03 -05:00
|
|
|
/*
|
2019-04-09 14:39:42 -04:00
|
|
|
* MinIO Cloud Storage, (C) 2019 MinIO, Inc.
|
2019-02-20 11:12:03 -05:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"crypto/tls"
|
2019-03-25 04:01:02 -04:00
|
|
|
"errors"
|
2019-02-20 11:12:03 -05:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
xhttp "github.com/minio/minio/cmd/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
initGraceTime = 300
|
|
|
|
healthPath = "/minio/health/live"
|
|
|
|
timeout = time.Duration(30 * time.Second)
|
|
|
|
minioProcess = "minio"
|
|
|
|
)
|
|
|
|
|
|
|
|
// returns container boot time by finding
|
|
|
|
// modtime of /proc/1 directory
|
|
|
|
func getStartTime() time.Time {
|
|
|
|
di, err := os.Stat("/proc/1")
|
|
|
|
if err != nil {
|
|
|
|
// Cant stat proc dir successfully, exit with error
|
2019-03-25 04:01:02 -04:00
|
|
|
log.Fatalln(err)
|
2019-02-20 11:12:03 -05:00
|
|
|
}
|
|
|
|
return di.ModTime()
|
|
|
|
}
|
|
|
|
|
2019-04-09 14:39:42 -04:00
|
|
|
// Returns the ip:port of the MinIO process
|
2019-02-20 11:12:03 -05:00
|
|
|
// running in the container
|
2019-03-25 04:01:02 -04:00
|
|
|
func findEndpoint() (string, error) {
|
2019-02-20 11:12:03 -05:00
|
|
|
cmd := exec.Command("netstat", "-ntlp")
|
|
|
|
stdout, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
// error getting stdout pipe
|
2019-03-25 04:01:02 -04:00
|
|
|
return "", err
|
2019-02-20 11:12:03 -05:00
|
|
|
}
|
|
|
|
if err = cmd.Start(); err != nil {
|
2019-03-25 04:01:02 -04:00
|
|
|
// error executing the command.
|
|
|
|
return "", err
|
2019-02-20 11:12:03 -05:00
|
|
|
}
|
|
|
|
// split netstat output in rows
|
|
|
|
scanner := bufio.NewScanner(stdout)
|
|
|
|
scanner.Split(bufio.ScanLines)
|
2019-04-09 14:39:42 -04:00
|
|
|
// loop over the rows to find MinIO process
|
2019-02-20 11:12:03 -05:00
|
|
|
for scanner.Scan() {
|
|
|
|
if strings.Contains(scanner.Text(), minioProcess) {
|
|
|
|
line := scanner.Text()
|
|
|
|
newLine := strings.Replace(line, ":::", "127.0.0.1:", 1)
|
|
|
|
fields := strings.Fields(newLine)
|
|
|
|
// index 3 in the row has the Local address
|
|
|
|
// find the last index of ":" - address will
|
|
|
|
// have port number after this index
|
|
|
|
i := strings.LastIndex(fields[3], ":")
|
|
|
|
// split address and port
|
|
|
|
addr := fields[3][:i]
|
|
|
|
port := fields[3][i+1:]
|
|
|
|
// add surrounding [] for ip6 address
|
|
|
|
if strings.Count(addr, ":") > 0 {
|
|
|
|
addr = strings.Join([]string{"[", addr, "]"}, "")
|
|
|
|
}
|
2019-02-23 12:32:53 -05:00
|
|
|
// wait for cmd to complete before return
|
|
|
|
if err = cmd.Wait(); err != nil {
|
2019-03-25 04:01:02 -04:00
|
|
|
return "", err
|
2019-02-23 12:32:53 -05:00
|
|
|
}
|
2019-02-20 11:12:03 -05:00
|
|
|
// return joint address and port
|
2019-03-25 04:01:02 -04:00
|
|
|
return strings.Join([]string{addr, port}, ":"), nil
|
2019-02-20 11:12:03 -05:00
|
|
|
}
|
|
|
|
}
|
2019-03-25 04:01:02 -04:00
|
|
|
if err = scanner.Err(); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2019-02-20 11:12:03 -05:00
|
|
|
if err = cmd.Wait(); err != nil {
|
|
|
|
// command failed to run
|
2019-03-25 04:01:02 -04:00
|
|
|
return "", err
|
2019-02-20 11:12:03 -05:00
|
|
|
}
|
|
|
|
// minio process not found, exit with error
|
2019-03-25 04:01:02 -04:00
|
|
|
return "", errors.New("no minio process found")
|
2019-02-20 11:12:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
startTime := getStartTime()
|
|
|
|
|
|
|
|
// In distributed environment like Swarm, traffic is routed
|
|
|
|
// to a container only when it reports a `healthy` status. So, we exit
|
2019-04-09 14:39:42 -04:00
|
|
|
// with 0 to ensure healthy status till distributed MinIO starts (120s).
|
2019-02-20 11:12:03 -05:00
|
|
|
|
|
|
|
// Refer: https://github.com/moby/moby/pull/28938#issuecomment-301753272
|
|
|
|
|
2019-03-25 04:01:02 -04:00
|
|
|
if (time.Now().Sub(startTime) / time.Second) > initGraceTime {
|
|
|
|
endPoint, err := findEndpoint()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
2019-02-20 11:12:03 -05:00
|
|
|
u, err := url.Parse(fmt.Sprintf("http://%s%s", endPoint, healthPath))
|
|
|
|
if err != nil {
|
|
|
|
// Could not parse URL successfully
|
2019-03-25 04:01:02 -04:00
|
|
|
log.Fatalln(err)
|
2019-02-20 11:12:03 -05:00
|
|
|
}
|
2019-04-09 14:39:42 -04:00
|
|
|
// MinIO server may be using self-signed or CA certificates. To avoid
|
2019-02-20 11:12:03 -05:00
|
|
|
// making Docker setup complicated, we skip verifying certificates here.
|
|
|
|
// This is because, following request tests for health status within
|
2019-04-09 14:39:42 -04:00
|
|
|
// containerized environment, i.e. requests are always made to the MinIO
|
2019-02-20 11:12:03 -05:00
|
|
|
// server running on the same host.
|
|
|
|
tr := &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
}
|
|
|
|
client := &http.Client{Transport: tr, Timeout: timeout}
|
|
|
|
resp, err := client.Get(u.String())
|
|
|
|
if err != nil {
|
|
|
|
// GET failed exit
|
2019-03-25 04:01:02 -04:00
|
|
|
log.Fatalln(err)
|
2019-02-20 11:12:03 -05:00
|
|
|
}
|
|
|
|
if resp.StatusCode == http.StatusOK {
|
|
|
|
// Drain any response.
|
|
|
|
xhttp.DrainBody(resp.Body)
|
|
|
|
// exit with success
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
// Drain any response.
|
|
|
|
xhttp.DrainBody(resp.Body)
|
2019-04-13 01:29:12 -04:00
|
|
|
// 400 response may mean sever is configured with https
|
|
|
|
if resp.StatusCode == http.StatusBadRequest {
|
2019-02-20 11:12:03 -05:00
|
|
|
// Try with https
|
|
|
|
u.Scheme = "https"
|
|
|
|
resp, err = client.Get(u.String())
|
|
|
|
if err != nil {
|
|
|
|
// GET failed exit
|
2019-03-25 04:01:02 -04:00
|
|
|
log.Fatalln(err)
|
2019-02-20 11:12:03 -05:00
|
|
|
}
|
|
|
|
if resp.StatusCode == http.StatusOK {
|
|
|
|
// Drain any response.
|
|
|
|
xhttp.DrainBody(resp.Body)
|
|
|
|
// exit with success
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
// Drain any response.
|
|
|
|
xhttp.DrainBody(resp.Body)
|
|
|
|
}
|
2019-03-25 04:01:02 -04:00
|
|
|
// Execution reaching here means none of
|
|
|
|
// the success cases were satisfied
|
|
|
|
os.Exit(1)
|
2019-02-20 11:12:03 -05:00
|
|
|
}
|
2019-03-25 04:01:02 -04:00
|
|
|
os.Exit(0)
|
2019-02-20 11:12:03 -05:00
|
|
|
}
|