Skip to content

Commit ce5005c

Browse files
committed
Add list_nodes script
1 parent 4deced0 commit ce5005c

File tree

4 files changed

+77
-6
lines changed

4 files changed

+77
-6
lines changed

citc/list_nodes.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from pathlib import Path
2+
from typing import List, Optional
3+
4+
from tabulate import tabulate
5+
6+
from . import slurm, cloud, utils
7+
8+
9+
def create_table(
10+
slurm_nodes: List[slurm.SlurmNode], cloud_nodes: List[cloud.CloudNode]
11+
):
12+
table = []
13+
for slurm_node in slurm_nodes:
14+
matches = [node for node in cloud_nodes if node.name == slurm_node.name]
15+
if len(matches) == 1:
16+
cloud_state = matches[0].state # type: Optional[cloud.NodeState]
17+
else:
18+
cloud_state = None
19+
table.append(
20+
[
21+
slurm_node.name,
22+
slurm_node.state,
23+
slurm.NODE_STATE_FLAGS.get(slurm_node.state_flag or "", ""),
24+
cloud_state,
25+
]
26+
)
27+
28+
return table
29+
30+
31+
def main():
32+
slurm_conf = Path("/mnt/shared/etc/slurm/slurm.conf")
33+
34+
cloud_nodes = utils.get_cloud_nodes()
35+
slurm_nodes = slurm.all_nodes(slurm_conf)
36+
37+
table_data = create_table(slurm_nodes, cloud_nodes)
38+
table = tabulate(table_data, headers=["Name", "Slurm state", "Flag", "Cloud state"])
39+
print(table)
40+
41+
42+
if __name__ == "__main__":
43+
main()

citc/slurm.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ def node_list(slurm_conf: pathlib.Path) -> Iterator[str]:
2020

2121

2222
NODE_STATE_FLAGS = {
23-
"*": "not_responding",
24-
"~": "power_save",
25-
"#": "powering_up",
26-
"%": "powering_down",
27-
"$": "main_reservation",
28-
"@": "pending_reboot",
23+
"*": "not responding",
24+
"~": "power save",
25+
"#": "powering up",
26+
"%": "powering down",
27+
"$": "main reservation",
28+
"@": "pending reboot",
2929
}
3030

3131

pyproject.toml

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ python = "^3.6"
1010
boto3 = "^1.10.46"
1111
PyYaml = "^5.2"
1212
oci = "^2.8.0"
13+
tabulate = "^0.8.6"
1314

1415
[tool.poetry.dev-dependencies]
1516
pytest = "^5.3"
@@ -21,6 +22,7 @@ moto = "^1.3.14"
2122

2223
[tool.poetry.scripts]
2324
watchdog = 'citc.watchdog:main'
25+
list_nodes = 'citc.list_nodes:main'
2426

2527
[build-system]
2628
requires = ["poetry>=0.12"]

tests/test_list_nodes.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from tabulate import tabulate
2+
3+
from citc.aws import AwsNode
4+
from citc.list_nodes import create_table
5+
from citc.cloud import NodeState
6+
from citc.slurm import SlurmNode
7+
8+
9+
def test_create_table():
10+
slurm_nodes = [
11+
SlurmNode(name="foo-1", state="idle", state_flag=None, features={}),
12+
SlurmNode(name="foo-2", state="idle", state_flag="~", features={}),
13+
]
14+
cloud_nodes = [AwsNode(name="foo-1", state=NodeState.RUNNING)]
15+
table = create_table(slurm_nodes, cloud_nodes)
16+
assert table == [
17+
["foo-1", "idle", "", NodeState.RUNNING],
18+
["foo-2", "idle", "power save", None],
19+
]
20+
21+
22+
def test_print_table():
23+
slurm_nodes = [SlurmNode(name="foo-1", state="idle", state_flag=None, features={})]
24+
cloud_nodes = [AwsNode(name="foo-1", state=NodeState.RUNNING)]
25+
table_data = create_table(slurm_nodes, cloud_nodes)
26+
print(tabulate(table_data))

0 commit comments

Comments
 (0)