<commit_before>#!/usr/bin/env python

import time
import socket
import fcntl
import struct
import threading

from oslo.config import cfg

from vmthunder.openstack.common import log as logging
from vmthunder.path import connection_to_str
from vmthunder.path import Path
from vmthunder.drivers import fcg
from vmthunder.drivers import dmsetup
from vmthunder.drivers import iscsi
from vmthunder.drivers import volt

CONF = cfg.CONF
LOG = logging.getLogger(__name__)


class Enum(set):
    def __getattr__(self, item):
        if item in self:
            return item
        return AttributeError

STATUS = Enum(['empty', 'building', 'ok', 'destroying', 'error'])
ACTIONS = Enum(['build', 'destroy'])


class Session(object):
    def __init__(self, volume_name):
        self.volume_name = volume_name
        self.root = {}
        self.paths = {}
        self.cached_path = ''
        self.has_multipath = False
        self.has_cache = False
        self.has_origin = False
        self.has_target = False
        self.is_login = False
        #TODO: all virtual machines called image
        self.vm = []
        self.peer_id = ''
        self.target_id = 0
        self.__status = STATUS.empty
        self.status_lock = threading.Lock()
        LOG.debug("VMThunder: create a session of volume_name %s" % self.volume_name)

    @property
    def origin_name(self):
        return 'origin_' + self.volume_name

    @property
    def origin_path(self):
        return dmsetup.prefix + self.origin_name

    @property
    def multipath_name(self):
        return 'multipath_' + self.volume_name

    @property
    def multipath_path(self):
        return dmsetup.prefix + self.multipath_name

    #@synchronized("status_lock")
    def change_status(self, src_status, dst_status):
        with self.status_lock:
            ret = False
            if self.__status == src_status:
                self.__status = dst_status
                ret = True
            LOG.debug("VMThunder: source status = %s, dst status = %s, ret = %s" % (src_status, dst_status, ret))
            return ret

    def deploy_image(self, image_connection):
        success = self.change_status(STATUS.empty, STATUS.building)
        if not success:
            while self.__status == STATUS.building:
                LOG.debug("VMThunder: in deploy_image, sleep 3 seconds waiting for build completed")
                time.sleep(3)
        LOG.debug("VMThunder: ..........begin to deploy base image")
        try:
            origin_path = self._deploy_image(image_connection)
        except Exception, e:
            LOG.error(e)
            self.change_status(STATUS.building, STATUS.error)
            raise
        else:
            self.change_status(STATUS.building, STATUS.ok)
        LOG.debug("VMThunder: ..........deploy base image completed")
        return origin_path

    def _deploy_image(self, image_connection):
        #TODO: Roll back if failed !
        """
        deploy image in compute node, return the origin path to create snapshot
        :param image_connection: the connection towards to the base image
        :return: origin path to create snapshot
        """
        LOG.debug("VMThunder: in deploy_image, volume name = %s, has origin = %s, has cache = %s, "
                  "is_login = %s" % (self.volume_name, self.has_origin, self.has_cache, self.is_login))

        #Check current status
        if self.has_origin:
            return self.origin_path

        image_path = Path(self.reform_connection(image_connection))
        self.root[str(image_path)] = image_path

        LOG.debug("VMThunder: begin to rebuild paths")
        if len(self.paths) == 0:
            parent_list = self._get_parent()
            self.rebuild_paths(parent_list)
        LOG.debug("VMThunder: rebuild paths completed, multipath = %s" % self.multipath_path)
        if not self.has_cache:
            #TODO: NEED to fix here
            LOG.info("VMThunder: create cache for base image %s" % self.volume_name)
            self.cached_path = self._create_cache(self.multipath_path)
            LOG.debug("VMThunder: create cache completed, cache path = %s" % self.cached_path)

        if not self.has_origin:
            self._create_origin(self.cached_path)

        if not self.has_target:
            iqn = image_connection['target_iqn']
            self._create_target(iqn, self.cached_path)

        return self.origin_path

    def destroy(self):
        LOG.debug("VMThunder: destroy session %s" % self.volume_name)
        assert not self.has_vm(), 'Destroy session %s failed, still has vm' % self.volume_name
        if self.is_login is True:
            volt.logout(self.volume_name, peer_id=self.peer_id)
            self.is_login = False
        if self.has_target:
            if iscsi.is_connected(self.target_id):
                return False
            else:
                self._delete_target()

        if self.has_origin:
            self._delete_origin()<commit_msg>try to fix a bug<commit_after>#!/usr/bin/env python

import time
import socket
import fcntl
import struct
import threading

from oslo.config import cfg

from vmthunder.openstack.common import log as logging
from vmthunder.path import connection_to_str
from vmthunder.path import Path
from vmthunder.drivers import fcg
from vmthunder.drivers import dmsetup
from vmthunder.drivers import iscsi
from vmthunder.drivers import volt

CONF = cfg.CONF
LOG = logging.getLogger(__name__)


class Enum(set):
    def __getattr__(self, item):
        if item in self:
            return item
        return AttributeError

STATUS = Enum(['empty', 'building', 'ok', 'destroying', 'error'])
ACTIONS = Enum(['build', 'destroy'])


class Session(object):
    def __init__(self, volume_name):
        self.volume_name = volume_name
        self.root = {}
        self.paths = {}
        self.cached_path = ''
        self.has_multipath = False
        self.has_cache = False
        self.has_origin = False
        self.has_target = False
        self.is_login = False
        #TODO: all virtual machines called image
        self.vm = []
        self.peer_id = ''
        self.target_id = 0
        self.__status = STATUS.empty
        self.status_lock = threading.Lock()
        LOG.debug("VMThunder: create a session of volume_name %s" % self.volume_name)

    @property
    def origin_name(self):
        return 'origin_' + self.volume_name

    @property
    def origin_path(self):
        return dmsetup.prefix + self.origin_name

    @property
    def multipath_name(self):
        return 'multipath_' + self.volume_name

    @property
    def multipath_path(self):
        return dmsetup.prefix + self.multipath_name

    #@synchronized("status_lock")
    def change_status(self, src_status, dst_status):
        with self.status_lock:
            ret = False
            if self.__status == src_status:
                self.__status = dst_status
                ret = True
            LOG.debug("VMThunder: source status = %s, dst status = %s, ret = %s" % (src_status, dst_status, ret))
            return ret

    def deploy_image(self, image_connection):
        success = self.change_status(STATUS.empty, STATUS.building)
        if not success:
            while self.__status == STATUS.building:
                LOG.debug("VMThunder: in deploy_image, sleep 3 seconds waiting for build completed")
                time.sleep(3)
        LOG.debug("VMThunder: ..........begin to deploy base image")
        try:
            origin_path = self._deploy_image(image_connection)
        except Exception, e:
            LOG.error(e)
            self.change_status(STATUS.building, STATUS.error)
            raise
        else:
            self.change_status(STATUS.building, STATUS.ok)
        LOG.debug("VMThunder: ..........deploy base image completed")
        return origin_path

    def _deploy_image(self, image_connection):
        #TODO: Roll back if failed !
        """
        deploy image in compute node, return the origin path to create snapshot
        :param image_connection: the connection towards to the base image
        :return: origin path to create snapshot
        """
        LOG.debug("VMThunder: in deploy_image, volume name = %s, has origin = %s, has cache = %s, "
                  "is_login = %s" % (self.volume_name, self.has_origin, self.has_cache, self.is_login))

        #Check current status
        if self.has_origin:
            return self.origin_path

        image_path = Path(self.reform_connection(image_connection))
        self.root[str(image_path)] = image_path

        LOG.debug("VMThunder: begin to rebuild paths")
        if len(self.paths) == 0:
            parent_list = self._get_parent()
            self.rebuild_paths(parent_list)
        LOG.debug("VMThunder: rebuild paths completed, multipath = %s" % self.multipath_path)
        if not self.has_cache:
            #TODO: NEED to fix here
            LOG.debug("VMThunder: create cache for base image %s" % self.volume_name)
            self.cached_path = self._create_cache(self.multipath_path)
            LOG.debug("VMThunder: create cache completed, cache path = %s" % self.cached_path)

        if not self.has_origin:
            self._create_origin(self.cached_path)

        if not self.has_target:
            iqn = image_connection['target_iqn']
            self._create_target(iqn, self.cached_path)

        return self.origin_path

    def destroy(self):
        LOG.debug("VMThunder: destroy session %s" % self.volume_name)
        assert not self.has_vm(), 'Destroy session %s failed, still has vm' % self.volume_name
        if self.is_login is True:
            volt.logout(self.volume_name, peer_id=self.peer_id)
            self.is_login = False
        if self.has_target:
            if iscsi.is_connected(self.target_id):
                return False
            else:
                self._delete_target()

        if self.has_origin:
            self._delete_origin()
{'fraction_non_alphanumeric': 0.09302325581395349, 'fraction_numerical': 0.0008019246190858059, 'mean_word_length': 2.31571950814224, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '770167', 'n_tokens_mistral': 2913, 'n_tokens_neox': 2850, 'n_words': 753}