Apache Libcloud
Apache Libcloud is a standard Python library that abstracts away differences among multiple cloud provider APIs, offering a unified interface to manage various cloud resources like servers, object storage, load balancers, and DNS. The current version is 3.9.0. The project follows an event-driven release schedule, with new versions released as enough changes accumulate, or for critical bug and security fixes.
Warnings
- breaking Libcloud 3.9.0 (and newer) has significantly narrowed its Python version support. It now officially requires Python >= 3.10. Support for Python 3.9, 3.8, and 3.7 has been explicitly dropped in recent 3.x releases.
- breaking Several compute drivers for defunct or non-offering providers were removed in Libcloud 3.8.0. If you are upgrading from an older version and using one of these providers, your code will break.
- gotcha Hardcoding API credentials directly in your code is a security risk. It's a common mistake that leads to credential exposure.
- gotcha In older versions (prior to 0.4.2), Libcloud did not validate host SSL certificates by default, making applications vulnerable to Man-in-the-Middle attacks.
Install
-
pip install apache-libcloud
Imports
- Provider
from libcloud.compute.types import Provider
- get_driver
from libcloud.compute.providers import get_driver
Quickstart
import os
from libcloud.compute.types import Provider
from libcloud.compute.providers import get_driver
# Set your credentials using environment variables for security
RACKSPACE_USER = os.environ.get('RACKSPACE_USERNAME', 'your_username')
RACKSPACE_API_KEY = os.environ.get('RACKSPACE_API_KEY', 'your_api_key')
if RACKSPACE_USER == 'your_username' or RACKSPACE_API_KEY == 'your_api_key':
print("Please set RACKSPACE_USERNAME and RACKSPACE_API_KEY environment variables.")
exit(1)
# Obtain a reference to the Rackspace Compute driver
cls = get_driver(Provider.RACKSPACE)
driver = cls(RACKSPACE_USER, RACKSPACE_API_KEY, region='iad') # 'iad' for US-East (Virginia)
print("Connected to Rackspace Compute service.")
# List available sizes (flavors)
print("Available sizes:")
sizes = driver.list_sizes()
for size in sizes:
print(f" - {size.id}: {size.name} ({size.ram}MB RAM, {size.disk}GB Disk)")
# List available images
print("Available images:")
images = driver.list_images()
for image in images:
print(f" - {image.id}: {image.name}")
# Example: Select a size and image for node creation (adjust as needed)
# This is a placeholder and might require matching a specific image/size from your account/region
# For Rackspace, a common base image might be 'Ubuntu 18.04' or similar.
# For size, 'performance1-1' (1GB RAM) is common.
selected_size = next((s for s in sizes if s.id == 'performance1-1'), None)
selected_image = next((i for i in images if 'Ubuntu 18.04' in i.name), None)
if selected_size and selected_image:
print(f"Attempting to create node with size: {selected_size.name} and image: {selected_image.name}")
# Note: Creating a node usually incurs cost. This part is commented out.
# try:
# node = driver.create_node(name='libcloud-test-node', size=selected_size, image=selected_image)
# print(f"Node created: {node.name} (ID: {node.id}, State: {node.state})")
# except Exception as e:
# print(f"Error creating node: {e}")
else:
print("Could not find suitable size or image for node creation. Please check available options.")