Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion helpers/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def get_preset_by_albabackend(preset_name, albabackend_name):
"""
Fetches preset by albabackend_guid

:param preset_name: name of a existing preset
:param preset_name: name of an existing preset
:type preset_name: str
:param albabackend_name: name of a existing alba backend
:type albabackend_name: str
Expand Down
56 changes: 56 additions & 0 deletions helpers/ci_constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Copyright (C) 2016 iNuron NV
#
# This file is part of Open vStorage Open Source Edition (OSE),
# as available from
#
# http://www.openvstorage.org and
# http://www.openvstorage.com.
#
# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License v3 (GNU AGPLv3)
# as published by the Free Software Foundation, in version 3 as it comes
# in the LICENSE.txt file of the Open vStorage OSE distribution.
#
# Open vStorage is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY of any kind.
import json
from ci.api_lib.helpers.api import OVSClient


class CIConstants(object):
"""
Collection of multiple constants and constant related instances
"""

CONFIG_LOC = "/opt/OpenvStorage/ci/config/setup.json"
TEST_SCENARIO_LOC = "/opt/OpenvStorage/ci/scenarios/"
TESTRAIL_LOC = "/opt/OpenvStorage/ci/config/testrail.json"

with open(CONFIG_LOC, 'r') as JSON_CONFIG:
SETUP_CFG = json.load(JSON_CONFIG)

HYPERVISOR_INFO = SETUP_CFG['ci'].get('hypervisor')
DOMAIN_INFO = SETUP_CFG['setup']['domains']
BACKEND_INFO = SETUP_CFG['setup']['backends']
STORAGEROUTER_INFO = SETUP_CFG['setup']['storagerouters']

class classproperty(property):
def __get__(self, cls, owner):
return classmethod(self.fget).__get__(None, owner)()

@classproperty
def api(cls):
return OVSClient(cls.SETUP_CFG['ci']['grid_ip'],
cls.SETUP_CFG['ci']['user']['api']['username'],
cls.SETUP_CFG['ci']['user']['api']['password'],
)

@classmethod
def get_vpool_names(cls):
names = []
for sr_ip, items in cls.STORAGEROUTER_INFO.iteritems():
vpools = items.get('vpools')
for vp_name, vp_info in vpools.iteritems():
if vp_name not in names:
names.append(vp_name)
return names
38 changes: 14 additions & 24 deletions helpers/disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,10 @@ class DiskHelper(object):
DiskHelper class
"""

def __init__(self):
pass

@staticmethod
def get_diskpartitions_by_guid(diskguid):
"""
Fetch disk partitions by disk guid

:param diskguid: ip address of a storagerouter
:type diskguid: str
:return: list of DiskPartition Objects
Expand All @@ -41,59 +37,53 @@ def get_diskpartitions_by_guid(diskguid):
return [dp for dp in DiskPartitionList.get_partitions() if dp.disk_guid == diskguid]

@staticmethod
def get_roles_from_disks(storagerouter_ip=None):
def get_roles_from_disks(storagerouter_guid=None):
"""
Fetch disk roles from all disks with optional storagerouter_ip

:param storagerouter_ip: ip address of a storage router
:type storagerouter_ip: str
:param storagerouter_guid: guid of a storage router
:type storagerouter_guid: str
:return: list of lists with roles
:rtype: list > list
"""
if not storagerouter_ip:
if not storagerouter_guid:
return [partition.roles for disk in DiskList.get_disks() for partition in disk.partitions]
else:
storagerouter_guid = StoragerouterHelper.get_storagerouter_guid_by_ip(storagerouter_ip)
return [partition.roles for disk in DiskList.get_disks()
if disk.storagerouter_guid == storagerouter_guid for partition in disk.partitions]

@staticmethod
def get_disk_by_diskname(storagerouter_ip, disk_name):
def get_disk_by_diskname(storagerouter_guid, disk_name):
"""
Get a disk object by storagerouter ip and disk name

:param storagerouter_ip: ip address of a storage router
:type storagerouter_ip: str
Get a disk object by storagerouter guid and disk name
:param storagerouter_guid: guid address of a storage router
:type storagerouter_guid: str
:param disk_name: name of a disk (e.g. sda)
:type disk_name: str
:return: disk object
:rtype: ovs.dal.hybrids.Disk
"""

storagerouter = StoragerouterHelper.get_storagerouter_by_ip(storagerouter_ip=storagerouter_ip)
storagerouter = StoragerouterHelper.get_storagerouter_by_guid(storagerouter_guid=storagerouter_guid)
for disk in storagerouter.disks:
if disk.name == disk_name:
return disk

@staticmethod
def get_roles_from_disk(storagerouter_ip, disk_name):
def get_roles_from_disk(storagerouter_guid, disk_name):
"""
Get the roles from a certain disk

:param storagerouter_ip: ip address of a storage router
:type storagerouter_ip: str
:param storagerouter_guid: guid address of a storage router
:type storagerouter_guid: str
:param disk_name: name of a disk (e.g. sda)
:type disk_name: str
:return: list of roles of all partitions on a certain disk
:rtype: list
"""

disk = DiskHelper.get_disk_by_diskname(storagerouter_ip, disk_name)
disk = DiskHelper.get_disk_by_diskname(storagerouter_guid, disk_name)
roles_on_disk = []
if disk:
for diskpartition in disk.partitions:
for role in diskpartition.roles:
roles_on_disk.append(role)
return roles_on_disk
else:
raise RuntimeError("Disk with name `{0}` not found on storagerouter `{1}`".format(disk_name, storagerouter_ip))
raise RuntimeError("Disk with name `{0}` not found on storagerouter `{1}`".format(disk_name, storagerouter_guid))
4 changes: 3 additions & 1 deletion helpers/domain.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Copyright (C) 2016 iNuron NV
#
# This file is part of Open vStorage Open Source Edition (OSE),
# Copyright (C) 2016 iNuron NV
#
# This file is part of Open vStorage Open Source Edition (OSE),
# as available from
#
# http://www.openvstorage.org and
Expand Down Expand Up @@ -109,4 +112,3 @@ def get_storagedrivers_in_same_domain(domain_guid):
:rtype: list
"""
return [storagedriver for storagedriver in StorageDriverList.get_storagedrivers() if domain_guid in storagedriver.storagerouter.regular_domains]

2 changes: 1 addition & 1 deletion helpers/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,4 @@ class ImageConvertError(Exception):
"""
Raised when an object was queries that doesn't exist
"""
pass
pass
85 changes: 60 additions & 25 deletions helpers/hypervisor/hypervisor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,74 @@
Hypervisor/ManagementCenter factory module
Using the module requires libvirt api to be available on the MACHINE THAT EXECUTES THE CODE
"""

from ovs_extensions.generic.filemutex import file_mutex
from ovs_extensions.generic.toolbox import ExtensionsToolbox
from ovs.lib.helpers.toolbox import Toolbox
from ...helpers.ci_constants import CIConstants


class HypervisorFactory(object):
class HypervisorFactory(CIConstants):
"""
HypervisorFactory class provides functionality to get abstracted hypervisor
"""

hypervisors = {}

@staticmethod
def get(ip, username, password, hvtype):
@classmethod
def get(cls, hv_credentials=None):
"""
Returns the appropriate hypervisor client class for a given PMachine
:param hv_credentials: object that contains ip, user, password and hypervisor type
:type hv_credentials: HypervisorCredentials object
"""
key = '{0}_{1}'.format(ip, username)
if key not in HypervisorFactory.hypervisors:
mutex = file_mutex('hypervisor_{0}'.format(key))
try:
mutex.acquire(30)
if key not in HypervisorFactory.hypervisors:
if hvtype == 'VMWARE':
# Not yet tested. Needs to be rewritten
raise NotImplementedError("{0} has not yet been implemented".format(hvtype))
from .hypervisors.vmware import VMware
hypervisor = VMware(ip, username, password)
elif hvtype == 'KVM':
from .hypervisors.kvm import KVM
hypervisor = KVM(ip, username, password)
else:
raise NotImplementedError('Hypervisor {0} is not yet supported'.format(hvtype))
HypervisorFactory.hypervisors[key] = hypervisor
finally:
mutex.release()
return HypervisorFactory.hypervisors[key]
if hv_credentials is None:
return cls.get(HypervisorCredentials(ip=CIConstants.HYPERVISOR_INFO['ip'],
user=CIConstants.HYPERVISOR_INFO['user'],
password=CIConstants.HYPERVISOR_INFO['password'],
type=CIConstants.HYPERVISOR_INFO['type']))
if not isinstance(hv_credentials, HypervisorCredentials):
raise TypeError('Credentials must be of type HypervisorCredentials')
return cls.hypervisors.get(hv_credentials, cls._add_hypervisor(hv_credentials))

@staticmethod
def _add_hypervisor(hypervisor_credentials):
ip = hypervisor_credentials.ip
username = hypervisor_credentials.user
password = hypervisor_credentials.password
hvtype = hypervisor_credentials.type
mutex = file_mutex('hypervisor_{0}'.format(hash(hypervisor_credentials)))
try:
mutex.acquire(30)
if hypervisor_credentials not in HypervisorFactory.hypervisors:
if hvtype == 'VMWARE':
# Not yet tested. Needs to be rewritten
raise NotImplementedError("{0} has not yet been implemented".format(hvtype))
from .hypervisors.vmware import VMware
hypervisor = VMware(ip, username, password)
elif hvtype == 'KVM':
from .hypervisors.kvm import KVM
hypervisor = KVM(ip, username, password)
else:
raise NotImplementedError('Hypervisor {0} is not yet supported'.format(hvtype))
HypervisorFactory.hypervisors[hypervisor_credentials] = hypervisor
return hypervisor
finally:
mutex.release()


class HypervisorCredentials(object):
def __init__(self, ip, user, password, type):
required_params = {'ip': (str, Toolbox.regex_ip),
'user': (str, None),
'password': (str, None),
'type': (str, ['KVM', 'VMWARE'])}
ExtensionsToolbox.verify_required_params(required_params, {'ip': ip,
'user': user,
'password': password,
'type': type})
self.ip = ip
self.user = user
self.password = password
self.type = type

def __str__(self):
return 'hypervisor at ip {0} of type {1}'.format(self.ip, self.type)
52 changes: 52 additions & 0 deletions helpers/service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Copyright (C) 2016 iNuron NV
#
# This file is part of Open vStorage Open Source Edition (OSE),
# as available from
#
# http://www.openvstorage.org and
# http://www.openvstorage.com.
#
# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License v3 (GNU AGPLv3)
# as published by the Free Software Foundation, in version 3 as it comes
# in the LICENSE.txt file of the Open vStorage OSE distribution.
#
# Open vStorage is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY of any kind.

from ovs.dal.hybrids.service import Service
from ovs.extensions.generic.sshclient import SSHClient
from ovs.extensions.services.servicefactory import ServiceFactory
from ovs.log.log_handler import LogHandler
from ..helpers.ci_constants import CIConstants


class ServiceHelper(CIConstants):
"""
ServiceHelper class
"""

LOGGER = LogHandler.get(source="setup", name="ci_service_setup")
SERVICE_MANAGER = ServiceFactory.get_manager()

@staticmethod
def get_service(guid):
"""
Fetch Service object by service guid
:param guid: guid of the service
:return: a Service object
"""
return Service(guid)

@staticmethod
def restart_service(guid, storagerouter):
"""
Restart a service based on the guid located on storagerouter
:param guid: guid of the service
:param storagerouter: storagerouter where the service is running
:type storagerouter: StorageRouter
:return:
"""
client = SSHClient(storagerouter, username='root')
service = ServiceHelper.get_service(guid)
return ServiceHelper.SERVICE_MANAGER.restart_service(service.name, client=client)
2 changes: 1 addition & 1 deletion helpers/storagedriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
from ovs.dal.hybrids.storagedriver import StorageDriver
from ovs.dal.lists.storagedriverlist import StorageDriverList
from ovs.extensions.generic.configuration import Configuration
from ovs.log.log_handler import LogHandler
from ovs.extensions.generic.sshclient import SSHClient
from ovs.extensions.services.servicefactory import ServiceFactory
from ovs.log.log_handler import LogHandler


class StoragedriverHelper(object):
Expand Down
Loading