NVIDIA Base Command Manager Python API
This chapter introduces the Python API of NVIDIA Base Command Manager.
The Python API package was completely overhauled in NVIDIA Base Command Manager 8.2.
The cmdaemon-pythoncm package now provides a pure Python connection to the cluster manager, making it possible for cluster administrators to automate cluster operations via Python.
It also makes it possible to run Python code on any operating system that supports Python 3.5 and later.
The BCM Python API uses the following extra modules:
-
pyOpenSSL -
ply -
lxml -
tabulate -
monotonic -
humanfriendly -
pyYAML -
six
1.1 Getting Started
On the cluster head node itself the python3 module can simply be loaded:
Example
To execute or develop the Python code on any other machine usually requires some extra steps:
-
Python 3 (3.5, 3.6, 3.7, 3.8 will work) should be installed
-
The 8 extra modules listed previously should be installed using Pip
-
The
/cm/local/apps/cmd/pythoncm/lib/python3.9/site-packages/pythoncmdirectory should be copied over to the site-packages directory of the development machine. The version is enforced inlib/python3.9/site-packages/pythoncm/__init__.py, so version consistency would be required here.
The pythoncm module should then be loaded, to confirm everything was set up correctly:
Example
If connecting from outside the cluster, then port 8081 must not be blocked by a firewall.
A certificate is needed by the Python API to identify itself to CMDaemon.
The existence of the certificate on the head node should be checked. It should be copied over to the development machine, if it is needed there.
Example
Example
The developer many need to contact the cluster administrator to get a certificate.
1.2 Connecting To A Cluster
The first step when working with the Python API is to establish a connection to the CMDaemon process on the cluster:
If working outside the cluster, then the settings for connecting to the cluster must be specified:
1.3 Inspecting Settings
All settings in BCM are stored inside an entity.
Each entity has a type and a unique name among the entities of the same type.
To inspect an entity it should first be found inside the cluster:
If the name node001 was also given a different entity, then the type must be specified to ensure that the correct entity is returned:
Once the node entity is found, inspecting the settings is a matter of printing the desired field:
Complex settings, such as network interfaces, have their own settings:
Because many nodes could have a network interface called eth0, such a setting cannot be found from the cluster: The following code will return None.
To find all eth0 interfaces, all nodes need to be found, and then iterated over:
1.4 Modifying Settings
Basic entity settings are exported as Python properties and can simply be changed:
Similarly interfaces settings can be accessed and changed directly:
Removing an interface from a node can be done in various Pythonic ways:
To add a new interface, the entity instance needs to be created first, and then added to the node:
All changes are made on a local copy of the entity. The cluster has no knowledge of the changes until they are committed.
It is recommended to make many changes locally, and only commit once at the end.
The return value of the commit operation should always be checked.
Committing a badly-configured node will be blocked by the head node:
An entity found using the cluster object is removed differently.
As with commit, the result should always be checked: a removal can fail if a node is UP:
1.5 Inspecting The Entire Cluster
The example directory contains a script to inspect the entire cluster.
Example
The example directory also contains a script that prints all metadata for all available entities in BCM:
Example
1.6 Performing Operations On Entities
All Python API functionality is contained in /cm/local/apps/cmd/pythoncm/lib/python3.9/site-packages/pythoncm.
Methods are documented inside the python code itself.
When operating on multiple entities, it is possible to iterate over them and do each operation individually.
However the same can also be done with a parallel version of the operation. When possible the parallel version should be used, because it is faster and requires less network traffic.
1.7 Monitoring
All monitoring data can be accessed using the Python API.
Monitoring is a set of operations performed on entities.
For example, to get latest data for a single entity:
Monitoring operations on multiple operations should be done using the monitoring module:
1.8 Examples
The best way to get going is by looking at the examples. These can be found on the head node, at /cm/local/examples/cmd/pythoncm:
Example
The examples can be tried out after loading the Python environment:
Example