Compute Base API¶
-
class
libcloud.compute.base.
NodeDriver
(key, secret=None, secure=True, host=None, port=None, api_version=None, **kwargs)[source]¶ A base NodeDriver class to derive from
This class is always subclassed by a specific driver. For examples of base behavior of most functions (except deploy node) see the dummy driver.
-
attach_volume
(node, volume, device=None)[source]¶ Attaches volume to node.
Parameters: - node (
Node
) – Node to attach volume to. - volume (
StorageVolume
) – Volume to attach. - device (
str
) – Where the device is exposed, e.g. ‘/dev/sdb’
Rytpe: bool
- node (
-
connectionCls
¶ alias of
ConnectionKey
-
create_node
(**kwargs)[source]¶ Create a new node instance. This instance will be started automatically.
Not all hosting API’s are created equal and to allow libcloud to support as many as possible there are some standard supported variations of
create_node
. These are declared using afeatures
API. You can inspectdriver.features['create_node']
to see what variation of the API you are dealing with:ssh_key
- You can inject a public key into a new node allows key based SSH authentication.
password
- You can inject a password into a new node for SSH authentication.
If no password is provided libcloud will generated a password.
The password will be available as
return_value.extra['password']
. generates_password
- The hosting provider will generate a password. It will be returned
to you via
return_value.extra['password']
.
Some drivers allow you to set how you will authenticate with the instance that is created. You can inject this initial authentication information via the
auth
parameter.If a driver supports the
ssh_key
feature flag forcreated_node
you can upload a public key into the new instance:>>> from libcloud.compute.drivers.dummy import DummyNodeDriver >>> driver = DummyNodeDriver(0) >>> auth = NodeAuthSSHKey('pubkey data here') >>> node = driver.create_node("test_node", auth=auth)
If a driver supports the
password
feature flag forcreate_node
you can set a password:>>> driver = DummyNodeDriver(0) >>> auth = NodeAuthPassword('mysecretpassword') >>> node = driver.create_node("test_node", auth=auth)
If a driver supports the
password
feature and you don’t provide theauth
argument libcloud will assign a password:>>> driver = DummyNodeDriver(0) >>> node = driver.create_node("test_node") >>> password = node.extra['password']
A password will also be returned in this way for drivers that declare the
generates_password
feature, though in that case the password is actually provided to the driver API by the hosting provider rather than generated by libcloud.You can only pass a
NodeAuthPassword
orNodeAuthSSHKey
tocreate_node
via the auth parameter if has the corresponding feature flag.Parameters: - name (
str
) – String with a name for this new node (required) - size (
NodeSize
) – The size of resources allocated to this node. (required) - image (
NodeImage
) – OS Image to boot on node. (required) - location (
NodeLocation
) – Which data center to create a node in. If empty, undefined behavoir will be selected. (optional) - auth (
NodeAuthSSHKey
orNodeAuthPassword
) – Initial authentication information for the node (optional)
Returns: The newly created node.
Return type:
-
create_volume
(size, name, location=None, snapshot=None)[source]¶ Create a new volume.
Parameters: - size (
int
) – Size of volume in gigabytes (required) - name (
str
) – Name of the volume to be created - location (
NodeLocation
) – Which data center to create a volume in. If empty, undefined behavoir will be selected. (optional) - snapshot (
str
) – Name of snapshot from which to create the new volume. (optional)
Returns: The newly created volume.
Return type: StorageVolume
- size (
-
create_volume_snapshot
(volume, name)[source]¶ Creates a snapshot of the storage volume.
Return type: VolumeSnapshot
-
deploy_node
(**kwargs)[source]¶ Create a new node, and start deployment.
In order to be able to SSH into a created node access credentials are required.
A user can pass either a
NodeAuthPassword
orNodeAuthSSHKey
to theauth
argument. If thecreate_node
implementation supports that kind if credential (as declared inself.features['create_node']
) then it is passed on tocreate_node
. Otherwise it is not passed on tocreate_node
and it is only used for authentication.If the
auth
parameter is not supplied but the driver declares it supportsgenerates_password
then the password returned bycreate_node
will be used to SSH into the server.Finally, if the
ssh_key_file
is supplied that key will be used to SSH into the server.This function may raise a
DeploymentException
, if a create_node call was successful, but there is a later error (like SSH failing or timing out). This exception includes a Node object which you may want to destroy if incomplete deployments are not desirable.>>> from libcloud.compute.drivers.dummy import DummyNodeDriver >>> from libcloud.compute.deployment import ScriptDeployment >>> from libcloud.compute.deployment import MultiStepDeployment >>> from libcloud.compute.base import NodeAuthSSHKey >>> driver = DummyNodeDriver(0) >>> key = NodeAuthSSHKey('...') # read from file >>> script = ScriptDeployment("yum -y install emacs strace tcpdump") >>> msd = MultiStepDeployment([key, script]) >>> def d(): ... try: ... driver.deploy_node(deploy=msd) ... except NotImplementedError: ... print ("not implemented for dummy driver") >>> d() not implemented for dummy driver
Deploy node is typically not overridden in subclasses. The existing implementation should be able to handle most such.
Parameters: - deploy (
Deployment
) – Deployment to run once machine is online and availble to SSH. - ssh_username (
str
) – Optional name of the account which is used when connecting to SSH server (default is root) - ssh_alternate_usernames (
list
) – Optional list of ssh usernames to try to connect with if using the default one fails - ssh_port (
int
) – Optional SSH server port (default is 22) - ssh_timeout (
float
) – Optional SSH connection timeout in seconds (default is 10) - auth (
NodeAuthSSHKey
orNodeAuthPassword
) – Initial authentication information for the node (optional) - ssh_key (
str
orlist
ofstr
) – A path (or paths) to an SSH private key with which to attempt to authenticate. (optional) - timeout (
int
) – How many seconds to wait before timing out. (default is 600) - max_tries (
int
) – How many times to retry if a deployment fails before giving up (default is 3) - ssh_interface (
str
) – The interface to wait for. Default is ‘public_ips’, other option is ‘private_ips’.
- deploy (
-
destroy_node
(node)[source]¶ Destroy a node.
Depending upon the provider, this may destroy all data associated with the node, including backups.
Parameters: node ( Node
) – The node to be destroyedReturns: True if the destroy was successful, False otherwise. Return type: bool
-
destroy_volume
(volume)[source]¶ Destroys a storage volume.
Parameters: volume ( StorageVolume
) – Volume to be destroyedReturn type: bool
-
detach_volume
(volume)[source]¶ Detaches a volume from a node.
Parameters: volume ( StorageVolume
) – Volume to be detachedReturn type: bool
-
features
= {'create_node': []}¶ - List of available features for a driver.
libcloud.compute.base.NodeDriver.create_node()
- ssh_key: Supports
NodeAuthSSHKey
as an authentication method for nodes. - password: Supports
NodeAuthPassword
as an authentication method for nodes. - generates_password: Returns a password attribute on the Node object returned from creation.
- ssh_key: Supports
-
list_images
(location=None)[source]¶ List images on a provider
Parameters: location ( NodeLocation
) – The location at which to list imagesReturns: list of node image objects Return type: list
ofNodeImage
-
list_locations
()[source]¶ List data centers for a provider
Returns: list of node location objects Return type: list
ofNodeLocation
-
list_sizes
(location=None)[source]¶ List sizes on a provider
Parameters: location ( NodeLocation
) – The location at which to list sizesReturns: list of node size objects Return type: list
ofNodeSize
-
list_volume_snapshots
(volume)[source]¶ List snapshots for a storage volume.
Return type: list
ofVolumeSnapshot
-
list_volumes
()[source]¶ List storage volumes.
Return type: list
ofStorageVolume
-
reboot_node
(node)[source]¶ Reboot a node.
Parameters: node ( Node
) – The node to be rebootedReturns: True if the reboot was successful, otherwise False Return type: bool
-
wait_until_running
(nodes, wait_period=3, timeout=600, ssh_interface='public_ips', force_ipv4=True)[source]¶ Block until the given nodes are fully booted and have an IP address assigned.
Parameters: - nodes (
List
ofNode
) – list of node instances. - wait_period (
int
) – How many seconds to between each loop iteration (default is 3) - timeout (
int
) – How many seconds to wait before timing out (default is 600) - ssh_interface (
str
) – The interface to wait for. Default is ‘public_ips’, other option is ‘private_ips’. - force_ipv4 (
bool
) – Ignore ipv6 IP addresses (default is True).
Returns: [(Node, ip_addresses)]
list of tuple of Node instance and list of ip_address on success.Return type: list
oftuple
- nodes (
-
-
class
libcloud.compute.base.
Node
(id, name, state, public_ips, private_ips, driver, size=None, image=None, extra=None)[source]¶ Provide a common interface for handling nodes of all types.
The Node object provides the interface in libcloud through which we can manipulate nodes in different cloud providers in the same way. Node objects don’t actually do much directly themselves, instead the node driver handles the connection to the node.
You don’t normally create a node object yourself; instead you use a driver and then have that create the node for you.
>>> from libcloud.compute.drivers.dummy import DummyNodeDriver >>> driver = DummyNodeDriver(0) >>> node = driver.create_node() >>> node.public_ips[0] '127.0.0.3' >>> node.name 'dummy-3'
You can also get nodes from the driver’s list_node function.
>>> node = driver.list_nodes()[0] >>> node.name 'dummy-1'
the node keeps a reference to its own driver which means that we can work on nodes from different providers without having to know which is which.
>>> driver = DummyNodeDriver(72) >>> node2 = driver.create_node() >>> node.driver.creds 0 >>> node2.driver.creds 72
Althrough Node objects can be subclassed, this isn’t normally done. Instead, any driver specific information is stored in the “extra” proproperty of the node.
>>> node.extra {'foo': 'bar'}
Parameters: - id (
str
) – Node ID. - name (
str
) – Node name. - state (
libcloud.compute.types.NodeState
) – Node state. - public_ips (
list
) – Public IP addresses associated with this node. - private_ips (
list
) – Private IP addresses associated with this node. - driver (
NodeDriver
) – Driver this node belongs to. - size (:class:`.NodeImage:) – Size of this node. (optional)
- image – Image of this node. (optional)
- extra (
dict
) – Optional provided specific attributes associated with this node.
-
destroy
()[source]¶ Destroy this node
Returns: bool
This calls the node’s driver and destroys the node
>>> from libcloud.compute.drivers.dummy import DummyNodeDriver >>> driver = DummyNodeDriver(0) >>> from libcloud.compute.types import NodeState >>> node = driver.create_node() >>> node.state == NodeState.RUNNING True >>> node.destroy() True >>> node.state == NodeState.RUNNING False
-
reboot
()[source]¶ Reboot this node
Returns: bool
This calls the node’s driver and reboots the node
>>> from libcloud.compute.drivers.dummy import DummyNodeDriver >>> driver = DummyNodeDriver(0) >>> node = driver.create_node() >>> node.state == NodeState.RUNNING True >>> node.state == NodeState.REBOOTING False >>> node.reboot() True >>> node.state == NodeState.REBOOTING True
- id (
-
class
libcloud.compute.base.
NodeSize
(id, name, ram, disk, bandwidth, price, driver)[source]¶ A Base NodeSize class to derive from.
NodeSizes are objects which are typically returned a driver’s list_sizes function. They contain a number of different parameters which define how big an image is.
The exact parameters available depends on the provider.
N.B. Where a parameter is “unlimited” (for example bandwidth in Amazon) this will be given as 0.
>>> from libcloud.compute.drivers.dummy import DummyNodeDriver >>> driver = DummyNodeDriver(0) >>> size = driver.list_sizes()[0] >>> size.ram 128 >>> size.bandwidth 500 >>> size.price 4
Parameters: - id (
str
) – Size ID. - name (
str
) – Size name. - ram (
int
) – Amount of memory (in MB) provided by this size. - disk (
int
) – Amount of disk storage (in GB) provided by this image. - bandwidth (
int
) – Amount of bandiwdth included with this size. - price (
float
) – Price (in US dollars) of running this node for an hour. - driver (
NodeDriver
) – Driver this image belongs to.
- id (
-
class
libcloud.compute.base.
NodeImage
(id, name, driver, extra=None)[source]¶ An operating system image.
NodeImage objects are typically returned by the driver for the cloud provider in response to the list_images function
>>> from libcloud.compute.drivers.dummy import DummyNodeDriver >>> driver = DummyNodeDriver(0) >>> image = driver.list_images()[0] >>> image.name 'Ubuntu 9.10'
Apart from name and id, there is no further standard information; other parameters are stored in a driver specific “extra” variable
When creating a node, a node image should be given as an argument to the create_node function to decide which OS image to use.
>>> node = driver.create_node(image=image)
Parameters: - id (
str
) – Image ID. - name (
str
) – Image name. - driver (
NodeDriver
) – Driver this image belongs to. - extra (
dict
) – Optional provided specific attributes associated with this image.
- id (
-
class
libcloud.compute.base.
NodeLocation
(id, name, country, driver)[source]¶ A physical location where nodes can be.
>>> from libcloud.compute.drivers.dummy import DummyNodeDriver >>> driver = DummyNodeDriver(0) >>> location = driver.list_locations()[0] >>> location.country 'US'
Parameters: - id (
str
) – Location ID. - name (
str
) – Location name. - country – Location country.
:type country :
str
Parameters: driver ( NodeDriver
) – Driver this location belongs to.- id (
-
class
libcloud.compute.base.
NodeAuthSSHKey
(pubkey)[source]¶ An SSH key to be installed for authentication to a node.
This is the actual contents of the users ssh public key which will normally be installed as root’s public key on the node.
>>> pubkey = '...' # read from file >>> from libcloud.compute.base import NodeAuthSSHKey >>> k = NodeAuthSSHKey(pubkey) >>> k <NodeAuthSSHKey>
Parameters: pubkey ( str
) – Public key matetiral.
-
class
libcloud.compute.base.
NodeAuthPassword
(password, generated=False)[source]¶ A password to be used for authentication to a node.
Parameters: password ( str
) – Password.
-
class
libcloud.compute.base.
StorageVolume
(id, name, size, driver, extra=None)[source]¶ A base StorageVolume class to derive from.
Parameters: - id (
str
) – Storage volume ID. - name (
str
) – Storage volume name. - size (
int
) – Size of this volume (in GB). - driver (
NodeDriver
) – Driver this image belongs to. - extra (
dict
) – Optional provider specific attributes.
-
attach
(node, device=None)[source]¶ Attach this volume to a node.
Parameters: - node (
Node
) – Node to attach volume to - device (
str
) – Where the device is exposed, e.g. ‘/dev/sdb (optional)
Returns: True
if attach was successful,False
otherwise.Return type: bool
- node (
-
destroy
()[source]¶ Destroy this storage volume.
Returns: True
if destroy was successful,False
otherwise.Return type: bool
- id (