This article will provide Pythoncm script examples for software image related operations.
Pre-requisites:
Install Pythoncm and load the module:
#apt update
#apt install cmdaemon-pythoncm
#module load python3
Pythoncm script example of “image clone”
The following CMSH command list and Pythoncm script can achieve the same functionality:
CMSH command list:
#cmsh
%softwareimage
%clone default-image test
%commit –wait
Pythoncm script:
from __future__ import annotations
import logging
import time
from pythoncm.cluster import Cluster
from pythoncm.settings import Settings
LOGGER = logging.getLogger(__name__)
def main():
cluster = Cluster()
default_software_image = cluster.get_by_name(‘default-image’, ‘SoftwareImage’)
if default_software_image is None:
print(“Unable to find default software image”)
else:
test_software_image = default_software_image.clone()
test_software_image.name = “test”
test_software_image.path = “/cm/images/test”
print(test_software_image)
print(“==== validate ====”)
commit_result = test_software_image.validate()
print(commit_result)
print(“==== commit ====”)
commit_result = test_software_image.commit(wait_for_task=True)
print(commit_result)
print(test_software_image)
cluster.disconnect()
if __name__ == ‘__main__’:
import logging.config
log_level = logging.DEBUG
logging.config.dictConfig(
{
“version”: 1,
“formatters”: {
“default_formatter”: {
“format”: ‘%(levelname)-8s (%(asctime)-15s) [%(filename)-20s:%(lineno)3d] %(message)s’,
“datefmt”: “%d-%b-%Y %H:%M:%S”,
}
},
“handlers”: {
“console”: {
“class”: “logging.StreamHandler”,
“level”: “WARNING”,
“formatter”: “default_formatter”,
},
“file”: {
“class”: “logging.FileHandler”,
“level”: “DEBUG”,
“formatter”: “default_formatter”,
‘filename’: f'{__file__}.log’,
‘mode’: ‘a’,
},
},
“root”: {“level”: log_level, “handlers”: [“console”, “file”]},
}
)
main()
Pythoncm script example of “grabimage”
The following CMSH command list and Pythoncm script can achieve the same functionality:
CMSH command list:
#cmsh
%device
%grabimage -w -i test node001
Pythoncm script:
from __future__ import annotations
import logging
import time
from pythoncm.cluster import Cluster
from pythoncm.settings import Settings
LOGGER = logging.getLogger(__name__)
def main():
cluster = Cluster()
test_software_image = cluster.get_by_name(‘test’, ‘SoftwareImage’)
if test_software_image is None:
print(“Unable to find test software image”)
else:
node001 = cluster.get_by_name(‘node001’)
if node001 is None:
print(“Unable to find node001”)
else:
test_update = node001.grab_image(test_software_image, wait=True, timeout=500)
cluster.disconnect()
if __name__ == ‘__main__’:
import logging.config
log_level = logging.DEBUG
logging.config.dictConfig(
{
“version”: 1,
“formatters”: {
“default_formatter”: {
“format”: ‘%(levelname)-8s (%(asctime)-15s) [%(filename)-20s:%(lineno)3d] %(message)s’,
“datefmt”: “%d-%b-%Y %H:%M:%S”,
}
},
“handlers”: {
“console”: {
“class”: “logging.StreamHandler”,
“level”: “WARNING”,
“formatter”: “default_formatter”,
},
“file”: {
“class”: “logging.FileHandler”,
“level”: “DEBUG”,
“formatter”: “default_formatter”,
‘filename’: f'{__file__}.log’,
‘mode’: ‘a’,
},
},
“root”: {“level”: log_level, “handlers”: [“console”, “file”]},
}
)
main()