Skip to content

Commit 3e44e8c

Browse files
authored
Merge pull request #143 from stackhpc/fix/padded-nodenames
Fix slurm.conf inventory filtering for nodenames with padding
2 parents 52a2b2a + 9e66f37 commit 3e44e8c

File tree

6 files changed

+51
-23
lines changed

6 files changed

+51
-23
lines changed

filter_plugins/slurm_conf.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,19 @@ def hostlist_expression(hosts):
3838
E.g. with an inventory containing:
3939
4040
[compute]
41-
dev-foo-0 ansible_host=localhost
42-
dev-foo-3 ansible_host=localhost
41+
dev-foo-00 ansible_host=localhost
42+
dev-foo-3 ansible_host=localhost
4343
my-random-host
44-
dev-foo-4 ansible_host=localhost
45-
dev-foo-5 ansible_host=localhost
46-
dev-compute-0 ansible_host=localhost
47-
dev-compute-1 ansible_host=localhost
44+
dev-foo-04 ansible_host=localhost
45+
dev-foo-05 ansible_host=localhost
46+
dev-compute-000 ansible_host=localhost
47+
dev-compute-001 ansible_host=localhost
4848
4949
Then "{{ groups[compute] | hostlist_expression }}" will return:
5050
51-
["dev-foo-[0,3-5]", "dev-compute-[0-1]", "my-random-host"]
51+
['dev-foo-[00,04-05,3]', 'dev-compute-[000-001]', 'my-random-host']
52+
53+
NB: This does not guranteed to return parts in the same order as `scontrol hostlist`, but its output should return the same hosts when passed to `scontrol hostnames`.
5254
"""
5355

5456
results = {}
@@ -58,19 +60,23 @@ def hostlist_expression(hosts):
5860
if m:
5961
prefix, suffix = m.groups()
6062
r = results.setdefault(prefix, [])
61-
r.append(int(suffix))
63+
r.append(suffix)
6264
else:
6365
unmatchable.append(v)
6466
return ['{}[{}]'.format(k, _group_numbers(v)) for k, v in results.items()] + unmatchable
6567

6668
def _group_numbers(numbers):
6769
units = []
68-
prev = min(numbers)
69-
for v in sorted(numbers):
70+
ints = [int(n) for n in numbers]
71+
lengths = [len(n) for n in numbers]
72+
# sort numbers by int value and length:
73+
ints, lengths, numbers = zip(*sorted(zip(ints, lengths, numbers)))
74+
prev = min(ints)
75+
for i, v in enumerate(sorted(ints)):
7076
if v == prev + 1:
71-
units[-1].append(v)
77+
units[-1].append(numbers[i])
7278
else:
73-
units.append([v])
79+
units.append([numbers[i]])
7480
prev = v
7581
return ','.join(['{}-{}'.format(u[0], u[-1]) if len(u) > 1 else str(u[0]) for u in units])
7682

molecule/README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ test15 | 1 | N | No compute nodes.
2626

2727
# Local Installation & Running
2828

29-
Local installation on a CentOS 8 machine looks like:
29+
Local installation on a RockyLinux 8.x machine looks like:
3030

3131
sudo yum install -y gcc python3-pip python3-devel openssl-devel python3-libselinux
3232
sudo yum install -y yum-utils
@@ -35,8 +35,7 @@ Local installation on a CentOS 8 machine looks like:
3535
sudo yum install -y iptables
3636
sudo systemctl start docker
3737
sudo usermod -aG docker ${USER}
38-
# if not running as centos, also run:
39-
sudo usermod -aG docker centos
38+
sudo usermod -aG docker rocky
4039
newgrp docker
4140
docker run hello-world # test docker works without sudo
4241

molecule/test12/molecule.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ platforms:
1616
- /sys/fs/cgroup:/sys/fs/cgroup:ro
1717
networks:
1818
- name: net1
19+
docker_networks:
20+
- name: net1
21+
driver_options:
22+
com.docker.network.driver.mtu: ${DOCKER_MTU:-1500} # 1500 is docker default
1923
- name: testohpc-compute-0
2024
image: ${MOLECULE_IMAGE}
2125
pre_build_image: true

molecule/test12/verify.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
cmd: "sacct --completion --noheader --parsable2"
2424
changed_when: false
2525
register: sacct
26+
until: "sacct.stdout.strip() != ''"
27+
retries: 5
28+
delay: 1
2629
- assert:
2730
that: "(jobid + '|0|wrap|compute|2|testohpc-compute-[0-1]|COMPLETED') in sacct.stdout"
2831
fail_msg: "Didn't find expected output for {{ jobid }} in sacct output: {{ sacct.stdout }}"

tests/filter.yml

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@
55
vars:
66
grouped_0: "{{ groups['mock_group_0'] | hostlist_expression }}"
77
grouped_1: "{{ groups['mock_group_1'] | hostlist_expression }}"
8+
grouped_2: "{{ groups['mock_group_2'] | hostlist_expression }}"
89
tasks:
9-
- name: Show grouped mock-group-0
10-
debug: var=grouped_0
11-
- name: Show grouped mock-group-1
12-
debug: var=grouped_1
1310
- name: Test filter
1411
assert:
15-
that:
16-
- "['localhost-0-[0-3,5]', 'localhost-non-numerical'] == grouped_0"
17-
- "['localhost-1-[1-2,4-5,10]', 'localhost-2-[1-3]'] == grouped_1"
18-
12+
that: item.result == item.expected
13+
fail_msg: |
14+
expected: {{ item.expected }}
15+
got: {{ item.result }}
16+
loop:
17+
- result: "{{ grouped_0 }}"
18+
expected: ['localhost-0-[0-3,5]', 'localhost-non-numerical']
19+
- result: "{{ grouped_1 }}"
20+
expected: ['localhost-1-[1-2,4-5,10]', 'localhost-2-[1-3]']
21+
- result: "{{ grouped_2 }}"
22+
expected: ['localhost-[1,0001-0003,0008,0010]', 'localhost-admin']
1923
...

tests/inventory-mock-groups

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,15 @@ localhost-1-10 ansible_host=127.0.0.1 ansible_connection='local' ansible_pyt
1515
localhost-2-1 ansible_host=127.0.0.1 ansible_connection='local' ansible_python_interpreter='/usr/bin/env python'
1616
localhost-2-2 ansible_host=127.0.0.1 ansible_connection='local' ansible_python_interpreter='/usr/bin/env python'
1717
localhost-2-3 ansible_host=127.0.0.1 ansible_connection='local' ansible_python_interpreter='/usr/bin/env python'
18+
19+
[mock_group_2]
20+
# test padding:
21+
# $ scontrol show hostlist localhost-admin,localhost-0001,localhost-0002,localhost-0003,localhost-0008,localhost-0010,localhost-1
22+
# localhost-admin,localhost-[0001-0003,0008,0010,1]
23+
localhost-0001
24+
localhost-1
25+
localhost-0010
26+
localhost-admin
27+
localhost-0002
28+
localhost-0008
29+
localhost-0003

0 commit comments

Comments
 (0)