Skip to content

Instantly share code, notes, and snippets.

@EdLeafe
Created May 28, 2013 20:47
Show Gist options
  • Save EdLeafe/5665994 to your computer and use it in GitHub Desktop.
Save EdLeafe/5665994 to your computer and use it in GitHub Desktop.

#pyrax

pyrax is the Python SDK for OpenStack-based cloud providers, and includes support for additional services provided by Rackspace.

Installation

Installation works the same as any Python package:

pip install pyrax

If you are not using virtualenv, you will need to run the above as an admin.

Example

This example demonstrates provisioning two next generation servers and displays their corresponding information:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import pyrax

# First you need to authenticate. This will create clients for
# all available services.
pyrax.keyring_auth()

# Create a shortcut alias for the cloudservers service
cs = pyrax.cloudservers

# Get the image for Ubuntu 12.04
image = [img for img in cs.list_images()
        if "Ubuntu 12.04" in img.name][0]

# Flavors are RAM/disk combinations. We'll use the first.
flavor = cs.list_flavors()[0]

# create the first server
server1 = cs.servers.create(name="one", image=image.id,
        flavor=flavor.id)

# Make the second server a Windows Server
image_win = [img for img in cs.list_images()
        if "Windows Server 2012" in img.name][0]
                            
# select a 4 GB instance
flavor_4gb = [flavor for flavor in cs.list_flavors()
        if "4GB" in flavor.name][0]

# create the second server
server2 = cs.servers.create(name="two", image=image_win.id,
        flavor=flavor_4gb.id)

# Servers take a few minutes to build and configure. Wait until
# they are in an active status before proceeding. 
pyrax.utils.wait_until(server1, "status", ["ACTIVE", "ERROR"],
    interval=20, verbose=True, verbose_atts="progress")
pyrax.utils.wait_until(server2, "status", ["ACTIVE", "ERROR"],
    interval=20, verbose=True, verbose_atts="progress")

def print_server_details(srv):
    for att in dir(srv):
        if att.startswith("_"):
            continue
        print att, getattr(srv, att)
    print

# print out server one details
print "===[SERVER 1]==================="
print_server_details(server1)

# print out server two details
print "===[SERVER 2]==================="
print_server_details(server2)

print
print "Please remember to delete these servers via the cloud control panel"
print "(https://mycloud.rackspace.com/) to avoid being charged."
print   
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment