def device_name_to_device(self, name): # consult metadata service, that has # ephemeral0: sdb # and return 'sdb' for input 'ephemeral0' if "block-device-mapping" not in self.metadata: return None found = None for entname, device in self.metadata["block-device-mapping"].items(): if entname == name: found = device break # LP: #513842 mapping in Euca has 'ephemeral' not 'ephemeral0' if entname == "ephemeral" and name == "ephemeral0": found = device if found == None: log.debug("unable to convert %s to a device" % name) return None # LP: #611137 # the metadata service may believe that devices are named 'sda' # when the kernel named them 'vda' or 'xvda' # we want to return the correct value for what will actually # exist in this instance mappings = {"sd": ("vd", "xvd")} ofound = found short = os.path.basename(found) if not found.startswith("/"): found = "/dev/%s" % found if os.path.exists(found): return found for nfrom, tlist in mappings.items(): if not short.startswith(nfrom): continue for nto in tlist: cand = "/dev/%s%s" % (nto, short[len(nfrom) :]) if os.path.exists(cand): log.debug("remapped device name %s => %s" % (found, cand)) return cand # on t1.micro, ephemeral0 will appear in block-device-mapping from # metadata, but it will not exist on disk (and never will) # at this pint, we've verified that the path did not exist # in the special case of 'ephemeral0' return None to avoid bogus # fstab entry (LP: #744019) if name == "ephemeral0": return None return ofound
def get_data(self): try: if not self.wait_for_metadata_service(): return False start = time.time() log.info("Calling into metadata service using boto at: %s", self.metadata_address) self.userdata_raw = boto_utils.get_instance_userdata(self.api_ver, None, self.metadata_address) self.metadata = boto_utils.get_instance_metadata(self.api_ver, self.metadata_address) log.debug("Crawl of metadata service took %s seconds" % (time.time() - start)) log.debug("Received raw userdata: %s", self.userdata_raw) log.debug("Received metadata: %s", self.metadata) return True except Exception: return False
def wait_for_metadata_service(self): mcfg = self.ds_cfg if not hasattr(mcfg, "get"): mcfg = {} max_wait = 120 try: max_wait = int(mcfg.get("max_wait", max_wait)) except Exception: util.logexc(log) log.warn("Failed to get max wait. using %s" % max_wait) if max_wait == 0: return False timeout = 50 try: timeout = int(mcfg.get("timeout", timeout)) except Exception: util.logexc(log) log.warn("Failed to get timeout, using %s" % timeout) def_mdurls = ["http://169.254.169.254", "http://instance-data:8773"] mdurls = mcfg.get("metadata_urls", def_mdurls) # Remove addresses from the list that wont resolve. filtered = [x for x in mdurls if util.is_resolvable_url(x)] if set(filtered) != set(mdurls): log.debug( "Removed the following unresolveable addrs from metadata urls: %s" % list((set(mdurls) - set(filtered))) ) if len(filtered): mdurls = filtered else: log.warn("Empty metadata url list! using default list %s", def_mdurls) mdurls = def_mdurls urls = [] url2base = {} for url in mdurls: cur = "%s/%s/meta-data/instance-id" % (url, self.api_ver) urls.append(cur) url2base[cur] = url starttime = time.time() def status_cb(url, why, details): log.warn("Calling %r failed due to %r: %s", url, why, details) url = wait_for_metadata_service(urls=urls, max_wait=max_wait, timeout=timeout, status_cb=status_cb) if url: log.info("Using metadata source: '%s'" % url2base.get(url)) else: log.critical("Giving up on metadata fetching after %i seconds" % int(time.time() - starttime)) self.metadata_address = url2base.get(url) or False return bool(url)