6. Compute
To use the information in this section, you should have a general understanding of OpenStack Compute.
6.1. Set environment variables
Please see Section 2, “Authenticate” on how to setup environmental variables and authenticate against Compute API endpoints.
6.2. 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()
6.3. List servers v2
The following program lists servers using the v2 APIs:
Procedure 3.6. To list the servers
Import the following modules:
from credentials import get_nova_credentials_v2 from novaclient.client import Client
Get Nova Credentials. See Section 6.2, “Get nova credentials v2”.
Instantiate the
nova_clientclient object by using thecredentialsdictionary object:nova_client = Client(**credentials)
Get the list of servers by calling
servers.listonnova_clientobject: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())
6.4. Create a server v2
The following program creates a server (VM) using the v2 APIs:
Procedure 3.7. To create a server
Import the following modules:
import time from credentials import get_nova_credentials_v2 from novaclient.client import Client
Get Nova Credentials. See Section 6.2, “Get nova credentials v2”.
Instantiate the
nova_clientclient object by using thecredentialsdictionary object:nova_client = Client(**credentials)
In this step, search for the flavor and image to be used for creating a server. The following code assumes
cirrosimage andm1.tinyare being used.image = nova_client.images.find(name="cirros") flavor = nova_client.flavors.find(name="m1.tiny")
image = nova_client.images.find(name="cirros") flavor = nova_client.flavors.find(name="m1.tiny")
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)Check if the server/VM was created by calling
nova_client.servers.list()after waiting for five seconds.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")6.5. Delete server v2
The following program deletes a Server (VM) using the v2 API:
Procedure 3.8. To Delete a Server
Import the following modules:
import time from credentials import get_nova_credentials_v2 from novaclient.client import Client
Get Nova Credentials. See Section 6.2, “Get nova credentials v2”.
Instantiate the
nova_clientclient object by using thecredentialsdictionary object:nova_client = Client(**credentials)
Check if the server
"vm1"exists using the following stepsGet the list of servers:
servers_listIterate over the
servers_listand compare name with"vm1"If true set the variable name
server_existsasTrueand 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 breakIf the server exists execute
deletemethod ofnova_client.serversobject.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)