This is a guest post by Tomaz Muraus. Tomaz Muraus is a project chair and a PMC member on the Apache Libcloud project. In his free time, he loves writing code, contributing to the open-source projects, advocating for software freedom and cycling. Be sure to check out his website at http://www.tomaz.me.
What is Libcloud?
Apache Libcloud is an open-source Apache 2.0 licensed Python library for interacting with many of the popular cloud service providers using a unified API. It was created to make it easy for developers to build products that work between any of the services that it supports.
Latest version supports more than 30 different providers (including CloudSigma) and exposes four main APIs:
- Compute and Block Storage – services such as Amazon EC2, OpenStack Nova, CloudStack and CloudSigma
- Object Storage and CDN – services such as Amazon S3 and OpenStack Swift
- Load Balancers – services such as Amazon ELB and Rackspace Cloud Load Balancers
- DNS – services such as Linode DNS, Rackspace Cloud DNS and Zerigo DNS
Libcloud and CloudSigma
CloudSigma has been supported in Libcloud for a very long time. First driver for CloudSigma API v1.0 was introduced back in late 2010.
Since then a lot has changed. In early 2013, CloudSigma deprecated API v1.0 and released a new and much improved API v2.0.
Support for the API v2.0 has been added in Libcloud 0.14.1 which was released a couple of days ago.
In this post we are going to have an in-depth look at this new driver and show some examples of how you can effectively utilize it.
CloudSigma Libcloud driver functionality
Before diving in how you can use the driver, let’s have a quick look at the functionality supported by this driver.
Keep in mind that not all of this functionality fits into the Libcloud standard API so some of it is exposed through the extension methods.
- Server creation using pre-installed library drives or operating system installer CDs
- Server deletion
- Size, image and drive listing
- VLAN support
- Server cloning
- Drive creation
- Drive cloning
- Availability groups support
- Drive resizing
- Firewall policies management
- Resource tagging
- and more
Getting started with Libcloud and the CloudSigma driver
Installation
To get started, you first need to install the library. Libcloud is a Python library available on PyPi which means you can install it using pip:
[bash light=”true”] $ pip install apache-libcloud[/bash]
Example #1 – Instantiating a driver
This example show a basic workflow of instantiating a driver.
[python] from libcloud.compute.types import Providerfrom libcloud.compute.providers import get_driver
cls = get_driver(Provider.CLOUDSIGMA)
# "api_version" argument can be left out since it defaults to 2.0
driver = cls(‘username’, ‘password’, region=’zrh’, api_version=’2.0′)
[/python]
On line 7 the driver is instantiated. As you can see, you need to pass your CloudSigma username and password to the driver constructor. The username and password you need to pass to the constructor is the same one as you use to login to the CloudSigma control panel.
You can see that we have also passed “region” and “api_version” argument to the constructor. “api_version” argument already default to “2.0” so it can be omitted unless you, for some reason. still need to use legacy and deprecated API v1.0. In this case you can pass a value of “1.0”.
Region argument tells driver which region to use. In this case we have connected to Zürich region (“zrh”), but the driver also supports Las Vegas (“lvs”) and the recently added Washington D.C region (“wdc”).
Example #2 – Create a node using a pre-installed library drive
This example shows how to create a node using a pre-installed library drive. Using a pre-installed library drive means that the node is ready to use as soon after it has been created.
[python] from libcloud.compute.types import Providerfrom libcloud.compute.providers import get_driver
cls = get_driver(Provider.CLOUDSIGMA)
driver = cls(‘username’, ‘password’, region=’zrh’, api_version=’2.0′)
sizes = driver.list_sizes()
images = driver.list_images()[0]
name = ‘test-node-1’
size = [size for size in sizes if size.id == ‘micro-regular’][0]
image = [0]
node = driver.create_node(name=name, size=size, image=image)
print(node)
print(node.extra)
[/python]
On line 12 we select a size. A NodeSize object in Libcloud represents server’s physical configuration (CPU, amount of ram, etc.). In this case we have selected the smallest available size.
On line 13 select an image. A NodeImage object in Libcloud represents an operating system which will be installed on the server. In this case we have selected Debian 7.0 server. But you can also choose the latest version Debian 7.8 from our market place.
After the script finishes, your newly created server should appear in the control panel.
Example #3 Create a node with a VLAN
This example is very similar to the one above, only difference is that we also create a VLAN and assign it to the server. This means that created server will have two network interfaces – one for the public network and one for the VLAN.
[python] from libcloud.compute.types import Providerfrom libcloud.compute.providers import get_driver
cls = get_driver(Provider.CLOUDSIGMA)
driver = cls(‘username’, ‘password’, region=’zrh’, api_version=’2.0′)
sizes = driver.list_sizes()
images = driver.list_images()[0]
name = ‘test-node-vlan’
size = [size for size in sizes if size.id == ‘micro-regular’][0]
image = [0]
# 1. Create a VLAN. VLANs are created by purchasing a subscription.
subscription = driver.ex_create_subscription(amount=1, period=’1 month’,
resource=’vlan’, auto_renew=True)
vlan_uuid = subscription.subscribed_object
# 2. Create a node with a VLAN
node = driver.create_node(name=name, size=size, image=image,
ex_vlan=vlan_uuid)
print(node)
print(node.extra)
[/python]
On line 16 we purchased a 1 month subscription for a “vlan” resource. In CloudSigma, VLAN and some other resources are created by purchasing a subscription.
On line 22 we then pass “ex_vlan” argument to the create_node which tells the method which VLAN to attach to the created server.
As you can see, the created server now has two network interfaces.
Example #4 – Create a node using an operating system installation CD drive
This example shows how to create a server using an installation CD drive. When you create a server using an installation CD, the server can’t be used immediately after it has been created. Instead, you need to connect to the server using VNC and walk-through the installation process before you can use your server.
[python] from libcloud.compute.types import Providerfrom libcloud.compute.providers import get_driver
cls = get_driver(Provider.CLOUDSIGMA)
driver = cls(‘username’, ‘password’, region=’zrh’, api_version=’2.0′)
sizes = driver.list_sizes()
images = driver.list_images()[0]
name = ‘test-node-fbsd’
size = [size for size in sizes if size.id == ‘micro-regular’][0]
drives = driver.ex_list_library_drives()
image = [drive for drive in drives if drive.name == ‘FreeBSD 9.2 DVD’ and
drive.media == ‘cdrom’][0]
# 1. Create a node
node = driver.create_node(name=name, size=size, image=image)
print(node)
# 2. Wait for node to come online
driver.wait_until_running(nodes=[node])
# 3. Enable and obtain VNC URL so we can connect to the VNC server and walk
# through the installation process
tunnel_url = driver.ex_open_vnc_tunnel(node=node)
print(‘VNC tunnel URL: %s’ % (tunnel_url))
print(‘VNC password: %s’ % (node.extra[‘vnc_password’]))
[/python]
After the server has been created you can connect it using a VNC client available on your operating system or using the web based VNC viewer provided in the CloudSigma control panel.
Walking through the installation process using the CloudSigma web based VNC client.
Where I can find more information / Where to go from here?
I hope that this post showed that Libcloud is a very powerful library which makes writing cross-provider applications and scripts easy and fun.
In it we just barely scratched the surface of what you can do with Libcloud and CloudSigma driver. CloudSigma driver has an extensive documentation so for more information and examples, we encourage you to check the links below:
If you at some point get stuck or need additional help, don’t hesitate to contact us via our mailing list or IRC channel.
Share this Post
- CloudSigma API v2.0 is now supported in Libcloud - February 11, 2014