~ubuntu-branches/ubuntu/raring/cloud-init/raring-proposed

« back to all changes in this revision

Viewing changes to cloudinit/sources/DataSourceMAAS.py

  • Committer: Package Import Robot
  • Author(s): Scott Moser
  • Date: 2013-03-27 10:04:41 UTC
  • mfrom: (247.1.6 raring)
  • Revision ID: package-import@ubuntu.com-20130327100441-m8pamjvx9na1gdgl
Tags: 0.7.2~bzr804-0ubuntu1
* New upstream snapshot.
  * use python-requests rather than urllib2 for http (LP: #1067888)
  * handle failure of resizefs better.  Specifically, do not show
    warnings or stack trace in lxc (LP: #1160462)

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
 
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
32
32
 
33
33
LOG = logging.getLogger(__name__)
80
80
            self.base_url = url
81
81
 
82
82
            (userdata, metadata) = read_maas_seed_url(self.base_url,
83
 
                                                      self.md_headers)
 
83
                                                      self._md_headers,
 
84
                                                      paths=self.paths)
84
85
            self.userdata_raw = userdata
85
86
            self.metadata = metadata
86
87
            return True
88
89
            util.logexc(LOG, "Failed fetching metadata from url %s", url)
89
90
            return False
90
91
 
91
 
    def md_headers(self, url):
 
92
    def _md_headers(self, url):
92
93
        mcfg = self.ds_cfg
93
94
 
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,
 
137
                                      timeout=timeout,
 
138
                                      exception_cb=self._except_cb,
 
139
                                      headers_cb=self._md_headers)
138
140
 
139
141
        if url:
140
142
            LOG.debug("Using metadata source: '%s'", url)
141
143
        else:
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))
144
146
 
145
147
        return bool(url)
146
148
 
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)):
150
152
            return
 
153
 
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)
153
156
            return
154
157
 
155
158
        date = exception.headers['date']
156
 
 
157
159
        try:
158
160
            ret_time = time.mktime(parsedate(date))
159
 
        except:
160
 
            LOG.warn("failed to convert datetime '%s'")
 
161
        except Exception as e:
 
162
            LOG.warn("Failed to convert datetime '%s': %s", date, e)
161
163
            return
162
164
 
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)
165
167
        return
166
168
 
167
169
 
189
191
 
190
192
 
191
193
def read_maas_seed_url(seed_url, header_cb=None, timeout=None,
192
 
    version=MD_VERSION):
 
194
                       version=MD_VERSION, paths=None):
193
195
    """
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
        a given url
197
199
 
198
200
    Expected format of seed_url is are the following files:
199
201
      * <seed_url>/<version>/meta-data/instance-id
221
223
        else:
222
224
            headers = {}
223
225
        try:
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,
 
228
                                         headers=headers,
 
229
                                         timeout=timeout,
 
230
                                         ssl_details=ssl_details)
225
231
            if resp.ok():
226
232
                md[name] = str(resp)
227
233
            else:
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:
232
238
                raise
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,
 
380
                                                          version=args.apiver)
374
381
            else:
375
382
                (userdata, metadata) = read_maas_seed_url(args.url)
376
383
            print "=== userdata ==="