github.com/nilium/gitlab-runner@v12.5.0+incompatible/commands/helpers/health_check.go (about)

     1  package helpers
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  	"os"
     7  	"strings"
     8  	"time"
     9  
    10  	"github.com/sirupsen/logrus"
    11  	"github.com/urfave/cli"
    12  
    13  	"gitlab.com/gitlab-org/gitlab-runner/common"
    14  )
    15  
    16  type HealthCheckCommand struct{}
    17  
    18  func (c *HealthCheckCommand) Execute(ctx *cli.Context) {
    19  	var addr, port string
    20  
    21  	for _, e := range os.Environ() {
    22  		parts := strings.Split(e, "=")
    23  
    24  		if len(parts) != 2 {
    25  			continue
    26  		} else if strings.HasSuffix(parts[0], "_TCP_ADDR") {
    27  			addr = parts[1]
    28  		} else if strings.HasSuffix(parts[0], "_TCP_PORT") {
    29  			port = parts[1]
    30  		}
    31  	}
    32  
    33  	if addr == "" || port == "" {
    34  		logrus.Fatalln("No HOST or PORT found")
    35  	}
    36  
    37  	fmt.Fprintf(os.Stdout, "waiting for TCP connection to %s:%s...", addr, port)
    38  
    39  	for {
    40  		conn, err := net.Dial("tcp", net.JoinHostPort(addr, port))
    41  		if err != nil {
    42  			time.Sleep(time.Second)
    43  			continue
    44  		}
    45  
    46  		conn.Close()
    47  		return
    48  	}
    49  }
    50  
    51  func init() {
    52  	common.RegisterCommand2("health-check", "check health for a specific address", &HealthCheckCommand{})
    53  }