Skip to content

Instantly share code, notes, and snippets.

@FalcoSuessgott
Last active December 18, 2019 15:51
Show Gist options
  • Save FalcoSuessgott/ebd0f0518a82a781911018fcfb38b343 to your computer and use it in GitHub Desktop.
Save FalcoSuessgott/ebd0f0518a82a781911018fcfb38b343 to your computer and use it in GitHub Desktop.
Spacewalk Systems to Rundeck JSON Nodes resources
#!/usr/bin/env python
#
# Fetches systems from spacewalk when systems osad status equals "Online"
#
# Tags: Systems subscribed Groups
# BaseChannel: Systems basechannel
# OS: Systems activation key (if *centos* -> Centos, else RHEL + Systems Release Version)
# Virt_type: Systems virtualization Type
#
# Autor: tom-morelly@gmx.de
# Date: 18.12.2019
#
import json
import xmlrpclib
from xmlrpclib import ServerProxy
SPACEWALK_URL = ""
SPACEWALK_USER = ""
SPACEWALK_PASSWORD = ""
RUNDECK_USER = ""
def spacewalk_auth():
""" Authenticates at spacewalk """
global client
client = xmlrpclib.Server("http://" + SPACEWALK_URL + "/rpc/api", verbose=0) # type: ServerProxy
key = client.auth.login(SPACEWALK_USER, SPACEWALK_PASSWORD)
return key
def get_hosts(auth_key):
""" returns a list of all active spacewalk hosts"""
hosts = []
for system in client.system.listSystems(auth_key):
osa_status = client.system.getDetails(auth_key, system['id'])['osa_status']
if osa_status == "online":
hosts.append(system['id'])
return hosts
def build_rundeck_json_format(auth_key, hosts):
"""
parses the collected systems to the neccessary json format
see: https://github.com/rundeck/rundeck/tree/master/examples/json-plugin
"""
output = {}
for host in hosts:
try:
hostname = client.system.getDetails(auth_key, host)['hostname']
output[hostname] = {
"tags": [group['system_group_name'] for group in client.system.listGroups(auth_key, host) if group['subscribed'] == 1],
"username": str(RUNDECK_USER),
"hostname": str(hostname),
"basechannel": str(client.system.getSubscribedBaseChannel(auth_key, host)['label']),
"OS": "CentOS " + client.system.getDetails(auth_key, host)['release'] if "centos" in str(client.system.listActivationKeys(auth_key, host)).lower()
else str("RHEL " + client.system.getDetails(auth_key, host)['release']),
"virt_type": str(client.system.getDetails(auth_key, host)['virtualization'])
}
except Exception:
pass
print(json.dumps(output, indent=4, sort_keys=True))
if __name__ == '__main__':
auth_key = spacewalk_auth()
hosts = get_hosts(auth_key)
build_rundeck_json_format(auth_key, get_hosts(auth_key))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment