# vi: ts=4 expandtab # # Copyright (C) 2009-2010 Canonical Ltd. # Copyright (C) 2012 Hewlett-Packard Development Company, L.P. # # Author: Scott Moser # Author: Juerg Hafliger # Author: Cosmin Luta # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License version 3, as # published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . import cloudinit.DataSource as DataSource from cloudinit import seeddir as base_seeddir from cloudinit import log import cloudinit.util as util import socket import urllib2 import time import boto.utils as boto_utils from struct import pack class DataSourceCS(DataSource.DataSource): api_ver = 'latest' seeddir = base_seeddir + '/cs' metadata_address = None def __init__(self, sys_cfg=None): DataSource.DataSource.__init__(self, sys_cfg) # Cloudstack has its metadata/userdata URLs located on http:///latest/ self.metadata_address = "http://" + self._get_default_gateway() + "/" def _get_default_gateway(self): f = None try: f = open("/proc/net/route", "r") 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 socket.inet_ntoa(pack(" max_wait)) loop_n = 0 while True: sleeptime = int(loop_n / 5) + 1 for url in urls: now = time.time() if loop_n != 0: if timeup(max_wait, starttime): break if timeout and (now + timeout > (starttime + max_wait)): # shorten timeout to not run way over max_time timeout = int((starttime + max_wait) - now) try: req = urllib2.Request(url) resp = urllib2.urlopen(req, timeout=timeout) if resp.read() != "": return url reason = "empty data [%s]" % resp.getcode() except urllib2.HTTPError as e: reason = "http error [%s]" % e.code except urllib2.URLError as e: reason = "url error [%s]" % e.reason except socket.timeout as e: reason = "socket timeout [%s]" % e except Exception as e: reason = "unexpected error [%s]" % e if log: status_cb("'%s' failed [%s/%ss]: %s" % (url, int(time.time() - starttime), max_wait, reason)) if timeup(max_wait, starttime): break loop_n += 1 time.sleep(sleeptime) return False datasources = [ (DataSourceCS, (DataSource.DEP_FILESYSTEM, DataSource.DEP_NETWORK)), ] # return a list of data sources that match this set of dependencies def get_datasource_list(depends): return DataSource.list_from_depends(depends, datasources)