Beispiel #1
0
 def test_compress(self):
     def write_test_file( s):
         w = s.get_writer()
         with open(TEST_FILE,'rb') as f:
             while True:
                 read= f.read(10000)
                 if not read:
                     break
                 w.write(read)
         slot.close()
     test_file_size=os.stat(TEST_FILE).st_size
     sr=Storage(self.dir, 2,compression_level= 0)
     rack=sr.get_rack_for('test')
     slot=rack.create_slot()
     write_test_file(slot)
     res= rack.last_slot
     self.assertEqual(test_file_size, res.size_uncompressed)
     self.assertTrue(test_file_size < res.size)
     
     sr=Storage(self.dir, 2,compression_level= 1)
     rack=sr.get_rack_for('test')
     slot=rack.create_slot()
     write_test_file(slot)
     res= rack.last_slot
     self.assertEqual(test_file_size, res.size_uncompressed)
     self.assertTrue(test_file_size > res.size)
     comp_size = res.size
     
     sr=Storage(self.dir, 2,compression_level= 9)
     rack=sr.get_rack_for('test')
     slot=rack.create_slot()
     write_test_file(slot)
     res= rack.last_slot
     self.assertEqual(test_file_size, res.size_uncompressed)
     self.assertTrue(comp_size > res.size)
Beispiel #2
0
 def test_compress(self):
     test_file_size=os.stat(TEST_FILE).st_size
     sr=Storage(self.dir, 2,compression_level= 0)
     uid=gen_uuid()
     rack=sr.get_rack_for('test',uid )
     slot=rack.create_slot()
     write_sample_file(slot)
     res= rack.last_slot
     self.assertEqual(res._comp_method, 'client')
     self.assertEqual(test_file_size, res.size_uncompressed)
     self.assertTrue(test_file_size < res.size)
     
     sr=Storage(self.dir, 2,compression_level= 1)
     rack=sr.get_rack_for('test', uid)
     slot=rack.create_slot()
     write_sample_file(slot)
     res= rack.last_slot
     self.assertEqual(test_file_size, res.size_uncompressed)
     self.assertTrue(test_file_size > res.size)
     comp_size = res.size
     
     sr=Storage(self.dir, 2,compression_level= 9)
     rack=sr.get_rack_for('test', uid)
     slot=rack.create_slot()
     write_sample_file(slot)
     res= rack.last_slot
     self.assertEqual(test_file_size, res.size_uncompressed)
     self.assertTrue(comp_size > res.size)
Beispiel #3
0
 def after(self):
     def fmt_date(dt):
         if not dt:
             return ''
         return dt.strftime('%Y-%m-%d %H:%M')
     def fmt_dur(d):
         if not d:
             return ''
         return '%0.1f' % (d / 60.0)
     s=Storage(self.config['storage_root'])
     stats=s.get_status()
     tab=[]
     for vm in self.result:
         stat=stats.get(vm)
         row=self.result[vm]
         if stat:
             row.append(fmt_date(stat['last_backup']))
             row.append(fmt_dur(stat['duration']))
         else:
             row.extend(['', ''])
         tab.append(row)
     for vm in stats:
         if not self.result.has_key(vm):
             tab.append(['', vm, '', '', '', fmt_date(stats[vm]['last_backup']), fmt_dur(stats[vm]['duration'])])
     print tabulate(tab, ['Host', 'VM', 'State', 'AutoBck', 'AutoBck Batch', 'Last Backup', 'Dur. (m)'])
Beispiel #4
0
    def execute_for_each(self, session, host):
        all_vms = session.xenapi.VM.get_all_records()
        prog = not self.args.no_progress

        storage = Storage(self.config['storage_root'],
                          self.config.get('storage_retain', 3),
                          self.config.get('compress_level', 1))
        batch = self.args.batch
        with RuntimeLock(BACKUP_LOCK, 'Another backup is running!'):
            for vm_id in all_vms:
                vm = all_vms[vm_id]
                if not vm['is_control_domain'] and  \
                vm['other_config'].get(AUTOBACKUP_KEY) and \
                (not batch or batch == vm['other_config'].get(AUTOBACKUP_BATCH)):
                    vm_name = vm['name_label']
                    if prog:
                        print "Host %s - VM %s" % (host['name'], vm_name)
                    try:
                        self.backup(session,
                                    vm_id,
                                    vm_name,
                                    host,
                                    storage,
                                    self.args.shutdown,
                                    show_progress=prog)
                    except Exception, e:
                        log.error('Error while backing up VM %s on host %s',
                                  vm_name, host['name'])
Beispiel #5
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)
Beispiel #6
0
def write_file(dir_name,f, compression_level=0, block_size=10000):
    def w(s):
        w = s.get_writer()
        while True:
            read= f.read(block_size)
            if not read:
                break
            w.write(read)
        slot.close()
    sr=Storage(dir_name, 2,compression_level= compression_level)
    rack=sr.get_rack_for(TEST_RACK, '1234')
    slot=rack.create_slot()
    w(slot)
Beispiel #7
0
 def test_no_compress_server(self):
     test_file_size=os.stat(TEST_FILE).st_size                
     sr=Storage(self.dir, 2,compression_method= 'server')
     uid=gen_uuid()
     rack=sr.get_rack_for('test',uid)
     slot=rack.create_slot()
     write_sample_file(slot)
     res= rack.last_slot
     self.assertEqual(res._comp_method, 'server')
     self.assertEqual(test_file_size, res.size_uncompressed)
     self.assertEqual(test_file_size , res.size)
     
     self.assertTrue(slot._path.endswith('.xva.gz'))
     self.assertTrue(res._path.endswith('.xva.gz'))
Beispiel #8
0
 def test_basic(self):
     sr=Storage(self.dir, 2)
     uid=gen_uuid()
     rack=sr.get_rack_for('test', uid)
     slot=rack.create_slot()
     msg='Hi There Is Anybody In There'
     slot.get_writer().write(msg)
     time.sleep(0.001)
     slot.close()
     self.assertEqual(1, len(rack.get_all_slots()))
     m=rack.last_slot
     self.assertEqual(len(msg), m.size_uncompressed)
     self.assertTrue(m.duration>0)
     m.close()
     time.sleep(0.01)
     slot=rack.create_slot()
     slot.get_writer().write('Second')
     slot.close()
     self.assertEqual(2, len(rack.get_all_slots()))
     slot=rack.last_slot
     r=slot.get_reader().read()
     slot.close()
     self.assertEqual('Second',r)
     
     slot=rack.create_slot()
     w=slot.get_writer()
     w.write('aaa')
     w.close()
     self.assertEqual(2, len(rack.get_all_slots()))
     
     time.sleep(0.01)
     slot=rack.create_slot()
     slot.get_writer().write('Third')
     slot.close()
     self.assertEqual(3, len(rack.get_all_slots()))
     slot=rack.last_slot
     r=slot.get_reader().read()
     slot.close()
     self.assertEqual('Third',r)
     
     rack.shrink()
     self.assertEqual(2, len(rack.get_all_slots()))
     
     r=slot.get_reader().read()
     slot.close()
     self.assertEqual('Third',r)
Beispiel #9
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

        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),
                          self.config.get('compress', 'client'))
        with RuntimeLock(BACKUP_LOCK, 'Another backup is running!'):
            self.backup(session,
                        vm_id,
                        vm_name,
                        host,
                        storage,
                        force_shutdown,
                        show_progress,
                        self.config.get('compress') == 'server',
                        insecure=self.args.insecure)
Beispiel #10
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)
Beispiel #11
0
 def test_uuid(self):
     sr=Storage(self.dir, 2,compression_level= 0)
     uid=gen_uuid()
     rack=sr.get_rack_for('test',uid )
     slot=rack.create_slot()
     write_sample_file(slot)
     slot.close()
     uid2=gen_uuid()
     rack=sr.get_rack_for('test',uid2 )
     self.assertEqual(rack.last_slot, None)
     self.assertTrue(os.path.exists(rack._path))
     uid3=gen_uuid()
     rack=sr.get_rack_for('test',uid3,exists=True)
     self.assertFalse(rack)
     
     try:
         r=sr.find_rack_for('test')
         self.fail('Should raise exception')
     except Storage.NotFound:
         pass
     
     try:
         r=sr.find_rack_for('test', '123')
         self.fail('Should raise exception')
     except Storage.NotFound:
         pass
     
     r=sr.find_rack_for('test', uid)
     self.assertTrue(r)
     s=r.last_slot
     self.assertTrue(s)
     
     r=sr.find_rack_for('test', uid[:5].upper())
     self.assertTrue(r)
     s=r.last_slot
     self.assertTrue(s)
     
     r=sr.find_rack_for('test', uid2)
     self.assertTrue(r)
     s=r.last_slot
     self.assertFalse(s)
Beispiel #12
0
 def test_no_compress(self):
     test_file_size=os.stat(TEST_FILE).st_size
     sr=Storage(self.dir, 2,compression_method= None)
     uid=gen_uuid()
     rack=sr.get_rack_for('test', uid)
     slot=rack.create_slot()
     write_sample_file(slot)
     res= rack.last_slot
     self.assertEqual(res._comp_method, None)
     self.assertEqual(test_file_size, res.size_uncompressed)
     self.assertEqual(test_file_size , res.size)
     
     self.assertTrue(slot._path.endswith('.xva'))
     self.assertTrue(res._path.endswith('.xva'))
     self.assertTrue(res.duration)
     print 'duration',res.duration
     self.assertTrue(res.created)
     print 'created', res.created
     
     st=sr.get_status()
     self.assertTrue(st[uid])
Beispiel #13
0
class RestoreOneCommand(CommandForOneVM):
    name = "restore"
    description = "Restores backup to given host"

    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
        if srid:
            try:
                srid = session.xenapi.SR.get_by_uuid(srid)
            except XenAPI.Failure, f:
                raise CommandError('Invalid SR uuid: %s' % f.details[0])
        storage = Storage(self.config['storage_root'],
                          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))