Thursday, February 4, 2016

Python API for "boot from image creates new volume" RDO Liberty

Post bellow addresses several questions been posted at ask.openstack.org
In particular, code bellow doesn't require volume UUID to be  hard coded
to start server attached to boot able cinder's LVM, created via glance image,
which is supposed to be passed to script via command line. In the same way
name of cinder volume and instance name may be passed to script via CLI. 

Place in current directory following files :-

[root@ip-192-169-142-127 api(keystone_admin)]# cat credentials.py
#!/usr/bin/env python
import os

def get_keystone_creds():
    d = {}
    d['username'] = os.environ['OS_USERNAME']
    d['password'] = os.environ['OS_PASSWORD']
    d['auth_url'] = os.environ['OS_AUTH_URL']
    d['tenant_name'] = os.environ['OS_TENANT_NAME']
    return d

def get_nova_creds():
    d = {}
    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

[root@ip-192-169-142-127 api(keystone_admin)]# cat  startServer.py
#!/usr/bin/env python
import sys
import os
import time
from novaclient.v2.client import Client
from credentials import get_nova_creds

total = len(sys.argv)
cmdargs = str(sys.argv)
print ("The total numbers of args passed to the script: %d " % total)
print ("Args list: %s " % cmdargs)
print ("First argument: %s" % str(sys.argv[1]))

creds = get_nova_creds()
nova = Client(**creds)
if not nova.keypairs.findall(name="oskeyadm0302"):
    with open(os.path.expanduser('~/.ssh/id_rsa.pub')) as fpubkey:
        nova.keypairs.create(name="oskeyadm0302", public_key=fpubkey.read())

# Creating bootable volume

image = nova.images.find(name=str(sys.argv[1]))
flavor = nova.flavors.find(name="m1.small")
volume = nova.volumes.create(5,display_name="Ubuntu1510LVM",
               volume_type="lvms", imageRef=image.id )

# Wait until volume download will be done

status = volume.status
while ( status == 'creating' or status == 'downloading'):
    time.sleep(15)
    print "status: %s" % status
    volume = nova.volumes.get(volume.id)
    status = volume.status
print "status: %s" % status

# Select tenant's network

nova.networks.list()
network = nova.networks.find(label="demo_network1")
nics = [{'net-id': network.id}]

block_dev_mapping = {'vda': volume.id }

# Starting nova instance

instance = nova.servers.create(name="Ubuntu1510Devs", image='',
                  flavor=flavor,
                  availability_zone="nova:ip-192-169-142-137.ip.secureserver.net",
                  key_name="oskeyadm0302", nics=nics,       
                  block_device_mapping=block_dev_mapping)

# Poll at 5 second intervals, until the status is no longer 'BUILD'
status = instance.status
while status == 'BUILD':
    time.sleep(5)
    # Retrieve the instance again so the status field updates
    instance = nova.servers.get(instance.id)
    status = instance.status
print "status: %s" % status


[root@ip-192-169-142-127 api(keystone_admin)]# cat assignFIP.py

#!/usr/bin/env python
import os
import time
from novaclient.v2.client import Client
from credentials import get_nova_creds

# Assign floating IP for active instance

creds = get_nova_creds()
nova = Client(**creds)
nova.floating_ip_pools.list()
floating_ip = nova.floating_ips.create(nova.floating_ip_pools.list()[0].name)
instance = nova.servers.find(name="Ubuntu1510Devs")
instance.add_floating_ip(floating_ip)



[root@ip-192-169-142-127 api(keystone_admin)]# /usr/bin/python  \
startServer.pyc  Ubuntu1510Cloud-image

The total numbers of args passed to the script: 2
Args list: ['startServer.pyc', 'Ubuntu1510Cloud-image']


status: creating
status: downloading
status: downloading
status: available
status: ACTIVE

[root@ip-192-169-142-127 api(keystone_admin)]# nova list
+--------------------------------------+----------------+---------+------------+-------------+------------------------------------------+
| ID                                   | Name           | Status  | Task State | Power State | Networks                                 |
+--------------------------------------+----------------+---------+------------+-------------+------------------------------------------+
| e6ffa475-c026-4033-bb83-f3d32e5bf491 | Ubuntu1510Devs | ACTIVE  | -          | Running     | demo_network1=50.0.0.17                  |
| 4ceb4c64-f347-40a8-81a0-d032286cbd16 | VF23Devs0137   | SHUTOFF | -          | Shutdown    | private_admin=70.0.0.15, 192.169.142.181 |
| 6c0e29bb-2936-47f3-b94b-a9b00cc5bee6 | VF23Devs0157   | SHUTOFF | -          | Shutdown    | private_admin=70.0.0.16, 192.169.142.182 |
+--------------------------------------+----------------+---------+------------+-------------+------------------------------------------+

*************************************
Check ports available for server
*************************************

[root@ip-192-169-142-127 api(keystone_admin)]# neutron port-list --device-id e6ffa475-c026-4033-bb83-f3d32e5bf491
+--------------------------------------+------+-------------------+----------------------------------------------------------------------------------+
| id                                   | name | mac_address       | fixed_ips                                                                        |
+--------------------------------------+------+-------------------+----------------------------------------------------------------------------------+
| 53d18ed3-2ec1-487d-a975-0150ebcae23b |      | fa:16:3e:af:a1:21 | {"subnet_id": "5a148b53-780e-4282-8cbf-bf5e05624e5c", "ip_address": "50.0.0.17"} |
+--------------------------------------+------+-------------------+----------------------------------------------------------------------------------+

[root@ip-192-169-142-127 api(keystone_admin)]# python  assignFIP.pyc

[root@ip-192-169-142-127 api(keystone_admin)]# nova list
+--------------------------------------+----------------+---------+------------+-------------+------------------------------------------+
| ID                                   | Name           | Status  | Task State | Power State | Networks                                 |
+--------------------------------------+----------------+---------+------------+-------------+------------------------------------------+
| 5fd95200-4447-4866-868f-244071227640 | Ubuntu1510Devs | ACTIVE  | -          | Running     | demo_network1=50.0.0.20, 192.169.142.190 |
| 4ceb4c64-f347-40a8-81a0-d032286cbd16 | VF23Devs0137   | ACTIVE  | -          | Running     | private_admin=70.0.0.15, 192.169.142.181 |
| 6c0e29bb-2936-47f3-b94b-a9b00cc5bee6 | VF23Devs0157   | SHUTOFF | -          | Shutdown    | private_admin=70.0.0.16, 192.169.142.182 |
+--------------------------------------+----------------+---------+------------+-------------+---

  

 [root@ip-192-169-142-127 api(keystone_admin)]# nova show Ubuntu1510Devs

+--------------------------------------+----------------------------------------------------------+
| Property                             | Value                                                    |
+--------------------------------------+----------------------------------------------------------+
| OS-DCF:diskConfig                    | MANUAL                                                   |
| OS-EXT-AZ:availability_zone          | nova                                                     |
| OS-EXT-SRV-ATTR:host                 | ip-192-169-142-137.ip.secureserver.net                   |
| OS-EXT-SRV-ATTR:hypervisor_hostname | ip-192-169-142-137.ip.secureserver.net   |
| OS-EXT-SRV-ATTR:instance_name        | instance-0000000e                                        |
| OS-EXT-STS:power_state               | 1                                                        |
| OS-EXT-STS:task_state                | -                                                        |
| OS-EXT-STS:vm_state                  | active                                                   |
| OS-SRV-USG:launched_at               | 2016-02-04T14:49:56.000000                               |
| OS-SRV-USG:terminated_at             | -                                                        |
| accessIPv4                           |                                                          |
| accessIPv6                           |                                                          |
| config_drive                         |                                                          |
| created                              | 2016-02-04T14:49:48Z                                     |
| demo_network1 network                | 50.0.0.20, 192.169.142.190                               |
| flavor                               | m1.small (2)                                             |
| hostId                               | fcbdcecdd81fc89ce3ff23e041c16d119b5860926feb0c6e165791f6 |
| id                                   | 5fd95200-4447-4866-868f-244071227640                     |
| image                                | Attempt to boot from volume - no image supplied          |
| key_name                             | oskeyadm0302                                             |
| metadata                             | {}                                                       |
| name                                 | Ubuntu1510Devs                                           |
| os-extended-volumes:volumes_attached | [{"id": "bbc3f6e7-cc6e-4e7a-8e34-dc329293e157"}]         |
| progress                             | 0                                                        |
| security_groups                      | default                                                  |
| status                               | ACTIVE                                                   |
| tenant_id                            | c90f8ee371d04850afd2eab63628cfca                         |
| updated                              | 2016-02-04T14:49:55Z                                     |
| user_id                              | 4551168e044a43a9ae63cc7c8fe08b94                         |
+--------------------------------------+----------------------------------------------------------+
[root@ip-192-169-142-127 api(keystone_admin)]# cinder list

+--------------------------------------+--------+------------------+---------------+------+-------------+----------+-------------+--------------------------------------+
|                  ID                  | Status | Migration Status |      Name     | Size | Volume Type | Bootable | Multiattach |             Attached to              |
+--------------------------------------+--------+------------------+---------------+------+-------------+----------+-------------+--------------------------------------+
| a154f434-16e7-4ef7-9305-9d557369e819 | in-use |        -         |   VF23LVMS02  |  5   |     lvms    |   true   |    False    | 6c0e29bb-2936-47f3-b94b-a9b00cc5bee6 |
| bbc3f6e7-cc6e-4e7a-8e34-dc329293e157 | in-use |        -         | Ubuntu1510LVM |  5   |     lvms    |   true   |    False    | 5fd95200-4447-4866-868f-244071227640 |
+--------------------------------------+--------+------------------+---------------+------+-------------+----------+-------------+--------------------------------------+

References
1. http://www.ibm.com/developerworks/cloud/library/cl-openstack-pythonapis/
2. http://docs.openstack.org/developer/python-novaclient/api/novaclient.v1_1.volumes.html

1 comment: