Skip to content

Instantly share code, notes, and snippets.

@lenards
Created July 6, 2016 23:19
Show Gist options
  • Save lenards/22fdb24a700559b413b3e1607c41b237 to your computer and use it in GitHub Desktop.
Save lenards/22fdb24a700559b413b3e1607c41b237 to your computer and use it in GitHub Desktop.
A utility script to test launching instances in OpenStack via apache libcloud
# python libcloud_cloudlab_test.py <image-id> <flavor-id> <instance-name>
#
# python libcloud_cloudlab_test.py 91a3072b-c755-43e8-b8bc-b9cb62338ebf 2 lenardsagain
import os
import sys
from libcloud.compute.types import Provider
from libcloud.compute.providers import get_driver
import libcloud.security
if len(sys.argv) < 4:
sys.exit("missing expected arguments")
libcloud.security.VERIFY_SSL_CERT = False
# source the admin-openrc shell script to set environment variables
auth_username = os.environ['OS_USERNAME']
auth_password = os.environ['OS_PASSWORD']
auth_url = os.environ['OS_AUTH_URL'] + '/tokens'
project_name = os.environ['OS_TENANT_NAME']
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_project_name=project_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 = sys.argv[1]
image = conn.get_image(image_id)
# Get the flavor that we want to use by its id
flavor_id = sys.argv[2]
flavor = conn.ex_get_size(flavor_id)
#specify networks.
nets = filter(lambda net: net.name == 'flat-lan-1-net', conn.ex_list_networks())
network = [nets[0]]
instance = conn.create_node(name=sys.argv[3], image=image, size=flavor, networks=network)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment