Atom feed of this document
  
 

 Compute

To use the information in this section, you should have a general understanding of OpenStack Compute.

 Set environment variables

Please see the section called “Authenticate” on how to setup environmental variables and authenticate against Compute API endpoints.

 Get nova credentials v2

The examples in this section use the get_nova_credentials_v2 method:

def get_nova_credentials_v2():
    d = {}
    d['version'] = '2'
    d['username'] = os.environ['OS_USERNAME']
    d['api_key'] = os.environ['OS_PASSWORD']
    d['auth_url'] = os.environ['OS_AUTH_URL']
    d['project_id'] = os.environ['OS_TENANT_NAME']
    return d

This code resides in the credentials.py file, which all samples import.

Use the get_nova_credentials_v2() method to populate and get a dictionary:

credentials = get_nova_credentials_v2()

 List servers v2

The following program lists servers using the v2 APIs:

 

To list the servers

  1. Import the following modules:

    from credentials import get_nova_credentials_v2
    from novaclient.client import Client
  2. Get Nova Credentials. See the section called “Get nova credentials v2”.

  3. Instantiate the nova_client client object by using the credentials dictionary object:

    nova_client = Client(**credentials)
  4. Get the list of servers by calling servers.list on nova_client object:

    print(nova_client.servers.list())
 

Example 3.6. List servers: complete code listing

#!/usr/bin/python
# -*- coding: utf-8 -*-
from credentials import get_nova_credentials_v2
from novaclient.client import Client

credentials = get_nova_credentials_v2()
nova_client = Client(**credentials)

print(nova_client.servers.list())

 Create a server v2

The following program creates a server (VM) using the v2 APIs:

 

To create a server

  1. Import the following modules:

    import time
    from credentials import get_nova_credentials_v2
    from novaclient.client import Client
  2. Get Nova Credentials. See the section called “Get nova credentials v2”.

  3. Instantiate the nova_client client object by using the credentials dictionary object:

    nova_client = Client(**credentials)
  4. In this step, search for the flavor and image to be used for creating a server. The following code assumes cirros image and m1.tiny are being used.

    image = nova_client.images.find(name="cirros")
    flavor = nova_client.flavors.find(name="m1.tiny")
  5. image = nova_client.images.find(name="cirros")
    flavor = nova_client.flavors.find(name="m1.tiny")
  6. In this step determine the network with which the server is going to be attached. Use this along with flavor and image to create the server.

    net_id = 'd05a7729-4bcf-4149-9d8f-6a4764520a04'
    nic_d = [{'net-id': net_id}]
    instance = nova_client.servers.create(name="vm2", image=image,
                                          flavor=flavor, key_name="keypair-1", nics=nic_d)
  7. Sleep for 5 secs and check if the server/vm got created by calling nova_client.servers.list()

    print("Sleeping for 5s after create command")
    time.sleep(5)
    print("List of VMs")
    print(nova_client.servers.list)
 

Example 3.7. Create server: complete code listing

#!/usr/bin/python
# -*- coding: utf-8 -*-
import time
from credentials import get_nova_credentials_v2
from novaclient.client import Client

try:
    credentials = get_nova_credentials_v2()
    nova_client = Client(**credentials)

    image = nova_client.images.find(name="cirros")
    flavor = nova_client.flavors.find(name="m1.tiny")
    net_id = 'd05a7729-4bcf-4149-9d8f-6a4764520a04'
    nic_d = [{'net-id': net_id}]
    instance = nova_client.servers.create(name="vm2", image=image,
                                      flavor=flavor, key_name="keypair-1", nics=nic_d)
    print("Sleeping for 5s after create command")
    time.sleep(5)
    print("List of VMs")
    print(nova_client.servers.list())
finally:
    print("Execution Completed")

 Delete server v2

The following program deletes a Server (VM) using the v2 API:

 

To Delete a Server

  1. Import the following modules:

    import time
    from credentials import get_nova_credentials_v2
    from novaclient.client import Client
  2. Get Nova Credentials. See the section called “Get nova credentials v2”.

  3. Instantiate the nova_client client object by using the credentials dictionary object:

    nova_client = Client(**credentials)
  4. Check if the server "vm1" exists using the following steps

    1. Get the list of servers: servers_list

    2. Iterate over the servers_list and compare name with "vm1"

    3. If true set the variable name server_exists as True and break from the for loop

    servers_list = nova_client.servers.list()
    server_del = "vm1"
    server_exists = False
    
    for s in servers_list:
        if s.name == server_del:
            print("This server %s exists" % server_del)
            server_exists = True
            break
  5. If the server exists execute delete method of nova_client.servers object.

    nova_client.servers.delete(s)
 

Example 3.8. Delete: complete code listing

#!/usr/bin/python
# -*- coding: utf-8 -*-
from credentials import get_nova_credentials_v2
from novaclient.client import Client

credentials = get_nova_credentials_v2()
nova_client = Client(**credentials)

servers_list = nova_client.servers.list()
server_del = "vm1"
server_exists = False

for s in servers_list:
    if s.name == server_del:
        print("This server %s exists" % server_del)
        server_exists = True
        break
if not server_exists:
    print("server %s does not exist" % server_del)
else:
    print("deleting server..........")
    nova_client.servers.delete(s)
    print("server %s deleted" % server_del)

Questions? Discuss on ask.openstack.org
Found an error? Report a bug against this page

loading table of contents...