예제 #1
0
    def execute_for_one(self, session, host):
        vm_name = self.args.vm
        show_progress = not self.args.no_progress
        restore = self.args.restore
        srid = self.args.sr_id

        storage = Storage(self.config['storage_root'],
                          self.config.get('storage_retain', 3))
        rack = storage.get_rack_for(vm_name, exists=True)
        if not rack:
            raise CommandError('No backup to restore for VM %s' % vm_name)
        slot = rack.last_slot
        if not slot:
            raise CommandError('No backup to restore for VM %s' % vm_name)

        sid = session._session
        progress = None
        task_id = None
        if show_progress:
            task_id = session.xenapi.task.create('VM.import',
                                                 'Import of %s' % vm_name)
            log.debug("Starting progress monitor")
            progress = ProgressMonitor(session, task_id)
            progress.start()
        with Client(host['url']) as c:
            log.info('Starting import of VM %s' % vm_name)
            params = {'session_id': sid, 'task_id': task_id}
            if srid:
                params['sr_id'] = srid
            if restore:
                params['restore'] = restore
            resp = c.put('/import', slot.get_reader(), slot.size_uncompressed,
                         params)

            log.info('Finished import of VM %s' % vm_name)
예제 #2
0
 def execute_for_one(self, session, host):
     vm_name=self.args.vm
     show_progress=not self.args.no_progress
     restore= self.args.restore
     srid = self.args.sr_id
     
     storage=Storage(self.config['storage_root'], self.config.get('storage_retain', 3))
     rack=storage.get_rack_for(vm_name, exists=True)
     if not rack:
         raise CommandError('No backup to restore for VM %s' % vm_name)
     slot=rack.last_slot
     if not slot:
         raise CommandError('No backup to restore for VM %s' % vm_name)
     
     sid=session._session
     progress=None
     task_id=None
     if show_progress:
                 task_id=session.xenapi.task.create('VM.import', 'Import of %s'%vm_name)
                 log.debug("Starting progress monitor")
                 progress=ProgressMonitor(session, task_id)
                 progress.start()
     with Client(host['url']) as c:
         log.info('Starting import of VM %s'% vm_name)
         params= {'session_id':sid,  'task_id':task_id}
         if srid:
             params['sr_id']=srid
         if restore:
             params['restore']=restore
         resp=c.put('/import', slot.get_reader(), slot.size_uncompressed, params)
    
         
         log.info('Finished import of VM %s'% vm_name)
예제 #3
0
 def execute_for_one(self, session, host):
     ids = session.xenapi.VM.get_by_name_label(self.args.vm)
     log.debug('Found this VMs %s', ids)
     if not ids:
         raise CommandError('VM %s not found on server %s' %
                            (self.args.vm, host['name']))
     elif len(ids) > 1:
         raise CommandError('Name %s is not unique, please fix' %
                            self.args.vm)
     id = ids[0]  # @ReservedAssignment
     session.xenapi.VM.remove_from_other_config(id, AUTOBACKUP_KEY)
     session.xenapi.VM.remove_from_other_config(id, AUTOBACKUP_BATCH)
예제 #4
0
 def execute_for_one(self, session, host):
     ids=session.xenapi.VM.get_by_name_label(self.args.vm)
     log.debug('Found this VMs %s', ids)
     if not ids:
         raise CommandError('VM %s not found on server %s' % (self.args.vm, host['name']))
     elif len(ids)>1:
         raise CommandError('Name %s is not unique, please fix'% self.args.vm)
     id=ids[0]  # @ReservedAssignment
     session.xenapi.VM.remove_from_other_config(id, AUTOBACKUP_KEY)
     session.xenapi.VM.add_to_other_config(id, AUTOBACKUP_KEY, bool2str(True))
     if self.args.batch:
         session.xenapi.VM.remove_from_other_config(id, AUTOBACKUP_BATCH)
         session.xenapi.VM.add_to_other_config(id, AUTOBACKUP_BATCH, self.args.batch)
     else:
         session.xenapi.VM.remove_from_other_config(id, AUTOBACKUP_BATCH)
예제 #5
0
 def execute_for_one(self, session, host):
     vm_name=self.args.vm
     show_progress=not self.args.no_progress
     force_shutdown=self.args.shutdown
     
     ids=session.xenapi.VM.get_by_name_label(vm_name)
     log.debug('Found this VMs %s', ids)
     if not ids:
         raise CommandError('VM %s not found on server %s' % (vm_name, host['name']))
     elif len(ids)>1:
         raise CommandError('Name %s is not unique, please fix'% vm_name)
     
     vm_id=ids[0]
     storage=Storage(self.config['storage_root'], self.config.get('storage_retain', 3),
                     self.config.get('compress_level', 1))
     with RuntimeLock(BACKUP_LOCK,'Another backup is running!'):
         self.backup(session, vm_id, vm_name, host, storage, force_shutdown, show_progress)
예제 #6
0
    def execute_for_one(self, session, host):
        vm_name = self.args.vm
        show_progress = not self.args.no_progress
        force_shutdown = self.args.shutdown

        ids = session.xenapi.VM.get_by_name_label(vm_name)
        log.debug('Found this VMs %s', ids)
        if not ids:
            raise CommandError('VM %s not found on server %s' %
                               (vm_name, host['name']))
        elif len(ids) > 1:
            raise CommandError('Name %s is not unique, please fix' % vm_name)

        vm_id = ids[0]
        storage = Storage(self.config['storage_root'],
                          self.config.get('storage_retain', 3),
                          self.config.get('compress_level', 1))
        with RuntimeLock(BACKUP_LOCK, 'Another backup is running!'):
            self.backup(session, vm_id, vm_name, host, storage, force_shutdown,
                        show_progress)
예제 #7
0
 def backup(self, session, vm_id, vm_name, host, storage, force_shutdown=False, show_progress=False):
     uuid=session.xenapi.VM.get_uuid(vm_id)  # @ReservedAssignment
     state = session.xenapi.VM.get_power_state(vm_id)
     restore_actions=[]
     try:
         
         if state =='Paused':
             log.error('Cannot backup VM is state Paused')
             return
         elif state =='Suspended':
             log.warn('Backing up machine in suspended stated')
         elif state =='Running':
             if not force_shutdown:
                 try:
                     tt_id=session.xenapi.VM.snapshot(vm_id,'Temp.backup of %s'% vm_name )
                 except XenAPI.Failure, f:
                     if f.details[0]== 'SR_OPERATION_NOT_SUPPORTED':
                         raise CommandError('Cannot create snapshot of vm %s (try backup in halted state, or detach disks, that do not support snapshots)'% vm_name)
                     else:
                         raise
                 uuid=session.xenapi.VM.get_uuid(tt_id)
                 restore_actions.append(lambda: uninstall_VM(session, tt_id))
                 log.debug('Made snapshot of %s to uuid: %s', vm_name, uuid)
             else:
                 log.debug('Shutting down VM')
                 session.xenapi.VM.clean_shutdown(vm_id)
                 restore_actions.append(lambda: session.xenapi.VM.start(vm_id, False, False))
     
         sid=session._session
         rack=storage.get_rack_for(vm_name)
         log.info('Starting backup for VM %s (uuid=%s) on server %s', vm_name, uuid, host['name'])
         progress=None
         with Client(host['url']) as c:
             resp=c.get('/export', {'session_id':sid, 'uuid': uuid})
             task_id=resp.getheader('task-id')
             restore_actions.append(lambda: cancel_task(session,task_id))
             if show_progress:
                 log.debug("Starting progress monitor")
                 progress=ProgressMonitor(session, task_id)
                 progress.start()
             s=rack.create_slot()
             writer=s.get_writer()
             bufsize=1048576
             while True:
                 data=resp.read(bufsize)
                 if not data: break
                 writer.write(data)
             s.close() # close only if finished
         if progress:
             progress.stop()
         rack.shrink()
예제 #8
0
                          self.config.get('storage_retain', 3),
                          compression_method=self.config.get(
                              'compress', 'client'))
        try:
            rack = storage.find_rack_for(vm_name, self.args.uuid)
        except Storage.NotFound, e:
            raise CommandError(str(e))
        slot = rack.last_slot
        if not slot:
            raise CommandError('No backup to restore for VM %s' % vm_name)

        sid = session._session

        task_id = session.xenapi.task.create('VM.import',
                                             'Import of %s' % vm_name)
        log.debug("Starting progress monitor")
        progress = ProgressMonitor(session,
                                   task_id,
                                   print_progress=show_progress)
        progress.start()

        with Client(unsecure(host['url'], self.args.insecure)) as c:
            log.info('Starting import of VM %s' % vm_name)
            params = {'session_id': sid, 'task_id': task_id}
            if srid:
                params['sr_id'] = srid
            if restore:
                params['restore'] = restore
            log.debug('PUT with following params: %s', params)
            _resp = c.put('/import', slot.get_reader(), slot.size_uncompressed,
                          params)
예제 #9
0
 def restart_vm():
     session.xenapi.VM.start(vm_id, False, False)
     log.debug('Started VM')
예제 #10
0
    def backup(self,
               session,
               vm_id,
               vm_name,
               host,
               storage,
               force_shutdown=False,
               show_progress=False,
               compress_on_server=False,
               insecure=False):
        uuid = session.xenapi.VM.get_uuid(vm_id)  # @ReservedAssignment
        vm_uuid = uuid
        state = session.xenapi.VM.get_power_state(vm_id)
        restore_actions = []
        try:

            if state == 'Paused':
                log.error('Cannot backup VM is state Paused')
                return
            elif state == 'Suspended':
                log.warn('Backing up machine in suspended stated')
            elif state == 'Running':
                if not force_shutdown:
                    try:
                        tt_id = session.xenapi.VM.snapshot(
                            vm_id, SNAPSHOT_PREFIX + vm_name)
                    except XenAPI.Failure, f:
                        if f.details[0] == 'SR_OPERATION_NOT_SUPPORTED':
                            raise CommandError(
                                'Cannot create snapshot of vm %s (try backup in halted state, or detach disks, that do not support snapshots)'
                                % vm_name)
                        else:
                            raise
                    uuid = session.xenapi.VM.get_uuid(tt_id)
                    restore_actions.append(
                        lambda: uninstall_VM(session, tt_id))
                    log.debug('Made snapshot of %s(%s) to snapshot uuid: %s',
                              vm_name, vm_uuid, uuid)
                else:
                    log.debug('Shutting down VM')
                    session.xenapi.VM.clean_shutdown(vm_id)

                    def restart_vm():
                        session.xenapi.VM.start(vm_id, False, False)
                        log.debug('Started VM')

                    restore_actions.append(restart_vm)

            sid = session._session
            rack = storage.get_rack_for(vm_name, vm_uuid)
            log.info('Starting backup for VM %s (uuid=%s) on server %s',
                     vm_name, vm_uuid, host['name'])
            progress = None
            with Client(unsecure(host['url'], insecure)) as c:
                params = {'session_id': sid, 'uuid': uuid}
                if compress_on_server:
                    params['use_compression'] = "true"
                resp = c.get('/export', params)
                task_id = resp.getheader('task-id')
                restore_actions.append(lambda: cancel_task(session, task_id))
                if show_progress:
                    log.debug("Starting progress monitor")
                    progress = ProgressMonitor(session, task_id)
                    progress.start()
                s = rack.create_slot()
                writer = s.get_writer()
                bufsize = 65536  # 64k is enough - around this size got best performance
                while True:
                    data = resp.read(bufsize)
                    if not data: break
                    writer.write(data)
                s.close()  # close only if finished
            if progress:
                progress.join(10)
                if progress.is_alive():
                    log.warn('Task did not finished')
                else:
                    if progress.error:
                        msg = 'Export failed: %s' % progress.result
                        log.error(msg)
                        print >> sys.stderr, msg
                progress.stop()
            rack.shrink()
예제 #11
0
 def restart_vm():
     session.xenapi.VM.start(vm_id, False, False)
     log.debug('Started VM')
예제 #12
0
 def backup(self, session, vm_id, vm_name, host, storage, force_shutdown=False, show_progress=False,
            compress_on_server=False, insecure=False):
     uuid=session.xenapi.VM.get_uuid(vm_id)  # @ReservedAssignment
     vm_uuid=uuid
     state = session.xenapi.VM.get_power_state(vm_id)
     restore_actions=[]
     try:
         
         if state =='Paused':
             log.error('Cannot backup VM is state Paused')
             return
         elif state =='Suspended':
             log.warn('Backing up machine in suspended stated')
         elif state =='Running':
             if not force_shutdown:
                 try:
                     tt_id=session.xenapi.VM.snapshot(vm_id,SNAPSHOT_PREFIX+ vm_name )
                 except XenAPI.Failure, f:
                     if f.details[0]== 'SR_OPERATION_NOT_SUPPORTED':
                         raise CommandError('Cannot create snapshot of vm %s (try backup in halted state, or detach disks, that do not support snapshots)'% vm_name)
                     else:
                         raise
                 uuid=session.xenapi.VM.get_uuid(tt_id)
                 restore_actions.append(lambda: uninstall_VM(session, tt_id))
                 log.debug('Made snapshot of %s(%s) to snapshot uuid: %s', vm_name, vm_uuid, uuid)
             else:
                 log.debug('Shutting down VM')
                 session.xenapi.VM.clean_shutdown(vm_id)
                 def restart_vm():
                     session.xenapi.VM.start(vm_id, False, False)
                     log.debug('Started VM')
                 restore_actions.append(restart_vm)
     
         sid=session._session
         rack=storage.get_rack_for(vm_name, vm_uuid)
         log.info('Starting backup for VM %s (uuid=%s) on server %s', vm_name, vm_uuid, host['name'])
         progress=None
         with Client(unsecure(host['url'],insecure)) as c:
             params={'session_id':sid, 'uuid': uuid}
             if compress_on_server:
                 params['use_compression']="true"
             resp=c.get('/export', params)
             task_id=resp.getheader('task-id')
             restore_actions.append(lambda: cancel_task(session,task_id))
             if show_progress:
                 log.debug("Starting progress monitor")
                 progress=ProgressMonitor(session, task_id)
                 progress.start()
             s=rack.create_slot()
             writer=s.get_writer()
             bufsize=65536 # 64k is enough - around this size got best performance
             while True:
                 data=resp.read(bufsize)
                 if not data: break
                 writer.write(data)
             s.close() # close only if finished
         if progress:
             progress.join(10)
             if progress.is_alive():
                 log.warn('Task did not finished')
             else:
                 if progress.error:
                     msg='Export failed: %s'%progress.result
                     log.error(msg)
                     print >>sys.stderr, msg
             progress.stop()
         rack.shrink()
예제 #13
0
    def backup(self,
               session,
               vm_id,
               vm_name,
               host,
               storage,
               force_shutdown=False,
               show_progress=False):
        uuid = session.xenapi.VM.get_uuid(vm_id)  # @ReservedAssignment
        state = session.xenapi.VM.get_power_state(vm_id)
        restore_actions = []
        try:

            if state == 'Paused':
                log.error('Cannot backup VM is state Paused')
                return
            elif state == 'Suspended':
                log.warn('Backing up machine in suspended stated')
            elif state == 'Running':
                if not force_shutdown:
                    try:
                        tt_id = session.xenapi.VM.snapshot(
                            vm_id, 'Temp.backup of %s' % vm_name)
                    except XenAPI.Failure, f:
                        if f.details[0] == 'SR_OPERATION_NOT_SUPPORTED':
                            raise CommandError(
                                'Cannot create snapshot of vm %s (try backup in halted state, or detach disks, that do not support snapshots)'
                                % vm_name)
                        else:
                            raise
                    uuid = session.xenapi.VM.get_uuid(tt_id)
                    restore_actions.append(
                        lambda: uninstall_VM(session, tt_id))
                    log.debug('Made snapshot of %s to uuid: %s', vm_name, uuid)
                else:
                    log.debug('Shutting down VM')
                    session.xenapi.VM.clean_shutdown(vm_id)
                    restore_actions.append(
                        lambda: session.xenapi.VM.start(vm_id, False, False))

            sid = session._session
            rack = storage.get_rack_for(vm_name)
            log.info('Starting backup for VM %s (uuid=%s) on server %s',
                     vm_name, uuid, host['name'])
            progress = None
            with Client(host['url']) as c:
                resp = c.get('/export', {'session_id': sid, 'uuid': uuid})
                task_id = resp.getheader('task-id')
                restore_actions.append(lambda: cancel_task(session, task_id))
                if show_progress:
                    log.debug("Starting progress monitor")
                    progress = ProgressMonitor(session, task_id)
                    progress.start()
                s = rack.create_slot()
                writer = s.get_writer()
                bufsize = 1048576
                while True:
                    data = resp.read(bufsize)
                    if not data: break
                    writer.write(data)
                s.close()  # close only if finished
            if progress:
                progress.stop()
            rack.shrink()