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

#
# Copyright (c) 2011, Centre National de la Recherche Scientifique (CNRS)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

__version__ = "1.0"
__author__ = "<NAME> <<EMAIL>>"

import sys

sys.path.append('/var/lib/stratuslab/python')

import os
import re
from optparse import OptionParser, OptionGroup
import ConfigParser
import socket
import httplib2
import json
from StringIO import StringIO
from urllib import urlencode
from subprocess import call
from time import sleep

sample_example = """
[main]
pdisk_user=pdisk
pdisk_passwd=<PASSWORD>
register_filename=pdisk
vm_dir=/var/lib/one
volume_mgmt_dir=/var/run/stratuslab

[iscsi]
iscsiadm=/usr/sbin/iscsiadm
"""
CONFFILE = '/etc/stratuslab/pdisk-host.conf'
config = ConfigParser.RawConfigParser()
config.read(CONFFILE)

iscsiadm = config.get("iscsi", "iscsiadm")
login = config.get("main", "pdisk_user")
pswd = config.get("main", "pdisk_passwd")
vm_dir = config.get("main", "vm_dir")
VOLUME_MGMT_DIR = config.get("main", "volume_mgmt_dir")

register_filename = config.get('main', 'register_filename')
if not register_filename:
    register_filename = 'pdisk'

parser = OptionParser()
parser.add_option("--pdisk-id", dest="persistent_disk_id",
                  help="persistent disk id ( pdisk:endpoint:port:disk_uuid )", metavar="PID")

parser.add_option("--vm-id", dest="vm_id",
                  help="VM ID", metavar="ID")

parser.add_option("--vm-dir", dest="vm_dir",
                  help="directory where device will be created", metavar="DIR")

parser.add_option("--vm-disk-name", dest="disk_name",
                  help="name of disk in Virtual Machine directory")

parser.add_option("--target", dest="target",
                  help="device name on Virtual Machine")

parser.add_option("--turl", dest="turl", metavar="TURL", default="",
                  help="transport URL of pdisk (protocol://server/protocol-option-to-access-file)")

parser.add_option("--username", dest="username",
                  help="username use to interact with pdisk server")

parser.add_option("--password", dest="password",
                  help="password use to interact with pdisk server")

action = OptionGroup(parser, " Action command")
action.add_option("--attach", dest="attach", action="store_true",
                  help="attach/detach backend to hypervisor")

action.add_option("--register", dest="registration", action="store_true",
                  help="register/unregister persistent disk as used on service")

action.add_option("--mark", dest="mark", action="store_true",
                  help="mark/unmark persistent disk as used in the VM")

action.add_option("--link", dest="link", action="store_true",
                  help="link/unlink attached disk in Virtual Machine directory")

action.add_option("--link-to", dest="link_to", default="",
                  help="link attached disk to the specified link name")

action.add_option("--mount", dest="mount", action="store_true",
                  help="mount/unmount disk into Virtual Machine")

action.add_option("--no-check", dest="no_check", action="store_true",
                  help="disable check if device is used")

action.add_option("--op", dest="operation", metavar="OP",
                  help="up : activate persistent disk ( register / attach / link / mount ) "
                       "-- down : deactivate persistent disk ( unmount / unlink / detach / unregister )")

parser.add_option_group(action)
(options, args) = parser.parse_args()

if not options.operation:
    raise parser.error("--op option is mandatory")

if not options.persistent_disk_id:
    raise parser.error("--pdisk-id option is mandatory")

if options.registration:
    if not options.vm_id:
        raise parser.error("--register option needs --vm-id options")

if options.link:
    if (not vm_dir and not options.vm_dir) or not options.vm_id or not options.disk_name:
        raise parser.error(
            "--link option needs --vm-disk-name, --vm-id options and --vm-dir if not defined in %s" % CONFFILE)

if options.mount:
    if not options.vm_id or not options.target:
        raise parser.error("--mount option needs --target and --vm-id options")

if options.mark:
    if (not vm_dir and not options.vm_dir) or not options.vm_id:
        raise parser.error(
            "--mark option needs --vm-id option and --vm-dir if not defined in %s" % CONFFILE)

if options.vm_dir:
    vm_dir = options.vm_dir

if options.username:
    login = options.username

if options.password:
    pswd = options.password

# Define the name of the file that lists the allocated persistent volumes.
registration_file = None
if options.mark:
    registration_file = os.path.join(vm_dir, options.vm_id, register_filename)


class VolumeManagement(object):
    """
    Class to manage local information about file
    """

    def __init__(self, directory):
        self.directory = directory

    def isFree(self, target):
        if len(os.listdir("%s/%s" % (self.directory, target))) == 0:
            return True
        else:
            return False

    def deleteTarget(self, target):
        f = '%s/%s/' % (self.directory, target)
        os.rmdir(f)

    def deleteVolume(self, target, turl):
        _turl = turl.replace('/', '-')
        f = '%s/%s/%s' % (self.directory, target, _turl)
        os.rmdir(f)

    def insertVolume(self, target, turl):
        _turl = turl.replace('/', '-')
        targetDir = "%s/%s" % (self.directory, target)
        f = "%s/%s" % (targetDir, _turl)
        if not os.path.isdir(self.directory):
            os.mkdir(self.directory)
        if not os.path.isdir(targetDir):
            os.mkdir(targetDir)
        os.mkdir(f)


class PersistentDisk:
    """Metaclass for general persistent disk client"""

    def __init__(self, pdisk_id, turl):
        try:
            __pdisk__ = re.match(r"pdisk:(?P<server>.*):(?P<port>.*):(?P<disk_uuid>.*)", pdisk_id)
            self.pdisk_uri = pdisk_id
            self.endpoint = __pdisk__.group('server')
            self.port = __pdisk__.group('port')
            self.disk_uuid = __pdisk__.group('disk_uuid')
            self.__checkTurl__(turl)
            self.volumeCheck = VolumeManagement(VOLUME_MGMT_DIR)
        except AttributeError:
            raise PersistentDiskException('URI ' + pdisk_id + ' not match expression pdisk:endpoint:port:disk_uuid')

    def __copy__(self, pdisk):
        """
          creates a xxxPersistentDisk object from PersistentDisk (xxxPersistentDisk is a inherited class)
        """
        self.pdisk_uri = pdisk.pdisk_uri
        self.endpoint = pdisk.endpoint
        self.port = pdisk.port
        self.disk_uuid = pdisk.disk_uuid
        self.protocol = pdisk.protocol
        self.server = pdisk.server
        self.image = pdisk.image<commit_msg>check if target directory with turl already exists<commit_after>#!/usr/bin/env python

#
# Copyright (c) 2011, Centre National de la Recherche Scientifique (CNRS)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

__version__ = "1.0"
__author__ = "<NAME> <<EMAIL>>"

import sys

sys.path.append('/var/lib/stratuslab/python')

import os
import re
from optparse import OptionParser, OptionGroup
import ConfigParser
import socket
import httplib2
import json
from StringIO import StringIO
from urllib import urlencode
from subprocess import call
from time import sleep

sample_example = """
[main]
pdisk_user=pdisk
pdisk_passwd=<PASSWORD>
register_filename=pdisk
vm_dir=/var/lib/one
volume_mgmt_dir=/var/run/stratuslab

[iscsi]
iscsiadm=/usr/sbin/iscsiadm
"""
CONFFILE = '/etc/stratuslab/pdisk-host.conf'
config = ConfigParser.RawConfigParser()
config.read(CONFFILE)

iscsiadm = config.get("iscsi", "iscsiadm")
login = config.get("main", "pdisk_user")
pswd = config.get("main", "pdisk_passwd")
vm_dir = config.get("main", "vm_dir")
VOLUME_MGMT_DIR = config.get("main", "volume_mgmt_dir")

register_filename = config.get('main', 'register_filename')
if not register_filename:
    register_filename = 'pdisk'

parser = OptionParser()
parser.add_option("--pdisk-id", dest="persistent_disk_id",
                  help="persistent disk id ( pdisk:endpoint:port:disk_uuid )", metavar="PID")

parser.add_option("--vm-id", dest="vm_id",
                  help="VM ID", metavar="ID")

parser.add_option("--vm-dir", dest="vm_dir",
                  help="directory where device will be created", metavar="DIR")

parser.add_option("--vm-disk-name", dest="disk_name",
                  help="name of disk in Virtual Machine directory")

parser.add_option("--target", dest="target",
                  help="device name on Virtual Machine")

parser.add_option("--turl", dest="turl", metavar="TURL", default="",
                  help="transport URL of pdisk (protocol://server/protocol-option-to-access-file)")

parser.add_option("--username", dest="username",
                  help="username use to interact with pdisk server")

parser.add_option("--password", dest="password",
                  help="password use to interact with pdisk server")

action = OptionGroup(parser, " Action command")
action.add_option("--attach", dest="attach", action="store_true",
                  help="attach/detach backend to hypervisor")

action.add_option("--register", dest="registration", action="store_true",
                  help="register/unregister persistent disk as used on service")

action.add_option("--mark", dest="mark", action="store_true",
                  help="mark/unmark persistent disk as used in the VM")

action.add_option("--link", dest="link", action="store_true",
                  help="link/unlink attached disk in Virtual Machine directory")

action.add_option("--link-to", dest="link_to", default="",
                  help="link attached disk to the specified link name")

action.add_option("--mount", dest="mount", action="store_true",
                  help="mount/unmount disk into Virtual Machine")

action.add_option("--no-check", dest="no_check", action="store_true",
                  help="disable check if device is used")

action.add_option("--op", dest="operation", metavar="OP",
                  help="up : activate persistent disk ( register / attach / link / mount ) "
                       "-- down : deactivate persistent disk ( unmount / unlink / detach / unregister )")

parser.add_option_group(action)
(options, args) = parser.parse_args()

if not options.operation:
    raise parser.error("--op option is mandatory")

if not options.persistent_disk_id:
    raise parser.error("--pdisk-id option is mandatory")

if options.registration:
    if not options.vm_id:
        raise parser.error("--register option needs --vm-id options")

if options.link:
    if (not vm_dir and not options.vm_dir) or not options.vm_id or not options.disk_name:
        raise parser.error(
            "--link option needs --vm-disk-name, --vm-id options and --vm-dir if not defined in %s" % CONFFILE)

if options.mount:
    if not options.vm_id or not options.target:
        raise parser.error("--mount option needs --target and --vm-id options")

if options.mark:
    if (not vm_dir and not options.vm_dir) or not options.vm_id:
        raise parser.error(
            "--mark option needs --vm-id option and --vm-dir if not defined in %s" % CONFFILE)

if options.vm_dir:
    vm_dir = options.vm_dir

if options.username:
    login = options.username

if options.password:
    pswd = options.password

# Define the name of the file that lists the allocated persistent volumes.
registration_file = None
if options.mark:
    registration_file = os.path.join(vm_dir, options.vm_id, register_filename)


class VolumeManagement(object):
    """
    Class to manage local information about file
    """

    def __init__(self, directory):
        self.directory = directory

    def isFree(self, target):
        if len(os.listdir("%s/%s" % (self.directory, target))) == 0:
            return True
        else:
            return False

    def deleteTarget(self, target):
        f = '%s/%s/' % (self.directory, target)
        os.rmdir(f)

    def deleteVolume(self, target, turl):
        _turl = turl.replace('/', '-')
        f = '%s/%s/%s' % (self.directory, target, _turl)
        os.rmdir(f)

    def insertVolume(self, target, turl):
        _turl = turl.replace('/', '-')
        targetDir = "%s/%s" % (self.directory, target)
        f = "%s/%s" % (targetDir, _turl)
        if not os.path.isdir(self.directory):
            os.mkdir(self.directory)
        if not os.path.isdir(targetDir):
            os.mkdir(targetDir)
        if not os.path.isdir(f):
            os.mkdir(f)


class PersistentDisk:
    """Metaclass for general persistent disk client"""

    def __init__(self, pdisk_id, turl):
        try:
            __pdisk__ = re.match(r"pdisk:(?P<server>.*):(?P<port>.*):(?P<disk_uuid>.*)", pdisk_id)
            self.pdisk_uri = pdisk_id
            self.endpoint = __pdisk__.group('server')
            self.port = __pdisk__.group('port')
            self.disk_uuid = __pdisk__.group('disk_uuid')
            self.__checkTurl__(turl)
            self.volumeCheck = VolumeManagement(VOLUME_MGMT_DIR)
        except AttributeError:
            raise PersistentDiskException('URI ' + pdisk_id + ' not match expression pdisk:endpoint:port:disk_uuid')

    def __copy__(self, pdisk):
        """
          creates a xxxPersistentDisk object from PersistentDisk (xxxPersistentDisk is a inherited class)
        """
        self.pdisk_uri = pdisk.pdisk_uri
        self.endpoint = pdisk.endpoint
        self.port = pdisk.port
        self.disk_uuid = pdisk.disk_uuid
        self.protocol = pdisk.protocol
        self.server = pdisk.server
        self.image = pdisk.image
{'fraction_non_alphanumeric': 0.12590123792681268, 'fraction_numerical': 0.0016324309617739084, 'mean_word_length': 3.083032490974729, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1559854', 'n_tokens_mistral': 4470, 'n_tokens_neox': 4353, 'n_words': 1193}