Differences
This shows you the differences between two versions of the page.
python_wiki:spacewalk_describe [2018/06/19 16:09] billdozor [The Code] |
python_wiki:spacewalk_describe [2019/05/25 23:50] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Spacewalk Describe ====== | ||
- | |||
- | **General Information** | ||
- | |||
- | View/Edit descriptions for Spacewalk registered systems. | ||
- | |||
- | **Checklist** | ||
- | * Spacewalk server setup | ||
- | * Spacewalk user account | ||
- | * API Reference: https:// | ||
- | |||
- | ---- | ||
- | |||
- | ====== Usage ====== | ||
- | |||
- | sw_describe.py -h | ||
- | <code bash> | ||
- | usage: sw_describe.py [-h] [-l] [-e] [-d DESCRIBE] [-n NAME] [-g GROUP] [-t] | ||
- | |||
- | View/Edit descriptions for Spacewalk registered systems. | ||
- | |||
- | optional arguments: | ||
- | -h, --help | ||
- | -l, --list | ||
- | -e, --edit | ||
- | --name). | ||
- | -d DESCRIBE, --describe DESCRIBE | ||
- | Set new description of system (use with --edit). | ||
- | -n NAME, --name NAME Single system name (use with --list or --edit) | ||
- | -g GROUP, --group GROUP | ||
- | Spacewalk group or ' | ||
- | -t, --test | ||
- | would have happened. | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ====== The Code ====== | ||
- | |||
- | <code python sw_describe.py> | ||
- | # | ||
- | # Title: sw_describe.py | ||
- | # Description: | ||
- | # Date: 2017-06-13 | ||
- | # Recent Changes: | ||
- | ############################################################################## | ||
- | |||
- | # | ||
- | # Import Modules | ||
- | # | ||
- | |||
- | # Future print function capabilities | ||
- | from __future__ import print_function | ||
- | |||
- | # sys: for stdout print buffer flush | ||
- | import sys | ||
- | |||
- | # argparse: Command line arguments | ||
- | import argparse | ||
- | |||
- | # ConfigParser: | ||
- | import ConfigParser | ||
- | |||
- | # os: Use bash environment variables for path to spacecmd config | ||
- | 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(" | ||
- | parser.add_argument(" | ||
- | parser.add_argument(" | ||
- | parser.add_argument(" | ||
- | parser.add_argument(" | ||
- | parser.add_argument(" | ||
- | args = vars(parser.parse_args()) | ||
- | |||
- | # If testing, we don't actually want to do anything | ||
- | if args[' | ||
- | print(" | ||
- | |||
- | # Requirement: | ||
- | if args[' | ||
- | print(">> | ||
- | exit(1) | ||
- | elif args[' | ||
- | print(">> | ||
- | exit(1) | ||
- | |||
- | # Requirement: | ||
- | if args[' | ||
- | print(">> | ||
- | exit(1) | ||
- | elif not args[' | ||
- | print(">> | ||
- | exit(1) | ||
- | |||
- | # Requirement: | ||
- | if args[' | ||
- | print(">> | ||
- | exit(1) | ||
- | |||
- | # Requirement: | ||
- | if args[' | ||
- | print(">> | ||
- | exit(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(" | ||
- | #### End of Config Settings #### | ||
- | |||
- | # | ||
- | # Functions; Main starts after | ||
- | # | ||
- | |||
- | # Function: List Descriptions | ||
- | def list_descriptions(system_type, | ||
- | |||
- | # Single System | ||
- | if system_type == " | ||
- | # Get System Details | ||
- | sw_system = get_system_details(system_name) | ||
- | |||
- | # Print System and Description | ||
- | print(" | ||
- | |||
- | # Group of Systems | ||
- | elif system_type == " | ||
- | # Get all Spacewalk Registered systems from group | ||
- | sw_system_list_details = get_system_list_details(system_name) | ||
- | |||
- | # Loop through list and show descriptions | ||
- | for sw_system in sw_system_list_details: | ||
- | print(" | ||
- | |||
- | else: | ||
- | print(" | ||
- | exit(1) | ||
- | |||
- | return | ||
- | |||
- | # Function: Edit Descriptions | ||
- | def edit_descriptions(system_type, | ||
- | |||
- | # Single System | ||
- | if system_type == " | ||
- | # Get System Details | ||
- | sw_system = get_system_details(system_name) | ||
- | |||
- | # Edit System Description | ||
- | if args[' | ||
- | # Show existing description | ||
- | print(" | ||
- | |||
- | # Get new description from argument | ||
- | new_description = args[' | ||
- | |||
- | if not new_description: | ||
- | # If new_description is empty, do not set | ||
- | print(" | ||
- | else: | ||
- | if args[' | ||
- | # Testing; don't actually set the description | ||
- | print(" | ||
- | else: | ||
- | # Not testing; Set new description and display it | ||
- | print(" | ||
- | set_system_description(sw_system[' | ||
- | |||
- | else: | ||
- | # Error catching for edit ' | ||
- | print(" | ||
- | exit(1) | ||
- | |||
- | else: | ||
- | print(" | ||
- | exit(1) | ||
- | |||
- | return | ||
- | |||
- | ##-- Utility functions: Short/ | ||
- | # Function: Get all group names | ||
- | def get_all_groups(): | ||
- | # Get all group details | ||
- | all_groups = server.systemgroup.listAllGroups(key) | ||
- | group_list = [] | ||
- | |||
- | # Build a list of just group names | ||
- | for group in all_groups: | ||
- | group_list.append(group[' | ||
- | |||
- | return group_list | ||
- | |||
- | # Function: Get a list of system details given a group name | ||
- | def get_system_list_details(group_name): | ||
- | if group_name == " | ||
- | # Get all systems | ||
- | system_list_details = server.system.listSystems(key) | ||
- | else: | ||
- | # Get a group' | ||
- | system_list_details = server.systemgroup.listSystemsMinimal(key, | ||
- | |||
- | return system_list_details | ||
- | |||
- | # Function: Get a system' | ||
- | def get_system_details(system_name): | ||
- | |||
- | # Get the system' | ||
- | system_id = get_system_id(system_name) | ||
- | if system_id == None: | ||
- | print(" | ||
- | exit(1) | ||
- | else: | ||
- | # Get System Details | ||
- | system_details = server.system.getDetails(key, | ||
- | |||
- | return system_details | ||
- | |||
- | # Function: Get a system' | ||
- | def get_system_description(system_id): | ||
- | |||
- | # Retrieve system details | ||
- | system_details = server.system.getDetails(key, | ||
- | |||
- | return system_details[' | ||
- | |||
- | # Function: Set a system' | ||
- | def set_system_description(system_id, | ||
- | |||
- | try: | ||
- | server.system.setDetails(key, | ||
- | except: | ||
- | print(" | ||
- | return False | ||
- | |||
- | return True | ||
- | |||
- | # 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 | ||
- | # | ||
- | |||
- | # Banner | ||
- | print(" | ||
- | |||
- | # 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) | ||
- | |||
- | #-- Group Name Validation --# | ||
- | if args[' | ||
- | #- Ensure group exists if it is not " | ||
- | try: | ||
- | # Get group' | ||
- | group_details = server.systemgroup.getDetails(key, | ||
- | |||
- | # If no systems are in the group, there is nothing to do | ||
- | if group_details[' | ||
- | print(" | ||
- | exit(1) | ||
- | |||
- | except: | ||
- | print(" | ||
- | |||
- | print(" | ||
- | group_list = get_all_groups() | ||
- | for group in group_list: | ||
- | if group.startswith(" | ||
- | print(" | ||
- | print(" | ||
- | exit(1) | ||
- | |||
- | #-- Edit or List System Descriptions --# | ||
- | |||
- | if args[' | ||
- | # List Descriptions | ||
- | print(">> | ||
- | |||
- | if args[' | ||
- | list_descriptions(" | ||
- | elif args[' | ||
- | list_descriptions(" | ||
- | else: | ||
- | print(" | ||
- | exit(1) | ||
- | |||
- | elif args[' | ||
- | # Edit Descriptions | ||
- | print(">> | ||
- | |||
- | if args[' | ||
- | edit_descriptions(" | ||
- | elif args[' | ||
- | edit_descriptions(" | ||
- | else: | ||
- | print(" | ||
- | exit(1) | ||
- | |||
- | else: | ||
- | # Error catching, should not get here | ||
- | print(" | ||
- | exit(1) | ||
- | |||
- | # Logout of Spacewalk server | ||
- | server.auth.logout(key) | ||
- | </ | ||
- | |||
- | ---- | ||