|
|
@@ -2,22 +2,35 @@
|
|
|
DataTypes used by this provider
|
|
|
"""
|
|
|
|
|
|
+from cloudbridge.providers.base import BaseInstance
|
|
|
from cloudbridge.providers.base import BaseKeyPair
|
|
|
+from cloudbridge.providers.base import BaseMachineImage
|
|
|
from cloudbridge.providers.base import BaseSecurityGroup
|
|
|
-from cloudbridge.providers.interfaces import Instance
|
|
|
+from cloudbridge.providers.interfaces import InstanceState
|
|
|
from cloudbridge.providers.interfaces import InstanceType
|
|
|
-from cloudbridge.providers.interfaces import MachineImage
|
|
|
+from cloudbridge.providers.interfaces import MachineImageState
|
|
|
|
|
|
|
|
|
-class OpenStackImage(MachineImage):
|
|
|
+class OpenStackMachineImage(BaseMachineImage):
|
|
|
+
|
|
|
+ # ref: http://docs.openstack.org/developer/glance/statuses.html
|
|
|
+ IMAGE_STATE_MAP = {
|
|
|
+ 'QUEUED': MachineImageState.PENDING,
|
|
|
+ 'SAVING': MachineImageState.PENDING,
|
|
|
+ 'ACTIVE': MachineImageState.AVAILABLE,
|
|
|
+ 'KILLED': MachineImageState.ERROR,
|
|
|
+ 'DELETED': MachineImageState.ERROR,
|
|
|
+ 'PENDING_DELETE': MachineImageState.ERROR
|
|
|
+ }
|
|
|
|
|
|
def __init__(self, provider, os_image):
|
|
|
self.provider = provider
|
|
|
- if isinstance(os_image, MachineImage):
|
|
|
+ if isinstance(os_image, OpenStackMachineImage):
|
|
|
self._os_image = os_image._os_image
|
|
|
else:
|
|
|
self._os_image = os_image
|
|
|
|
|
|
+ @property
|
|
|
def image_id(self):
|
|
|
"""
|
|
|
Get the image identifier.
|
|
|
@@ -27,6 +40,7 @@ class OpenStackImage(MachineImage):
|
|
|
"""
|
|
|
return self._os_image.id
|
|
|
|
|
|
+ @property
|
|
|
def name(self):
|
|
|
"""
|
|
|
Get the image name.
|
|
|
@@ -36,6 +50,7 @@ class OpenStackImage(MachineImage):
|
|
|
"""
|
|
|
return self._os_image.name
|
|
|
|
|
|
+ @property
|
|
|
def description(self):
|
|
|
"""
|
|
|
Get the image description.
|
|
|
@@ -51,6 +66,18 @@ class OpenStackImage(MachineImage):
|
|
|
"""
|
|
|
self._os_image.delete()
|
|
|
|
|
|
+ @property
|
|
|
+ def image_state(self):
|
|
|
+ return OpenStackMachineImage.IMAGE_STATE_MAP.get(
|
|
|
+ self._os_image.status, MachineImageState.UNKNOWN)
|
|
|
+
|
|
|
+ def refresh(self):
|
|
|
+ """
|
|
|
+ Refreshes the state of this instance by re-querying the cloud provider
|
|
|
+ for its latest state.
|
|
|
+ """
|
|
|
+ self._os_image = self.provider.images.get_image(self.image_id)._os_image
|
|
|
+
|
|
|
|
|
|
class OpenStackInstanceType(InstanceType):
|
|
|
|
|
|
@@ -69,12 +96,36 @@ class OpenStackInstanceType(InstanceType):
|
|
|
return "<OSInstanceType: {0}={1}>".format(self.id, self.name)
|
|
|
|
|
|
|
|
|
-class OpenStackInstance(Instance):
|
|
|
+class OpenStackInstance(BaseInstance):
|
|
|
+
|
|
|
+ # ref: http://docs.openstack.org/developer/nova/v2/2.0_server_concepts.html
|
|
|
+ # and http://developer.openstack.org/api-ref-compute-v2.html
|
|
|
+ INSTANCE_STATE_MAP = {
|
|
|
+ 'ACTIVE': InstanceState.RUNNING,
|
|
|
+ 'BUILD': InstanceState.PENDING,
|
|
|
+ 'DELETED': InstanceState.TERMINATED,
|
|
|
+ 'ERROR': InstanceState.ERROR,
|
|
|
+ 'HARD_REBOOT': InstanceState.REBOOTING,
|
|
|
+ 'PASSWORD': InstanceState.PENDING,
|
|
|
+ 'PAUSED': InstanceState.STOPPED,
|
|
|
+ 'REBOOT': InstanceState.REBOOTING,
|
|
|
+ 'REBUILD': InstanceState.CONFIGURING,
|
|
|
+ 'RESCUE': InstanceState.CONFIGURING,
|
|
|
+ 'RESIZE': InstanceState.CONFIGURING,
|
|
|
+ 'REVERT_RESIZE': InstanceState.CONFIGURING,
|
|
|
+ 'SOFT_DELETED': InstanceState.STOPPED,
|
|
|
+ 'STOPPED': InstanceState.STOPPED,
|
|
|
+ 'SUSPENDED': InstanceState.STOPPED,
|
|
|
+ 'SHUTOFF': InstanceState.STOPPED,
|
|
|
+ 'UNKNOWN': InstanceState.UNKNOWN,
|
|
|
+ 'VERIFY_RESIZE': InstanceState.CONFIGURING
|
|
|
+ }
|
|
|
|
|
|
def __init__(self, provider, os_instance):
|
|
|
self.provider = provider
|
|
|
self._os_instance = os_instance
|
|
|
|
|
|
+ @property
|
|
|
def instance_id(self):
|
|
|
"""
|
|
|
Get the instance identifier.
|
|
|
@@ -95,18 +146,21 @@ class OpenStackInstance(Instance):
|
|
|
"""
|
|
|
self._os_instance.name = value
|
|
|
|
|
|
+ @property
|
|
|
def public_ips(self):
|
|
|
"""
|
|
|
Get all the public IP addresses for this instance.
|
|
|
"""
|
|
|
return self._os_instance.networks['public']
|
|
|
|
|
|
+ @property
|
|
|
def private_ips(self):
|
|
|
"""
|
|
|
Get all the private IP addresses for this instance.
|
|
|
"""
|
|
|
return self._os_instance.networks['private']
|
|
|
|
|
|
+ @property
|
|
|
def instance_type(self):
|
|
|
"""
|
|
|
Get the instance type.
|
|
|
@@ -125,18 +179,21 @@ class OpenStackInstance(Instance):
|
|
|
"""
|
|
|
self._os_instance.delete()
|
|
|
|
|
|
+ @property
|
|
|
def image_id(self):
|
|
|
"""
|
|
|
- Get the image ID for this insance.
|
|
|
+ Get the image ID for this instance.
|
|
|
"""
|
|
|
return self._os_instance.image_id
|
|
|
|
|
|
+ @property
|
|
|
def placement_zone(self):
|
|
|
"""
|
|
|
Get the placement zone where this instance is running.
|
|
|
"""
|
|
|
return self._os_instance.availability_zone
|
|
|
|
|
|
+ @property
|
|
|
def mac_address(self):
|
|
|
"""
|
|
|
Get the MAC address for this instance.
|
|
|
@@ -144,12 +201,14 @@ class OpenStackInstance(Instance):
|
|
|
raise NotImplementedError(
|
|
|
'mac_address not implemented by this provider')
|
|
|
|
|
|
+ @property
|
|
|
def security_group_ids(self):
|
|
|
"""
|
|
|
Get the security group IDs associated with this instance.
|
|
|
"""
|
|
|
return [BaseSecurityGroup(group.name) for group in self._os_instance.security_groups]
|
|
|
|
|
|
+ @property
|
|
|
def key_pair_name(self):
|
|
|
"""
|
|
|
Get the name of the key pair associated with this instance.
|
|
|
@@ -161,4 +220,16 @@ class OpenStackInstance(Instance):
|
|
|
Create a new image based on this instance.
|
|
|
"""
|
|
|
image_id = self._os_instance.create_image(name)
|
|
|
- return OpenStackImage(self.provider, self.provider.images.get_image(image_id))
|
|
|
+ return OpenStackMachineImage(self.provider, self.provider.images.get_image(image_id))
|
|
|
+
|
|
|
+ @property
|
|
|
+ def instance_state(self):
|
|
|
+ return OpenStackInstance.INSTANCE_STATE_MAP.get(
|
|
|
+ self._os_instance.status, InstanceState.UNKNOWN)
|
|
|
+
|
|
|
+ def refresh(self):
|
|
|
+ """
|
|
|
+ Refreshes the state of this instance by re-querying the cloud provider
|
|
|
+ for its latest state.
|
|
|
+ """
|
|
|
+ self._os_instance = self.provider.compute.get_instance(self.instance_id)._os_instance
|