|
24 | 24 | from cloudinit import log |
25 | 25 | import cloudinit.util as util |
26 | 26 | import socket |
27 | | -import urllib2 |
28 | 27 | import time |
29 | 28 | import boto.utils as boto_utils |
30 | 29 | import os.path |
@@ -134,8 +133,8 @@ def wait_for_metadata_service(self): |
134 | 133 | url2base[cur] = url |
135 | 134 |
|
136 | 135 | starttime = time.time() |
137 | | - url = wait_for_metadata_service(urls=urls, max_wait=max_wait, |
138 | | - timeout=timeout, status_cb=log.warn) |
| 136 | + url = util.wait_for_url(urls=urls, max_wait=max_wait, |
| 137 | + timeout=timeout, status_cb=log.warn) |
139 | 138 |
|
140 | 139 | if url: |
141 | 140 | log.debug("Using metadata source: '%s'" % url2base[url]) |
@@ -208,87 +207,6 @@ def is_vpc(self): |
208 | 207 | return False |
209 | 208 |
|
210 | 209 |
|
211 | | -def wait_for_metadata_service(urls, max_wait=None, timeout=None, |
212 | | - status_cb=None): |
213 | | - """ |
214 | | - urls: a list of urls to try |
215 | | - max_wait: roughly the maximum time to wait before giving up |
216 | | - The max time is *actually* len(urls)*timeout as each url will |
217 | | - be tried once and given the timeout provided. |
218 | | - timeout: the timeout provided to urllib2.urlopen |
219 | | - status_cb: call method with string message when a url is not available |
220 | | -
|
221 | | - the idea of this routine is to wait for the EC2 metdata service to |
222 | | - come up. On both Eucalyptus and EC2 we have seen the case where |
223 | | - the instance hit the MD before the MD service was up. EC2 seems |
224 | | - to have permenantely fixed this, though. |
225 | | -
|
226 | | - In openstack, the metadata service might be painfully slow, and |
227 | | - unable to avoid hitting a timeout of even up to 10 seconds or more |
228 | | - (LP: #894279) for a simple GET. |
229 | | -
|
230 | | - Offset those needs with the need to not hang forever (and block boot) |
231 | | - on a system where cloud-init is configured to look for EC2 Metadata |
232 | | - service but is not going to find one. It is possible that the instance |
233 | | - data host (169.254.169.254) may be firewalled off Entirely for a sytem, |
234 | | - meaning that the connection will block forever unless a timeout is set. |
235 | | - """ |
236 | | - starttime = time.time() |
237 | | - |
238 | | - sleeptime = 1 |
239 | | - |
240 | | - def nullstatus_cb(msg): |
241 | | - return |
242 | | - |
243 | | - if status_cb == None: |
244 | | - status_cb = nullstatus_cb |
245 | | - |
246 | | - def timeup(max_wait, starttime): |
247 | | - return((max_wait <= 0 or max_wait == None) or |
248 | | - (time.time() - starttime > max_wait)) |
249 | | - |
250 | | - loop_n = 0 |
251 | | - while True: |
252 | | - sleeptime = int(loop_n / 5) + 1 |
253 | | - for url in urls: |
254 | | - now = time.time() |
255 | | - if loop_n != 0: |
256 | | - if timeup(max_wait, starttime): |
257 | | - break |
258 | | - if timeout and (now + timeout > (starttime + max_wait)): |
259 | | - # shorten timeout to not run way over max_time |
260 | | - timeout = int((starttime + max_wait) - now) |
261 | | - |
262 | | - reason = "" |
263 | | - try: |
264 | | - req = urllib2.Request(url) |
265 | | - resp = urllib2.urlopen(req, timeout=timeout) |
266 | | - if resp.read() != "": |
267 | | - return url |
268 | | - reason = "empty data [%s]" % resp.getcode() |
269 | | - except urllib2.HTTPError as e: |
270 | | - reason = "http error [%s]" % e.code |
271 | | - except urllib2.URLError as e: |
272 | | - reason = "url error [%s]" % e.reason |
273 | | - except socket.timeout as e: |
274 | | - reason = "socket timeout [%s]" % e |
275 | | - except Exception as e: |
276 | | - reason = "unexpected error [%s]" % e |
277 | | - |
278 | | - if log: |
279 | | - status_cb("'%s' failed [%s/%ss]: %s" % |
280 | | - (url, int(time.time() - starttime), max_wait, |
281 | | - reason)) |
282 | | - |
283 | | - if timeup(max_wait, starttime): |
284 | | - break |
285 | | - |
286 | | - loop_n = loop_n + 1 |
287 | | - time.sleep(sleeptime) |
288 | | - |
289 | | - return False |
290 | | - |
291 | | - |
292 | 210 | datasources = [ |
293 | 211 | (DataSourceEc2, (DataSource.DEP_FILESYSTEM, DataSource.DEP_NETWORK)), |
294 | 212 | ] |
|
0 commit comments