Using OpenStack Compute

Before working with the Compute service, you’ll need to create a connection to your OpenStack cloud by following the Connect user guide. This will provide you with the conn variable used in the examples below.

The primary resource of the Compute service is the server.

List Servers

A server is a virtual machine that provides access to a compute instance being run by your cloud provider.

def list_servers(conn):
    print("List Servers:")

    for server in conn.compute.servers():
        print(server)

Full example: compute resource list

List Images

An image is the operating system you want to use for your server.

def list_images(conn):
    print("List Images:")

    for image in conn.compute.images():
        print(image)

Full example: compute resource list

List Flavors

A flavor is the resource configuration for a server. Each flavor is a unique combination of disk, memory, vCPUs, and network bandwidth.

def list_flavors(conn):
    print("List Flavors:")

    for flavor in conn.compute.flavors():
        print(flavor)

Full example: compute resource list

List Networks

A network provides connectivity to servers.

def list_networks(conn):
    print("List Networks:")

    for network in conn.network.networks():
        print(network)

Full example: network resource list

Create Key Pair

A key pair is the public key and private key of public–key cryptography. They are used to encrypt and decrypt login information when connecting to your server.

def create_keypair(conn):
    keypair = conn.compute.find_keypair(KEYPAIR_NAME)

    if not keypair:
        print("Create Key Pair:")

        keypair = conn.compute.create_keypair(name=KEYPAIR_NAME)

        print(keypair)

        try:
            os.mkdir(SSH_DIR)
        except OSError as e:
            if e.errno != errno.EEXIST:
                raise e

        with open(PRIVATE_KEYPAIR_FILE, 'w') as f:
            f.write(str(keypair.private_key))

        os.chmod(PRIVATE_KEYPAIR_FILE, 0o400)

    return keypair

Full example: compute resource create

Create Server

At minimum, a server requires a name, an image, a flavor, and a network on creation. You can discover the names and IDs of these attributes by listing them as above and then using the find methods to get the appropriate resources.

Ideally you’ll also create a server using a keypair so you can login to that server with the private key.

Servers take time to boot so we call wait_for_server to wait for it to become active.

def create_server(conn):
    print("Create Server:")

    image = conn.image.find_image(IMAGE_NAME)
    flavor = conn.compute.find_flavor(FLAVOR_NAME)
    network = conn.network.find_network(NETWORK_NAME)
    keypair = create_keypair(conn)

    server = conn.compute.create_server(
        name=SERVER_NAME,
        image_id=image.id,
        flavor_id=flavor.id,
        networks=[{"uuid": network.id}],
        key_name=keypair.name,
    )

    server = conn.compute.wait_for_server(server)

    print(f"ssh -i {PRIVATE_KEYPAIR_FILE} root@{server.access_ipv4}")

Full example: compute resource create