python_wiki:spacewalk_list_groups

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
Next revision Both sides next revision
python_wiki:spacewalk_list_groups [2016/11/30 22:05]
billdozor created
python_wiki:spacewalk_list_groups [2017/07/07 23:30]
billdozor [Usage]
Line 12: Line 12:
 ---- ----
  
-====== Script Usage ======+====== Usage ======
  
 <code bash> <code bash>
-./sw_listgroups.py HOSTNAME+./sw_listgroups.py -n HOSTNAME
 </code> </code>
 +
 +----
  
 ====== The Code ====== ====== The Code ======
  
 <code python sw_listgroups.py> <code python sw_listgroups.py>
-#!/usr/bin/python +#!/usr/bin/python
 # Title: sw_listgroups.py # Title: sw_listgroups.py
 # Description: Output a space separated list of Spacewalk groups a system is in. # Description: Output a space separated list of Spacewalk groups a system is in.
-# Date: 2016-10-07 +# Date: 2017-06-27 
-# Recent Changes:-header format/comment modifications +# Recent Changes:-updated arguments to use argparse 
-#                -Use spacecmd config file for credentials (ConfigParser)+#                -check for variants of system name (lower,upper,fqdn of each)
 #################################################################################### ####################################################################################
  
-#### Import Modules ####  +#======================= 
-sys.argv: Command line arguments +# Import Modules 
-from sys import argv +#======================= 
- +argparse: Command line arguments 
 +import argparse 
 # ConfigParser: Use variables from external file # ConfigParser: Use variables from external file
 import ConfigParser import ConfigParser
Line 41: 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:]) !1: +#======================= 
-    print "==== Spacewalk: List Groups ====" +# Domain Name (include first '.') 
-    print +domain_name=".example.com"
-    print "Description: Output a space separated list of Spacewalk groups a system is in." +
-    print +
-    print "--Usage--"  +
-    print "./sw_listgroups.py hostname" +
-    print +
-    print "--Other Requirements--" +
-    print "-> spacecmd config file setup." +
-    print +
-    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="Output a space separated list of Spacewalk groups a system is in."
 +parser.add_argument("-n","--name", help="System Name.", required=True) 
 +args = vars(parser.parse_args()) 
 + 
 +system_name = args['name'
 + 
 +#======================= 
 +# 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 71: Line 73:
 spacewalk_password = config.get("spacecmd", "password") spacewalk_password = config.get("spacecmd", "password")
 spacewalk_server = config.get("spacecmd", "server") spacewalk_server = config.get("spacecmd", "server")
-#########################+#### End of Config Settings ####
  
-#### Main ####+#===================================== 
 +Functions; Main starts after 
 +#===================================== 
 +Function: Get a system's id 
 +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's id for each 
 +  system_id_lower = server.system.getId(key, system_name_lower) 
 +  system_id_lower_fqdn = server.system.getId(key, system_name_lower_fqdn) 
 +  system_id_upper = server.system.getId(key, system_name_upper) 
 +  system_id_upper_fqdn = server.system.getId(key, system_name_upper_fqdn) 
 + 
 +  Check each for success 
 +  if system_id_lower: 
 +    system_id = system_id_lower[0]['id'
 +  elif system_id_lower_fqdn: 
 +    system_id = system_id_lower_fqdn[0]['id'
 +  elif system_id_upper: 
 +    system_id = system_id_upper[0]['id'
 +  elif system_id_upper_fqdn: 
 +    system_id = system_id_upper_fqdn[0]['id'
 +  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 = "https://" + spacewalk_server + "/rpc/api" spacewalk_url = "https://" + spacewalk_server + "/rpc/api"
 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 85: Line 122:
     print "Spacewalk Login failed."     print "Spacewalk Login failed."
     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 == None:
     server.auth.logout(key)     server.auth.logout(key)
-    print "Error:",system_name,"not found in Spacewalk!"+    print("--> Error: (system_name "not found in Spacewalk!")
     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,system_id[0]['id']+available_groups = server.system.listGroups(key, system_id) 
-     +
 # 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['subscribed'] == 1:     if flag['subscribed'] == 1:
         subscribed_groups.append(flag['system_group_name'])         subscribed_groups.append(flag['system_group_name'])
- +
 # A non-empty list evaluates to true # A non-empty list evaluates to true
 if subscribed_groups: if subscribed_groups:
Line 114: 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 "Node"system_name"is not in any groups."+    print("Node (system_name "is not in any groups.")
  
 </code> </code>
  • python_wiki/spacewalk_list_groups.txt
  • Last modified: 2019/05/25 23:50
  • (external edit)