From 8efeb750747b4de9022bb0f8f3731da4df384d22 Mon Sep 17 00:00:00 2001 From: DavidWein94 Date: Thu, 12 Jul 2018 12:09:03 +0200 Subject: [PATCH] Feature/snapshot (#56) * feat(snapshot):added snapshot function * feat(handler):fixed response createsnapshot * feat(snapshot):added baseimtagetag --- .../VirtualMachineHandler.py | 9 + .../VirtualMachineService-remote | 7 + .../VirtualMachineService.py | 226 +++++++++++++++++- portal_client.thrift | 2 + 4 files changed, 242 insertions(+), 2 deletions(-) diff --git a/gen-py/VirtualMachineService/VirtualMachineHandler.py b/gen-py/VirtualMachineService/VirtualMachineHandler.py index a34ae0bb..a878ce11 100644 --- a/gen-py/VirtualMachineService/VirtualMachineHandler.py +++ b/gen-py/VirtualMachineService/VirtualMachineHandler.py @@ -340,6 +340,15 @@ def get_IP_PORT(self, openstack_id): 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)) + + 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)) + return True + def add_floating_ip_to_server(self, openstack_id, network): diff --git a/gen-py/VirtualMachineService/VirtualMachineService-remote b/gen-py/VirtualMachineService/VirtualMachineService-remote index ec23536c..3d2960d1 100755 --- a/gen-py/VirtualMachineService/VirtualMachineService-remote +++ b/gen-py/VirtualMachineService/VirtualMachineService-remote @@ -37,6 +37,7 @@ 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(' VM check_server_status(string openstack_id, int diskspace, string volume_id)') print(' string setUserPassword(string user, string password)') @@ -198,6 +199,12 @@ elif cmd == 'stop_server': sys.exit(1) pp.pprint(client.stop_server(args[0],)) +elif cmd == 'create_snapshot': + if len(args) != 4: + print('create_snapshot requires 4 args') + sys.exit(1) + pp.pprint(client.create_snapshot(args[0], args[1], args[2], args[3],)) + elif cmd == 'attach_volume_to_server': if len(args) != 3: print('attach_volume_to_server requires 3 args') diff --git a/gen-py/VirtualMachineService/VirtualMachineService.py b/gen-py/VirtualMachineService/VirtualMachineService.py index 0f088d5f..31ca3f26 100755 --- a/gen-py/VirtualMachineService/VirtualMachineService.py +++ b/gen-py/VirtualMachineService/VirtualMachineService.py @@ -157,11 +157,21 @@ def stop_server(self, openstack_id): """ pass - def attach_volume_to_server(self, openstack_id, diskspace, volume_id): + def create_snapshot(self, openstack_id, name, elixir_id, base_tag): """ @ This Method unpause a VirtualMachine with a specific Openstack-ID. + Parameters: + - openstack_id + - name + - elixir_id + - base_tag + """ + pass + + def attach_volume_to_server(self, openstack_id, diskspace, volume_id): + """ Parameters: - openstack_id - diskspace @@ -696,11 +706,48 @@ def recv_stop_server(self): raise result.e raise TApplicationException(TApplicationException.MISSING_RESULT, "stop_server failed: unknown result") - def attach_volume_to_server(self, openstack_id, diskspace, volume_id): + def create_snapshot(self, openstack_id, name, elixir_id, base_tag): """ @ This Method unpause a VirtualMachine with a specific Openstack-ID. + Parameters: + - openstack_id + - name + - elixir_id + - base_tag + """ + self.send_create_snapshot(openstack_id, name, elixir_id, base_tag) + return self.recv_create_snapshot() + + def send_create_snapshot(self, openstack_id, name, elixir_id, base_tag): + self._oprot.writeMessageBegin('create_snapshot', TMessageType.CALL, self._seqid) + args = create_snapshot_args() + args.openstack_id = openstack_id + args.name = name + args.elixir_id = elixir_id + args.base_tag = base_tag + args.write(self._oprot) + self._oprot.writeMessageEnd() + self._oprot.trans.flush() + + def recv_create_snapshot(self): + iprot = self._iprot + (fname, mtype, rseqid) = iprot.readMessageBegin() + if mtype == TMessageType.EXCEPTION: + x = TApplicationException() + x.read(iprot) + iprot.readMessageEnd() + raise x + result = create_snapshot_result() + result.read(iprot) + iprot.readMessageEnd() + if result.success is not None: + return result.success + raise TApplicationException(TApplicationException.MISSING_RESULT, "create_snapshot failed: unknown result") + + def attach_volume_to_server(self, openstack_id, diskspace, volume_id): + """ Parameters: - openstack_id - diskspace @@ -861,6 +908,7 @@ def __init__(self, handler): self._processMap["start_server"] = Processor.process_start_server self._processMap["get_server"] = Processor.process_get_server self._processMap["stop_server"] = Processor.process_stop_server + self._processMap["create_snapshot"] = Processor.process_create_snapshot self._processMap["attach_volume_to_server"] = Processor.process_attach_volume_to_server self._processMap["check_server_status"] = Processor.process_check_server_status self._processMap["setUserPassword"] = Processor.process_setUserPassword @@ -1173,6 +1221,25 @@ def process_stop_server(self, seqid, iprot, oprot): oprot.writeMessageEnd() oprot.trans.flush() + def process_create_snapshot(self, seqid, iprot, oprot): + args = create_snapshot_args() + args.read(iprot) + iprot.readMessageEnd() + result = create_snapshot_result() + try: + result.success = self._handler.create_snapshot(args.openstack_id, args.name, args.elixir_id, args.base_tag) + msg_type = TMessageType.REPLY + except (TTransport.TTransportException, KeyboardInterrupt, SystemExit): + raise + except Exception as ex: + msg_type = TMessageType.EXCEPTION + logging.exception(ex) + result = TApplicationException(TApplicationException.INTERNAL_ERROR, 'Internal error') + oprot.writeMessageBegin("create_snapshot", msg_type, seqid) + result.write(oprot) + oprot.writeMessageEnd() + oprot.trans.flush() + def process_attach_volume_to_server(self, seqid, iprot, oprot): args = attach_volume_to_server_args() args.read(iprot) @@ -3216,6 +3283,161 @@ def __ne__(self, other): return not (self == other) +class create_snapshot_args(object): + """ + Attributes: + - openstack_id + - name + - elixir_id + - base_tag + """ + + thrift_spec = ( + None, # 0 + (1, TType.STRING, 'openstack_id', 'UTF8', None, ), # 1 + (2, TType.STRING, 'name', 'UTF8', None, ), # 2 + (3, TType.STRING, 'elixir_id', 'UTF8', None, ), # 3 + (4, TType.STRING, 'base_tag', 'UTF8', None, ), # 4 + ) + + def __init__(self, openstack_id=None, name=None, elixir_id=None, base_tag=None,): + self.openstack_id = openstack_id + self.name = name + self.elixir_id = elixir_id + self.base_tag = base_tag + + def read(self, iprot): + if iprot._fast_decode is not None and isinstance(iprot.trans, TTransport.CReadableTransport) and self.thrift_spec is not None: + iprot._fast_decode(self, iprot, (self.__class__, self.thrift_spec)) + return + iprot.readStructBegin() + while True: + (fname, ftype, fid) = iprot.readFieldBegin() + if ftype == TType.STOP: + break + if fid == 1: + if ftype == TType.STRING: + self.openstack_id = iprot.readString().decode('utf-8') if sys.version_info[0] == 2 else iprot.readString() + else: + iprot.skip(ftype) + elif fid == 2: + if ftype == TType.STRING: + self.name = iprot.readString().decode('utf-8') if sys.version_info[0] == 2 else iprot.readString() + else: + iprot.skip(ftype) + elif fid == 3: + if ftype == TType.STRING: + self.elixir_id = iprot.readString().decode('utf-8') if sys.version_info[0] == 2 else iprot.readString() + else: + iprot.skip(ftype) + elif fid == 4: + if ftype == TType.STRING: + self.base_tag = iprot.readString().decode('utf-8') if sys.version_info[0] == 2 else iprot.readString() + else: + iprot.skip(ftype) + else: + iprot.skip(ftype) + iprot.readFieldEnd() + iprot.readStructEnd() + + def write(self, oprot): + if oprot._fast_encode is not None and self.thrift_spec is not None: + oprot.trans.write(oprot._fast_encode(self, (self.__class__, self.thrift_spec))) + return + oprot.writeStructBegin('create_snapshot_args') + if self.openstack_id is not None: + oprot.writeFieldBegin('openstack_id', TType.STRING, 1) + oprot.writeString(self.openstack_id.encode('utf-8') if sys.version_info[0] == 2 else self.openstack_id) + oprot.writeFieldEnd() + if self.name is not None: + oprot.writeFieldBegin('name', TType.STRING, 2) + oprot.writeString(self.name.encode('utf-8') if sys.version_info[0] == 2 else self.name) + oprot.writeFieldEnd() + if self.elixir_id is not None: + oprot.writeFieldBegin('elixir_id', TType.STRING, 3) + oprot.writeString(self.elixir_id.encode('utf-8') if sys.version_info[0] == 2 else self.elixir_id) + oprot.writeFieldEnd() + if self.base_tag is not None: + oprot.writeFieldBegin('base_tag', TType.STRING, 4) + oprot.writeString(self.base_tag.encode('utf-8') if sys.version_info[0] == 2 else self.base_tag) + oprot.writeFieldEnd() + oprot.writeFieldStop() + oprot.writeStructEnd() + + def validate(self): + return + + def __repr__(self): + L = ['%s=%r' % (key, value) + for key, value in self.__dict__.items()] + return '%s(%s)' % (self.__class__.__name__, ', '.join(L)) + + def __eq__(self, other): + return isinstance(other, self.__class__) and self.__dict__ == other.__dict__ + + def __ne__(self, other): + return not (self == other) + + +class create_snapshot_result(object): + """ + Attributes: + - success + """ + + thrift_spec = ( + (0, TType.BOOL, 'success', None, None, ), # 0 + ) + + def __init__(self, success=None,): + self.success = success + + def read(self, iprot): + if iprot._fast_decode is not None and isinstance(iprot.trans, TTransport.CReadableTransport) and self.thrift_spec is not None: + iprot._fast_decode(self, iprot, (self.__class__, self.thrift_spec)) + return + iprot.readStructBegin() + while True: + (fname, ftype, fid) = iprot.readFieldBegin() + if ftype == TType.STOP: + break + if fid == 0: + if ftype == TType.BOOL: + self.success = iprot.readBool() + else: + iprot.skip(ftype) + else: + iprot.skip(ftype) + iprot.readFieldEnd() + iprot.readStructEnd() + + def write(self, oprot): + if oprot._fast_encode is not None and self.thrift_spec is not None: + oprot.trans.write(oprot._fast_encode(self, (self.__class__, self.thrift_spec))) + return + oprot.writeStructBegin('create_snapshot_result') + if self.success is not None: + oprot.writeFieldBegin('success', TType.BOOL, 0) + oprot.writeBool(self.success) + oprot.writeFieldEnd() + oprot.writeFieldStop() + oprot.writeStructEnd() + + def validate(self): + return + + def __repr__(self): + L = ['%s=%r' % (key, value) + for key, value in self.__dict__.items()] + return '%s(%s)' % (self.__class__.__name__, ', '.join(L)) + + def __eq__(self, other): + return isinstance(other, self.__class__) and self.__dict__ == other.__dict__ + + def __ne__(self, other): + return not (self == other) + + class attach_volume_to_server_args(object): """ Attributes: diff --git a/portal_client.thrift b/portal_client.thrift index ece3197c..e6eab747 100644 --- a/portal_client.thrift +++ b/portal_client.thrift @@ -224,6 +224,8 @@ 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) + bool attach_volume_to_server(1:string openstack_id,2:int diskspace,3:string volume_id) throws (1:serverNotFoundException e),