9
class DataSourceEc2(DataSource.DataSource):
10
api_ver = '2009-04-04'
11
cachedir = cloudinit.cachedir + '/ec2'
13
location_locale_map = {
16
'default' : 'en_US.UTF-8',
24
udf = open(self.cachedir + "/user-data.raw")
25
self.userdata_raw = udf.read()
28
mdf = open(self.cachedir + "/meta-data.raw")
30
self.metadata = eval(data)
38
if not self.wait_for_metadata_service():
40
self.userdata_raw = boto_utils.get_instance_userdata(self.api_ver)
41
self.metadata = boto_utils.get_instance_metadata(self.api_ver)
43
except Exception as e:
47
def get_instance_id(self):
48
return(self.metadata['instance-id'])
50
def get_availability_zone(self):
51
return(self.metadata['placement']['availability-zone'])
53
def get_local_mirror(self):
54
return(self.get_mirror_from_availability_zone())
57
az = self.metadata['placement']['availability-zone']
58
if self.location_locale_map.has_key(az[0:2]):
59
return(self.location_locale_map[az[0:2]])
61
return(self.location_locale_map["default"])
63
def get_hostname(self):
64
toks = self.metadata['local-hostname'].split('.')
65
# if there is an ipv4 address in 'local-hostname', then
66
# make up a hostname (LP: #475354)
69
r = filter(lambda x: int(x) < 256 and x > 0, toks)
71
return("ip-%s" % '-'.join(r))
75
def get_mirror_from_availability_zone(self, availability_zone = None):
76
# availability is like 'us-west-1b' or 'eu-west-1a'
77
if availability_zone == None:
78
availability_zone = self.get_availability_zone()
81
host="%s.ec2.archive.ubuntu.com" % availability_zone[:-1]
82
socket.getaddrinfo(host, None, 0, socket.SOCK_STREAM)
83
return 'http://%s/ubuntu/' % host
85
return 'http://archive.ubuntu.com/ubuntu/'
87
def wait_for_metadata_service(self, sleeps = 10):
89
for x in range(sleeps):
92
address = '169.254.169.254'
94
s.connect((address,port))
97
except socket.error, e:
98
print "sleeping %s" % sleeptime
100
#timeout = timeout * 2
103
def get_public_ssh_keys(self):
105
if not self.metadata.has_key('public-keys'): return([])
106
for keyname, klist in self.metadata['public-keys'].items():
107
# lp:506332 uec metadata service responds with
108
# data that makes boto populate a string for 'klist' rather
110
if isinstance(klist,str):
113
# there is an empty string at the end of the keylist, trim it
119
def device_name_to_device(self, name):
120
# consult metadata service, that has
122
# and return 'sdb' for input 'ephemeral0'
123
if not self.metadata.has_key('block-device-mapping'):
126
for entname, device in self.metadata['block-device-mapping'].items():
129
# LP: #513842 mapping in Euca has 'ephemeral' not 'ephemeral0'
130
if entname == "ephemeral" and name == "ephemeral0":