def rsync_region( src_region_name, dst_region_name, tag_name=DEFAULT_TAG_NAME, tag_value=DEFAULT_TAG_VALUE, native_only=True): """Duplicates latest snapshots with given tag into dst_region. src_region_name, dst_region_name every latest volume snapshot from src_region will be rsynced to the dst_region; tag_name, tag_value snapshots will be filtered by tag. Tag will be fetched from config by default; native_only sync only snapshots, created in the src_region_name. True by default.""" src_conn = get_region_conn(src_region_name) dst_conn = get_region_conn(dst_region_name) snaps = get_relevant_snapshots(src_conn, tag_name, tag_value, native_only) if not snaps: return with nested(create_temp_inst(src_conn.region), create_temp_inst(dst_conn.region)) as (src_inst, dst_inst): snaps = sorted(snaps, key=get_snap_vol) # Prepare for grouping. for vol, vol_snaps in groupby(snaps, get_snap_vol): latest_snap = sorted(vol_snaps, key=get_snap_time)[-1] for inst in src_inst, dst_inst: logger.debug('Rebooting {0} in {0.region} ' 'to refresh attachments'.format(inst)) inst.reboot() args = (src_region_name, latest_snap.id, dst_region_name, src_inst, dst_inst) try: rsync_snapshot(*args) except: logger.exception('rsync of {1} from {0} to {2} failed'.format( *args))
def rsync_region(src_region_name, dst_region_name, tag_name=None, tag_value=None, native_only=True): """Duplicates latest snapshots with given tag into dst_region. src_region_name, dst_region_name every latest volume snapshot from src_region will be rsynced to the dst_region; tag_name, tag_value snapshots will be filtered by tag. Tag will be fetched from config by default, may be configured per region; native_only sync only snapshots, created in the src_region_name. True by default.""" src_conn = get_region_conn(src_region_name) dst_conn = get_region_conn(dst_region_name) tag_name = tag_name or config.get(src_conn.region.name, 'TAG_NAME') tag_value = tag_value or config.get(src_conn.region.name, 'TAG_VALUE') filters = {'tag-key': tag_name, 'tag-value': tag_value} snaps = src_conn.get_all_snapshots(owner='self', filters=filters) snaps = [snp for snp in snaps if not snp.status == 'error'] _is_described = lambda snap: get_snap_vol(snap) and get_snap_time(snap) snaps = [snp for snp in snaps if _is_described(snp)] if native_only: def is_native(snap, region): return get_descr_attr(snap, 'Region') == region.name snaps = [snp for snp in snaps if is_native(snp, src_conn.region)] with nested(create_temp_inst(src_conn.region), create_temp_inst(dst_conn.region)) as (src_inst, dst_inst): snaps = sorted(snaps, key=get_snap_vol) # Prepare for grouping. for vol, vol_snaps in groupby(snaps, get_snap_vol): latest_snap = sorted(vol_snaps, key=get_snap_time)[-1] for inst in src_inst, dst_inst: logger.debug('Rebooting {0} in {0.region} ' 'to refresh attachments'.format(inst)) inst.reboot() args = (src_region_name, latest_snap.id, dst_region_name, src_inst, dst_inst) try: rsync_snapshot(*args) except: logger.exception('rsync of {1} from {0} to {2} failed'.format( *args))
def create_tmp_volume(region, size): """Format new filesystem.""" with create_temp_inst(region) as inst: earmarking_tag = config.get(region.name, 'TAG_NAME') try: vol = get_region_conn(region.name).create_volume(size, inst.placement) vol.add_tag(earmarking_tag, 'temporary') vol.attach(inst.id, get_avail_dev(inst)) yield vol, mount_volume(vol, mkfs=True) finally: vol.detach(force=True) wait_for(vol, 'available', limit=DETACH_TIME) vol.delete()
def create_empty_snapshot(region, size): """Format new filesystem.""" with create_temp_inst(region) as inst: vol = get_region_conn(region.name).create_volume(size, inst.placement) earmarking_tag = config.get(region.name, 'TAG_NAME') vol.add_tag(earmarking_tag, 'temporary') vol.attach(inst.id, get_avail_dev(inst)) mount_volume(vol, mkfs=True) snap = vol.create_snapshot() snap.add_tag(earmarking_tag, 'temporary') vol.detach(True) wait_for(vol, 'available') vol.delete() return snap