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 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 downloading 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 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]))
# 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=str(sys.argv[1]))
instance.add_floating_ip(floating_ip)
[root@ip-192-169-142-127 api(keystone_admin)]# 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 \
Ubuntu1510Devs
[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, 192.169.142.190 |
| 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 |
+--------------------------------------+----------------+---------+------------+-------------+------------------------------------------+
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
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 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 downloading 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 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]))
# 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=str(sys.argv[1]))
instance.add_floating_ip(floating_ip)
[root@ip-192-169-142-127 api(keystone_admin)]# 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 \
Ubuntu1510Devs
[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, 192.169.142.190 |
| 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 |
+--------------------------------------+----------------+---------+------------+-------------+------------------------------------------+
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