Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
python_wiki:spacewalk_list_groups [2016/11/30 22:05] billdozor created |
python_wiki:spacewalk_list_groups [2019/05/25 23:50] (current) |
||
---|---|---|---|
Line 12: | Line 12: | ||
---- | ---- | ||
- | ====== | + | ====== Usage ====== |
<code bash> | <code bash> | ||
- | ./ | + | ./ |
</ | </ | ||
+ | |||
+ | ---- | ||
====== The Code ====== | ====== The Code ====== | ||
- | <code python sw_listgroups.py> | + | The script is maintained here: https://gitlab.com/whowe/spacewalk |
- | #!/usr/bin/python | + | |
- | # Title: sw_listgroups.py | + | |
- | # Description: | + | |
- | # Date: 2016-10-07 | + | |
- | # Recent Changes: | + | |
- | # -Use spacecmd config file for credentials (ConfigParser) | + | |
- | #################################################################################### | + | |
- | #### Import Modules #### | + | ---- |
- | # sys.argv: Command line arguments | + | |
- | from sys import argv | + | |
- | + | ||
- | # ConfigParser: | + | |
- | import ConfigParser | + | |
- | # os: Use bash environment variables | ||
- | import os | ||
- | |||
- | # xmlrpclib: Allows for parsing of spacewalk APIs | ||
- | import xmlrpclib | ||
- | #################### | ||
- | |||
- | #### Command Line Arguments #### | ||
- | # If there is not 1 argument, show usage and exit | ||
- | if len(argv[1: | ||
- | print "==== Spacewalk: List Groups ====" | ||
- | |||
- | print " | ||
- | |||
- | print " | ||
- | print " | ||
- | |||
- | print " | ||
- | print "-> spacecmd config file setup." | ||
- | |||
- | exit(1) | ||
- | |||
- | # Set hostname from command line argument | ||
- | system_name = argv[1] | ||
- | ################################ | ||
- | |||
- | #### Config Settings #### | ||
- | # Read Spacecmd Config file - Path set from $HOME variable + expected spacecmd config file | ||
- | config = ConfigParser.ConfigParser() | ||
- | config.read(os.environ.get(' | ||
- | |||
- | # Parse Spacecmd Config file - Set username, password, spacewalk server | ||
- | spacewalk_login = config.get(" | ||
- | spacewalk_password = config.get(" | ||
- | spacewalk_server = config.get(" | ||
- | ######################### | ||
- | |||
- | #### Main #### | ||
- | |||
- | # Setup server info (rpc api endpoint) | ||
- | spacewalk_url = " | ||
- | server = xmlrpclib.Server(spacewalk_url) | ||
- | |||
- | # Attempt to login and get a session id | ||
- | try: | ||
- | key = server.auth.login(spacewalk_login, | ||
- | except: | ||
- | print " | ||
- | exit(1) | ||
- | |||
- | # Get the system id and check to see if that system_name exists | ||
- | system_id = server.system.getId(key, | ||
- | if not system_id: | ||
- | server.auth.logout(key) | ||
- | print " | ||
- | exit(1) | ||
- | |||
- | # Get all available groups for the system_name | ||
- | available_groups = server.system.listGroups(key, | ||
- | |||
- | # Logout of Spacewalk server | ||
- | server.auth.logout(key) | ||
- | |||
- | # Empty list to hold what groups the system_name is subscribed to | ||
- | subscribed_groups = [] | ||
- | |||
- | # Check available groups for subscribed = 1 (true), add to list | ||
- | for flag in available_groups: | ||
- | if flag[' | ||
- | subscribed_groups.append(flag[' | ||
- | |||
- | # A non-empty list evaluates to true | ||
- | if subscribed_groups: | ||
- | for line in subscribed_groups: | ||
- | # Print each group with no newline (,) | ||
- | print line, | ||
- | else: | ||
- | # We have an empty list; the system is not subscribed to any groups | ||
- | print " | ||
- | |||
- | </ |