daily pastebin goal
46%
SHARE
TWEET

Untitled

a guest Apr 25th, 2016 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from charms.reactive import hook  
  2. from charms.reactive import RelationBase  
  3. from charms.reactive import scopes  
  4.  
  5. class HttpRequires(RelationBase):  
  6.     scope = scopes.UNIT  
  7.  
  8.     @hook('dcosmaster-relation-{joined,changed}')  
  9.     def changed(self):  
  10.         conv = self.conversation()  
  11.         onv.set_state('{relation_name}.available')  
  12.  
  13.     @hook('dcosmaster-relation-{departed,broken}')  
  14.     def broken(self):  
  15.         conv = self.conversation()  
  16.         conv.remove_state('{relation_name}.available')  
  17.  
  18.     def services(self):  
  19.         """  
  20.         Returns a list of available HTTP services and their associated hosts  
  21.         and ports.  
  22.         The return value is a list of dicts of the following form::  
  23.             [  
  24.                 {  
  25.                     'service_name': name_of_service,  
  26.                     'hosts': [  
  27.                         {                              
  28.                             'hostname': address_of_host,
  29.                             'port': port_for_host,
  30.                         },
  31.                         # ...
  32.                     ],
  33.                 },
  34.                 # ...
  35.             ]
  36.         """
  37.         services = {}
  38.         for conv in self.conversations():
  39.             service_name = conv.scope.split('/')[0]
  40.             service = services.setdefault(service_name, {
  41.                 'service_name': service_name,
  42.                 'hosts': [],
  43.             })
  44.             host = conv.get_remote('hostname') or conv.get_remote('private-address')
  45.             port = conv.get_remote('port')
  46.             if host and port:
  47.                 service['hosts'].append({
  48.                     'hostname': host,
  49.                     'port': port,
  50.                 })
  51.         return [s for s in services.values() if s['hosts']]
RAW Paste Data
Top