From 2584b4f125bdc1852b4427ddda4ec778c129a1c6 Mon Sep 17 00:00:00 2001 From: Alex Bezdieniezhnykh Date: Sat, 31 May 2025 17:48:26 +0300 Subject: [PATCH] hardware service works on linux --- .../Services/LoaderClient.cs | 51 +++++++++++++++++++ Azaion.Inference/hardware_service.pxd | 1 - Azaion.Inference/hardware_service.pyx | 32 ++++-------- Azaion.Suite.sln | 25 +++++++-- 4 files changed, 80 insertions(+), 29 deletions(-) create mode 100644 Azaion.CommonSecurity/Services/LoaderClient.cs diff --git a/Azaion.CommonSecurity/Services/LoaderClient.cs b/Azaion.CommonSecurity/Services/LoaderClient.cs new file mode 100644 index 0000000..7fc231f --- /dev/null +++ b/Azaion.CommonSecurity/Services/LoaderClient.cs @@ -0,0 +1,51 @@ +using System.Diagnostics; +using Azaion.CommonSecurity.DTO; +using MediatR; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; + +namespace Azaion.CommonSecurity.Services; + +public class LoaderClient +{ + private readonly IMediator _mediator; + private readonly ILogger _logger; + + public LoaderClient(IMediator mediator, IOptions config, ILogger logger) + { + _mediator = mediator; + _logger = logger; + try + { + using var process = new Process(); + process.StartInfo = new ProcessStartInfo + { + FileName = SecurityConstants.LoaderPath, + Arguments = $"--port {config.Value.ZeroMqPort} --api {config.Value.ApiUrl}", + //RedirectStandardOutput = true, + //RedirectStandardError = true, + //CreateNoWindow = true + }; + + process.OutputDataReceived += (_, e) => { if (e.Data != null) Console.WriteLine(e.Data); }; + process.ErrorDataReceived += (_, e) => { if (e.Data != null) Console.WriteLine(e.Data); }; + process.Start(); + } + catch (Exception e) + { + Console.WriteLine(e); + //throw; + } + + _requestAddress = $"tcp://{gpsConfig.Value.ZeroMqHost}:{gpsConfig.Value.ZeroMqPort}"; + _requestSocket.Connect(_requestAddress); + + _subscriberAddress = $"tcp://{gpsConfig.Value.ZeroMqHost}:{gpsConfig.Value.ZeroMqReceiverPort}"; + _subscriberSocket.Connect(_subscriberAddress); + _subscriberSocket.Subscribe(""); + _subscriberSocket.ReceiveReady += async (sender, e) => await ProcessClientCommand(sender, e); + + _poller.Add(_subscriberSocket); + _poller.RunAsync(); + } +} \ No newline at end of file diff --git a/Azaion.Inference/hardware_service.pxd b/Azaion.Inference/hardware_service.pxd index 628e9e7..dcbb929 100644 --- a/Azaion.Inference/hardware_service.pxd +++ b/Azaion.Inference/hardware_service.pxd @@ -1,5 +1,4 @@ cdef class HardwareService: - cdef bint is_windows @staticmethod cdef has_nvidia_gpu() diff --git a/Azaion.Inference/hardware_service.pyx b/Azaion.Inference/hardware_service.pyx index 9501b77..11e122e 100644 --- a/Azaion.Inference/hardware_service.pyx +++ b/Azaion.Inference/hardware_service.pyx @@ -1,20 +1,9 @@ +import os import subprocess import pynvml cdef class HardwareService: - """Handles hardware information retrieval and hash generation.""" - - def __init__(self): - try: - res = subprocess.check_output("ver", shell=True).decode('utf-8') - if "Microsoft Windows" in res: - self.is_windows = True - else: - self.is_windows = False - except Exception: - print('Error during os type checking') - self.is_windows = False @staticmethod cdef has_nvidia_gpu(): @@ -40,7 +29,7 @@ cdef class HardwareService: pass cdef str get_hardware_info(self): - if self.is_windows: + if os.name == 'nt': # windows os_command = ( "powershell -Command \"" "Get-CimInstance -ClassName Win32_Processor | Select-Object -ExpandProperty Name | Write-Output; " @@ -51,20 +40,17 @@ cdef class HardwareService: ) else: os_command = ( - "/bin/bash -c \" lscpu | grep 'Model name:' | cut -d':' -f2 && " + "lscpu | grep 'Model name:' | cut -d':' -f2 && " "lspci | grep VGA | cut -d':' -f3 && " - "free -g | grep Mem: | awk '{print $2}' && \"" - "udevadm info --query=property --name=\"/dev/$(lsblk -no pkname \"$(findmnt -n -o SOURCE --target /)\")\" | grep -E 'ID_SERIAL=|ID_SERIAL_SHORT=' | cut -d'=' -f2- | head -n1 && " + "free -k | awk '/^Mem:/ {print $2}' && " + "cat /sys/block/sda/device/vpd_pg80 2>/dev/null || cat /sys/block/sda/device/serial 2>/dev/null" ) - # in case of subprocess error do: - # cdef bytes os_command_bytes = os_command.encode('utf-8') - # and use os_command_bytes - result = subprocess.check_output(os_command, shell=True).decode('utf-8') - lines = [line.strip() for line in result.splitlines() if line.strip()] + result = subprocess.check_output(os_command, shell=True).decode('utf-8', errors='ignore') + lines = [line.replace(" ", " ").replace("Name=", "").strip('\x00\x14 \t\n\r\v\f') for line in result.splitlines() if line.strip()] - cdef str cpu = lines[0].replace("Name=", "").replace(" ", " ") - cdef str gpu = lines[1].replace("Name=", "").replace(" ", " ") + cdef str cpu = lines[0] + cdef str gpu = lines[1] # could be multiple gpus len_lines = len(lines) diff --git a/Azaion.Suite.sln b/Azaion.Suite.sln index 4c14f84..7dac5b3 100644 --- a/Azaion.Suite.sln +++ b/Azaion.Suite.sln @@ -1,5 +1,8 @@  Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.14.36127.28 d17.14 +MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Azaion.Annotator", "Azaion.Annotator\Azaion.Annotator.csproj", "{8E0809AF-2920-4267-B14D-84BAB334A46F}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Azaion.Test", "Azaion.Test\Azaion.Test.csproj", "{85359558-FB59-4542-A597-FD9E1B04C8E7}" @@ -24,19 +27,21 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Azaion.CommonSecurity", "Az EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Build", "Build", "{CF141A48-8002-4006-81CF-6B85AE5B0B5F}" ProjectSection(SolutionItems) = preProject - build\requirements.txt = build\requirements.txt - build\cdn_manager.py = build\cdn_manager.py - build\downloader_config.yaml = build\downloader_config.yaml build\build_cdn_manager.cmd = build\build_cdn_manager.cmd build\build_dotnet.cmd = build\build_dotnet.cmd - build\init.cmd = build\init.cmd + build\cdn_manager.py = build\cdn_manager.py + build\downloader_config.yaml = build\downloader_config.yaml build\download_models.cmd = build\download_models.cmd - build\publish.cmd = build\publish.cmd + build\init.cmd = build\init.cmd build\installer.full.iss = build\installer.full.iss build\installer.iterative.iss = build\installer.iterative.iss build\publish-full.cmd = build\publish-full.cmd + build\publish.cmd = build\publish.cmd + build\requirements.txt = build\requirements.txt EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Azaion.LoaderUI", "Azaion.LoaderUI\Azaion.LoaderUI.csproj", "{C96C142E-3ED3-4455-9C22-93A12022B8A9}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -81,9 +86,19 @@ Global {E0C7176D-2E91-4928-B3C1-55CC91C8F77D}.Release|Any CPU.ActiveCfg = Release|Any CPU {E0C7176D-2E91-4928-B3C1-55CC91C8F77D}.Release|Any CPU.Build.0 = Release|Any CPU {E0C7176D-2E91-4928-B3C1-55CC91C8F77D}.Release|Any CPU.Deploy.0 = Release|Any CPU + {C96C142E-3ED3-4455-9C22-93A12022B8A9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C96C142E-3ED3-4455-9C22-93A12022B8A9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C96C142E-3ED3-4455-9C22-93A12022B8A9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C96C142E-3ED3-4455-9C22-93A12022B8A9}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE EndGlobalSection GlobalSection(NestedProjects) = preSolution {32C4747F-F700-44FD-B4ED-21B4A66B5FAB} = {C307BE2E-FFCC-4BD7-AD89-C82D40B65D03} {A2E3D3AE-5DB7-4342-BE20-88A9D1B0C05E} = {C307BE2E-FFCC-4BD7-AD89-C82D40B65D03} EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {788BD4AD-E4EC-43A1-85A0-AEC644BD8D48} + EndGlobalSection EndGlobal