<s> import sys <EOL> import re <EOL> import os <EOL> import ConfigParser <EOL> from novaclient import client as nova_client <EOL> try : <EOL> import json <EOL> except ImportError : <EOL> import simplejson as json <EOL> NOVA_CONFIG_FILES = [ os . path . join ( os . path . dirname ( os . path . realpath ( __file__ ) ) , "<STR_LIT>" ) , <EOL> os . path . expanduser ( os . environ . get ( '<STR_LIT>' , "<STR_LIT>" ) ) , <EOL> "<STR_LIT>" ] <EOL> NOVA_DEFAULTS = { <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> def nova_load_config_file ( ) : <EOL> p = ConfigParser . SafeConfigParser ( NOVA_DEFAULTS ) <EOL> for path in NOVA_CONFIG_FILES : <EOL> if os . path . exists ( path ) : <EOL> p . read ( path ) <EOL> return p <EOL> return None <EOL> def get_fallback ( config , value , section = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return config . get ( section , value ) <EOL> except ConfigParser . NoOptionError : <EOL> return False <EOL> def push ( data , key , element ) : <EOL> """<STR_LIT>""" <EOL> if ( not element ) or ( not key ) : <EOL> return <EOL> if key in data : <EOL> data [ key ] . append ( element ) <EOL> else : <EOL> data [ key ] = [ element ] <EOL> def to_safe ( word ) : <EOL> '''<STR_LIT>''' <EOL> return re . sub ( r"<STR_LIT>" , "<STR_LIT:_>" , word ) <EOL> def get_ips ( server , access_ip = True ) : <EOL> """<STR_LIT>""" <EOL> private = [ ] <EOL> public = [ ] <EOL> address_list = [ ] <EOL> addresses = getattr ( server , '<STR_LIT>' , { } ) <EOL> if len ( addresses ) > <NUM_LIT:0> : <EOL> for network in addresses . itervalues ( ) : <EOL> for address in network : <EOL> if address . get ( '<STR_LIT>' , False ) == '<STR_LIT>' : <EOL> private . append ( address [ '<STR_LIT>' ] ) <EOL> elif address . get ( '<STR_LIT>' , False ) == '<STR_LIT>' : <EOL> public . append ( address [ '<STR_LIT>' ] ) <EOL> if not access_ip : <EOL> address_list . append ( server . accessIPv4 ) <EOL> address_list . extend ( private ) <EOL> address_list . extend ( public ) <EOL> return address_list <EOL> access_ip = None <EOL> if server . accessIPv4 : <EOL> access_ip = server . accessIPv4 <EOL> if ( not access_ip ) and public and not ( private and prefer_private ) : <EOL> access_ip = public [ <NUM_LIT:0> ] <EOL> if private and not access_ip : <EOL> access_ip = private [ <NUM_LIT:0> ] <EOL> return access_ip <EOL> def get_metadata ( server ) : <EOL> """<STR_LIT>""" <EOL> get_ips ( server , False ) <EOL> results = { } <EOL> for key in vars ( server ) : <EOL> value = getattr ( server , key ) <EOL> key = '<STR_LIT>' + re . sub ( r"<STR_LIT>" , "<STR_LIT:_>" , key ) . lower ( ) <EOL> if key != '<STR_LIT>' : <EOL> results [ key ] = value <EOL> return results <EOL> config = nova_load_config_file ( ) <EOL> if not config : <EOL> sys . exit ( '<STR_LIT>' % '<STR_LIT:U+002CU+0020>' . join ( NOVA_CONFIG_FILES ) ) <EOL> username = ( get_fallback ( config , '<STR_LIT:username>' ) or <EOL> os . environ . get ( '<STR_LIT>' , None ) ) <EOL> api_key = ( get_fallback ( config , '<STR_LIT>' ) or <EOL> os . environ . get ( '<STR_LIT>' , None ) ) <EOL> auth_url = ( get_fallback ( config , '<STR_LIT>' ) or <EOL> os . environ . get ( '<STR_LIT>' , None ) ) <EOL> project_id = ( get_fallback ( config , '<STR_LIT>' ) or <EOL> os . environ . get ( '<STR_LIT>' , None ) ) <EOL> region_name = ( get_fallback ( config , '<STR_LIT>' ) or <EOL> os . environ . get ( '<STR_LIT>' , None ) ) <EOL> auth_system = ( get_fallback ( config , '<STR_LIT>' ) or <EOL> os . environ . get ( '<STR_LIT>' , None ) ) <EOL> prefer_private = False <EOL> try : <EOL> prefer_private = config . getboolean ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> except ConfigParser . NoOptionError : <EOL> pass <EOL> client = nova_client . Client ( <EOL> version = config . get ( '<STR_LIT>' , '<STR_LIT:version>' ) , <EOL> username = username , <EOL> api_key = api_key , <EOL> auth_url = auth_url , <EOL> region_name = region_name , <EOL> project_id = project_id , <EOL> auth_system = auth_system , <EOL> service_type = config . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) <EOL> if ( len ( sys . argv ) == <NUM_LIT:2> and sys . argv [ <NUM_LIT:1> ] == '<STR_LIT>' ) or len ( sys . argv ) == <NUM_LIT:1> : <EOL> groups = { '<STR_LIT>' : { '<STR_LIT>' : { } } } <EOL> for server in client . servers . list ( ) : <EOL> access_ip = get_ips ( server ) <EOL> push ( groups , server . name , access_ip ) <EOL> for key , value in server . metadata . iteritems ( ) : <EOL> composed_key = to_safe ( '<STR_LIT>' . format ( key , value ) ) <EOL> push ( groups , composed_key , access_ip ) <EOL> group = server . metadata [ '<STR_LIT>' ] if '<STR_LIT>' in server . metadata else '<STR_LIT>' <EOL> push ( groups , group , access_ip ) <EOL> groups [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ access_ip ] = get_metadata ( server ) <EOL> print ( json . dumps ( groups , sort_keys = True , indent = <NUM_LIT:2> ) ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> elif len ( sys . argv ) == <NUM_LIT:3> and ( sys . argv [ <NUM_LIT:1> ] == '<STR_LIT>' ) : <EOL> results = { } <EOL> ips = [ ] <EOL> for server in client . servers . list ( ) : <EOL> if sys . argv [ <NUM_LIT:2> ] in ( get_ips ( server ) or [ ] ) : <EOL> results = get_metadata ( server ) <EOL> print ( json . dumps ( results , sort_keys = True , indent = <NUM_LIT:2> ) ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> else : <EOL> print "<STR_LIT>" <EOL> sys . exit ( <NUM_LIT:1> ) </s>
