types.py 6.5 KB

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