2018-03-01 07:10:04 +00:00
|
|
|
package healthcheck
|
2017-11-09 16:08:03 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
2017-11-23 15:10:04 +00:00
|
|
|
"errors"
|
2017-11-09 16:08:03 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/containous/flaeg"
|
2018-03-01 07:10:04 +00:00
|
|
|
"github.com/containous/traefik/cmd"
|
2018-11-27 16:42:04 +00:00
|
|
|
"github.com/containous/traefik/config/static"
|
2017-11-09 16:08:03 +00:00
|
|
|
)
|
|
|
|
|
2018-03-01 07:10:04 +00:00
|
|
|
// NewCmd builds a new HealthCheck command
|
|
|
|
func NewCmd(traefikConfiguration *cmd.TraefikConfiguration, traefikPointersConfiguration *cmd.TraefikConfiguration) *flaeg.Command {
|
2017-11-09 16:08:03 +00:00
|
|
|
return &flaeg.Command{
|
|
|
|
Name: "healthcheck",
|
|
|
|
Description: `Calls traefik /ping to check health (web provider must be enabled)`,
|
|
|
|
Config: traefikConfiguration,
|
|
|
|
DefaultPointersConfig: traefikPointersConfiguration,
|
2018-09-07 07:40:03 +00:00
|
|
|
Run: runCmd(traefikConfiguration),
|
2017-11-09 16:08:03 +00:00
|
|
|
Metadata: map[string]string{
|
|
|
|
"parseAllSources": "true",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-01 07:10:04 +00:00
|
|
|
func runCmd(traefikConfiguration *cmd.TraefikConfiguration) func() error {
|
2017-11-09 16:08:03 +00:00
|
|
|
return func() error {
|
2018-11-27 16:42:04 +00:00
|
|
|
traefikConfiguration.Configuration.SetEffectiveConfiguration(traefikConfiguration.ConfigFile)
|
2017-11-09 16:08:03 +00:00
|
|
|
|
2018-11-27 16:42:04 +00:00
|
|
|
resp, errPing := Do(traefikConfiguration.Configuration)
|
2017-11-09 16:08:03 +00:00
|
|
|
if errPing != nil {
|
|
|
|
fmt.Printf("Error calling healthcheck: %s\n", errPing)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
fmt.Printf("Bad healthcheck status: %s\n", resp.Status)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
fmt.Printf("OK: %s\n", resp.Request.URL)
|
|
|
|
os.Exit(0)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2017-11-23 15:10:04 +00:00
|
|
|
|
2018-03-01 07:10:04 +00:00
|
|
|
// Do try to do a healthcheck
|
2018-11-27 16:42:04 +00:00
|
|
|
func Do(staticConfiguration static.Configuration) (*http.Response, error) {
|
|
|
|
if staticConfiguration.Ping == nil {
|
2018-02-13 22:42:03 +00:00
|
|
|
return nil, errors.New("please enable `ping` to use health check")
|
|
|
|
}
|
2018-11-27 16:42:04 +00:00
|
|
|
pingEntryPoint, ok := staticConfiguration.EntryPoints[staticConfiguration.Ping.EntryPoint]
|
2017-11-23 15:10:04 +00:00
|
|
|
if !ok {
|
2018-02-13 22:42:03 +00:00
|
|
|
return nil, errors.New("missing `ping` entrypoint")
|
2017-11-23 15:10:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
client := &http.Client{Timeout: 5 * time.Second}
|
|
|
|
protocol := "http"
|
2018-11-27 16:42:04 +00:00
|
|
|
|
2017-11-23 15:10:04 +00:00
|
|
|
if pingEntryPoint.TLS != nil {
|
|
|
|
protocol = "https"
|
|
|
|
tr := &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
}
|
|
|
|
client.Transport = tr
|
|
|
|
}
|
2018-11-27 16:42:04 +00:00
|
|
|
|
2017-12-06 10:20:03 +00:00
|
|
|
path := "/"
|
2018-07-31 17:28:03 +00:00
|
|
|
|
2017-12-06 10:20:03 +00:00
|
|
|
return client.Head(protocol + "://" + pingEntryPoint.Address + path + "ping")
|
2017-11-23 15:10:04 +00:00
|
|
|
}
|