| 1 |
.. _guide-NodeSet: |
| 2 |
|
| 3 |
Node sets handling |
| 4 |
================== |
| 5 |
|
| 6 |
.. highlight:: python |
| 7 |
|
| 8 |
.. _class-NodeSet: |
| 9 |
|
| 10 |
NodeSet class |
| 11 |
------------- |
| 12 |
|
| 13 |
:class:`.NodeSet` is a class to represent an ordered set of node names |
| 14 |
(optionally indexed). It's a convenient way to deal with cluster nodes and |
| 15 |
ease their administration. :class:`.NodeSet` is implemented with the help of |
| 16 |
two other ClusterShell public classes, :class:`.RangeSet` and |
| 17 |
:class:`.RangeSetND`, which implement methods to manage a set of numeric |
| 18 |
ranges in one or multiple dimensions. :class:`.NodeSet`, :class:`.RangeSet` |
| 19 |
and :class:`.RangeSetND` APIs match standard Python sets. A command-line |
| 20 |
interface (:ref:`nodeset-tool`) which implements most of :class:`.NodeSet` |
| 21 |
features, is also available. |
| 22 |
|
| 23 |
Other classes of the ClusterShell library make use of the :class:`.NodeSet` |
| 24 |
class when they come to deal with distant nodes. |
| 25 |
|
| 26 |
Using NodeSet |
| 27 |
^^^^^^^^^^^^^ |
| 28 |
|
| 29 |
If you are used to `Python sets`_, :class:`.NodeSet` interface will be easy |
| 30 |
for you to learn. The main conceptual difference is that :class:`.NodeSet` |
| 31 |
iterators always provide ordered results (and also |
| 32 |
:meth:`.NodeSet.__getitem__()` by index or slice is allowed). Furthermore, |
| 33 |
:class:`.NodeSet` provides specific methods like |
| 34 |
:meth:`.NodeSet.split()`, :meth:`.NodeSet.contiguous()` (see below), or |
| 35 |
:meth:`.NodeSet.groups()`, :meth:`.NodeSet.regroup()` (these last two are |
| 36 |
related to :ref:`class-NodeSet-groups`). The following code snippet shows you |
| 37 |
a basic usage of the :class:`.NodeSet` class:: |
| 38 |
|
| 39 |
>>> from ClusterShell.NodeSet import NodeSet |
| 40 |
>>> nodeset = NodeSet() |
| 41 |
>>> nodeset.add("node7") |
| 42 |
>>> nodeset.add("node6") |
| 43 |
>>> print(nodeset) |
| 44 |
node[6-7] |
| 45 |
|
| 46 |
:class:`.NodeSet` class provides several object constructors:: |
| 47 |
|
| 48 |
>>> print(NodeSet("node[1-5]")) |
| 49 |
node[1-5] |
| 50 |
>>> print(NodeSet.fromlist(["node1", "node2", "node3"])) |
| 51 |
node[1-3] |
| 52 |
>>> print(NodeSet.fromlist(["node[1-5]", "node[6-10]"])) |
| 53 |
node[1-10] |
| 54 |
>>> print(NodeSet.fromlist(["clu-1-[1-4]", "clu-2-[1-4]"])) |
| 55 |
clu-[1-2]-[1-4] |
| 56 |
|
| 57 |
All corresponding Python sets operations are available, for example:: |
| 58 |
|
| 59 |
>>> from ClusterShell.NodeSet import NodeSet |
| 60 |
>>> ns1 = NodeSet("node[10-42]") |
| 61 |
>>> ns2 = NodeSet("node[11-16,18-39]") |
| 62 |
>>> print(ns1.difference(ns2)) |
| 63 |
node[10,17,40-42] |
| 64 |
>>> print(ns1 - ns2) |
| 65 |
node[10,17,40-42] |
| 66 |
>>> ns3 = NodeSet("node[1-14,40-200]") |
| 67 |
>>> print(ns3.intersection(ns1)) |
| 68 |
node[10-14,40-42] |
| 69 |
|
| 70 |
|
| 71 |
Unlike Python sets, it is important to notice that :class:`.NodeSet` is |
| 72 |
somewhat not so strict about the type of element used for set operations. Thus |
| 73 |
when a string object is encountered, it is automatically converted to a |
| 74 |
NodeSet object for convenience. The following example shows an example of |
| 75 |
this (set operation is working with either a native nodeset or a string):: |
| 76 |
|
| 77 |
>>> nodeset = NodeSet("node[1-10]") |
| 78 |
>>> nodeset2 = NodeSet("node7") |
| 79 |
>>> nodeset.difference_update(nodeset2) |
| 80 |
>>> print(nodeset) |
| 81 |
node[1-6,8-10] |
| 82 |
>>> |
| 83 |
>>> nodeset.difference_update("node8") |
| 84 |
>>> print(nodeset) |
| 85 |
node[1-6,9-10] |
| 86 |
|
| 87 |
NodeSet ordered content leads to the following being allowed:: |
| 88 |
|
| 89 |
>>> nodeset = NodeSet("node[10-49]") |
| 90 |
>>> print(nodeset[0]) |
| 91 |
node10 |
| 92 |
>>> print(nodeset[-1]) |
| 93 |
node49 |
| 94 |
>>> print(nodeset[10:]) |
| 95 |
node[20-49] |
| 96 |
>>> print(nodeset[:5]) |
| 97 |
node[10-14] |
| 98 |
>>> print(nodeset[::4]) |
| 99 |
node[10,14,18,22,26,30,34,38,42,46] |
| 100 |
|
| 101 |
And it works for node names without index, for example:: |
| 102 |
|
| 103 |
>>> nodeset = NodeSet("lima,oscar,zulu,alpha,delta,foxtrot,tango,x-ray") |
| 104 |
>>> print(nodeset) |
| 105 |
alpha,delta,foxtrot,lima,oscar,tango,x-ray,zulu |
| 106 |
>>> print(nodeset[0]) |
| 107 |
alpha |
| 108 |
>>> print(nodeset[-2]) |
| 109 |
x-ray |
| 110 |
|
| 111 |
And also for multidimensional node sets:: |
| 112 |
|
| 113 |
>>> nodeset = NodeSet("clu1-[1-10]-ib[0-1],clu2-[1-10]-ib[0-1]") |
| 114 |
>>> print(nodeset) |
| 115 |
clu[1-2]-[1-10]-ib[0-1] |
| 116 |
>>> print(nodeset[0]) |
| 117 |
clu1-1-ib0 |
| 118 |
>>> print(nodeset[-1]) |
| 119 |
clu2-10-ib1 |
| 120 |
>>> print(nodeset[::2]) |
| 121 |
clu[1-2]-[1-10]-ib0 |
| 122 |
|
| 123 |
Conversely, to find the zero-based position of a node in the set, use the |
| 124 |
:meth:`.NodeSet.index()` method (the inverse of ``__getitem__``), for |
| 125 |
example:: |
| 126 |
|
| 127 |
>>> print(NodeSet("node[10-49]").index("node20")) |
| 128 |
10 |
| 129 |
|
| 130 |
.. _class-NodeSet-split: |
| 131 |
|
| 132 |
To split a NodeSet object into *n* subsets, use the :meth:`.NodeSet.split()` |
| 133 |
method, for example:: |
| 134 |
|
| 135 |
>>> for nodeset in NodeSet("node[10-49]").split(2): |
| 136 |
... print(nodeset) |
| 137 |
... |
| 138 |
node[10-29] |
| 139 |
node[30-49] |
| 140 |
|
| 141 |
.. _class-NodeSet-contiguous: |
| 142 |
|
| 143 |
To split a NodeSet object into contiguous subsets, use the |
| 144 |
:meth:`.NodeSet.contiguous()` method, for example:: |
| 145 |
|
| 146 |
>>> for nodeset in NodeSet("node[10-49,51-53,60-64]").contiguous(): |
| 147 |
... print(nodeset) |
| 148 |
... |
| 149 |
node[10-49] |
| 150 |
node[51-53] |
| 151 |
node[60-64] |
| 152 |
|
| 153 |
For further details, please see the full :doc:`NodeSet API documentation |
| 154 |
</api/NodeSet>`. |
| 155 |
|
| 156 |
|
| 157 |
.. _class-NodeSet-nD: |
| 158 |
|
| 159 |
Multidimensional considerations |
| 160 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 161 |
|
| 162 |
Version 1.7 introduces full support of multidimensional NodeSet (e.g. |
| 163 |
*da[2-5]c[1-2]p[0-1]*). The :class:`.NodeSet` interface is the same, |
| 164 |
multidimensional patterns are automatically detected by the parser and |
| 165 |
processed internally. While expanding a multidimensional NodeSet is easily |
| 166 |
solved by performing a Cartesian product of all dimensions, folding nodes is |
| 167 |
much more complex and time consuming. To reduce the performance impact of such |
| 168 |
feature, the :class:`.NodeSet` class still relies on :class:`.RangeSet` when |
| 169 |
only one dimension is varying (see :ref:`class-RangeSet`). Otherwise, it uses |
| 170 |
a new class named :class:`.RangeSetND` for full multidimensional support (see |
| 171 |
:ref:`class-RangeSetND`). |
| 172 |
|
| 173 |
.. _class-NodeSet-extended-patterns: |
| 174 |
|
| 175 |
Extended String Pattern |
| 176 |
^^^^^^^^^^^^^^^^^^^^^^^ |
| 177 |
|
| 178 |
:class:`.NodeSet` class parsing engine recognizes an *extended string |
| 179 |
pattern*, adding support for union (with special character *","*), difference |
| 180 |
(with special character *"!"*), intersection (with special character *"&"*) |
| 181 |
and symmetric difference (with special character *"^"*) operations. String |
| 182 |
patterns are read from left to right, by processing any character operators |
| 183 |
accordingly. The following example shows how you can use this feature:: |
| 184 |
|
| 185 |
>>> print(NodeSet("node[10-42],node46!node10")) |
| 186 |
node[11-42,46] |
| 187 |
|
| 188 |
|
| 189 |
.. _class-NodeSet-groups: |
| 190 |
|
| 191 |
Node groups |
| 192 |
----------- |
| 193 |
|
| 194 |
Node groups are very useful and are needed to group similar cluster nodes in |
| 195 |
terms of configuration, installed software, available resources, etc. A node |
| 196 |
can be a member of more than one node group. |
| 197 |
|
| 198 |
Using node groups |
| 199 |
^^^^^^^^^^^^^^^^^ |
| 200 |
|
| 201 |
Node groups are prefixed with **@** character. Please see |
| 202 |
:ref:`nodeset-groupsexpr` for more details about node group expression/syntax |
| 203 |
rules. |
| 204 |
|
| 205 |
Please also have a look at :ref:`Node groups configuration <groups-config>` to |
| 206 |
learn how to configure external node group bindings (sources). Once setup |
| 207 |
(please use the :ref:`nodeset-tool` command to check your configuration), the |
| 208 |
NodeSet parsing engine automatically resolves node groups. For example:: |
| 209 |
|
| 210 |
>>> print(NodeSet("@oss")) |
| 211 |
example[4-5] |
| 212 |
>>> print(NodeSet("@compute")) |
| 213 |
example[32-159] |
| 214 |
>>> print(NodeSet("@compute,@oss")) |
| 215 |
example[4-5,32-159] |
| 216 |
|
| 217 |
That is, all NodeSet-based applications share the same system-wide node group |
| 218 |
configuration (unless explicitly disabled --- see |
| 219 |
:ref:`class-NodeSet-disable-group`). |
| 220 |
|
| 221 |
When the **all** group upcall is configured (:ref:`node groups configuration |
| 222 |
<groups-config>`), you can also use the following :class:`.NodeSet` |
| 223 |
constructor:: |
| 224 |
|
| 225 |
>>> print(NodeSet.fromall()) |
| 226 |
example[4-6,32-159] |
| 227 |
|
| 228 |
When group upcalls are not properly configured, this constructor will raise a |
| 229 |
*NodeSetExternalError* exception. |
| 230 |
|
| 231 |
.. _class-NodeSet-groups-finding: |
| 232 |
|
| 233 |
Finding node groups |
| 234 |
^^^^^^^^^^^^^^^^^^^ |
| 235 |
|
| 236 |
In order to find node groups a specified node set belongs to, you can use the |
| 237 |
:meth:`.NodeSet.groups()` method. This method is used by the ``nodeset -l |
| 238 |
<nodeset>`` or ``cluset -l <nodeset>`` command (see |
| 239 |
:ref:`nodeset-group-finding`). It returns a Python |
| 240 |
dictionary where keys are groups found and values, provided for convenience, |
| 241 |
are tuples of the form *(group_nodeset, contained_nodeset)*. For example:: |
| 242 |
|
| 243 |
>>> for group, (group_nodes, contained_nodes) in NodeSet("@oss").groups().items(): |
| 244 |
... print(group, group_nodes, contained_nodes) |
| 245 |
... |
| 246 |
@all example[4-6,32-159] example[4-5] |
| 247 |
@oss example[4-5] example[4-5] |
| 248 |
|
| 249 |
|
| 250 |
More usage examples follow:: |
| 251 |
|
| 252 |
>>> list(NodeSet("example4").groups().keys()) |
| 253 |
['@all', '@oss'] |
| 254 |
>>> list(NodeSet("@mds").groups().keys()) |
| 255 |
['@all', '@mds'] |
| 256 |
>>> list(NodeSet("dummy0").groups().keys()) |
| 257 |
[] |
| 258 |
|
| 259 |
.. _class-NodeSet-regroup: |
| 260 |
|
| 261 |
Regrouping node sets |
| 262 |
^^^^^^^^^^^^^^^^^^^^ |
| 263 |
|
| 264 |
If needed group configuration conditions are met (cf. :ref:`node groups |
| 265 |
configuration <groups-config>`), you can use the :meth:`.NodeSet.regroup()` |
| 266 |
method to reduce node sets using matching groups, whenever possible:: |
| 267 |
|
| 268 |
>>> print(NodeSet("example[4-6]").regroup()) |
| 269 |
@mds,@oss |
| 270 |
|
| 271 |
The ``nodeset`` and ``cluset`` commands make use of the |
| 272 |
:meth:`.NodeSet.regroup()` method when using the *-r* switch (see |
| 273 |
:ref:`nodeset-regroup`). |
| 274 |
|
| 275 |
|
| 276 |
.. _class-NodeSet-groups-override: |
| 277 |
|
| 278 |
Overriding default groups configuration |
| 279 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 280 |
|
| 281 |
It is possible to override the library default groups configuration by |
| 282 |
changing the default :class:`.NodeSet` *resolver* object. Usually, this is |
| 283 |
done for testing or special purposes. Here is an example of how to override |
| 284 |
the *resolver* object using :func:`.NodeSet.set_std_group_resolver()` in order |
| 285 |
to use another configuration file:: |
| 286 |
|
| 287 |
>>> from ClusterShell.NodeSet import NodeSet, set_std_group_resolver |
| 288 |
>>> from ClusterShell.NodeUtils import GroupResolverConfig |
| 289 |
>>> set_std_group_resolver(GroupResolverConfig("/other/groups.conf")) |
| 290 |
>>> print(NodeSet("@oss")) |
| 291 |
other[10-20] |
| 292 |
|
| 293 |
It is possible to restore :class:`.NodeSet` *default group resolver* by |
| 294 |
passing None to the :func:`.NodeSet.set_std_group_resolver()` module function, |
| 295 |
for example:: |
| 296 |
|
| 297 |
>>> from ClusterShell.NodeSet import set_std_group_resolver |
| 298 |
>>> set_std_group_resolver(None) |
| 299 |
|
| 300 |
|
| 301 |
.. _class-NodeSet-disable-group: |
| 302 |
|
| 303 |
Disabling node group resolution |
| 304 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 305 |
|
| 306 |
If for any reason, you want to disable host groups resolution, you can use the |
| 307 |
special resolver value *RESOLVER_NOGROUP*. In that case, :class:`.NodeSet` |
| 308 |
parsing engine will not recognize **@** group characters anymore, for |
| 309 |
instance:: |
| 310 |
|
| 311 |
>>> from ClusterShell.NodeSet import NodeSet, RESOLVER_NOGROUP |
| 312 |
>>> print(NodeSet("@oss")) |
| 313 |
example[4-5] |
| 314 |
>>> print(NodeSet("@oss", resolver=RESOLVER_NOGROUP)) |
| 315 |
@oss |
| 316 |
|
| 317 |
Any attempts to use a group-based method (like :meth:`.NodeSet.groups()` or |
| 318 |
:meth:`.NodeSet.regroup()`) on such "no group" NodeSet will raise a |
| 319 |
*NodeSetExternalError* exception. |
| 320 |
|
| 321 |
|
| 322 |
NodeSet object serialization |
| 323 |
---------------------------- |
| 324 |
|
| 325 |
The :class:`.NodeSet` class supports object serialization through the standard |
| 326 |
*pickling*. Group resolution is done before *pickling*. |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
.. _Python sets: https://docs.python.org/3/library/stdtypes.html#set-types-set-frozenset |