def test_get_regions_explicit_master(self): """test get_regions with the paremeter target='master'""" glancesync = GlanceSync(self.config) result = glancesync.get_regions(target='master') result.sort() expected = ['Burgos'] expected.sort() self.assertEquals(result, expected) result = glancesync.get_regions(target='master', omit_master_region=False) result.sort() expected = ['Burgos', 'Valladolid'] expected.sort() self.assertEquals(result, expected)
def test_get_regions_other_target(self): """test get_regions with the paremeter target='other'""" glancesync = GlanceSync(self.config) result = glancesync.get_regions(target='other') result.sort() expected = ['other:Madrid', 'other:Region2'] expected.sort() self.assertEquals(result, expected) # omit_master_region does not affect a region different # from master result = glancesync.get_regions(target='other', omit_master_region=False) result.sort() self.assertEquals(result, expected)
def test_get_regions_include_master(self): """test get_regions with the paremeter to include master""" glancesync = GlanceSync(self.config) result = glancesync.get_regions(omit_master_region=False) result.sort() expected = ['Burgos', 'Valladolid'] expected.sort() self.assertEquals(result, expected)
def test_get_regions(self): """test get_regions method""" glancesync = GlanceSync(self.config) result = glancesync.get_regions() result.sort() expected = ['Burgos'] expected.sort() self.assertEquals(result, expected)
class Sync(object): def __init__(self, regions, override_d=None): """init object""" GlanceSync.init_logs() self.glancesync = GlanceSync(options_dict=override_d) regions_expanded = list() already_sorted = True for region in regions: if region.endswith(':'): regions_expanded.extend(self.glancesync.get_regions( target=region[:-1])) already_sorted = False else: regions_expanded.append(region) regions = regions_expanded if not regions: regions = self.glancesync.get_regions() already_sorted = False if not already_sorted: regions_unsorted = regions regions = list() for region in self.glancesync.preferable_order: if region in regions_unsorted: regions.append(region) regions_unsorted.remove(region) regions.extend(regions_unsorted) self.regions = regions def report_status(self): """Report the synchronisation status of the regions""" for region in self.regions: try: stream = StringIO.StringIO() self.glancesync.export_sync_region_status(region, stream) print(stream.getvalue()) except Exception: # Don't do anything. Message has been already printed # try next region continue def parallel_sync(self): """Run the synchronisation in several regions in parallel. The synchronisation inside the region is sequential (i.e. several regions are synchronised simultaneously, but only one image at time is uploaded for each region)""" max_children = self.glancesync.max_children now = datetime.datetime.now() datestr = str(now.year) + str(now.month).zfill(2) + \ str(now.day).zfill(2) + '_' + str(now.hour).zfill(2) +\ str(now.minute).zfill(2) msg = '======Master is ' + self.glancesync.master_region print(msg) sys.stdout.flush() os.mkdir('sync_' + datestr) children = dict() for region in self.regions: try: if len(children) >= max_children: self._wait_child(children) pid = os.fork() if pid > 0: children[pid] = region continue else: path = os.path.join('sync_' + datestr, region + '.txt') handler = logging.FileHandler(path) handler.setFormatter(logging.Formatter('%(message)s')) logger = self.glancesync.log # Remove old handlers for h in logger.handlers: logger.removeHandler(h) logger.addHandler(handler) logger.setLevel(logging.INFO) logger.propagate = 0 self.glancesync.sync_region(region) # After a fork, os_exit() and not sys.exit() must be used. os._exit(0) except Exception: raise sys.stderr.flush() sys.exit(-1) while len(children) > 0: self._wait_child(children) print('All is done.') def sequential_sync(self, dry_run=False): """Run the synchronisation sequentially (that is, do not start the synchronisation to a region before the previous one was completed or failed :param dry_run: if true, do not synchronise images actually """ msg = '======Master is ' + self.glancesync.master_region print(msg) for region in self.regions: try: msg = "======" + region print(msg) sys.stdout.flush() self.glancesync.sync_region(region, dry_run=dry_run) except Exception: # Don't do anything. Message has been already printed # try next region continue def _wait_child(self, children): """ Wait until one of the regions ends its synchronisation and then print the result :param children: :return: a dictionary or regions, indexed by the pid of the process """ finish_direct_child = False while not finish_direct_child: (pid, status) = os.wait() if pid not in children: continue else: finish_direct_child = True if status == 0: msg = 'Region {0} has finished'.format(children[pid]) print(msg) else: msg = 'Region {0} has finished with errors' print(msg.format(children[pid])) del children[pid] sys.stdout.flush() def show_regions(self): """print a full list of the regions available (excluding the master region) in all the targets defined in the configuration file""" regions = self.glancesync.get_regions() for target in self.glancesync.targets.keys(): if target == 'facade' or target == 'master': continue regions.extend(self.glancesync.get_regions(target=target)) print(' '.join(regions)) def make_backup(self): """make a backup of the metadata in the regions specified at the constructor (in addition to the master region). The backup is created in a directory named 'backup_glance_' with the date and time as suffix There is a file for each region (the name is backup_<region>.csv) and inside the file a line for each image. Only the information about public images/ the images owned by the tenant, can be obtained, regardless if the user is an admin. This is a limitation of the glance API""" now = datetime.datetime.now().isoformat() directory = 'backup_glance_' + now os.mkdir(directory) regions = set(self.regions) regions.add(self.glancesync.master_region) for region in regions: try: self.glancesync.backup_glancemetadata_region(region, directory) except Exception: # do nothing. Already logged. continue