+
+func getListenAddr(svcs arvados.Services, prog arvados.ServiceName, log logrus.FieldLogger) (arvados.URL, error) {
+ svc, ok := svcs.Map()[prog]
+ if !ok {
+ return arvados.URL{}, fmt.Errorf("unknown service name %q", prog)
+ }
+ errors := []string{}
+ for url := range svc.InternalURLs {
+ listener, err := net.Listen("tcp", url.Host)
+ if err == nil {
+ listener.Close()
+ return url, 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", url, err))
+ }
+ }
+ if len(errors) > 0 {
+ return arvados.URL{}, fmt.Errorf("could not enable the %q service on this host: %s", prog, strings.Join(errors, "; "))
+ }
+ return 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
+}