Merge branch '14383-java-sdk-double-slash'. Fixes #14383.
[arvados.git] / lib / dispatchcloud / dispatcher.go
index 97aacf6044fd8af5f97659b19d41fc7c91561994..adf1028b35fe16ab13afbfcb4f0c91672ec17849 100644 (file)
@@ -5,6 +5,7 @@
 package dispatchcloud
 
 import (
+       "context"
        "crypto/md5"
        "encoding/json"
        "fmt"
@@ -20,10 +21,12 @@ import (
        "git.curoverse.com/arvados.git/lib/dispatchcloud/worker"
        "git.curoverse.com/arvados.git/sdk/go/arvados"
        "git.curoverse.com/arvados.git/sdk/go/auth"
+       "git.curoverse.com/arvados.git/sdk/go/ctxlog"
        "git.curoverse.com/arvados.git/sdk/go/httpserver"
-       "github.com/Sirupsen/logrus"
+       "github.com/julienschmidt/httprouter"
        "github.com/prometheus/client_golang/prometheus"
        "github.com/prometheus/client_golang/prometheus/promhttp"
+       "github.com/sirupsen/logrus"
        "golang.org/x/crypto/ssh"
 )
 
@@ -35,10 +38,13 @@ const (
 type pool interface {
        scheduler.WorkerPool
        Instances() []worker.InstanceView
+       SetIdleBehavior(cloud.InstanceID, worker.IdleBehavior) error
+       Stop()
 }
 
 type dispatcher struct {
        Cluster       *arvados.Cluster
+       Context       context.Context
        InstanceSetID cloud.InstanceSetID
 
        logger      logrus.FieldLogger
@@ -51,6 +57,7 @@ type dispatcher struct {
 
        setupOnce sync.Once
        stop      chan struct{}
+       stopped   chan struct{}
 }
 
 // Start starts the dispatcher. Start can be called multiple times
@@ -79,11 +86,13 @@ func (disp *dispatcher) Close() {
        case disp.stop <- struct{}{}:
        default:
        }
+       <-disp.stopped
 }
 
 // Make a worker.Executor for the given instance.
 func (disp *dispatcher) newExecutor(inst cloud.Instance) worker.Executor {
        exr := ssh_executor.New(inst)
+       exr.SetTargetPort(disp.Cluster.CloudVMs.SSHPort)
        exr.SetSigners(disp.sshKey)
        return exr
 }
@@ -109,36 +118,48 @@ func (disp *dispatcher) initialize() {
                }
        }
        disp.stop = make(chan struct{}, 1)
-       disp.logger = logrus.StandardLogger()
+       disp.stopped = make(chan struct{})
+       disp.logger = ctxlog.FromContext(disp.Context)
 
-       if key, err := ssh.ParsePrivateKey(disp.Cluster.Dispatch.PrivateKey); err != nil {
+       if key, err := ssh.ParsePrivateKey([]byte(disp.Cluster.Dispatch.PrivateKey)); err != nil {
                disp.logger.Fatalf("error parsing configured Dispatch.PrivateKey: %s", err)
        } else {
                disp.sshKey = key
        }
 
-       instanceSet, err := newInstanceSet(disp.Cluster, disp.InstanceSetID)
+       instanceSet, err := newInstanceSet(disp.Cluster, disp.InstanceSetID, disp.logger)
        if err != nil {
                disp.logger.Fatalf("error initializing driver: %s", err)
        }
-       disp.instanceSet = &instanceSetProxy{instanceSet}
+       disp.instanceSet = instanceSet
        disp.reg = prometheus.NewRegistry()
-       disp.pool = worker.NewPool(disp.logger, disp.reg, disp.instanceSet, disp.newExecutor, disp.Cluster)
+       disp.pool = worker.NewPool(disp.logger, arvClient, disp.reg, disp.instanceSet, disp.newExecutor, disp.sshKey.PublicKey(), disp.Cluster)
        disp.queue = container.NewQueue(disp.logger, disp.reg, disp.typeChooser, arvClient)
 
-       mux := http.NewServeMux()
-       mux.HandleFunc("/arvados/v1/dispatch/containers", disp.apiContainers)
-       mux.HandleFunc("/arvados/v1/dispatch/instances", disp.apiInstances)
-       metricsH := promhttp.HandlerFor(disp.reg, promhttp.HandlerOpts{
-               ErrorLog: disp.logger,
-       })
-       mux.Handle("/metrics", metricsH)
-       mux.Handle("/metrics.json", metricsH)
-       disp.httpHandler = auth.RequireLiteralToken(disp.Cluster.ManagementToken, mux)
+       if disp.Cluster.ManagementToken == "" {
+               disp.httpHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+                       http.Error(w, "Management API authentication is not configured", http.StatusForbidden)
+               })
+       } else {
+               mux := httprouter.New()
+               mux.HandlerFunc("GET", "/arvados/v1/dispatch/containers", disp.apiContainers)
+               mux.HandlerFunc("GET", "/arvados/v1/dispatch/instances", disp.apiInstances)
+               mux.HandlerFunc("POST", "/arvados/v1/dispatch/instances/hold", disp.apiInstanceHold)
+               mux.HandlerFunc("POST", "/arvados/v1/dispatch/instances/drain", disp.apiInstanceDrain)
+               mux.HandlerFunc("POST", "/arvados/v1/dispatch/instances/run", disp.apiInstanceRun)
+               metricsH := promhttp.HandlerFor(disp.reg, promhttp.HandlerOpts{
+                       ErrorLog: disp.logger,
+               })
+               mux.Handler("GET", "/metrics", metricsH)
+               mux.Handler("GET", "/metrics.json", metricsH)
+               disp.httpHandler = auth.RequireLiteralToken(disp.Cluster.ManagementToken, mux)
+       }
 }
 
 func (disp *dispatcher) run() {
+       defer close(disp.stopped)
        defer disp.instanceSet.Stop()
+       defer disp.pool.Stop()
 
        staleLockTimeout := time.Duration(disp.Cluster.Dispatch.StaleLockTimeout)
        if staleLockTimeout == 0 {
@@ -148,7 +169,7 @@ func (disp *dispatcher) run() {
        if pollInterval <= 0 {
                pollInterval = defaultPollInterval
        }
-       sched := scheduler.New(disp.logger, disp.queue, disp.pool, staleLockTimeout, pollInterval)
+       sched := scheduler.New(disp.Context, disp.queue, disp.pool, staleLockTimeout, pollInterval)
        sched.Start()
        defer sched.Stop()
 
@@ -157,12 +178,8 @@ func (disp *dispatcher) run() {
 
 // Management API: all active and queued containers.
 func (disp *dispatcher) apiContainers(w http.ResponseWriter, r *http.Request) {
-       if r.Method != "GET" {
-               httpserver.Error(w, "method not allowed", http.StatusMethodNotAllowed)
-               return
-       }
        var resp struct {
-               Items []container.QueueEnt
+               Items []container.QueueEnt `json:"items"`
        }
        qEntries, _ := disp.queue.Entries()
        for _, ent := range qEntries {
@@ -173,13 +190,37 @@ func (disp *dispatcher) apiContainers(w http.ResponseWriter, r *http.Request) {
 
 // Management API: all active instances (cloud VMs).
 func (disp *dispatcher) apiInstances(w http.ResponseWriter, r *http.Request) {
-       if r.Method != "GET" {
-               httpserver.Error(w, "method not allowed", http.StatusMethodNotAllowed)
-               return
-       }
        var resp struct {
-               Items []worker.InstanceView
+               Items []worker.InstanceView `json:"items"`
        }
        resp.Items = disp.pool.Instances()
        json.NewEncoder(w).Encode(resp)
 }
+
+// Management API: set idle behavior to "hold" for specified instance.
+func (disp *dispatcher) apiInstanceHold(w http.ResponseWriter, r *http.Request) {
+       disp.apiInstanceIdleBehavior(w, r, worker.IdleBehaviorHold)
+}
+
+// Management API: set idle behavior to "drain" for specified instance.
+func (disp *dispatcher) apiInstanceDrain(w http.ResponseWriter, r *http.Request) {
+       disp.apiInstanceIdleBehavior(w, r, worker.IdleBehaviorDrain)
+}
+
+// Management API: set idle behavior to "run" for specified instance.
+func (disp *dispatcher) apiInstanceRun(w http.ResponseWriter, r *http.Request) {
+       disp.apiInstanceIdleBehavior(w, r, worker.IdleBehaviorRun)
+}
+
+func (disp *dispatcher) apiInstanceIdleBehavior(w http.ResponseWriter, r *http.Request, want worker.IdleBehavior) {
+       id := cloud.InstanceID(r.FormValue("instance_id"))
+       if id == "" {
+               httpserver.Error(w, "instance_id parameter not provided", http.StatusBadRequest)
+               return
+       }
+       err := disp.pool.SetIdleBehavior(id, want)
+       if err != nil {
+               httpserver.Error(w, err.Error(), http.StatusNotFound)
+               return
+       }
+}