~gary/charms/precise/juju-gui/authtoken1

« back to all changes in this revision

Viewing changes to server/guiserver/bundles/utils.py

  • Committer: Brad Crittenden
  • Date: 2013-11-15 18:51:28 UTC
  • mfrom: (125.1.15 increment-deployments)
  • Revision ID: bac@canonical.com-20131115185128-rphg06r069p5tnzb
After deploying a bundle increment counter.

The guiserver will make a GET request to the deployment counter incrementer
URL for the bundle.  This required accepting the bundle ID in the
Deployer/Import path.

Note neither the GUI nor quickstart have been updated to pass this value yet.

The charmworldurl is also now passed to the server via the command line.

QA: just deploy your favorite bundle and see that it works as it should.  You
can observe the bundle page in charmworld and see the counts remain at 0.

R=frankban
CC=
https://codereview.appspot.com/26740043

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
import itertools
22
22
import logging
23
23
import time
 
24
import urllib
24
25
 
25
26
from tornado import (
26
27
    gen,
27
28
    escape,
28
29
)
 
30
from tornado.httpclient import AsyncHTTPClient
29
31
 
30
32
from guiserver.watchers import AsyncWatcher
31
33
 
204
206
        logging.error('deployer: {}'.format(escape.utf8(error)))
205
207
        data['Error'] = error
206
208
    return gen.Return(data)
 
209
 
 
210
 
 
211
@gen.coroutine
 
212
def increment_deployment_counter(bundle_id, charmworld_url):
 
213
    """Increment the deployment count in Charmworld.
 
214
 
 
215
    If the call to Charmworld fails we log the error but don't report it.
 
216
    This counter is a 'best effort' attempt but it will not impede our
 
217
    deployment of the bundle.
 
218
 
 
219
    Arguments are:
 
220
          - bundle_id: the ID for the bundle in Charmworld.
 
221
          - charmworld_url: the URL for charmworld, including the protocol.
 
222
            If None, do nothing.
 
223
 
 
224
    Returns True if the counter is successfully incremented else False.
 
225
    """
 
226
    if charmworld_url is None:
 
227
        raise gen.Return(False)
 
228
 
 
229
    if not all((isinstance(bundle_id, basestring),
 
230
                isinstance(charmworld_url, basestring))):
 
231
        raise gen.Return(False)
 
232
 
 
233
    path = 'metric/deployments/increment'
 
234
    url = u'{}api/3/bundle/{}/{}'.format(
 
235
        charmworld_url,
 
236
        urllib.quote(bundle_id), path)
 
237
    logging.info('Incrementing bundle deployment count using\n{}.'.format(
 
238
        url.encode('utf-8')))
 
239
    client = AsyncHTTPClient()
 
240
    # We use a GET instead of a POST since there is not request body.
 
241
    try:
 
242
        resp = yield client.fetch(url, callback=None)
 
243
    except Exception as exc:
 
244
        logging.error('Attempt to increment deployment counter failed.')
 
245
        logging.error('URL: {}'.format(url))
 
246
        logging.exception(exc)
 
247
        raise gen.Return(False)
 
248
    success = bool(resp.code == 200)
 
249
    raise gen.Return(success)