mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Use 'except ... as' syntax in contrib/ and test/ too
This commit is contained in:
parent
eb99aa8c68
commit
9ae66a7f5c
14 changed files with 25 additions and 25 deletions
|
@ -76,7 +76,7 @@ def save_cache(data, config):
|
||||||
cache = open('/'.join([dpath,'inventory']), 'w')
|
cache = open('/'.join([dpath,'inventory']), 'w')
|
||||||
cache.write(json.dumps(data))
|
cache.write(json.dumps(data))
|
||||||
cache.close()
|
cache.close()
|
||||||
except IOError, e:
|
except IOError as e:
|
||||||
pass # not really sure what to do here
|
pass # not really sure what to do here
|
||||||
|
|
||||||
|
|
||||||
|
@ -88,7 +88,7 @@ def get_cache(cache_item, config):
|
||||||
cache = open('/'.join([dpath,'inventory']), 'r')
|
cache = open('/'.join([dpath,'inventory']), 'r')
|
||||||
inv = cache.read()
|
inv = cache.read()
|
||||||
cache.close()
|
cache.close()
|
||||||
except IOError, e:
|
except IOError as e:
|
||||||
pass # not really sure what to do here
|
pass # not really sure what to do here
|
||||||
|
|
||||||
return inv
|
return inv
|
||||||
|
@ -172,7 +172,7 @@ def generate_inv_from_api(enterprise_entity,config):
|
||||||
else:
|
else:
|
||||||
vm_metadata = metadata['metadata']['metadata']
|
vm_metadata = metadata['metadata']['metadata']
|
||||||
inventory['_meta']['hostvars'][vm_nic] = vm_metadata
|
inventory['_meta']['hostvars'][vm_nic] = vm_metadata
|
||||||
except Exception, e:
|
except Exception as e:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
inventory[vm_vapp]['children'].append(vmcollection['name'])
|
inventory[vm_vapp]['children'].append(vmcollection['name'])
|
||||||
|
@ -183,7 +183,7 @@ def generate_inv_from_api(enterprise_entity,config):
|
||||||
inventory[vmcollection['name']].append(vm_nic)
|
inventory[vmcollection['name']].append(vm_nic)
|
||||||
|
|
||||||
return inventory
|
return inventory
|
||||||
except Exception, e:
|
except Exception as e:
|
||||||
# Return empty hosts output
|
# Return empty hosts output
|
||||||
return { 'all': {'hosts': []}, '_meta': { 'hostvars': {} } }
|
return { 'all': {'hosts': []}, '_meta': { 'hostvars': {} } }
|
||||||
|
|
||||||
|
@ -214,7 +214,7 @@ if __name__ == '__main__':
|
||||||
try:
|
try:
|
||||||
login = api_get(None,config)
|
login = api_get(None,config)
|
||||||
enterprise = next(link for link in (login['links']) if (link['rel']=='enterprise'))
|
enterprise = next(link for link in (login['links']) if (link['rel']=='enterprise'))
|
||||||
except Exception, e:
|
except Exception as e:
|
||||||
enterprise = None
|
enterprise = None
|
||||||
|
|
||||||
if cache_available(config):
|
if cache_available(config):
|
||||||
|
|
|
@ -98,7 +98,7 @@ class CloudStackInventory(object):
|
||||||
options = parser.parse_args()
|
options = parser.parse_args()
|
||||||
try:
|
try:
|
||||||
self.cs = CloudStack(**read_config())
|
self.cs = CloudStack(**read_config())
|
||||||
except CloudStackException, e:
|
except CloudStackException as e:
|
||||||
print >> sys.stderr, "Error: Could not connect to CloudStack API"
|
print >> sys.stderr, "Error: Could not connect to CloudStack API"
|
||||||
|
|
||||||
project_id = ''
|
project_id = ''
|
||||||
|
|
|
@ -136,7 +136,7 @@ except ImportError:
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import consul
|
import consul
|
||||||
except ImportError, e:
|
except ImportError as e:
|
||||||
print """failed=True msg='python-consul required for this module. see
|
print """failed=True msg='python-consul required for this module. see
|
||||||
http://python-consul.readthedocs.org/en/latest/#installation'"""
|
http://python-consul.readthedocs.org/en/latest/#installation'"""
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
|
@ -145,7 +145,7 @@ except ImportError:
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from dopy.manager import DoError, DoManager
|
from dopy.manager import DoError, DoManager
|
||||||
except ImportError, e:
|
except ImportError as e:
|
||||||
print "failed=True msg='`dopy` library required for this script'"
|
print "failed=True msg='`dopy` library required for this script'"
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
|
@ -237,7 +237,7 @@ class GceInventory(object):
|
||||||
'''Gets details about a specific instance '''
|
'''Gets details about a specific instance '''
|
||||||
try:
|
try:
|
||||||
return self.driver.ex_get_node(instance_name)
|
return self.driver.ex_get_node(instance_name)
|
||||||
except Exception, e:
|
except Exception as e:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def group_instances(self):
|
def group_instances(self):
|
||||||
|
|
|
@ -101,7 +101,7 @@ except:
|
||||||
from chube.linode_obj import Linode
|
from chube.linode_obj import Linode
|
||||||
|
|
||||||
sys.path = old_path
|
sys.path = old_path
|
||||||
except Exception, e:
|
except Exception as e:
|
||||||
raise Exception("could not import chube")
|
raise Exception("could not import chube")
|
||||||
|
|
||||||
load_chube_config()
|
load_chube_config()
|
||||||
|
@ -184,7 +184,7 @@ class LinodeInventory(object):
|
||||||
try:
|
try:
|
||||||
for node in Linode.search(status=Linode.STATUS_RUNNING):
|
for node in Linode.search(status=Linode.STATUS_RUNNING):
|
||||||
self.add_node(node)
|
self.add_node(node)
|
||||||
except chube_api.linode_api.ApiError, e:
|
except chube_api.linode_api.ApiError as e:
|
||||||
print "Looks like Linode's API is down:"
|
print "Looks like Linode's API is down:"
|
||||||
print
|
print
|
||||||
print e
|
print e
|
||||||
|
@ -194,7 +194,7 @@ class LinodeInventory(object):
|
||||||
"""Gets details about a specific node."""
|
"""Gets details about a specific node."""
|
||||||
try:
|
try:
|
||||||
return Linode.find(api_id=linode_id)
|
return Linode.find(api_id=linode_id)
|
||||||
except chube_api.linode_api.ApiError, e:
|
except chube_api.linode_api.ApiError as e:
|
||||||
print "Looks like Linode's API is down:"
|
print "Looks like Linode's API is down:"
|
||||||
print
|
print
|
||||||
print e
|
print e
|
||||||
|
|
|
@ -412,7 +412,7 @@ def setup():
|
||||||
pyrax.keyring_auth(keyring_username, region=region)
|
pyrax.keyring_auth(keyring_username, region=region)
|
||||||
else:
|
else:
|
||||||
pyrax.set_credential_file(creds_file, region=region)
|
pyrax.set_credential_file(creds_file, region=region)
|
||||||
except Exception, e:
|
except Exception as e:
|
||||||
sys.stderr.write("%s: %s\n" % (e, e.message))
|
sys.stderr.write("%s: %s\n" % (e, e.message))
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
|
@ -132,7 +132,7 @@ try:
|
||||||
for group in spacewalk_report('system-groups'):
|
for group in spacewalk_report('system-groups'):
|
||||||
org_groups[group['spacewalk_group_id']] = group['spacewalk_org_id']
|
org_groups[group['spacewalk_group_id']] = group['spacewalk_org_id']
|
||||||
|
|
||||||
except (OSError), e:
|
except (OSError) as e:
|
||||||
print >> sys.stderr, 'Problem executing the command "%s system-groups": %s' % \
|
print >> sys.stderr, 'Problem executing the command "%s system-groups": %s' % \
|
||||||
(SW_REPORT, str(e))
|
(SW_REPORT, str(e))
|
||||||
sys.exit(2)
|
sys.exit(2)
|
||||||
|
@ -148,7 +148,7 @@ if options.list:
|
||||||
for item in spacewalk_report('inventory'):
|
for item in spacewalk_report('inventory'):
|
||||||
host_vars[ item['spacewalk_profile_name'] ] = dict( ( key, ( value.split(';') if ';' in value else value) ) for key, value in item.items() )
|
host_vars[ item['spacewalk_profile_name'] ] = dict( ( key, ( value.split(';') if ';' in value else value) ) for key, value in item.items() )
|
||||||
|
|
||||||
except (OSError), e:
|
except (OSError) as e:
|
||||||
print >> sys.stderr, 'Problem executing the command "%s inventory": %s' % \
|
print >> sys.stderr, 'Problem executing the command "%s inventory": %s' % \
|
||||||
(SW_REPORT, str(e))
|
(SW_REPORT, str(e))
|
||||||
sys.exit(2)
|
sys.exit(2)
|
||||||
|
@ -185,7 +185,7 @@ if options.list:
|
||||||
if system['spacewalk_server_name'] in host_vars and not system['spacewalk_server_name'] in meta[ "hostvars" ]:
|
if system['spacewalk_server_name'] in host_vars and not system['spacewalk_server_name'] in meta[ "hostvars" ]:
|
||||||
meta[ "hostvars" ][ system['spacewalk_server_name'] ] = host_vars[ system['spacewalk_server_name'] ]
|
meta[ "hostvars" ][ system['spacewalk_server_name'] ] = host_vars[ system['spacewalk_server_name'] ]
|
||||||
|
|
||||||
except (OSError), e:
|
except (OSError) as e:
|
||||||
print >> sys.stderr, 'Problem executing the command "%s system-groups-systems": %s' % \
|
print >> sys.stderr, 'Problem executing the command "%s system-groups-systems": %s' % \
|
||||||
(SW_REPORT, str(e))
|
(SW_REPORT, str(e))
|
||||||
sys.exit(2)
|
sys.exit(2)
|
||||||
|
@ -212,7 +212,7 @@ elif options.host:
|
||||||
host_details = system
|
host_details = system
|
||||||
break
|
break
|
||||||
|
|
||||||
except (OSError), e:
|
except (OSError) as e:
|
||||||
print >> sys.stderr, 'Problem executing the command "%s inventory": %s' % \
|
print >> sys.stderr, 'Problem executing the command "%s inventory": %s' % \
|
||||||
(SW_REPORT, str(e))
|
(SW_REPORT, str(e))
|
||||||
sys.exit(2)
|
sys.exit(2)
|
||||||
|
|
|
@ -164,7 +164,7 @@ class VMwareInventory(object):
|
||||||
obj_info = self._get_obj_info(val, depth - 1, seen)
|
obj_info = self._get_obj_info(val, depth - 1, seen)
|
||||||
if obj_info != ():
|
if obj_info != ():
|
||||||
d[attr] = obj_info
|
d[attr] = obj_info
|
||||||
except Exception, e:
|
except Exception as e:
|
||||||
pass
|
pass
|
||||||
return d
|
return d
|
||||||
elif isinstance(obj, SudsObject):
|
elif isinstance(obj, SudsObject):
|
||||||
|
@ -207,7 +207,7 @@ class VMwareInventory(object):
|
||||||
host_info[k] = v
|
host_info[k] = v
|
||||||
try:
|
try:
|
||||||
host_info['ipAddress'] = host.config.network.vnic[0].spec.ip.ipAddress
|
host_info['ipAddress'] = host.config.network.vnic[0].spec.ip.ipAddress
|
||||||
except Exception, e:
|
except Exception as e:
|
||||||
print >> sys.stderr, e
|
print >> sys.stderr, e
|
||||||
host_info = self._flatten_dict(host_info, prefix)
|
host_info = self._flatten_dict(host_info, prefix)
|
||||||
if ('%s_ipAddress' % prefix) in host_info:
|
if ('%s_ipAddress' % prefix) in host_info:
|
||||||
|
|
|
@ -109,7 +109,7 @@ class ZabbixInventory(object):
|
||||||
try:
|
try:
|
||||||
api = ZabbixAPI(server=self.zabbix_server)
|
api = ZabbixAPI(server=self.zabbix_server)
|
||||||
api.login(user=self.zabbix_username, password=self.zabbix_password)
|
api.login(user=self.zabbix_username, password=self.zabbix_password)
|
||||||
except BaseException, e:
|
except BaseException as e:
|
||||||
print >> sys.stderr, "Error: Could not login to Zabbix server. Check your zabbix.ini."
|
print >> sys.stderr, "Error: Could not login to Zabbix server. Check your zabbix.ini."
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
|
@ -175,5 +175,5 @@ if __name__ == '__main__':
|
||||||
filters = {"tag:Name":opts.match_re.replace('^',''), "instance-state-name": ['running', 'pending', 'stopped' ]}
|
filters = {"tag:Name":opts.match_re.replace('^',''), "instance-state-name": ['running', 'pending', 'stopped' ]}
|
||||||
delete_aws_instances(aws.get_all_instances(filters=filters), opts)
|
delete_aws_instances(aws.get_all_instances(filters=filters), opts)
|
||||||
|
|
||||||
except KeyboardInterrupt, e:
|
except KeyboardInterrupt as e:
|
||||||
print "\nExiting on user command."
|
print "\nExiting on user command."
|
||||||
|
|
|
@ -73,5 +73,5 @@ if __name__ == '__main__':
|
||||||
delete_gce_resources(get_snapshots, 'name', opts)
|
delete_gce_resources(get_snapshots, 'name', opts)
|
||||||
# Delete matching disks
|
# Delete matching disks
|
||||||
delete_gce_resources(gce.list_volumes, 'name', opts)
|
delete_gce_resources(gce.list_volumes, 'name', opts)
|
||||||
except KeyboardInterrupt, e:
|
except KeyboardInterrupt as e:
|
||||||
print "\nExiting on user command."
|
print "\nExiting on user command."
|
||||||
|
|
|
@ -20,7 +20,7 @@ else:
|
||||||
def createDaemon():
|
def createDaemon():
|
||||||
try:
|
try:
|
||||||
pid = os.fork()
|
pid = os.fork()
|
||||||
except OSError, e:
|
except OSError as e:
|
||||||
raise Exception, "%s [%d]" % (e.strerror, e.errno)
|
raise Exception, "%s [%d]" % (e.strerror, e.errno)
|
||||||
|
|
||||||
if (pid == 0):
|
if (pid == 0):
|
||||||
|
@ -28,7 +28,7 @@ def createDaemon():
|
||||||
|
|
||||||
try:
|
try:
|
||||||
pid = os.fork()
|
pid = os.fork()
|
||||||
except OSError, e:
|
except OSError as e:
|
||||||
raise Exception, "%s [%d]" % (e.strerror, e.errno)
|
raise Exception, "%s [%d]" % (e.strerror, e.errno)
|
||||||
|
|
||||||
if (pid == 0):
|
if (pid == 0):
|
||||||
|
|
|
@ -38,5 +38,5 @@ if __name__ == '__main__':
|
||||||
gce.create_volume_snapshot(base_volume, name=prefix+'-snapshot')
|
gce.create_volume_snapshot(base_volume, name=prefix+'-snapshot')
|
||||||
gce.create_volume(
|
gce.create_volume(
|
||||||
size=10, name=prefix+'-extra', location='us-central1-a')
|
size=10, name=prefix+'-extra', location='us-central1-a')
|
||||||
except KeyboardInterrupt, e:
|
except KeyboardInterrupt as e:
|
||||||
print "\nExiting on user command."
|
print "\nExiting on user command."
|
||||||
|
|
Loading…
Reference in a new issue