Advertisement
Guest User

Untitled

a guest
Aug 1st, 2015
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. So this thing:
  2.  
  3. ```python
  4. class BotoConnection(object):
  5.  
  6. def __new__(cls, profile_name, services):
  7. if not hasattr(cls, '__instance__'):
  8. cls.__instance__ = super(BotoConnection, cls)\
  9. .__new__(cls, profile_name, services)
  10.  
  11. return cls.__instance__
  12.  
  13. def __init__(self, profile_name, services):
  14. self.__connect_to_boto_services(profile_name, services)
  15.  
  16. def __connect_to_boto_services(self, profile_name, services):
  17. for service_name in services:
  18. attribute_name = service_name.split('.')[-1]
  19.  
  20. if hasattr(self, attribute_name):
  21. raise Exception("'%s' service connection already exists. Did "
  22. "you define the same service twice, or have "
  23. "two services with the same module "
  24. "name?" % attribute_name)
  25.  
  26. service = import_module(service_name)
  27. connection = service.connect_to_region(
  28. env.region, profile_name=env.profile_name)
  29. setattr(self, attribute_name, connection)
  30. ```
  31.  
  32. Allows me to go:
  33.  
  34. ```python
  35. # Boto connections
  36. services = [
  37. 'boto.ec2',
  38. 'boto.ec2.elb',
  39. 'boto.ec2.autoscale',
  40. 'boto.ec2.cloudwatch',
  41. 'boto.route53',
  42. 'boto.s3']
  43. env.connections = utils.BotoConnection(
  44. profile_name=env.profile_name, services=services)
  45. ```
  46.  
  47. In my `fabconfig.py` and then:
  48.  
  49. ```python
  50. reservations = env.connections.ec2.get_all_instances(
  51. instance_ids=[instance_id])
  52. ```
  53.  
  54. In my build modules.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement