diff --git a/app/Data/Server/MacAddressData.php b/app/Data/Server/MacAddressData.php new file mode 100644 index 00000000..0dc0ddb9 --- /dev/null +++ b/app/Data/Server/MacAddressData.php @@ -0,0 +1,13 @@ + $node->id], $request->safe()->except('sync_server_config')))->load('server'); - if ($request->sync_server_config) { - + if ($request->sync_server_config && $request->server_id) { + $this->networkService->syncSettings($address->server); } return fractal($address, new AddressTransformer)->respond(); @@ -38,15 +43,33 @@ class AddressController extends Controller public function update(UpdateAddressRequest $request, Node $node, IPAddress $address) { + $oldServer = $address->server; + $address->update($request->safe()->except('sync_server_config')); + $newServer = $address->server; + + if ($request->sync_server_config && $oldServer?->id !== $newServer?->id) { + if ($oldServer) + $this->networkService->syncSettings($oldServer); + + if ($newServer) + $this->networkService->syncSettings($newServer); + } + return fractal($address, new AddressTransformer)->respond(); } public function destroy(Request $request, Node $node, IPAddress $address) { + $server = $address->server; + $address->delete(); + if ($request->sync_server_config && $server) { + $this->networkService->syncSettings($server); + } + return response()->noContent(); } } diff --git a/app/Http/Controllers/Admin/ServerController.php b/app/Http/Controllers/Admin/ServerController.php index ce978e03..5a185a75 100644 --- a/app/Http/Controllers/Admin/ServerController.php +++ b/app/Http/Controllers/Admin/ServerController.php @@ -18,7 +18,6 @@ class ServerController extends Controller public function index(Request $request) { - return 't'; $servers = QueryBuilder::for(Server::query()) ->with(['addresses', 'user', 'node']) ->allowedFilters([AllowedFilter::exact('node_id'), AllowedFilter::exact('user_id'), 'name',]) diff --git a/app/Services/Servers/NetworkService.php b/app/Services/Servers/NetworkService.php index d8bad473..5dcc53a8 100644 --- a/app/Services/Servers/NetworkService.php +++ b/app/Services/Servers/NetworkService.php @@ -4,11 +4,13 @@ namespace Convoy\Services\Servers; use Convoy\Data\Server\Deployments\CloudinitAddressConfigData; use Convoy\Data\Server\Eloquent\ServerAddressesData; +use Convoy\Data\Server\MacAddressData; use Convoy\Data\Server\Proxmox\ServerProxmoxData; use Convoy\Enums\Network\AddressType; use Convoy\Models\IPAddress; use Convoy\Models\Server; use Convoy\Repositories\Proxmox\Server\ProxmoxAllocationRepository; +use Convoy\Repositories\Proxmox\Server\ProxmoxCloudinitRepository; use Convoy\Services\ProxmoxService; use Illuminate\Database\ConnectionInterface; use Illuminate\Support\Arr; @@ -16,7 +18,7 @@ use Webmozart\Assert\Assert; class NetworkService extends ProxmoxService { - public function __construct(private CloudinitService $cloudinitService,private ProxmoxAllocationRepository $allocationRepository, private ConnectionInterface $connection, private ServerDetailService $detailService) + public function __construct(private CloudinitService $cloudinitService, private ProxmoxCloudinitRepository $cloudinitRepository, private ProxmoxAllocationRepository $allocationRepository, private ConnectionInterface $connection) { } @@ -55,40 +57,60 @@ class NetworkService extends ProxmoxService } } - public function getPrimaryMacAddress(Server $server) + public function getMacAddresses(Server $server, bool $eloquent = true, bool $proxmox = false) { - $details = $this->detailService->getByProxmox($server); + if ($eloquent) { + $addresses = $this->getAddresses($server); - return $details->limits->mac_address ?? $details->config->mac_address; + $eloquentMacAddress = $addresses->ipv4->first()?->mac_address ?? $addresses->ipv6->first()?->mac_address; + } + + if ($proxmox) { + $config = $this->cloudinitRepository->setServer($server)->getConfig(); + + $proxmoxMacAddress = null; + if (preg_match("/\b[[:xdigit:]]{2}:[[:xdigit:]]{2}:[[:xdigit:]]{2}:[[:xdigit:]]{2}:[[:xdigit:]]{2}:[[:xdigit:]]{2}\b/su", Arr::get($config, 'net0', ''), $matches)) { + $proxmoxMacAddress = $matches[0]; + } + } + + return MacAddressData::from([ + 'eloquent' => $eloquentMacAddress ?? null, + 'proxmox' => $proxmoxMacAddress ?? null, + ]); } public function getAddresses(Server $server): ServerAddressesData { - $server = $server->loadMissing('addresses'); - return ServerAddressesData::from([ 'ipv4' => $server->addresses->where('type', AddressType::IPV4->value)->toArray(), 'ipv6' => $server->addresses->where('type', AddressType::IPV6->value)->toArray() ]); } - public function syncSettings(Server $server, ServerProxmoxData $deployment) + public function syncSettings(Server $server) { - $macAddress = $this->getPrimaryMacAddress($server); + $this->setServer($server); + + $macAddresses = $this->getMacAddresses($server, true, true); + $addresses = $this->getAddresses($server); $this->clearIpsets(); $this->cloudinitService->updateIpConfig($server, CloudinitAddressConfigData::from([ - 'ipv4' => $deployment->limits->addresses->ipv4->first()?->toArray(), - 'ipv6' => $deployment->limits->addresses->ipv6->first()?->toArray(), + 'ipv4' => $addresses->ipv4->first()?->toArray(), + 'ipv6' => $addresses->ipv6->first()?->toArray(), ])); $this->lockIps(Arr::flatten($server->addresses()->get(['address'])->toArray())); + $macAddress = $macAddresses->eloquent ?? $macAddresses->proxmox; + $this->allocationRepository->setServer($this->server)->update(['net0' => "virtio={$macAddress},bridge={$this->node->network}"]); } public function updateRateLimit(Server $server, ?int $mebibytes = null) { - $macAddress = $this->getPrimaryMacAddress($server); + $macAddresses = $this->getMacAddresses($server, true, true); + $macAddress = $macAddresses->eloquent ?? $macAddresses->proxmox; $payload = "virtio={$macAddress},bridge={$this->node->network}"; diff --git a/app/Services/Servers/ServerDetailService.php b/app/Services/Servers/ServerDetailService.php index 64a6e0fd..e6fa6ed3 100644 --- a/app/Services/Servers/ServerDetailService.php +++ b/app/Services/Servers/ServerDetailService.php @@ -40,7 +40,7 @@ class ServerDetailService 'backups' => $server->backup_limit, 'bandwidth' => $server->bandwidth_limit, 'addresses' => $addresses, - 'mac_address' => $addresses->ipv4->first()?->mac_address ?? $addresses->ipv6->first()?->mac_address, + 'mac_address' => $this->networkService->getMacAddresses($server)->eloquent, ] ]); } @@ -49,18 +49,7 @@ class ServerDetailService { $server = $server->loadMissing(['addresses', 'node']); - $addresses = [ - 'ipv4' => $server->addresses->where('type', AddressType::IPV4->value)->toArray(), - 'ipv6' => $server->addresses->where('type', AddressType::IPV6->value)->toArray() - ]; - $resources = $this->allocationRepository->setServer($server)->getResources(); - $config = $this->cloudinitRepository->setServer($server)->getConfig(); - - $mac_address = null; - if (preg_match("/\b[[:xdigit:]]{2}:[[:xdigit:]]{2}:[[:xdigit:]]{2}:[[:xdigit:]]{2}:[[:xdigit:]]{2}:[[:xdigit:]]{2}\b/su", Arr::get($config, 'net0', ''), $matches)) { - $mac_address = $matches[0]; - } return ServerProxmoxData::from([ 'id' => $server->id, @@ -70,7 +59,7 @@ class ServerDetailService 'state' => Arr::get($resources, 'status'), 'locked' => Arr::get($resources, 'lock', false), 'config' => [ - 'mac_address' => $mac_address, + 'mac_address' => $this->networkService->getMacAddresses($server, false, true)->proxmox, 'boot_order' => $this->allocationService->getBootOrder($server), 'disks' => $this->allocationService->getDisks($server), 'addresses' => $this->cloudinitService->getIpConfig($server), diff --git a/composer.json b/composer.json index 22abcfe7..1199e6da 100644 --- a/composer.json +++ b/composer.json @@ -31,6 +31,7 @@ "webmozart/assert": "^1.11" }, "require-dev": { + "barryvdh/laravel-debugbar": "^3.7", "fakerphp/faker": "^1.9.1", "laravel/breeze": "^1.10", "laravel/pint": "^1.2", diff --git a/composer.lock b/composer.lock index 783f8744..867a252b 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "9b0fa007c133bbc90f751afe5b498a9a", + "content-hash": "3d63238e8df331192ead7c15598f5ec9", "packages": [ { "name": "bacon/bacon-qr-code", @@ -7106,6 +7106,90 @@ } ], "packages-dev": [ + { + "name": "barryvdh/laravel-debugbar", + "version": "v3.7.0", + "source": { + "type": "git", + "url": "https://github.com/barryvdh/laravel-debugbar.git", + "reference": "3372ed65e6d2039d663ed19aa699956f9d346271" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/barryvdh/laravel-debugbar/zipball/3372ed65e6d2039d663ed19aa699956f9d346271", + "reference": "3372ed65e6d2039d663ed19aa699956f9d346271", + "shasum": "" + }, + "require": { + "illuminate/routing": "^7|^8|^9", + "illuminate/session": "^7|^8|^9", + "illuminate/support": "^7|^8|^9", + "maximebf/debugbar": "^1.17.2", + "php": ">=7.2.5", + "symfony/finder": "^5|^6" + }, + "require-dev": { + "mockery/mockery": "^1.3.3", + "orchestra/testbench-dusk": "^5|^6|^7", + "phpunit/phpunit": "^8.5|^9.0", + "squizlabs/php_codesniffer": "^3.5" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.6-dev" + }, + "laravel": { + "providers": [ + "Barryvdh\\Debugbar\\ServiceProvider" + ], + "aliases": { + "Debugbar": "Barryvdh\\Debugbar\\Facades\\Debugbar" + } + } + }, + "autoload": { + "files": [ + "src/helpers.php" + ], + "psr-4": { + "Barryvdh\\Debugbar\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Barry vd. Heuvel", + "email": "barryvdh@gmail.com" + } + ], + "description": "PHP Debugbar integration for Laravel", + "keywords": [ + "debug", + "debugbar", + "laravel", + "profiler", + "webprofiler" + ], + "support": { + "issues": "https://github.com/barryvdh/laravel-debugbar/issues", + "source": "https://github.com/barryvdh/laravel-debugbar/tree/v3.7.0" + }, + "funding": [ + { + "url": "https://fruitcake.nl", + "type": "custom" + }, + { + "url": "https://github.com/barryvdh", + "type": "github" + } + ], + "time": "2022-07-11T09:26:42+00:00" + }, { "name": "doctrine/instantiator", "version": "1.4.1", @@ -7553,6 +7637,72 @@ }, "time": "2022-12-19T15:41:32+00:00" }, + { + "name": "maximebf/debugbar", + "version": "v1.18.1", + "source": { + "type": "git", + "url": "https://github.com/maximebf/php-debugbar.git", + "reference": "ba0af68dd4316834701ecb30a00ce9604ced3ee9" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/maximebf/php-debugbar/zipball/ba0af68dd4316834701ecb30a00ce9604ced3ee9", + "reference": "ba0af68dd4316834701ecb30a00ce9604ced3ee9", + "shasum": "" + }, + "require": { + "php": "^7.1|^8", + "psr/log": "^1|^2|^3", + "symfony/var-dumper": "^2.6|^3|^4|^5|^6" + }, + "require-dev": { + "phpunit/phpunit": "^7.5.20 || ^9.4.2", + "twig/twig": "^1.38|^2.7|^3.0" + }, + "suggest": { + "kriswallsmith/assetic": "The best way to manage assets", + "monolog/monolog": "Log using Monolog", + "predis/predis": "Redis storage" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.18-dev" + } + }, + "autoload": { + "psr-4": { + "DebugBar\\": "src/DebugBar/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Maxime Bouroumeau-Fuseau", + "email": "maxime.bouroumeau@gmail.com", + "homepage": "http://maximebf.com" + }, + { + "name": "Barry vd. Heuvel", + "email": "barryvdh@gmail.com" + } + ], + "description": "Debug bar in the browser for php application", + "homepage": "https://github.com/maximebf/php-debugbar", + "keywords": [ + "debug", + "debugbar" + ], + "support": { + "issues": "https://github.com/maximebf/php-debugbar/issues", + "source": "https://github.com/maximebf/php-debugbar/tree/v1.18.1" + }, + "time": "2022-03-31T14:55:54+00:00" + }, { "name": "mockery/mockery", "version": "1.5.1",