-
Notifications
You must be signed in to change notification settings - Fork 1
Homework 5. Cinder API CURD
life1347 edited this page Jan 20, 2014
·
3 revisions
import logging
import pprint
import time
import os
import string
import pycurl
from time import sleep, asctime
from cinderclient.v1 import client as cinclient
from neutronclient.v2_0 import client as neclient
from novaclient.v1_1 import client as nclient
logging.basicConfig(level=logging.INFO)
logging.basicConfig(level=logging.DEBUG)
KEYSTONE_URL='http://openstack.nctu.edu.tw:5000/v2.0'
def curl(host, port, action, options = ""):
buf = cStringIO.StringIO()
#print 'http://'+host+':'+port+'/'+action+options
c = pycurl.Curl()
c.setopt(c.URL, 'http://'+host+':'+port+'/'+action+options)
c.setopt(c.WRITEFUNCTION, buf.write)
c.perform()
value = buf.getvalue()
buf.close()
class Volume():
def __init__(self, user, pwd, tName):
global KEYSTONE_URL
self.cinder = cinclient.Client(auth_url=KEYSTONE_URL, username = user, api_key = pwd, project_id = tName)
self.nova = nclient.Client(auth_url=KEYSTONE_URL, username = user, api_key = pwd, project_id = tName,service_type = 'compute')
self.neutron = neclient.Client(auth_url=KEYSTONE_URL, username = user, tenant_name = tName, password = pwd)
def createServer(self, name):
server = self.nova.servers.create(
name = name,
image = self.nova.images.find(name = "nginx-proxy-server-cloudOSproject").id,
flavor = self.nova.flavors.find(name = "cloudos_flavor").id,
nics = [{'net-id' : self.neutron.list_networks(name = 'CloudOS_2013')['networks'][0]['id']}])
while server.status != 'ACTIVE':
time.sleep(3)
server = self.nova.servers.get(server.id)
return server.id
def createVolume(self):
vid = str(self.cinder.volumes.create(size='1', volume_type='volume'))
return vid.strip('>').split(': ')[1]
def attachVolume(self, serverID, volumeID):
self.nova.volumes.create_server_volume(serverID, volumeID, '/dev/vdb')
def detachVolume(self, serverID, volumeID):
self.nova.volumes.delete_server_volume(serverID, volumeID)
def main():
v = Volume('0256081', '[email protected]', 'CloudOS2013_0256081')
vID = v.createVolume()
raw_input('Press enter to create master:')
MinsID = v.createServer('Master')
raw_input('Press enter to attach volumes to master:')
v.attachVolume(MinsID, vID)
raw_input('Press enter to detach volumes from master:')
v.detachVolume(MinsID, vID)
raw_input('Press enter to create slave:')
SinsID = v.createServer('Slave')
raw_input('Press enter to attach volumes to slave:')
v.attachVolume(SinsID, vID)
raw_input('Press enter to detach volumes from slave:')
v.detachVolume(SinsID, vID)
if __name__ == '__main__': main()
Since Cinder cannot formant volume, its hard to use cinder api for map/reduce-like application. We have to format volume through command line
$ mkfs.ext4 /dev/vdb
$ mount /dev/vdb /mnt
$ cd /mnt
$ touch Hello_World
$ mount /dev/vdb /mnt
$ ls