From 15648e327ad19c8332ab770698643c0e0335bc28 Mon Sep 17 00:00:00 2001 From: Cosmin Luță Date: Wed, 25 Jan 2012 13:30:55 +0200 Subject: Added support for CloudStack meta-data --- cloudinit/DataSourceCS.py | 238 ++++++++++++++++++++++++++++++++++++++++++++++ config/cloud.cfg | 2 +- 2 files changed, 239 insertions(+), 1 deletion(-) create mode 100644 cloudinit/DataSourceCS.py diff --git a/cloudinit/DataSourceCS.py b/cloudinit/DataSourceCS.py new file mode 100644 index 00000000..f70a17a7 --- /dev/null +++ b/cloudinit/DataSourceCS.py @@ -0,0 +1,238 @@ +# 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) diff --git a/config/cloud.cfg b/config/cloud.cfg index 25d02cee..81085d68 100644 --- a/config/cloud.cfg +++ b/config/cloud.cfg @@ -1,7 +1,7 @@ user: ubuntu disable_root: 1 preserve_hostname: False -# datasource_list: [ "NoCloud", "OVF", "Ec2" ] +# datasource_list: [ "NoCloud", "OVF", "Ec2", "CS" ] cloud_init_modules: - bootcmd -- cgit v1.2.3