diff options
Diffstat (limited to 'cloudinit')
-rw-r--r-- | cloudinit/DataSourceCS.py | 20 |
1 files changed, 7 insertions, 13 deletions
diff --git a/cloudinit/DataSourceCS.py b/cloudinit/DataSourceCS.py index cf56ebe6..d31d0445 100644 --- a/cloudinit/DataSourceCS.py +++ b/cloudinit/DataSourceCS.py @@ -40,21 +40,16 @@ class DataSourceCS(DataSource.DataSource): self.metadata_address = "http://%s/" % self.get_default_gateway() def get_default_gateway(self): - f = None - try: - f = open("/proc/net/route", "r") + """ Returns the default gateway ip address in the dotted format + """ + with open("/proc/net/route", "r") as f: for line in f.readlines(): items = line.split("\t") if items[1] == "00000000": # found the default route, get the gateway - gw = int(items[2], 16) - log.debug("found default route, gateway %s" % items[2]) - return inet_ntoa(pack("<L", gw)) - f.close() - except: - if f is not None: - f.close() - return "localhost" + gw = inet_ntoa(pack("<L", int(items[2], 16))) + log.debug("found default route, gateway is %s" % gw) + return gw def __str__(self): return "DataSourceCS" @@ -73,8 +68,7 @@ class DataSourceCS(DataSource.DataSource): None, self.metadata_address) self.metadata = boto_utils.get_instance_metadata(self.api_ver, self.metadata_address) - log.debug("crawl of metadata service took %ds" % (time.time() - - start)) + log.debug("crawl of metadata service took %ds" % (time.time() - start)) return True except Exception as e: log.exception(e) |