Refactor the multi-host salt install page.
[arvados.git] / services / crunch-dispatch-slurm / node_type.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package dispatchslurm
6
7 import (
8         "log"
9         "os/exec"
10         "strings"
11         "time"
12
13         "git.arvados.org/arvados.git/sdk/go/arvados"
14 )
15
16 // SlurmNodeTypeFeatureKludge ensures SLURM accepts every instance
17 // type name as a valid feature name, even if no instances of that
18 // type have appeared yet.
19 //
20 // It takes advantage of some SLURM peculiarities:
21 //
22 // (1) A feature is valid after it has been offered by a node, even if
23 // it is no longer offered by any node. So, to make a feature name
24 // valid, we can add it to a dummy node ("compute0"), then remove it.
25 //
26 // (2) To test whether a set of feature names are valid without
27 // actually submitting a job, we can call srun --test-only with the
28 // desired features.
29 //
30 // SlurmNodeTypeFeatureKludge does a test-and-fix operation
31 // immediately, and then periodically, in case slurm restarts and
32 // forgets the list of valid features. It never returns (unless there
33 // are no node types configured, in which case it returns
34 // immediately), so it should generally be invoked with "go".
35 func SlurmNodeTypeFeatureKludge(cc *arvados.Cluster) {
36         if len(cc.InstanceTypes) == 0 {
37                 return
38         }
39         var features []string
40         for _, it := range cc.InstanceTypes {
41                 features = append(features, "instancetype="+it.Name)
42         }
43         for {
44                 slurmKludge(features)
45                 time.Sleep(2 * time.Second)
46         }
47 }
48
49 const slurmDummyNode = "compute0"
50
51 func slurmKludge(features []string) {
52         allFeatures := strings.Join(features, ",")
53
54         cmd := exec.Command("sinfo", "--nodes="+slurmDummyNode, "--format=%f", "--noheader")
55         out, err := cmd.CombinedOutput()
56         if err != nil {
57                 log.Printf("running %q %q: %s (output was %q)", cmd.Path, cmd.Args, err, out)
58                 return
59         }
60         if string(out) == allFeatures+"\n" {
61                 // Already configured correctly, nothing to do.
62                 return
63         }
64
65         log.Printf("configuring node %q with all node type features", slurmDummyNode)
66         cmd = exec.Command("scontrol", "update", "NodeName="+slurmDummyNode, "Features="+allFeatures)
67         log.Printf("running: %q %q", cmd.Path, cmd.Args)
68         out, err = cmd.CombinedOutput()
69         if err != nil {
70                 log.Printf("error: scontrol: %s (output was %q)", err, out)
71         }
72 }