types.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. """
  2. DataTypes used by this provider
  3. """
  4. from cloudbridge.providers.base import BaseInstance
  5. from cloudbridge.providers.base import BaseKeyPair
  6. from cloudbridge.providers.base import BaseMachineImage
  7. from cloudbridge.providers.base import BaseSecurityGroup
  8. from cloudbridge.providers.interfaces import InstanceState
  9. from cloudbridge.providers.interfaces import InstanceType
  10. from cloudbridge.providers.interfaces import MachineImageState
  11. from cloudbridge.providers.interfaces import Region
  12. class OpenStackMachineImage(BaseMachineImage):
  13. # ref: http://docs.openstack.org/developer/glance/statuses.html
  14. IMAGE_STATE_MAP = {
  15. 'QUEUED': MachineImageState.PENDING,
  16. 'SAVING': MachineImageState.PENDING,
  17. 'ACTIVE': MachineImageState.AVAILABLE,
  18. 'KILLED': MachineImageState.ERROR,
  19. 'DELETED': MachineImageState.ERROR,
  20. 'PENDING_DELETE': MachineImageState.ERROR
  21. }
  22. def __init__(self, provider, os_image):
  23. self.provider = provider
  24. if isinstance(os_image, OpenStackMachineImage):
  25. self._os_image = os_image._os_image
  26. else:
  27. self._os_image = os_image
  28. @property
  29. def image_id(self):
  30. """
  31. Get the image identifier.
  32. :rtype: ``str``
  33. :return: ID for this instance as returned by the cloud middleware.
  34. """
  35. return self._os_image.id
  36. @property
  37. def name(self):
  38. """
  39. Get the image name.
  40. :rtype: ``str``
  41. :return: Name for this image as returned by the cloud middleware.
  42. """
  43. return self._os_image.name
  44. @property
  45. def description(self):
  46. """
  47. Get the image description.
  48. :rtype: ``str``
  49. :return: Description for this image as returned by the cloud middleware
  50. """
  51. return self._os_image.description
  52. def delete(self):
  53. """
  54. Delete this image
  55. """
  56. self._os_image.delete()
  57. @property
  58. def state(self):
  59. return OpenStackMachineImage.IMAGE_STATE_MAP.get(
  60. self._os_image.status, MachineImageState.UNKNOWN)
  61. def refresh(self):
  62. """
  63. Refreshes the state of this instance by re-querying the cloud provider
  64. for its latest state.
  65. """
  66. self._os_image = self.provider.images.get_image(self.image_id)._os_image
  67. class OpenStackInstanceType(InstanceType):
  68. def __init__(self, os_flavor):
  69. self.os_flavor = os_flavor
  70. @property
  71. def id(self):
  72. return self.os_flavor.id
  73. @property
  74. def name(self):
  75. return self.os_flavor.name
  76. def __repr__(self):
  77. return "<OSInstanceType: {0}={1}>".format(self.id, self.name)
  78. class OpenStackInstance(BaseInstance):
  79. # ref: http://docs.openstack.org/developer/nova/v2/2.0_server_concepts.html
  80. # and http://developer.openstack.org/api-ref-compute-v2.html
  81. INSTANCE_STATE_MAP = {
  82. 'ACTIVE': InstanceState.RUNNING,
  83. 'BUILD': InstanceState.PENDING,
  84. 'DELETED': InstanceState.TERMINATED,
  85. 'ERROR': InstanceState.ERROR,
  86. 'HARD_REBOOT': InstanceState.REBOOTING,
  87. 'PASSWORD': InstanceState.PENDING,
  88. 'PAUSED': InstanceState.STOPPED,
  89. 'REBOOT': InstanceState.REBOOTING,
  90. 'REBUILD': InstanceState.CONFIGURING,
  91. 'RESCUE': InstanceState.CONFIGURING,
  92. 'RESIZE': InstanceState.CONFIGURING,
  93. 'REVERT_RESIZE': InstanceState.CONFIGURING,
  94. 'SOFT_DELETED': InstanceState.STOPPED,
  95. 'STOPPED': InstanceState.STOPPED,
  96. 'SUSPENDED': InstanceState.STOPPED,
  97. 'SHUTOFF': InstanceState.STOPPED,
  98. 'UNKNOWN': InstanceState.UNKNOWN,
  99. 'VERIFY_RESIZE': InstanceState.CONFIGURING
  100. }
  101. def __init__(self, provider, os_instance):
  102. self.provider = provider
  103. self._os_instance = os_instance
  104. @property
  105. def instance_id(self):
  106. """
  107. Get the instance identifier.
  108. """
  109. return self._os_instance.id
  110. @property
  111. def name(self):
  112. """
  113. Get the instance name.
  114. """
  115. return self._os_instance.name
  116. @name.setter
  117. def name(self, value):
  118. """
  119. Set the instance name.
  120. """
  121. self._os_instance.name = value
  122. @property
  123. def public_ips(self):
  124. """
  125. Get all the public IP addresses for this instance.
  126. """
  127. return self._os_instance.networks['public']
  128. @property
  129. def private_ips(self):
  130. """
  131. Get all the private IP addresses for this instance.
  132. """
  133. return self._os_instance.networks['private']
  134. @property
  135. def instance_type(self):
  136. """
  137. Get the instance type.
  138. """
  139. return OpenStackInstanceType(self._os_instance.flavor)
  140. def reboot(self):
  141. """
  142. Reboot this instance (using the cloud middleware API).
  143. """
  144. self._os_instance.reboot()
  145. def terminate(self):
  146. """
  147. Permanently terminate this instance.
  148. """
  149. self._os_instance.delete()
  150. @property
  151. def image_id(self):
  152. """
  153. Get the image ID for this instance.
  154. """
  155. return self._os_instance.image_id
  156. @property
  157. def placement_zone(self):
  158. """
  159. Get the placement zone where this instance is running.
  160. """
  161. return self._os_instance.availability_zone
  162. @property
  163. def mac_address(self):
  164. """
  165. Get the MAC address for this instance.
  166. """
  167. raise NotImplementedError(
  168. 'mac_address not implemented by this provider')
  169. @property
  170. def security_group_ids(self):
  171. """
  172. Get the security group IDs associated with this instance.
  173. """
  174. return [BaseSecurityGroup(group.name)
  175. for group in self._os_instance.security_groups]
  176. @property
  177. def key_pair_name(self):
  178. """
  179. Get the name of the key pair associated with this instance.
  180. """
  181. return BaseKeyPair(self._os_instance.key_name)
  182. def create_image(self, name):
  183. """
  184. Create a new image based on this instance.
  185. """
  186. image_id = self._os_instance.create_image(name)
  187. return OpenStackMachineImage(
  188. self.provider, self.provider.images.get_image(image_id))
  189. @property
  190. def state(self):
  191. return OpenStackInstance.INSTANCE_STATE_MAP.get(
  192. self._os_instance.status, InstanceState.UNKNOWN)
  193. def refresh(self):
  194. """
  195. Refreshes the state of this instance by re-querying the cloud provider
  196. for its latest state.
  197. """
  198. self._os_instance = self.provider.compute.get_instance(
  199. self.instance_id)._os_instance
  200. class OpenStackRegion(Region):
  201. def __init__(self, provider, os_region):
  202. self.provider = provider
  203. self._os_region = os_region
  204. @property
  205. def name(self):
  206. return self._os_region.zoneName
  207. def __repr__(self):
  208. return "<OSRegion: {0}>".format(self.name)