Esempio n. 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)
Esempio n. 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)
Esempio n. 3
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()
Esempio n. 4
0
        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)

            log.info(
                'Finished import of VM %s',
                vm_name,
            )

        if progress:
Esempio n. 5
0
 def after(self):
     log.info('Finished backup bacth %s', self.args.batch)
Esempio n. 6
0
 def before(self):
     log.info('Starting backup bacth %s', self.args.batch)
Esempio n. 7
0
 def after(self):
     log.info('Finished backup bacth %s', self.args.batch)
Esempio n. 8
0
 def before(self):
     log.info('Starting backup bacth %s', self.args.batch)
Esempio n. 9
0
                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()
        finally:
            try:
                for a in reversed(restore_actions):
                    a()
            except Exception, e:
                log.error('Restore action after backup failed, this may leave some temporary snapshots or machine is halted. Error: %s' % e)
        log.info('Finished backup for VM %s (uuid=%s) on server %s', vm_name, uuid, host['name']) 
        


class BackupOneCommand(CommandForOneHost, BackupOne):
    name="backup"
    description="Backups one VM"
    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']))
Esempio n. 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()
Esempio n. 11
0
                    if progress.error:
                        msg = 'Export failed: %s' % progress.result
                        log.error(msg)
                        print >> sys.stderr, msg
                progress.stop()
            rack.shrink()
        finally:
            try:
                for a in reversed(restore_actions):
                    a()
            except Exception, e:
                traceback.print_exc()
                log.error(
                    'Restore action %s after backup failed, this may leave some temporary snapshots or machine is halted. Error: %s %s',
                    str(a), type(e), str(e))
        log.info('Finished backup for VM %s (uuid=%s) on server %s', vm_name,
                 uuid, host['name'])


class BackupOneCommand(CommandForOneVM, BackupOne):
    name = "backup"
    description = "Backups one VM"

    def execute_for_one(self, session, host):
        vm_name = self.args.vm
        show_progress = not self.args.no_progress
        force_shutdown = self.args.shutdown

        vm_id = self.find_vm(session, host)
        storage = Storage(self.config['storage_root'],
                          self.config.get('storage_retain', 3),
                          self.config.get('compress_level', 1),
Esempio n. 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()
Esempio n. 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()