kernel->getProjectDir() . DIRECTORY_SEPARATOR . 'env.json'; if (!is_file($envFile)) { throw new \RuntimeException('Configuration file not found: ' . $envFile); } $config = json_decode(file_get_contents($envFile), true); $vars = $config['vars'] ?? []; $this->udsAPIurl = $vars['UDS_URL'] ?? throw new \RuntimeException('UDS_URL not set in env.json'); $this->udsAuthLogin = $vars['UDS_AUTH_LOGIN'] ?? throw new \RuntimeException('UDS_AUTH_LOGIN not set in env.json'); $this->udsAuthUsername = $vars['UDS_AUTH_USERNAME'] ?? throw new \RuntimeException('UDS_AUTH_USERNAME not set in env.json'); $this->udsAuthPassword = $vars['UDS_AUTH_PASSWORD'] ?? throw new \RuntimeException('UDS_AUTH_PASSWORD not set in env.json'); } /** * @throws TransportExceptionInterface */ public function __invoke(): array { $this->login(); $servicePools = $this->getServicePools(); $results = []; foreach ($servicePools as $servicePool) { $servicePoolInfo = $this->getServicePool($servicePool['provider_id'], $servicePool['service_id']); if (!$servicePoolInfo) { continue; } $labId = $servicePoolInfo['lab'] ?? null; $imageId = $servicePoolInfo['image'] ?? null; if (!$labId) { continue; } $maxAvailableSeats = $this->getMaxAvailableSeats($labId, $imageId); if ($maxAvailableSeats === null) { continue; } if ($maxAvailableSeats === -1) { $this->setLabReserved($labId, false); continue; } $servicePool['osmanager_id'] = null; $servicePool['max_srvs'] = $maxAvailableSeats; //$servicePool['initial_srvs'] = $maxAvailableSeats; //$servicePool['cache_l1_srvs'] = 0; $servicePool['visible'] = $maxAvailableSeats > 0; $response = $this->httpClient->request('PUT', $this->udsAPIurl . 'servicespools/' . $servicePool['id'], [ 'verify_host' => false, 'headers' => [ 'X-Auth-Token' => $this->token, 'Content-Type' => 'application/json', 'Scrambler' => $this->scrambler ], 'body' => json_encode($servicePool) ]); $this->setLabReserved($labId, true); $results[] = json_decode($response->getContent(false), true); } return $results; } /** * @throws TransportExceptionInterface */ public function login(): void { try { $response = $this->httpClient->request('POST', $this->udsAPIurl . 'auth/login', [ 'verify_host' => false, 'headers' => [ 'Content-Type' => 'application/json' ], 'json' => [ 'auth' => $this->udsAuthLogin, 'username' => $this->udsAuthUsername, 'password' => $this->udsAuthPassword ] ]); $statusCode = $response->getStatusCode(); $data = json_decode($response->getContent(false), true); if ($statusCode !== 200 || empty($data['token'])) { $this->logger->error('UDS login failed', [ 'status_code' => $statusCode, 'response' => $data, 'uds_url' => $this->udsAPIurl, 'uds_auth' => $this->udsAuthLogin, 'uds_username' => $this->udsAuthUsername, ]); throw new \RuntimeException('UDS login failed with status ' . $statusCode); } $this->token = $data['token']; $this->scrambler = $data['scrambler']; } catch (TransportExceptionInterface $e) { $this->logger->error('UDS login transport error', [ 'message' => $e->getMessage(), 'uds_url' => $this->udsAPIurl, ]); throw new TransportException('Error while logging in to UDS: ' . $e->getMessage(), 0, $e); } catch (ClientExceptionInterface|ServerExceptionInterface|RedirectionExceptionInterface $e) { $this->logger->error('UDS login HTTP error', [ 'message' => $e->getMessage(), 'uds_url' => $this->udsAPIurl, ]); throw new \RuntimeException('UDS login HTTP error: ' . $e->getMessage(), 0, $e); } } /** * @throws TransportExceptionInterface */ public function getServicePools(): array { try { $response = $this->httpClient->request('GET', $this->udsAPIurl . '/servicespools/overview', [ 'verify_host' => false, 'headers' => [ 'X-Auth-Token' => $this->token, 'Content-Type' => 'application/json', 'Scrambler' => $this->scrambler ] ]); return json_decode($response->getContent(), true); } catch (TransportExceptionInterface $e) { throw new TransportException('Error while fetching service pools'); } catch (ClientExceptionInterface|ServerExceptionInterface|RedirectionExceptionInterface $e) { } } /** * @throws TransportExceptionInterface */ public function getServicePool(string $providerId, string $serviceId): ?array { try { $response = $this->httpClient->request('GET', $this->udsAPIurl . 'providers/' . $providerId .'/services/'. $serviceId, [ 'verify_host' => false, 'headers' => [ 'X-Auth-Token' => $this->token, 'Content-Type' => 'application/json', 'Scrambler' => $this->scrambler ] ]); return json_decode($response->getContent(), true); } catch (TransportExceptionInterface $e) { throw new TransportException('Error while fetching service pool'); } catch (ClientExceptionInterface|ServerExceptionInterface|RedirectionExceptionInterface $e) { } return null; } public function getMaxAvailableSeats(int $labId, ?int $imageId = null): ?int { $organizationalUnit = $this->entityManager->getRepository(OrganizationalUnit::class)->findOneBy(['id' => $labId]); if (!$organizationalUnit || !$organizationalUnit->isRemotePc()) { return -1; } $remoteCalendar = $organizationalUnit->getRemoteCalendar(); if (!$remoteCalendar || !$remoteCalendar->isAvailable()) { return -1; } $totalAvailableClients = 0; foreach ($organizationalUnit->getClients() as $client) { if ($client->isMaintenance()) { continue; } if (!in_array($client->getStatus(), [ClientStatus::OG_LIVE, ClientStatus::WINDOWS, ClientStatus::LINUX])) { continue; } $remotePC = $client->getRemotePC(); if ($remotePC && $remotePC->isReservedActive()) { continue; } if ($imageId !== null && !$this->clientHasImage($client, $imageId)) { continue; } $totalAvailableClients++; } return $totalAvailableClients; } private function clientHasImage(Client $client, int $imageId): bool { foreach ($client->getPartitions() as $partition) { $iir = $partition->getImage(); if ($iir && $iir->getId() === $imageId) { return true; } } return false; } private function setLabReserved(int $labId, bool $reserved): void { $organizationalUnit = $this->entityManager->getRepository(OrganizationalUnit::class)->findOneBy(['id' => $labId]); if (!$organizationalUnit) { return; } $organizationalUnit->setReserved($reserved); $this->entityManager->flush(); } }