How to Launch Instances Using Python and Apache Libcloud

You can start a new virtual machine, install an application in it, set security group to access such application, assign ssh key and public IPv4 without using the web control panel. All features of DreamCompute can be programmed with Python to automate application deployment: This is what makes OpenStack clouds like DreamCompute so powerful and different from other virtual servers.
When you want to create an instance on DreamCompute, you can login to the DreamCompute dashboard in a browser and follow the steps to create a new instance as described on DreamCompute documentation. It’s pretty easy but also requires lots of clicking that cannot be automated.
Why would you want to script these steps? Because you can — with a full cloud platform based on OpenStack like DreamCompute, you can consider resources such as a virtual machine, storage and network fully programmable. Since you can automate the creation and configuration of instances, then you can replicate actions more easily and develop applications that launch instances under certain circumstances and kill them in others. This not only helps you provide a better service, it can save you money by letting you use only the compute and storage resources you need.
When you partner with us, your website is in good hands! Our services pair friendly expertise with top-notch technology to give you all you need to succeed on the web.Cloud Computing and DreamHost
The alternative to clicking on the DreamCompute web panel is to script actions with a programming language. Since DreamHost cloud is based on open source components like OpenStack and Ceph, there are already lots of tools and SDKs for many programming languages.
For example, Python enthusiasts like us use the Apache Libcloud project to create an instance with the image and flavor you want, assign volumes, networks and all the things you do on the web panel. It also gives you the ability to deploy an application to the instance in a seamless manner, using cloud-init to apply the user data to the instance.
Below is python code that will spin up a small instance running Ubuntu 14.04 using python code and libcloud. To get authentication information needed to run the code, download the OpenStack RC file and see we have already provided some of the information for you. You will also need to install libcloud with `pip install apache-libcloud`.
from libcloud.compute.types import Provider from libcloud.compute.providers import get_driver # Authentication information so you can authenticate to DreamCompute # copy the details from the OpenStack RC file # https://dashboard.dreamcompute.com/project/access_and_security/api_access/openrc/ auth_username = 'your_auth_username' auth_password = 'your_auth_password' project_name = 'your_project_name_or_id' auth_url = 'https://keystone.dream.io' region_name = 'RegionOne' provider = get_driver(Provider.OPENSTACK) conn = provider(auth_username, auth_password, ex_force_auth_url=auth_url, ex_force_auth_version='2.0_password', ex_tenant_name=project_name, ex_force_service_region=region_name) # Get the image that we want to use by its id # NOTE: the image_id may change. See the documentation to find # all the images available to your user image_id = '90d5e049-aaed-4abc-aa75-60c2b1ed6516' image = conn.get_image(image_id) # Get the flavor that we want to use by its id flavor_id = '100' flavor = conn.ex_get_size(flavor_id) # Create the node with the name “PracticeInstance” # and the size and image we chose above instance = conn.create_node(name='PracticeInstance', image=image, size=flavor)
Feature image: Benjamin Hell (en:User:Siebengang).Siebengang at en.wikibooks [Public domain], from Wikimedia Commons
3 Comments
Comments are closed.
Hey Stefano,
I’m wondering if you have tried to create instances behind a proxy, we are doing some testing using libcloud without too much success.
By the way, thanks for sharing this.
Hi Victor, I haven’t tried working behind a proxy: I’m sure things get a lot more complicated that way.
Hi Victor, I’m not sure I understand your question, but it sounds like you are trying to run a script, behind a proxy, that creates instances. If this is the case, it is possible that your proxy blocks the IP address that your script needs to send information to, or it may block a port that the script is trying to use.