118
122
self._exchange.exchange()
125
def _get_data(self, path, accumulate):
127
Get data at C{path} on the EC2 API endpoint, and add the result to the
130
return self._fetch_async(EC2_API + path).addCallback(accumulate.append)
121
132
def _fetch_ec2_data(self):
133
"""Retrieve available EC2 information, if in a EC2 compatible cloud."""
122
134
id = self._identity
123
135
if self._cloud and not id.secure_id:
124
136
# Fetch data from the EC2 API, to be used later in the registration
126
registration_data = gather_results([
127
# We ignore errors from user-data because it's common for the
128
# URL to return a 404 when the data is unavailable.
129
self._fetch_async(EC2_API + "/user-data")
130
.addErrback(log_failure),
131
# The rest of the fetches don't get protected because we just
132
# fall back to regular registration if any of them don't work.
133
self._fetch_async(EC2_API + "/meta-data/instance-id"),
134
self._fetch_async(EC2_API + "/meta-data/reservation-id"),
135
self._fetch_async(EC2_API + "/meta-data/local-hostname"),
136
self._fetch_async(EC2_API + "/meta-data/public-hostname"),
137
self._fetch_async(EC2_API + "/meta-data/ami-launch-index"),
138
self._fetch_async(EC2_API + "/meta-data/kernel-id"),
139
self._fetch_async(EC2_API + "/meta-data/ramdisk-id"),
140
self._fetch_async(EC2_API + "/meta-data/ami-id"),
138
# We ignore errors from user-data because it's common for the
139
# URL to return a 404 when the data is unavailable.
141
deferred = self._fetch_async(EC2_API + "/user-data").addErrback(
142
log_failure).addCallback(ec2_data.append)
144
"/meta-data/instance-id",
145
"/meta-data/reservation-id",
146
"/meta-data/local-hostname",
147
"/meta-data/public-hostname",
148
"/meta-data/ami-launch-index",
149
"/meta-data/kernel-id",
150
"/meta-data/ramdisk-id",
152
# We're not using a DeferredList here because we want to keep the
153
# number of connections to the backend minimal. See lp:567515.
155
deferred.addCallback(
156
lambda ignore, path=path: self._get_data(path, ec2_data))
144
def record_data(ec2_data):
158
def record_data(ignore):
145
159
"""Record the instance data returned by the EC2 API."""
146
160
(raw_user_data, instance_key, reservation_key,
147
161
local_hostname, public_hostname, launch_index,
176
190
log_failure(error, msg="Got error while fetching meta-data: %r"
177
191
% (error.value,))
179
# It sucks that this deferred is never returned
180
registration_data.addCallback(record_data)
181
registration_data.addErrback(log_error)
193
deferred.addCallback(record_data)
194
deferred.addErrback(log_error)
183
196
def _handle_exchange_done(self):
184
197
"""Registered handler for the C{"exchange-done"} event.
226
239
self._exchange.send(message)
227
240
elif id.account_name:
228
241
with_tags = ["", u"and tags %s " % tags][bool(tags)]
229
logging.info(u"Queueing message to register with account %r %s"
230
"as an EC2 instance." % (
231
id.account_name, with_tags,))
243
u"Queueing message to register with account %r %s"
244
u"as an EC2 instance." % (id.account_name, with_tags))
232
245
message = {"type": "register-cloud-vm",
234
247
"hostname": socket.getfqdn(),