diff options
author | keyz182 <keyz182@gmail.com> | 2011-05-26 14:14:03 +0100 |
---|---|---|
committer | keyz182 <keyz182@gmail.com> | 2011-05-26 14:14:03 +0100 |
commit | 7a251d636671828a51374933463d336774fe8403 (patch) | |
tree | bc0a27d0d82ee026b83eb72f9c24de42871aba2d /cloudinit/DataSourceEc2.py | |
parent | 6bdaa810dd02c63b6c560c0283bf375716ff6ad0 (diff) | |
download | vyos-cloud-init-7a251d636671828a51374933463d336774fe8403.tar.gz vyos-cloud-init-7a251d636671828a51374933463d336774fe8403.zip |
Remove any unresolveable addresses before attempting to access metadata.
Diffstat (limited to 'cloudinit/DataSourceEc2.py')
-rw-r--r-- | cloudinit/DataSourceEc2.py | 28 |
1 files changed, 20 insertions, 8 deletions
diff --git a/cloudinit/DataSourceEc2.py b/cloudinit/DataSourceEc2.py index e90c29b7..942581bf 100644 --- a/cloudinit/DataSourceEc2.py +++ b/cloudinit/DataSourceEc2.py @@ -80,6 +80,12 @@ class DataSourceEc2(DataSource.DataSource): except: return fallback + def try_to_resolve_metadata(self,addresstup): + try: + socket.getaddrinfo(addresstup[0],addresstup[1]) + return True + except Exception as e: + return False def wait_for_metadata_service(self, sleeps = None): mcfg = self.ds_cfg @@ -103,7 +109,15 @@ class DataSourceEc2(DataSource.DataSource): sleeptime = 1 addresslist = [['169.254.169.254',80], ["instance-data",8773]] starttime = time.time() - + + # Remove addresses from the list that wont resolve. + addresslist[:] = [x for x in addresslist if try_to_resolve_metadata(x)] + + log.warning("Checking the following for metadata service:") + for addr in addresslist: + log.warning("\thttp://%s:%i/meta-data/instance-id" % (addr[0],addr[1])) + + for x in range(sleeps): for address in addresslist: host="http://%s:%i/" % (address[0],address[1]) @@ -112,10 +126,9 @@ class DataSourceEc2(DataSource.DataSource): # given 100 sleeps, this ends up total sleep time of 1050 sec sleeptime=int(x/5)+1 - reason = "" try: - socket.getaddrinfo(address[0],address[1]) + log.warning("Trying to access metadata service at %s" % url) req = urllib2.Request(url) resp = urllib2.urlopen(req, timeout=timeout) if resp.read() != "": @@ -127,11 +140,10 @@ class DataSourceEc2(DataSource.DataSource): reason = "http error [%s]" % e.code except urllib2.URLError as e: reason = "url error [%s]" % e.reason - except socket.gaierror as e: - reason = "url error [%s]" % e - - if x == 0: - log.warning("waiting for metadata service at %s\n" % url) + + #not needed? Addresses being checked are displayed above + #if x == 0: + # log.warning("waiting for metadata service at %s" % url) log.warning(" %s [%02s/%s]: %s\n" % (time.strftime("%H:%M:%S",time.gmtime()), x+1, sleeps, reason)) |