RESILIENCY
==========

1) Offline and de-register guest disks      (guest disk metadata)
2) Unmount VMFS datastore                   (esx vdisk metadata)
3) Detach IBOX LUN                          (ibox vol metadata)
4) Refresh IBOX snapshot                    (ibox vol metadata, esx hosts metadata)
    - iff new snapshot, map it and unmap prior and rescan storage 
5) re-attach IBOX LUN to one host           (esx host metadata, ibox map metadata)
6) re-sign datastore                        (ibox vol metadata, esx hosts metadata)
7) attach lun to remaining hosts in cluster (ibox lun metadata, esx hosts metadata)
8) register new disks to VM                 (esx_vdisk metadata. esx_vm metadata)
9) verify disk online and volume mounted    (guest disk metadata)


How would I know if the snapshot has been refreshed or not?  
    - Evaluate the parent snapshot metadata.  If the targeted parent is a different ID or is newer than the guest snap, we need to proceed with the refresh
What if the prior execution died and left the VMFS unmounted?
    - scan ibox for snaps with snapsync metadata matching what we're looking for; select newest metadata?
    - we just need to discover the vdisk and local disk metadata in order to put it back together again


CODING
======
DONE: Verify disk state after registering new vdisk and bring online or mount
Verify no stray vdisk registrations before beginning deconstruction process
Save meta-data and state information somewhere for resilient re-start

Handle config change when new disks are added to origin server
Perform initial copy-data mapping
Snapshot creation tool for the production side


OPERATIONAL FLOW
================
   1) Define a copy-data source by running snapsync define-source on the originating 
      (source) system.  This will perform discovery on the source system to determine
      the relationship between application mounts and the underlying volumes and backing
      store files that will ultimately need to be mapped on a copy-data instance
   2) Configure a copy-data instance by running the snapsync define-copy command
      on a target system.  This command will persist the desired mapping relationships
      in metadata, using the copy-instance tag 
   3) Capture a new snapshot image from the production system by running the snapsync
      snap command and specifying an application tag.  The snapped volumes will be 
      tagged with a user-provided tag to assist in snapshot selection for mounting
   4) Mount a copy-data instance by running the snapsync mount command with a copy-data 
      tag indicating which copy-dat instance is to be mounted.  
   5) Unmount or Refresh the copy-data instance using the unmount or refresh command and
      specifying the copy-data instance name


SUBCOMMANDS
===========
discover --vcenter <vcenter_name>

The discover subcommand discovers the relationship between the specified mountpoint and the underlying datastore and Infinidat volume.  It is used to assess the environment prior to executing a snapshot or refresh operation.

snapshot --mountpoint <drive letter or mounted folder path> <... additional mounts>

The snapshot command will capture a snapshot for the underlying VMFS stores contining the specified mountpoint

refresh  [--existing | --from_snap <snap_name>]

The refresh subcommand is invoked from the copy-data platform, and is used to replace the existing copy-data with another version. When --existing is not specified, snapsync will find the master volume and will list the availaboe RO child snaps and prompt the user to make a choice.  Once a selection is made, snapsync will make a RW snap of the RO snap and attach that to the guest.  If --from_snap is specified, it should indicate a RO snap from which a RW snap will be cloned and attached to the guest

snapsync configure --tag <app_name> --source [<vcenter>:]<hostname>  \
        --target [<vcenter>:]<hostname> --map <source>:<target> \
        --map <source>:target --map <source>:<target> 

    The configure command is run on the production or source system source host is probed for drive / datastore relationships.  If the source or target host have a colon in the string, it's split and the first is assumed to be a hypervisor.  Discovery will proceed accordingly.  The mapping is compared to the source and target systems for compatibility.  Perhaps we'll want to verify all the source vols for the maps are in a consistency group; don't know if that's a requirement.  Assuming compatibility is verified, the relationships are persisted as metadata attached to the target infinidat host entity (ESX host in this case).  This metadata blob will be a data dictionary keyed by the guest within the hypervisor.  Same format even for standalone hosts, it'll just be a dictionary with a single key.  

snapsync unmount [--guest-only] --app <app_name_tag> --host [<vcenter>:]<hostname>
    The source host is discovered, the mapping metadata retrieved, and copy-data mounts associated with the specified app tag are unmounted.  Unless --guest-only is specified, the underlying datastores are also unmounted and the infinidat volume unmapped from the hypervisor.
    
snapsync query_snaps --snap <snap_tag> [<vcenter>:]<hostname>
    The copy-host is discovered, mapping metadata retrieved, and now we know the source volumes / cg.  We query the snaps of the source vols/cg and present this information along with the snap name metadata tag that can be used for selection
    
To configure a copy-data instance: snapsync prepare --host <x> --drives g:/@h:/,h:/@c:/appdata/foo

To mount a snap set: snapsync mount --app <app_name_tag> --snap <snap_tag> --host [<vcenter>:]<hostname>
    The copy-dat host is discovered, mapping metadata retrieved, snap is queried by tag.  We proceed with our standard mapping process.  


TESTING
=======

INSTALLING WITH AV
===================

Insert the following in c:/program files/infinidat/snapsync/parts/python/lib/site-packages/zc/buildout/easy_install.py
# We have installed the dist. Now try to rename/move it.
        newloc = os.path.join(dest, os.path.basename(tmp_loc))
        for tries in range(3):
            if tries:
                print(f"re-driving rename {tmp_loc} -> {newloc}")
            try:
                os.rename(tmp_loc, newloc)
            except OSError:
                # Might be for various reasons.  If it is because newloc already
                # exists, we can investigate.
                if not os.path.exists(newloc):
                    # No, it is a different reason.  Give up.
                    raise
                # Try to use it as environment and check if our project is in it.
                newdist = _get_matching_dist_in_location(dist, newloc)
                if newdist is None:
                    # Path exists, but is not our package.  We could
                    # try something, but it seems safer to bail out
                    # with the original error.
                    raise
                # newloc looks okay to use.  Do print a warning.
                logger.warn(
                    "Path %s unexpectedly already exists.\n"
                    "Maybe a buildout running in parallel has added it. "
                    "We will accept it.\n"
                    "If this contains a wrong package, please remove it yourself.",
                    newloc)
                break
            except PermissionError:
                print(f"PermissionError: rename {tmp_loc} -> {newloc}")
                time.sleep(2)
                continue
            else:
                # There were no problems during the rename.
                # Do the compile step.
                redo_pyc(newloc)
                newdist = _get_matching_dist_in_location(dist, newloc)
                assert newdist is not None  # newloc above is missing our dist?!
                break
    finally: