+// If an incoming request's target vhost has an embedded collection
+// UUID or PDH, handle it with hTrue, otherwise handle it with
+// hFalse.
+//
+// Facilitates routing "http://collections.example/metrics" to metrics
+// and "http://{uuid}.collections.example/metrics" to a file in a
+// collection.
+func ifCollectionInHost(hTrue, hFalse http.Handler) http.Handler {
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if arvados.CollectionIDFromDNSName(r.Host) != "" {
+ hTrue.ServeHTTP(w, r)
+ } else {
+ hFalse.ServeHTTP(w, r)
+ }
+ })
+}
+
+func interceptHealthReqs(mgtToken string, checkHealth func() error, next http.Handler) http.Handler {
+ mux := httprouter.New()
+ mux.Handler("GET", "/_health/ping", &health.Handler{
+ Token: mgtToken,
+ Prefix: "/_health/",
+ Routes: health.Routes{"ping": checkHealth},
+ })
+ mux.NotFound = next
+ return ifCollectionInHost(next, mux)
+}
+
+func getListenAddr(svcs arvados.Services, prog arvados.ServiceName, log logrus.FieldLogger) (arvados.URL, arvados.URL, error) {
+ svc, ok := svcs.Map()[prog]
+ if !ok {
+ return arvados.URL{}, arvados.URL{}, fmt.Errorf("unknown service name %q", prog)
+ }
+
+ if want := os.Getenv("ARVADOS_SERVICE_INTERNAL_URL"); want == "" {
+ } else if url, err := url.Parse(want); err != nil {
+ return arvados.URL{}, arvados.URL{}, fmt.Errorf("$ARVADOS_SERVICE_INTERNAL_URL (%q): %s", want, err)
+ } else {
+ if url.Path == "" {
+ url.Path = "/"
+ }
+ internalURL := arvados.URL(*url)
+ listenURL := arvados.URL(*url)
+ if svc.ListenAddress != "" {
+ listenURL.Host = svc.ListenAddress
+ }
+ return listenURL, internalURL, nil
+ }
+
+ if svc.ListenAddress != "" {
+ scheme := ""
+ for internalURL := range svc.InternalURLs {
+ if internalURL.Host == svc.ListenAddress {
+ if len(svc.InternalURLs) > 1 {
+ log.Warnf("possible configuration error: multiple InternalURLs entries exist for %s but only %q will ever be used because it matches ListenAddress", prog, internalURL.String())
+ }
+ return internalURL, internalURL, nil
+ }
+ switch scheme {
+ case "":
+ scheme = internalURL.Scheme
+ case internalURL.Scheme:
+ default:
+ scheme = "-" // different InternalURLs have different schemes
+ }
+ }
+ if scheme == "-" {
+ return arvados.URL{}, arvados.URL{}, fmt.Errorf("cannot use ListenAddress %q: InternalURLs use multiple schemes and none have host %q", svc.ListenAddress, svc.ListenAddress)
+ }
+ if scheme == "" {
+ // No entries at all in InternalURLs
+ scheme = "http"
+ }
+ listenURL := arvados.URL{}
+ listenURL.Host = svc.ListenAddress
+ listenURL.Scheme = scheme
+ listenURL.Path = "/"
+ return listenURL, listenURL, nil
+ }
+
+ errors := []string{}
+ for internalURL := range svc.InternalURLs {
+ listener, err := net.Listen("tcp", internalURL.Host)
+ if err == nil {
+ listener.Close()
+ return internalURL, internalURL, nil
+ } else if strings.Contains(err.Error(), "cannot assign requested address") {
+ // If 'Host' specifies a different server than
+ // the current one, it'll resolve the hostname
+ // to IP address, and then fail because it
+ // can't bind an IP address it doesn't own.
+ continue
+ } else {
+ errors = append(errors, fmt.Sprintf("tried %v, got %v", internalURL, err))
+ }
+ }
+ if len(errors) > 0 {
+ return arvados.URL{}, arvados.URL{}, fmt.Errorf("could not enable the %q service on this host: %s", prog, strings.Join(errors, "; "))
+ }
+ return arvados.URL{}, arvados.URL{}, fmt.Errorf("configuration does not enable the %q service on this host", prog)
+}
+
+type contextKeyURL struct{}
+
+func URLFromContext(ctx context.Context) (arvados.URL, bool) {
+ u, ok := ctx.Value(contextKeyURL{}).(arvados.URL)
+ return u, ok
+}