Playing With Uyuni Api Using Python
During last SUSE Hackweek, I was working on a project to create some short tutorials and videos doing some demos about different cool stuff.
I wanted to create some short demos to demostrate how you can easily operate the Uyuni XMLRPC API using Python, allowing you to integrate Uyuni with other applications, create your own automation, etc.
1) Basic operations:
- List registered systems
- List event history for a system
- Delete systems
- Bootstrap new systems
Some examples:
import xmlrpc.client as xmlrpclib
UYUNI_API_URL = "https://my-uyuni-server-fqdn/rpc/api"
UYUNI_API_LOGIN = "my-uyuni-user"
UYUNI_API_PASSWORD = "my-uyuni-pass"
# Connect to Uyuni XMLRPC API
client = xmlrpclib.Server(UYUNI_API_URL, verbose=0)
# Login and get session key
key = client.auth.login(UYUNI_API_LOGIN, UYUNI_API_PASSWORD)
# List registered system in Uyuni
client.system.listSystems(key)
# List the Event history for the system with ID 1111111111
client.system.listSystemEvents(key, 1111111111)
# Remove the registered system from Uyuni with ID = 1111111111
client.system.deleteSystem(key, 1111111111)
# Bootstrapping a new minion as normal minion. No activation key
activation_key = ""
client.system.bootstrap(key, "my-new-minion-fqdn", 22, "root", "linux", activation_key, False)
2) Scheduling some actions:
- Apply Highstate
- Run a remote command
# Schedule a highstate now on system with ID 1111111111. TestMode = False
client.system.scheduleApplyHighstate(key, "1111111111", xmlrpclib.DateTime(), False)
# Schedule a remote execution command now on system with ID 1111111111: "date" - user: root, group: root, timeout 10
client.system.scheduleScriptRun(key, [1111111111], "root", "root", 10, "date", xmlrpclib.DateTime())
3) Checking actions status:
- Check pending actions
- Check actions results
# List all action currently in progress
client.schedule.listInProgressActions(key)
# List systems where action with ID 555 is still in progress
client.schedule.listInProgressSystems(key, 555)
# List systems where action with ID 555 is completed
client.schedule.listCompletedSystems(key, 555)
# List systems where action with ID 555 has failed
client.schedule.listFailedSystems(key, 555)
4) Python script to automate some operations:
- List registered clients in the Uyuni server.
- Delete test client if already registered.
- Bootstrap test client as minion using SSH
- Schedule “highstate” action and remote command execution
- Check the results from the actions