Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
python_wiki:spacewalk_list_groups [2017/07/07 23:30] billdozor [The Code] |
python_wiki:spacewalk_list_groups [2019/05/25 23:50] (current) |
||
---|---|---|---|
Line 15: | Line 15: | ||
<code bash> | <code bash> | ||
- | ./ | + | ./ |
</ | </ | ||
Line 22: | Line 22: | ||
====== The Code ====== | ====== The Code ====== | ||
- | <code python sw_listgroups.py> | + | The script is maintained here: https:// |
- | #!/usr/bin/ | + | |
- | # Title: sw_listgroups.py | + | |
- | # Description: | + | |
- | # Date: 2017-06-27 | + | |
- | # Recent Changes: | + | |
- | # -check for variants of system name (lower, | + | |
- | #################################################################################### | + | |
- | # | + | ---- |
- | # Import Modules | + | |
- | # | + | |
- | # argparse: Command line arguments | + | |
- | import argparse | + | |
- | # ConfigParser: | ||
- | import ConfigParser | ||
- | |||
- | # os: Use bash environment variables | ||
- | import os | ||
- | |||
- | # xmlrpclib: Allows for parsing of spacewalk APIs | ||
- | import xmlrpclib | ||
- | |||
- | # | ||
- | # CUSTOMIZE HERE | ||
- | # | ||
- | # Domain Name (include first ' | ||
- | domain_name=" | ||
- | |||
- | # | ||
- | # Get Script Arguments | ||
- | # | ||
- | # Build argument parser information | ||
- | parser = argparse.ArgumentParser(description=" | ||
- | parser.add_argument(" | ||
- | args = vars(parser.parse_args()) | ||
- | |||
- | system_name = args[' | ||
- | |||
- | # | ||
- | # 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(" | ||
- | #### End of Config Settings #### | ||
- | |||
- | # | ||
- | # Functions; Main starts after | ||
- | # | ||
- | # Function: Get a system' | ||
- | def get_system_id(system_name): | ||
- | |||
- | # Create all system name variants | ||
- | system_name_lower = system_name.strip(domain_name).lower() | ||
- | system_name_lower_fqdn = system_name_lower + domain_name | ||
- | system_name_upper = system_name.strip(domain_name).upper() | ||
- | system_name_upper_fqdn = system_name_upper + domain_name | ||
- | |||
- | # Attempt to get the system' | ||
- | system_id_lower = server.system.getId(key, | ||
- | system_id_lower_fqdn = server.system.getId(key, | ||
- | system_id_upper = server.system.getId(key, | ||
- | system_id_upper_fqdn = server.system.getId(key, | ||
- | |||
- | # Check each for success | ||
- | if system_id_lower: | ||
- | system_id = system_id_lower[0][' | ||
- | elif system_id_lower_fqdn: | ||
- | system_id = system_id_lower_fqdn[0][' | ||
- | elif system_id_upper: | ||
- | system_id = system_id_upper[0][' | ||
- | elif system_id_upper_fqdn: | ||
- | system_id = system_id_upper_fqdn[0][' | ||
- | else: | ||
- | # System cannot be found | ||
- | system_id = None | ||
- | |||
- | return system_id | ||
- | |||
- | # | ||
- | # Main starts here | ||
- | # | ||
- | |||
- | # 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 = get_system_id(system_name) | ||
- | if system_id == None: | ||
- | 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(" | ||
- | |||
- | </ |