2015-02-26 02:15:07 +00:00
|
|
|
package checks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2016-11-17 14:25:04 +00:00
|
|
|
"fmt"
|
2015-08-20 00:57:18 +00:00
|
|
|
"net"
|
2015-02-26 02:15:07 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
2016-11-17 14:25:04 +00:00
|
|
|
"path/filepath"
|
2015-08-19 00:19:46 +00:00
|
|
|
"strconv"
|
2015-08-20 00:57:18 +00:00
|
|
|
"time"
|
2015-08-19 00:19:46 +00:00
|
|
|
|
|
|
|
"github.com/docker/distribution/health"
|
2015-02-26 02:15:07 +00:00
|
|
|
)
|
|
|
|
|
2015-08-20 00:57:18 +00:00
|
|
|
// FileChecker checks the existence of a file and returns an error
|
|
|
|
// if the file exists.
|
2015-02-26 02:15:07 +00:00
|
|
|
func FileChecker(f string) health.Checker {
|
|
|
|
return health.CheckFunc(func() error {
|
2016-11-17 14:25:04 +00:00
|
|
|
absoluteFilePath, err := filepath.Abs(f)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get absolute path for %q: %v", f, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = os.Stat(absoluteFilePath)
|
|
|
|
if err == nil {
|
2015-02-26 02:15:07 +00:00
|
|
|
return errors.New("file exists")
|
2016-11-17 14:25:04 +00:00
|
|
|
} else if os.IsNotExist(err) {
|
|
|
|
return nil
|
2015-02-26 02:15:07 +00:00
|
|
|
}
|
2016-11-17 14:25:04 +00:00
|
|
|
|
|
|
|
return err
|
2015-02-26 02:15:07 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-08-20 00:57:18 +00:00
|
|
|
// HTTPChecker does a HEAD request and verifies that the HTTP status code
|
|
|
|
// returned matches statusCode.
|
2015-08-20 01:23:58 +00:00
|
|
|
func HTTPChecker(r string, statusCode int, timeout time.Duration, headers http.Header) health.Checker {
|
2015-02-26 02:15:07 +00:00
|
|
|
return health.CheckFunc(func() error {
|
2015-08-20 00:57:18 +00:00
|
|
|
client := http.Client{
|
|
|
|
Timeout: timeout,
|
|
|
|
}
|
2015-08-20 01:23:58 +00:00
|
|
|
req, err := http.NewRequest("HEAD", r, nil)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("error creating request: " + r)
|
|
|
|
}
|
|
|
|
for headerName, headerValues := range headers {
|
|
|
|
for _, headerValue := range headerValues {
|
|
|
|
req.Header.Add(headerName, headerValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
response, err := client.Do(req)
|
2015-02-26 02:15:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.New("error while checking: " + r)
|
|
|
|
}
|
2015-08-20 00:57:18 +00:00
|
|
|
if response.StatusCode != statusCode {
|
2015-08-19 00:19:46 +00:00
|
|
|
return errors.New("downstream service returned unexpected status: " + strconv.Itoa(response.StatusCode))
|
2015-02-26 02:15:07 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2015-08-20 00:57:18 +00:00
|
|
|
|
|
|
|
// TCPChecker attempts to open a TCP connection.
|
|
|
|
func TCPChecker(addr string, timeout time.Duration) health.Checker {
|
|
|
|
return health.CheckFunc(func() error {
|
|
|
|
conn, err := net.DialTimeout("tcp", addr, timeout)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("connection to " + addr + " failed")
|
|
|
|
}
|
|
|
|
conn.Close()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|