R&D/클라우드

OpenStack Python API

sunshout 2014. 3. 13. 00:04

Python APIs : The best-kept secret of OpenStack


http://www.ibm.com/developerworks/cloud/library/cl-openstack-pythonapis/index.html?ca=drs-



import os


def get_nova_creds():

    d = {}

    d['username'] = 'admin'

    d['api_key'] = 'admin_pass'

    d['auth_url'] = 'http://192.168.1.98:5000/v2.0'

    d['project_id'] = 'admin'

    return d


from novaclient import client as novaclient

creds = get_nova_creds()

nova = novaclient.Client('1.1', **creds)


vm = nova.servers.find(name='vm_lm')

print vm.migrate()



Booting a new instance


import os
import time
import novaclient.v1_1.client as nvclient
from credentials import get_nova_creds
creds = get_nova_creds()
nova = nvclient.Client(**creds)
if not nova.keypairs.findall(name="mykey"):
    with open(os.path.expanduser('~/.ssh/id_rsa.pub')) as fpubkey:
        nova.keypairs.create(name="mykey", public_key=fpubkey.read())
image = nova.images.find(name="cirros")
flavor = nova.flavors.find(name="m1.tiny")
instance = nova.servers.create(name="test", image=image, flavor=flavor, key_name="mykey")

# 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