From Christoph's Personal Wiki
Jump to: navigation, search
NOTE: This article was written in 2014 and is no longer maintained.

pyrax is the official Python SDK for OpenStack/Rackspace APIs.


Note: See my gist of this script.

# NAME: pyrax_create_cloud_server
# AUTHOR: Christoph Champ
# DESCRIPTION: This script creates a Cloud Server in your Rackspace account.
# It then creates an image of that server, and, finally, creates a new server
# from that saved image.
# NOTES: Within each of the created VMs, the generated credentials are in
# master_server.adminPass and clone_server.adminPass. To access the box, use
# master_server.accessIPv4 and clone_server.accessIPv4.
# However, it is _much_ better/safer/wiser to use SSH keypairs.
# SEE:
import os
import pyrax
USERNAME = "" # This is your Rackspace account username
API_KEY = ""
# Authenticate with Rackspace
pyrax.set_setting('identity_type', 'rackspace')
pyrax.set_credentials(USERNAME, API_KEY, region=REGION)
cs = pyrax.connect_to_cloudservers()
# NOTE: You could use the API to get an image ID instead
image_id = u'f70ed7c7-b42e-4d77-83d8-40fa29825b85' # CentOS 6.4
flavor = u'performance1-1'
# Create the master server
print "Creating master_server...\n"
master_server = cs.servers.create("master_server", image_id, flavor)
# It takes time to create the server, so poll it until it completes
master_server = pyrax.utils.wait_for_build(master_server, verbose=True)
# Create an image of the master server
print "\nCreating image of master_server...\n"
im = master_server.create_image("master_image")
image = cs.images.get(im)
# Wait for image creation to finish before moving on to clone
image = pyrax.utils.wait_until(image, "status", ["ACTIVE","ERROR"], attempts=0)
# NOTE: This clone can be a larger flavor (VM size), if needed/wanted
# Here we use the same flavor of the master server
print "Creating clone_server from master_image...\n"
clone_server = cs.servers.create(name="clone_server",,
clone_server = pyrax.utils.wait_for_build(clone_server, verbose=True)
print "Master_root_password: %s" % master_server.adminPass
print "Master_public_IPv4: %s" % master_server.accessIPv4
print "Clone_root_password: %s" % clone_server.adminPass
print "Clone_public_IPv4: %s" % clone_server.accessIPv4

External links