Skip to content
This repository has been archived by the owner on May 27, 2024. It is now read-only.

Commit

Permalink
Feature/volumes (#57)
Browse files Browse the repository at this point in the history
* feat(snapshot):added snapshot function

* feat(handler):fixed response createsnapshot

* feat(snapshot):added baseimtagetag

* feat(volumes):added methods to delete volume anda ttach to server

* fix(volumes):fixed a type bug

* feat(volumes):added method to delete volume attachment

* feat(volumes):changed delete volume a little bit

* feat(snapshots):added delete method

* feat(volumes):small changes

* fix(merge):fixed merge errors
dweinholz authored Jul 18, 2018
1 parent 8efeb75 commit a74ac6e
Showing 4 changed files with 734 additions and 107 deletions.
129 changes: 70 additions & 59 deletions gen-py/VirtualMachineService/VirtualMachineHandler.py
Original file line number Diff line number Diff line change
@@ -127,7 +127,7 @@ def get_Images(self):

image = Image(name=img['name'], min_disk=img['min_disk'], min_ram=img['min_ram'],
status=img['status'], created_at=img['created_at'], updated_at=img['updated_at'],
openstack_id=img['id'], description=description,tag=tags
openstack_id=img['id'], description=description, tag=tags
)
images.append(image)

@@ -141,7 +141,7 @@ def get_Image_with_Tag(self, id):
tags = img.get('tags')
image = Image(name=img['name'], min_disk=img['min_disk'], min_ram=img['min_ram'],
status=img['status'], created_at=img['created_at'], updated_at=img['updated_at'],
openstack_id=img['id'], description=description,tag=tags
openstack_id=img['id'], description=description, tag=tags
)
return image

@@ -209,7 +209,7 @@ def get_server(self, openstack_id):

def start_server(self, flavor, image, public_key, servername, elixir_id, diskspace):

volumeId=''
volumeId = ''
self.logger.info("Start Server {0}".format(servername))
try:
metadata = {'elixir_id': elixir_id}
@@ -226,7 +226,6 @@ def start_server(self, flavor, image, public_key, servername, elixir_id, diskspa
self.logger.error('Network {0} not found!'.format(network))
raise networkNotFoundException(Reason='Network {0} not found!'.format(network))


keyname = elixir_id[:-18]
public_key = urllib.parse.unquote(public_key)
keypair = self.import_keypair(keyname, public_key)
@@ -240,10 +239,10 @@ def start_server(self, flavor, image, public_key, servername, elixir_id, diskspa
'Trying to create volume with {0} GB for vm {1} error : {2}'.format(diskspace, openstack_id, e),
exc_info=True)
raise ressourceException(Reason=str(e))
volumeId =volume['id']
volumeId = volume['id']
with open('../../mount.sh', 'r') as file:
text = file.read()
text = text.replace('VOLUMEID', 'virtio-'+volumeId[0:20])
text = text.replace('VOLUMEID', 'virtio-' + volumeId[0:20])
text = encodeutils.safe_encode(text.encode('utf-8'))
init_script = base64.b64encode(text).decode('utf-8')

@@ -255,12 +254,12 @@ def start_server(self, flavor, image, public_key, servername, elixir_id, diskspa
name=servername, image_id=image.id, flavor_id=flavor.id,
networks=[{"uuid": network.id}], key_name=keypair.name, metadata=metadata)

return {'openstackid':server.to_dict()['id'],'volumeId':volumeId}
return {'openstackid': server.to_dict()['id'], 'volumeId': volumeId}
except Exception as e:
self.logger.error(e)
raise ressourceException(Reason=str(e))

def attach_volume_to_server(self, openstack_id, diskspace,volume_id):
def attach_volume_to_server(self, openstack_id, volume_id):
def checkStatusVolume(volume, conn):
self.logger.info("Checking Status Volume {0}".format(volume_id))
done = False
@@ -276,12 +275,11 @@ def checkStatusVolume(volume, conn):
time.sleep(2)
return volume


server = self.conn.compute.get_server(openstack_id)
if server is None:
self.logger.error("No Server {0} ".format(openstack_id))
raise serverNotFoundException(Reason='No Server {0}'.format(openstack_id))
checkStatusVolume(volume_id,self.conn)
checkStatusVolume(volume_id, self.conn)

self.logger.info('Attaching volume {0} to virtualmachine {1}'.format(volume_id, openstack_id))
try:
@@ -296,7 +294,7 @@ def checkStatusVolume(volume, conn):

return True

def check_server_status(self, openstack_id, diskspace,volume_id):
def check_server_status(self, openstack_id, diskspace, volume_id):
self.logger.info('Check Status VM {0}'.format(openstack_id))
try:
server = self.conn.compute.get_server(openstack_id)
@@ -310,7 +308,8 @@ def check_server_status(self, openstack_id, diskspace,volume_id):

if serv['status'] == 'ACTIVE':
if diskspace > 0:
attached = self.attach_volume_to_server(openstack_id=openstack_id, diskspace=diskspace,volume_id=volume_id)
attached = self.attach_volume_to_server(openstack_id=openstack_id,
volume_id=volume_id)

if attached is False:
server = self.get_server(openstack_id)
@@ -325,31 +324,45 @@ def check_server_status(self, openstack_id, diskspace,volume_id):
return server

def get_IP_PORT(self, openstack_id):
self.logger.info("Get IP and PORT for server {0}".format(openstack_id))
self.logger.info("Get IP and PORT for server {0}".format(openstack_id))

# check if jumphost is active
# check if jumphost is active

if 'True' == str(self.USE_JUMPHOST):
server = self.get_server(openstack_id)
server_base = server.fixed_ip.split(".")[-1]
port = int(self.JUMPHOST_BASE) + int(server_base) * 3
return {'IP': str(self.JUMPHOST_IP),'PORT':str(port)}
if 'True' == str(self.USE_JUMPHOST):
server = self.get_server(openstack_id)
server_base = server.fixed_ip.split(".")[-1]
port = int(self.JUMPHOST_BASE) + int(server_base) * 3
return {'IP': str(self.JUMPHOST_IP), 'PORT': str(port)}

else:
floating_ip = self.add_floating_ip_to_server(openstack_id, self.FLOATING_IP_NETWORK)
else:
floating_ip = self.add_floating_ip_to_server(openstack_id, self.FLOATING_IP_NETWORK)

return {'IP': str(floating_ip)}
return {'IP': str(floating_ip)}

def create_snapshot(self,openstack_id,name,elixir_id,base_tag):
self.logger.info('Create Snapshot from Instance {0} with name {1} for {2}'.format(openstack_id,name,elixir_id))
def create_snapshot(self, openstack_id, name, elixir_id, base_tag):
self.logger.info(
'Create Snapshot from Instance {0} with name {1} for {2}'.format(openstack_id, name, elixir_id))

snapshot_munch= self.conn.create_image_snapshot(server=openstack_id,name=name)
snapshot_id=snapshot_munch['id']
self.conn.image.add_tag(image=snapshot_id,tag=elixir_id)
self.conn.image.add_tag(image=snapshot_id,tag='snapshot_image:{0}'.format(base_tag))
try:
snapshot_munch = self.conn.create_image_snapshot(server=openstack_id, name=name)
except Exception:
self.logger.error("Instance {0} not found".format(openstack_id))
raise serverNotFoundException
snapshot_id = snapshot_munch['id']
self.conn.image.add_tag(image=snapshot_id, tag=elixir_id)
self.conn.image.add_tag(image=snapshot_id, tag='snapshot_image:{0}'.format(base_tag))
return snapshot_id

def delete_image(self,image_id):
self.logger.info('Delete Image {0}'.format(image_id))

image = self.conn.compute.get_image(image_id)
if image is None:
self.logger.error('Image {0} not found!'.format(image))
raise imageNotFoundException(Reason=('Image {0} not found'.format(image)))
self.conn.compute.delete_image(image)
return True


def add_floating_ip_to_server(self, openstack_id, network):

server = self.conn.compute.get_server(openstack_id)
@@ -367,7 +380,7 @@ def add_floating_ip_to_server(self, openstack_id, network):
if not floating_ip.fixed_ip_address:
self.conn.compute.add_floating_ip_to_server(server, floating_ip.floating_ip_address)
self.logger.info(
"Adding existing Floating IP {0} to {1}".format(str(floating_ip.floating_ip_address),openstack_id))
"Adding existing Floating IP {0} to {1}".format(str(floating_ip.floating_ip_address), openstack_id))
return str(floating_ip.floating_ip_address)

networkID = self.conn.network.find_network(network)
@@ -394,46 +407,44 @@ def delete_server(self, openstack_id):
while server.status != 'ACTIVE':
server = self.conn.compute.get_server(server)
time.sleep(3)
self.conn.compute.delete_server(server)

attachments = list()
volumeids = list()

for volume in self.conn.compute.volume_attachments(server=server):
attachments.append(volume)
volumeids.append(volume.to_dict()['volume_id'])
return True

def deleteVolumeAttachmenServer(attachments, server, logger, conn):

for a in attachments:
logger.info("Delete VolumeAttachment {0}".format(a))
conn.compute.delete_volume_attachment(volume_attachment=a, server=server)

deleteVolumeAttachmenServer(attachments=attachments, server=server, logger=self.logger, conn=self.conn)
def delete_volume_attachment(self, volume_id,server_id):
attachments=self.conn.block_storage.get_volume(volume_id).attachments
for attachment in attachments:
volume_attachment_id=attachment['id']
instance_id=attachment['server_id']
if instance_id == server_id:
self.logger.info("Delete Volume Attachment {0}".format(volume_attachment_id))
self.conn.compute.delete_volume_attachment(volume_attachment=volume_attachment_id, server=server_id)
return True

def checkStatusVolumes(volumes, conn):
def delete_volume(self, volume_id):
def checkStatusVolume(volume, conn):
self.logger.info("Checking Status Volume {0}".format(volume_id))
done = False
while done == False:
done = True
for a in volumes:
if conn.block_storage.get_volume(a).to_dict()['status'] != 'available':
conn.block_storage.get_volume(a).to_dict()['status']
done = False
time.sleep(5)
break
return

checkStatusVolumes(volumeids, self.conn)

def deleteVolume(volume_id, conn, logger):
logger.info("Delete Volume {0}".format(volume_id))
conn.block_storage.delete_volume(volume=volume_id)

for id in volumeids:
deleteVolume(id, self.conn, self.logger)
self.conn.compute.delete_server(server)
status = conn.block_storage.get_volume(volume).to_dict()['status']

if status != 'available':

time.sleep(3)
else:
done = True
return volume
checkStatusVolume(volume_id,self.conn)
self.logger.info("Delete Volume {0}".format(volume_id))
self.conn.block_storage.delete_volume(volume=volume_id)
return True




def stop_server(self, openstack_id):
self.logger.info("Stop Server " + openstack_id)
server = self.conn.compute.get_server(openstack_id)
31 changes: 26 additions & 5 deletions gen-py/VirtualMachineService/VirtualMachineService-remote
Original file line number Diff line number Diff line change
@@ -37,8 +37,11 @@ if len(sys.argv) <= 1 or sys.argv[1] == '--help':
print(' start_server(string flavor, string image, string public_key, string servername, string elixir_id, string diskspace)')
print(' VM get_server(string openstack_id)')
print(' bool stop_server(string openstack_id)')
print(' bool create_snapshot(string openstack_id, string name, string elixir_id, string base_tag)')
print(' bool attach_volume_to_server(string openstack_id, int diskspace, string volume_id)')
print(' string create_snapshot(string openstack_id, string name, string elixir_id, string base_tag)')
print(' bool delete_image(string image_id)')
print(' bool delete_volume_attachment(string volume_id, string server_id)')
print(' bool delete_volume(string volume_id)')
print(' bool attach_volume_to_server(string openstack_id, string volume_id)')
print(' VM check_server_status(string openstack_id, int diskspace, string volume_id)')
print(' string setUserPassword(string user, string password)')
print(' bool resume_server(string openstack_id)')
@@ -205,11 +208,29 @@ elif cmd == 'create_snapshot':
sys.exit(1)
pp.pprint(client.create_snapshot(args[0], args[1], args[2], args[3],))

elif cmd == 'delete_image':
if len(args) != 1:
print('delete_image requires 1 args')
sys.exit(1)
pp.pprint(client.delete_image(args[0],))

elif cmd == 'delete_volume_attachment':
if len(args) != 2:
print('delete_volume_attachment requires 2 args')
sys.exit(1)
pp.pprint(client.delete_volume_attachment(args[0], args[1],))

elif cmd == 'delete_volume':
if len(args) != 1:
print('delete_volume requires 1 args')
sys.exit(1)
pp.pprint(client.delete_volume(args[0],))

elif cmd == 'attach_volume_to_server':
if len(args) != 3:
print('attach_volume_to_server requires 3 args')
if len(args) != 2:
print('attach_volume_to_server requires 2 args')
sys.exit(1)
pp.pprint(client.attach_volume_to_server(args[0], eval(args[1]), args[2],))
pp.pprint(client.attach_volume_to_server(args[0], args[1],))

elif cmd == 'check_server_status':
if len(args) != 3:
667 changes: 626 additions & 41 deletions gen-py/VirtualMachineService/VirtualMachineService.py

Large diffs are not rendered by default.

14 changes: 12 additions & 2 deletions portal_client.thrift
Original file line number Diff line number Diff line change
@@ -224,11 +224,21 @@ service VirtualMachineService {
* This Method unpause a VirtualMachine with a specific Openstack-ID.
*/

bool create_snapshot(1:string openstack_id, 2:string name,3: string elixir_id,4:string base_tag)

string create_snapshot(1:string openstack_id, 2:string name,3: string elixir_id,4:string base_tag) throws (1:serverNotFoundException e),


bool attach_volume_to_server(1:string openstack_id,2:int diskspace,3:string volume_id) throws (1:serverNotFoundException e),



bool delete_image(1:string image_id) throws (1:imageNotFoundException e)

bool delete_volume_attachment(1:string volume_id,2:string server_id) throws (1:serverNotFoundException e),

bool delete_volume(1:string volume_id)


bool attach_volume_to_server(1:string openstack_id,2:string volume_id) throws (1:serverNotFoundException e),

VM check_server_status(1:string openstack_id,2:int diskspace,3:string volume_id) throws (1:serverNotFoundException e,2:ressourceException r),

0 comments on commit a74ac6e

Please sign in to comment.