# Path: virtman/utils/commands.py
# def execute(*cmd, **kwargs):
# def unlink(path):
# def link(src, dst):
# 
# Path: virtman/drivers/dmsetup.py
# class DmExecutor(dmsetup.Dmsetup):
#     def __init__(self):
# def remove_table(name):
# def reload_table(name, table):
# def multipath(name, disks):
# def reload_multipath(name, disks):
# def origin(origin_name, origin_dev):
# def snapshot(origin_path, snapshot_name, snapshot_dev):
# 
# Path: virtman/blockservice.py
# LOG = logging.getLogger(__name__)
# def findloop():
# def try_linkloop(loop_dev):
# def is_looped(loop_dev):
# def linkloop(loop_dev, path):
# def unlinkloop(loop_dev):
# 
# Path: virtman/drivers/connector.py
# class ISCSIExecutor(ISCSIConnector):
#     def __init__(self):
# def connect_volume(connection):
# def disconnect_volume(connection, device_info):
# 
# Path: virtman/drivers/fcg.py
# CONF = cfg.CONF
# class FcgExecutor(FCG):
#     def __init__(self):
# def is_valid():
# def create_group():
# def add_disk(disk):
# def rm_disk(disk):


import os
from oslo.config import cfg
from virtman.utils import commands
from virtman.drivers import dmsetup
from virtman import blockservice
from virtman.drivers import connector
from virtman.drivers import fcg
from virtman.openstack.common import log as logging
    Block device snapshot for OpenStack
    """
    def __init__(self, origin_path, instance_name, snapshot_connection):
        super(BlockDeviceSnapshot, self).__init__()
        self.instance_path = None
        self.instance_name = instance_name
        self.origin_path = origin_path
        self.snapshot_with_cache = CONF.snapshot_with_cache
        self.snapshot_connection = snapshot_connection
        self.snapshot_dev = None
        self.snapshot_dev_name = "snapshot_" + instance_name
        self.device_info = None
        self.snapshot_link = None

    def create(self):
        LOG.debug("Virtman: start VM instance %s according origin_path %s" %
                  (self.instance_name, self.origin_path))
        snapshot_path, self.snapshot_link = self._create_snap_dev()
        self.instance_path = dmsetup.snapshot(self.origin_path,
                                              self.snapshot_dev_name,
                                              snapshot_path)
        # change link for OpenStack
        commands.unlink(self.snapshot_link)
        if not os.path.exists(self.snapshot_link):
            commands.link(self.instance_path, self.snapshot_link)
        LOG.debug("Virtman: success! snapshot_link = %s" % self.snapshot_link)
        return self.snapshot_link

    def _create_snap_dev(self):
        LOG.debug("Virtman: creating a snapshot for the VM instance")

# Path: virtman/utils/commands.py
# def execute(*cmd, **kwargs):
# def unlink(path):
# def link(src, dst):
# 
# Path: virtman/drivers/dmsetup.py
# class DmExecutor(dmsetup.Dmsetup):
#     def __init__(self):
# def remove_table(name):
# def reload_table(name, table):
# def multipath(name, disks):
# def reload_multipath(name, disks):
# def origin(origin_name, origin_dev):
# def snapshot(origin_path, snapshot_name, snapshot_dev):
# 
# Path: virtman/blockservice.py
# LOG = logging.getLogger(__name__)
# def findloop():
# def try_linkloop(loop_dev):
# def is_looped(loop_dev):
# def linkloop(loop_dev, path):
# def unlinkloop(loop_dev):
# 
# Path: virtman/drivers/connector.py
# class ISCSIExecutor(ISCSIConnector):
#     def __init__(self):
# def connect_volume(connection):
# def disconnect_volume(connection, device_info):
# 
# Path: virtman/drivers/fcg.py
# CONF = cfg.CONF
# class FcgExecutor(FCG):
#     def __init__(self):
# def is_valid():
# def create_group():
# def add_disk(disk):
# def rm_disk(disk):


# Path: virtman/snapshot.py
import os
from oslo.config import cfg
from virtman.utils import commands
from virtman.drivers import dmsetup
from virtman import blockservice
from virtman.drivers import connector
from virtman.drivers import fcg
from virtman.openstack.common import log as logging

    Block device snapshot for OpenStack
    """
    def __init__(self, origin_path, instance_name, snapshot_connection):
        super(BlockDeviceSnapshot, self).__init__()
        self.instance_path = None
        self.instance_name = instance_name
        self.origin_path = origin_path
        self.snapshot_with_cache = CONF.snapshot_with_cache
        self.snapshot_connection = snapshot_connection
        self.snapshot_dev = None
        self.snapshot_dev_name = "snapshot_" + instance_name
        self.device_info = None
        self.snapshot_link = None

    def create(self):
        LOG.debug("Virtman: start VM instance %s according origin_path %s" %
                  (self.instance_name, self.origin_path))
        snapshot_path, self.snapshot_link = self._create_snap_dev()
        self.instance_path = dmsetup.snapshot(self.origin_path,
                                              self.snapshot_dev_name,
                                              snapshot_path)
        # change link for OpenStack
        commands.unlink(self.snapshot_link)
        if not os.path.exists(self.snapshot_link):
            commands.link(self.instance_path, self.snapshot_link)
        LOG.debug("Virtman: success! snapshot_link = %s" % self.snapshot_link)
        return self.snapshot_link

    def _create_snap_dev(self):
        LOG.debug("Virtman: creating a snapshot for the VM instance")

        self.device_info = connector.connect_volume(self.snapshot_connection)