28
28
from cloudinit import log as logging
29
29
from cloudinit import sources
30
from cloudinit import url_helper as uhelp
30
from cloudinit import url_helper
31
31
from cloudinit import util
33
33
LOG = logging.getLogger(__name__)
88
89
util.logexc(LOG, "Failed fetching metadata from url %s", url)
91
def md_headers(self, url):
92
def _md_headers(self, url):
94
95
# If we are missing token_key, token_secret or consumer_key
132
133
starttime = time.time()
133
134
check_url = "%s/%s/meta-data/instance-id" % (url, MD_VERSION)
134
135
urls = [check_url]
135
url = uhelp.wait_for_url(urls=urls, max_wait=max_wait,
136
timeout=timeout, exception_cb=self._except_cb,
137
headers_cb=self.md_headers)
136
url = url_helper.wait_for_url(urls=urls, max_wait=max_wait,
138
exception_cb=self._except_cb,
139
headers_cb=self._md_headers)
140
142
LOG.debug("Using metadata source: '%s'", url)
142
144
LOG.critical("Giving up on md from %s after %i seconds",
143
urls, int(time.time() - starttime))
145
urls, int(time.time() - starttime))
147
149
def _except_cb(self, msg, exception):
148
if not (isinstance(exception, urllib2.HTTPError) and
150
if not (isinstance(exception, url_helper.UrlError) and
149
151
(exception.code == 403 or exception.code == 401)):
151
154
if 'date' not in exception.headers:
152
LOG.warn("date field not in %d headers" % exception.code)
155
LOG.warn("Missing header 'date' in %s response", exception.code)
155
158
date = exception.headers['date']
158
160
ret_time = time.mktime(parsedate(date))
160
LOG.warn("failed to convert datetime '%s'")
161
except Exception as e:
162
LOG.warn("Failed to convert datetime '%s': %s", date, e)
163
165
self.oauth_clockskew = int(ret_time - time.time())
164
LOG.warn("set oauth clockskew to %d" % self.oauth_clockskew)
166
LOG.warn("Setting oauth clockskew to %d", self.oauth_clockskew)
191
193
def read_maas_seed_url(seed_url, header_cb=None, timeout=None,
194
version=MD_VERSION, paths=None):
194
196
Read the maas datasource at seed_url.
195
header_cb is a method that should return a headers dictionary that will
196
be given to urllib2.Request()
197
- header_cb is a method that should return a headers dictionary for
198
200
Expected format of seed_url is are the following files:
199
201
* <seed_url>/<version>/meta-data/instance-id
224
resp = uhelp.readurl(url, headers=headers, timeout=timeout)
226
ssl_details = util.fetch_ssl_details(paths)
227
resp = util.read_file_or_url(url,
230
ssl_details=ssl_details)
226
232
md[name] = str(resp)
228
234
LOG.warn(("Fetching from %s resulted in"
229
235
" an invalid http code %s"), url, resp.code)
230
except urllib2.HTTPError as e:
236
except url_helper.UrlError as e:
231
237
if e.code != 404:
233
239
return check_seed_contents(md, seed_url)
370
376
if args.subcmd == "check-seed":
371
377
if args.url.startswith("http"):
372
378
(userdata, metadata) = read_maas_seed_url(args.url,
373
header_cb=my_headers, version=args.apiver)
379
header_cb=my_headers,
375
382
(userdata, metadata) = read_maas_seed_url(args.url)
376
383
print "=== userdata ==="