~free.ekanayaka/landscape-client/lucid-1.5.4-0ubuntu0.10.04.0

« back to all changes in this revision

Viewing changes to landscape/broker/registration.py

  • Committer: Bazaar Package Importer
  • Author(s): Free Ekanayaka
  • Date: 2010-04-21 12:31:28 UTC
  • mfrom: (1.1.15 upstream)
  • Revision ID: james.westby@ubuntu.com-20100421123128-2o3a7yob88nilkls
Tags: 1.5.0.1-0ubuntu0.10.04.0
* New upstream version
  - Fix smart-update failing its very first run (LP: #562496)
  - Depend on pythonX.Y-dbus and pythonX.Y-pycurl (LP: #563063)
  - Make only one request at a time to retrieve EC2 instances (LP: #567515)

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 
6
6
from twisted.internet.defer import Deferred
7
7
 
8
 
from landscape.lib.twisted_util import gather_results
9
8
from landscape.lib.bpickle import loads
10
9
from landscape.lib.log import log_failure
11
10
from landscape.lib.fetch import fetch, FetchError
24
23
 
25
24
 
26
25
def persist_property(name):
 
26
 
27
27
    def get(self):
28
28
        return self._persist.get(name)
 
29
 
29
30
    def set(self, value):
30
31
        self._persist.set(name, value)
 
32
 
31
33
    return property(get, set)
32
34
 
33
35
 
34
36
def config_property(name):
 
37
 
35
38
    def get(self):
36
39
        return getattr(self._config, name)
 
40
 
37
41
    return property(get)
38
42
 
39
43
 
118
122
        self._exchange.exchange()
119
123
        return result
120
124
 
 
125
    def _get_data(self, path, accumulate):
 
126
        """
 
127
        Get data at C{path} on the EC2 API endpoint, and add the result to the
 
128
        C{accumulate} list.
 
129
        """
 
130
        return self._fetch_async(EC2_API + path).addCallback(accumulate.append)
 
131
 
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
125
137
            # process
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"),
141
 
                ],
142
 
                consume_errors=True)
 
138
            # We ignore errors from user-data because it's common for the
 
139
            # URL to return a 404 when the data is unavailable.
 
140
            ec2_data = []
 
141
            deferred = self._fetch_async(EC2_API + "/user-data").addErrback(
 
142
                log_failure).addCallback(ec2_data.append)
 
143
            paths = [
 
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",
 
151
                "/meta-data/ami-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.
 
154
            for path in paths:
 
155
                deferred.addCallback(
 
156
                    lambda ignore, path=path: self._get_data(path, ec2_data))
143
157
 
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,))
178
192
 
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)
182
195
 
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,))
 
242
                    logging.info(
 
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",
233
246
                               "otp": None,
234
247
                               "hostname": socket.getfqdn(),
365
378
            time.sleep(1)
366
379
            if time.time() - start > timeout:
367
380
                break
368
 
                
 
381
 
369
382
 
370
383
def is_cloud_managed(fetch=fetch):
371
384
    """
380
393
                             connect_timeout=5)
381
394
    except FetchError:
382
395
        return False
383
 
    instance_data = _extract_ec2_instance_data(raw_user_data, int(launch_index))
 
396
    instance_data = _extract_ec2_instance_data(
 
397
        raw_user_data, int(launch_index))
384
398
    return instance_data is not None