Skip to content

Commit

Permalink
Format python files with Black.
Browse files Browse the repository at this point in the history
Signed-off-by: Artsiom Koltun <[email protected]>
  • Loading branch information
artek-koltun committed May 25, 2022
1 parent a460f19 commit 2edb9b6
Show file tree
Hide file tree
Showing 13 changed files with 187 additions and 133 deletions.
7 changes: 5 additions & 2 deletions build/storage/core/host-target/device_exerciser.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ class DeviceExerciserError(RuntimeError):


class DeviceExerciser:
def __init__(self, fio_runner=fio_runner.run_fio,
virtio_blk_detector=pci_devices.get_virtio_blk_path_by_pci_address):
def __init__(
self,
fio_runner=fio_runner.run_fio,
virtio_blk_detector=pci_devices.get_virtio_blk_path_by_pci_address,
):
self.fio_runner = fio_runner
self.virtio_blk_detector = virtio_blk_detector

Expand Down
16 changes: 10 additions & 6 deletions build/storage/core/host-target/fio_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ def run_fio(fio_args, subprocess_run=subprocess.run):
fio_cmd = []
try:
fio_cmd = ["fio"] + fio_args.split()
result = subprocess_run(fio_cmd,
capture_output=True, text=True)
result = subprocess_run(fio_cmd, capture_output=True, text=True)
if result.returncode != 0:
raise FioExecutionError("fio execution error: '" +
str(result.stdout) + "' | '" +
str(result.stderr) + "' ")
raise FioExecutionError(
"fio execution error: '"
+ str(result.stdout)
+ "' | '"
+ str(result.stderr)
+ "' "
)
return result.stdout
except BaseException as ex:
raise FioExecutionError(
"Cannot execute cmd '" + " ".join(fio_cmd) + "' Error: " + str(ex))
"Cannot execute cmd '" + " ".join(fio_cmd) + "' Error: " + str(ex)
)
11 changes: 5 additions & 6 deletions build/storage/core/host-target/host_target_grpc_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@ def __init__(self, message):
class HostTargetService(host_target_pb2_grpc.HostTargetServicer):
def __init__(self, fio_runner, virtio_blk_detector):
super().__init__()
self.device_exerciser = DeviceExerciser(
fio_runner, virtio_blk_detector)
self.device_exerciser = DeviceExerciser(fio_runner, virtio_blk_detector)

def RunFio(self, request, context):
output = None
try:
output = self.device_exerciser.run_fio(
request.pciAddress, request.fioArgs)
output = self.device_exerciser.run_fio(request.pciAddress, request.fioArgs)
except BaseException as ex:
context.set_code(grpc.StatusCode.FAILED_PRECONDITION)
context.set_details(str(ex))
Expand All @@ -41,9 +39,10 @@ def run_grpc_server(ip_address, port, server_creator=grpc.server):
try:
server = server_creator(futures.ThreadPoolExecutor(max_workers=10))
host_target_pb2_grpc.add_HostTargetServicer_to_server(
HostTargetService(run_fio, get_virtio_blk_path_by_pci_address), server)
HostTargetService(run_fio, get_virtio_blk_path_by_pci_address), server
)
service_names = (
host_target_pb2.DESCRIPTOR.services_by_name['HostTarget'].full_name,
host_target_pb2.DESCRIPTOR.services_by_name["HostTarget"].full_name,
reflection.SERVICE_NAME,
)
reflection.enable_server_reflection(service_names, server)
Expand Down
13 changes: 7 additions & 6 deletions build/storage/core/host-target/host_target_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@

def parse_arguments():
parser = argparse.ArgumentParser(
description='Runs service for hot-plug/hot-detach virtio-blk devices to vms')
parser.add_argument('--ip', required=True,
help='ip address the server listens to')
parser.add_argument('--port', type=int, default=50051,
help='port number the server listens to')
description="Runs service for hot-plug/hot-detach virtio-blk devices to vms"
)
parser.add_argument("--ip", required=True, help="ip address the server listens to")
parser.add_argument(
"--port", type=int, default=50051, help="port number the server listens to"
)

args = parser.parse_args()
return args


if __name__ == '__main__':
if __name__ == "__main__":
logging.basicConfig()

args = parse_arguments()
Expand Down
53 changes: 34 additions & 19 deletions build/storage/core/host-target/pci_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@


pci_validator = re.compile(
r'[0-9a-fA-F]{4}:[0-9a-fA-F]{2}:[0-1]{1}[0-9a-fA-F]{1}\.[0-7]{1}')
r"[0-9a-fA-F]{4}:[0-9a-fA-F]{2}:[0-1]{1}[0-9a-fA-F]{1}\.[0-7]{1}"
)


class PciAddress:
def validate_pci_address(pci_address):
return pci_validator.search(pci_address) != None

def _parse_pci_address(pci_address):
split_pci_address = pci_address.replace('.', ':').split(":")
split_pci_address = pci_address.replace(".", ":").split(":")
split_pci_address.reverse()
function = split_pci_address[0].strip()
device = split_pci_address[1].strip()
Expand All @@ -27,8 +28,12 @@ def _parse_pci_address(pci_address):
def __init__(self, pci_address) -> None:
if not PciAddress.validate_pci_address(pci_address):
raise InvalidPciAddress(pci_address + " is invalid")
self.domain, self.bus, self.device, self.function = \
PciAddress._parse_pci_address(pci_address)
(
self.domain,
self.bus,
self.device,
self.function,
) = PciAddress._parse_pci_address(pci_address)

def get_domain_bus_prefix(self):
return self.domain + ":" + self.bus
Expand All @@ -52,7 +57,7 @@ def get_all_files_by_pattern(pattern):
return glob.glob(pattern)


def directory_find(atom, root='.'):
def directory_find(atom, root="."):
dirs = get_directories(root)
if dirs and atom in dirs:
return os.path.join(root, atom)
Expand All @@ -70,35 +75,45 @@ class FailedPciDeviceDetection(RuntimeError):
def get_virtio_blk_path_by_pci_address(pci_address):
addr = PciAddress(pci_address)

domain_bus_dir = os.path.join("/sys/devices", "pci" +
addr.get_domain_bus_prefix())
pci_dev_sysfs_dir = directory_find(addr.get_full_address(),
domain_bus_dir)
domain_bus_dir = os.path.join("/sys/devices", "pci" + addr.get_domain_bus_prefix())
pci_dev_sysfs_dir = directory_find(addr.get_full_address(), domain_bus_dir)
if pci_dev_sysfs_dir == None:
raise FailedPciDeviceDetection(
"No pci device with " + pci_address + " exists under " + domain_bus_dir)
"No pci device with " + pci_address + " exists under " + domain_bus_dir
)
block_directory_pattern = os.path.join(pci_dev_sysfs_dir, "virtio*/block")
block_device_matches = get_all_files_by_pattern(block_directory_pattern)
if len(block_device_matches) == 0:
raise FailedPciDeviceDetection(
"No devices found for pattern " + block_directory_pattern)
"No devices found for pattern " + block_directory_pattern
)
elif len(block_device_matches) > 1:
raise FailedPciDeviceDetection(
"Found more than one device for pattern" +
block_directory_pattern + " : " + str(block_device_matches))
"Found more than one device for pattern"
+ block_directory_pattern
+ " : "
+ str(block_device_matches)
)

devices = get_directories(block_device_matches[0])
if not devices or len(devices) == 0:
raise FailedPciDeviceDetection(
"No device exist under " + block_directory_pattern +
" for pci device '" + pci_address + "'")
"No device exist under "
+ block_directory_pattern
+ " for pci device '"
+ pci_address
+ "'"
)
elif len(devices) > 1:
raise FailedPciDeviceDetection(
"Multiple devices are dected " + str(devices) +
" for pci address '" + pci_address +"'")
"Multiple devices are dected "
+ str(devices)
+ " for pci address '"
+ pci_address
+ "'"
)
device_path = os.path.join("/dev", devices[0])
if not os.path.exists(device_path):
raise FailedPciDeviceDetection(
"Device " + device_path + " does not exist")
raise FailedPciDeviceDetection("Device " + device_path + " does not exist")

return device_path
13 changes: 7 additions & 6 deletions build/storage/core/proxy-container/hot_plug_grpc_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,22 @@ def __execute_server_operation(self, request, context, operation):

def HotPlugVirtioBlk(self, request, context):
return self.__execute_server_operation(
request, context,
self.hot_plug_provider.hot_plug_vhost_virtio_blk)
request, context, self.hot_plug_provider.hot_plug_vhost_virtio_blk
)

def HotUnplugVirtioBlk(self, request, context):
return self.__execute_server_operation(
request, context,
self.hot_plug_provider.hot_unplug_vhost_virtio_blk)
request, context, self.hot_plug_provider.hot_unplug_vhost_virtio_blk
)


def run_grpc_server(ip_address, port, hot_plug_provider):
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
hot_plug_pb2_grpc.add_HotPlugServicer_to_server(
HotPlugService(hot_plug_provider), server)
HotPlugService(hot_plug_provider), server
)
service_names = (
hot_plug_pb2.DESCRIPTOR.services_by_name['HotPlug'].full_name,
hot_plug_pb2.DESCRIPTOR.services_by_name["HotPlug"].full_name,
reflection.SERVICE_NAME,
)
reflection.enable_server_reflection(service_names, server)
Expand Down
29 changes: 19 additions & 10 deletions build/storage/core/proxy-container/hot_plug_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,30 @@

def parse_arguments():
parser = argparse.ArgumentParser(
description='Runs service for hot-plug/hot-detach virtio-blk devices to vms')
parser.add_argument('--ip', required=True,
help='ip address the server listens to')
parser.add_argument('--port', type=int, default=50051,
help='port number the server listens to')
parser.add_argument('--shared-dir', type=str, required=True,
help='Directory path to shared with Host resources')
parser.add_argument('--host-shared-dir', type=str, required=True,
help='Directory path to shared with container resources')
description="Runs service for hot-plug/hot-detach virtio-blk devices to vms"
)
parser.add_argument("--ip", required=True, help="ip address the server listens to")
parser.add_argument(
"--port", type=int, default=50051, help="port number the server listens to"
)
parser.add_argument(
"--shared-dir",
type=str,
required=True,
help="Directory path to shared with Host resources",
)
parser.add_argument(
"--host-shared-dir",
type=str,
required=True,
help="Directory path to shared with container resources",
)

args = parser.parse_args()
return args


if __name__ == '__main__':
if __name__ == "__main__":
logging.basicConfig()

args = parse_arguments()
Expand Down
37 changes: 20 additions & 17 deletions build/storage/core/proxy-container/hot_plug_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,21 @@ def __init__(self, shared_dir_path, host_shared_dir_path):
self.shared_dir_path = shared_dir_path
self.host_shared_dir_path = host_shared_dir_path

def _hot_plug_action(self, vm_monitor,
vhost_virtio_blk, device_id):
def _hot_plug_action(self, vm_monitor, vhost_virtio_blk, device_id):
vm_monitor_path = os.path.join(self.shared_dir_path, vm_monitor)
vhost_path = os.path.join(self.host_shared_dir_path, vhost_virtio_blk)
ret = subprocess.call("/hot-plug.sh " + vm_monitor_path +
" " + vhost_path +
" " + str(device_id), shell=True)
ret = subprocess.call(
"/hot-plug.sh " + vm_monitor_path + " " + vhost_path + " " + str(device_id),
shell=True,
)
if ret != 0:
raise ActionExecutionError("Error at hot-plug execution")

def _hot_unplug_action(self, vm_monitor,
unused, device_id):
def _hot_unplug_action(self, vm_monitor, unused, device_id):
vm_monitor_path = os.path.join(self.shared_dir_path, vm_monitor)
ret = subprocess.call("/hot-unplug.sh " + vm_monitor_path +
" " + str(device_id), shell=True)
ret = subprocess.call(
"/hot-unplug.sh " + vm_monitor_path + " " + str(device_id), shell=True
)
if ret != 0:
raise ActionExecutionError("Error at hot-unplug execution")

Expand All @@ -54,16 +54,17 @@ def __is_socket(socket_path):

def __check_socket_path(path):
if not HotPlugProvider.__is_socket(path):
raise FileNotFoundError(errno.ENOENT, os.strerror(
errno.ENOENT), path)
raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), path)

def _validate_vhost_virtio_blk(self, vhost_virtio_blk):
HotPlugProvider.__check_socket_path(os.path.join(
self.shared_dir_path, vhost_virtio_blk))
HotPlugProvider.__check_socket_path(
os.path.join(self.shared_dir_path, vhost_virtio_blk)
)

def _validate_vm_monitor(self, vm_monitor):
HotPlugProvider.__check_socket_path(os.path.join(
self.shared_dir_path, vm_monitor))
HotPlugProvider.__check_socket_path(
os.path.join(self.shared_dir_path, vm_monitor)
)

def _validate_input(self, vm_monitor, vhost_virtio_blk):
self._validate_vhost_virtio_blk(vhost_virtio_blk)
Expand Down Expand Up @@ -92,8 +93,10 @@ def _perform_disk_operation(self, vm_monitor, vhost_virtio_blk, disk_operation):

def hot_plug_vhost_virtio_blk(self, vm_monitor, vhost_virtio_blk):
self._perform_disk_operation(
vm_monitor, vhost_virtio_blk, self._hot_plug_action)
vm_monitor, vhost_virtio_blk, self._hot_plug_action
)

def hot_unplug_vhost_virtio_blk(self, vm_monitor, vhost_virtio_blk):
self._perform_disk_operation(
vm_monitor, vhost_virtio_blk, self._hot_unplug_action)
vm_monitor, vhost_virtio_blk, self._hot_unplug_action
)
12 changes: 7 additions & 5 deletions build/storage/tests/ut/host-target/test_device_exerciser.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ def tearDown(self):
pass

def test_successful_fio_run(self):
self.fs.create_dir(
"/sys/devices/pci0000:00/0000:00:04.0/virtio0/block/vda")
self.fs.create_dir("/sys/devices/pci0000:00/0000:00:04.0/virtio0/block/vda")
self.fs.create_file("/dev/vda")
fio_arguments = "--name=test --size=4MB"

Expand All @@ -27,20 +26,23 @@ def fio_do_nothing(fio_args):
self.assertTrue("vda" in fio_args)
self.assertTrue(fio_arguments in fio_args)
return "output"

fio_do_nothing.was_called = False

exerciser = DeviceExerciser(
fio_runner=fio_do_nothing,
virtio_blk_detector=get_virtio_blk_path_by_pci_address)
virtio_blk_detector=get_virtio_blk_path_by_pci_address,
)
out = exerciser.run_fio("0000:00:04.0", fio_arguments)
self.assertTrue(fio_do_nothing.was_called)
self.assertEqual(out, "output")

def test_any_errors_at_exercising(self):
def raise_exception(unused):
raise BaseException()

exerciser = DeviceExerciser(
fio_runner=raise_exception,
virtio_blk_detector=raise_exception)
fio_runner=raise_exception, virtio_blk_detector=raise_exception
)
with self.assertRaises(DeviceExerciserError) as ex:
exerciser.run_fio("pcie_address", "fio_arguments")
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ def test_run_fio_success(self):
def test_run_fio_does_not_propagate_exception(self):
def fio_throws_exception(unused):
raise BaseException()
server = HostTargetService(
fio_throws_exception, detect_virtio_blk_device)

server = HostTargetService(fio_throws_exception, detect_virtio_blk_device)
request = host_target_pb2.RunFioRequest()
request.pciAddress = "unused"
request.fioArgs = "unused"
Expand All @@ -58,8 +58,8 @@ def fio_throws_exception(unused):
def test_run_fio_does_not_propagate_exception(self):
def detect_virtio_blk_throws_exception(unused):
raise BaseException()
server = HostTargetService(
successfull_fio, detect_virtio_blk_throws_exception)

server = HostTargetService(successfull_fio, detect_virtio_blk_throws_exception)
request = host_target_pb2.RunFioRequest()
request.pciAddress = "unused"
request.fioArgs = "unused"
Expand Down
Loading

0 comments on commit 2edb9b6

Please sign in to comment.