Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
python_wiki:spacewalk_list_groups [2016/11/30 22:05] billdozor [Script Usage] |
python_wiki:spacewalk_list_groups [2017/07/07 23:30] billdozor [Usage] |
||
---|---|---|---|
Line 15: | Line 15: | ||
<code bash> | <code bash> | ||
- | ./ | + | ./ |
</ | </ | ||
Line 23: | Line 23: | ||
<code python sw_listgroups.py> | <code python sw_listgroups.py> | ||
- | # | + | # |
# Title: sw_listgroups.py | # Title: sw_listgroups.py | ||
# Description: | # Description: | ||
- | # Date: 2016-10-07 | + | # Date: 2017-06-27 |
- | # Recent Changes:-header format/ | + | # Recent Changes:-updated arguments to use argparse |
- | # -Use spacecmd config file for credentials | + | # -check for variants of system name (lower, |
#################################################################################### | #################################################################################### | ||
- | #### Import Modules #### | + | #======================= |
- | # sys.argv: Command line arguments | + | # Import Modules |
- | from sys import | + | #======================= |
- | + | # argparse: Command line arguments | |
+ | import | ||
# ConfigParser: | # ConfigParser: | ||
import ConfigParser | import ConfigParser | ||
Line 43: | Line 45: | ||
# xmlrpclib: Allows for parsing of spacewalk APIs | # xmlrpclib: Allows for parsing of spacewalk APIs | ||
import xmlrpclib | import xmlrpclib | ||
- | #################### | ||
- | #### Command Line Arguments #### | + | #======================= |
- | # If there is not 1 argument, show usage and exit | + | # CUSTOMIZE HERE |
- | if len(argv[1: | + | #======================= |
- | print "==== Spacewalk: List Groups | + | # Domain Name (include first '.') |
- | | + | domain_name=".example.com" |
- | print " | + | |
- | | + | |
- | print " | + | |
- | print "./ | + | |
- | + | ||
- | print " | + | |
- | print "-> spacecmd config file setup." | + | |
- | + | ||
- | exit(1) | + | |
- | + | ||
- | # Set hostname from command line argument | + | |
- | system_name = argv[1] | + | |
- | ################################ | + | |
- | #### Config Settings #### | + | #======================= |
+ | # 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 | # Read Spacecmd Config file - Path set from $HOME variable + expected spacecmd config file | ||
config = ConfigParser.ConfigParser() | config = ConfigParser.ConfigParser() | ||
Line 73: | Line 73: | ||
spacewalk_password = config.get(" | spacewalk_password = config.get(" | ||
spacewalk_server = config.get(" | spacewalk_server = config.get(" | ||
- | ##################### | + | #### End of Config Settings |
- | #### Main #### | + | #===================================== |
+ | # Functions; Main starts after | ||
+ | #===================================== | ||
+ | # Function: Get a system' | ||
+ | def get_system_id(system_name): | ||
+ | |||
+ | | ||
+ | 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 | ||
+ | |||
+ | | ||
+ | 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, | ||
+ | |||
+ | | ||
+ | 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) | # Setup server info (rpc api endpoint) | ||
spacewalk_url = " | spacewalk_url = " | ||
server = xmlrpclib.Server(spacewalk_url) | server = xmlrpclib.Server(spacewalk_url) | ||
- | + | ||
# Attempt to login and get a session id | # Attempt to login and get a session id | ||
try: | try: | ||
Line 87: | Line 122: | ||
print " | print " | ||
exit(1) | exit(1) | ||
- | + | ||
# Get the system id and check to see if that system_name exists | # Get the system id and check to see if that system_name exists | ||
- | system_id = server.system.getId(key,system_name) | + | system_id = get_system_id(system_name) |
- | if not system_id: | + | if system_id |
server.auth.logout(key) | server.auth.logout(key) | ||
- | print " | + | print("--> |
exit(1) | exit(1) | ||
- | + | ||
- | # Get all available groups for the system_name | + | # Get all available groups for the system_name |
- | available_groups = server.system.listGroups(key, | + | available_groups = server.system.listGroups(key, |
- | | + | |
# Logout of Spacewalk server | # Logout of Spacewalk server | ||
server.auth.logout(key) | server.auth.logout(key) | ||
- | + | ||
# Empty list to hold what groups the system_name is subscribed to | # Empty list to hold what groups the system_name is subscribed to | ||
subscribed_groups = [] | subscribed_groups = [] | ||
- | + | ||
# Check available groups for subscribed = 1 (true), add to list | # Check available groups for subscribed = 1 (true), add to list | ||
for flag in available_groups: | for flag in available_groups: | ||
if flag[' | if flag[' | ||
subscribed_groups.append(flag[' | subscribed_groups.append(flag[' | ||
- | + | ||
# A non-empty list evaluates to true | # A non-empty list evaluates to true | ||
if subscribed_groups: | if subscribed_groups: | ||
Line 116: | Line 151: | ||
else: | else: | ||
# We have an empty list; the system is not subscribed to any groups | # We have an empty list; the system is not subscribed to any groups | ||
- | print " | + | print(" |
</ | </ |