Skip to content

Commit

Permalink
WIP: Deploy options DB for update process (1)
Browse files Browse the repository at this point in the history
Signed-off-by: Andrey Borysenko <andrey18106x@gmail.com>
  • Loading branch information
andrey18106 committed Jan 17, 2025
1 parent ad473e8 commit eee8e6d
Show file tree
Hide file tree
Showing 10 changed files with 368 additions and 164 deletions.
17 changes: 0 additions & 17 deletions lib/Command/ExApp/Register.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ protected function configure(): void {
// Advanced deploy options
$this->addOption('env', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Optional deploy options (ENV_NAME=ENV_VALUE), passed to ExApp container as environment variables');
$this->addOption('mount', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Optional mount options (SRC_PATH=DST_PATH), passed to ExApp container as volume mounts');
$this->addOption('port', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Optional port mapping OPTIONAL_HOST_IP;HOST_PORT;CONTAINER_PORT (e.g. 443;9443/tcp, 0.0.0.0;80;8080/udp)');
}

protected function execute(InputInterface $input, OutputInterface $output): int {
Expand Down Expand Up @@ -102,22 +101,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}, $mounts);
$deployOptions['mounts'] = $mounts;

$ports = $input->getOption('port') ?? [];
// Parse array of port options strings (OPTIONAL_HOST_IP;HOST_PORT;CONTAINER_PORT)
// to array of arrays ['HostPort' => HOST_PORT, 'HostIp' => OPTIONAL_HOST_IP, 'ContainerPort' => CONTAINER_PORT]
// It could be two (without optional) or three parts separated by semicolon
$ports = array_map(function ($item) {
$parts = explode(';', $item, 3);
if (count($parts) === 2) {
return ['HostPort' => $parts[0], 'ContainerPort' => $parts[1]];
}
if (count($parts) === 3) {
return ['HostIp' => $parts[0], 'HostPort' => $parts[1], 'ContainerPort' => $parts[2]];
}
return []; // invalid port binding option
}, $ports);
$deployOptions['ports'] = $ports;

$appInfo = $this->exAppService->getAppInfo(
$appId, $input->getOption('info-xml'), $input->getOption('json-info'),
$deployOptions
Expand Down
24 changes: 15 additions & 9 deletions lib/Command/ExApp/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use OCA\AppAPI\Service\AppAPIService;
use OCA\AppAPI\Service\DaemonConfigService;

use OCA\AppAPI\Service\ExAppDeployOptionsService;
use OCA\AppAPI\Service\ExAppService;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Command\Command;
Expand All @@ -27,14 +28,15 @@
class Update extends Command {

public function __construct(
private readonly AppAPIService $service,
private readonly ExAppService $exAppService,
private readonly DaemonConfigService $daemonConfigService,
private readonly DockerActions $dockerActions,
private readonly ManualActions $manualActions,
private readonly LoggerInterface $logger,
private readonly ExAppArchiveFetcher $exAppArchiveFetcher,
private readonly ExAppFetcher $exAppFetcher,
private readonly AppAPIService $service,
private readonly ExAppService $exAppService,
private readonly DaemonConfigService $daemonConfigService,
private readonly DockerActions $dockerActions,
private readonly ManualActions $manualActions,
private readonly LoggerInterface $logger,
private readonly ExAppArchiveFetcher $exAppArchiveFetcher,
private readonly ExAppFetcher $exAppFetcher,
private readonly ExAppDeployOptionsService $exAppDeployOptionsService,
) {
parent::__construct();
}
Expand Down Expand Up @@ -90,8 +92,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int

private function updateExApp(InputInterface $input, OutputInterface $output, string $appId): int {
$outputConsole = !$input->getOption('silent');
$deployOptions = $this->exAppDeployOptionsService->formatDeployOptions(
$this->exAppDeployOptionsService->getDeployOptions()
);
$appInfo = $this->exAppService->getAppInfo(
$appId, $input->getOption('info-xml'), $input->getOption('json-info')
$appId, $input->getOption('info-xml'), $input->getOption('json-info'),
$deployOptions
);
if (isset($appInfo['error'])) {
$this->logger->error($appInfo['error']);
Expand Down
52 changes: 23 additions & 29 deletions lib/Controller/ExAppsPageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use OCA\AppAPI\Fetcher\ExAppFetcher;
use OCA\AppAPI\Service\AppAPIService;
use OCA\AppAPI\Service\DaemonConfigService;
use OCA\AppAPI\Service\ExAppDeployOptionsService;
use OCA\AppAPI\Service\ExAppService;
use OCP\App\IAppManager;
use OCP\AppFramework\Controller;
Expand Down Expand Up @@ -53,6 +54,7 @@ public function __construct(
private readonly LoggerInterface $logger,
private readonly IAppManager $appManager,
private readonly ExAppService $exAppService,
private readonly ExAppDeployOptionsService $exAppDeployOptionsService,
) {
parent::__construct(Application::APP_ID, $request);
}
Expand Down Expand Up @@ -319,38 +321,21 @@ public function enableApp(string $appId, array $deployOptions = []): JSONRespons
$envOptions = isset($deployOptions['environment_variables'])
? array_keys($deployOptions['environment_variables']) : [];
$envOptionsString = '';
// build --deploy-option ENV_NAME=ENV_VALUE string
foreach ($envOptions as $envOption) {
$envOptionsString .= sprintf(' --env %s=%s', $envOption, $deployOptions['environment_variables'][$envOption]);
}
$envOptionsString = trim($envOptionsString);

$mountOptions = $deployOptions['mounts'] ?? [];
$mountOptionsString = '';
// build --mount SRC_PATH=DST_PATH string
foreach ($mountOptions as $mountOption) {
$readonlyModifier = $mountOption['readonly'] ? 'ro' : 'rw';
$mountOptionsString .= sprintf(' --mount %s:%s:%s', $mountOption['hostPath'], $mountOption['containerPath'], $readonlyModifier);
}
$mountOptionsString = trim($mountOptionsString);

// port options in $deployOptions['ports'], array of ['hostPort' => '', 'hostIp' => '', 'containerPort' => '']
// convert to array of strings with --port HOST_PORT;CONTAINER_PORT or --port HOST_IP;HOST_PORT;CONTAINER_PORT
$portOptions = $deployOptions['ports'] ?? [];
$portOptionsString = '';
// build --port HOST_PORT;CONTAINER_PORT string
foreach ($portOptions as $portOption) {
if (isset($portOption['hostIp']) && $portOption['hostIp'] !== '') {
$portOptionsString .= sprintf(' --port %s;%s;%s', $portOption['hostIp'], $portOption['hostPort'], $portOption['containerPort']);
} else {
$portOptionsString .= sprintf(' --port %s;%s', $portOption['hostPort'], $portOption['containerPort']);
}
}
$portOptionsString = trim($portOptionsString);

// If ExApp is not registered - then it's a "Deploy and Enable" action.
if (!$exApp) {
// $command = sprintf("app_api:app:register --silent %s %s %s %s", $appId, $envOptionsString, $mountOptionsString, $portOptionsString);
if (!$this->service->runOccCommand(sprintf("app_api:app:register --silent %s %s %s %s", $appId, $envOptionsString, $mountOptionsString, $portOptionsString))) {

Check failure on line 339 in lib/Controller/ExAppsPageController.php

View workflow job for this annotation

GitHub Actions / php-psalm-analysis (8.1)

UndefinedVariable

lib/Controller/ExAppsPageController.php:339:140: UndefinedVariable: Cannot find referenced variable $portOptionsString (see https://psalm.dev/024)
return new JSONResponse(['data' => ['message' => $this->l10n->t('Error starting install of ExApp')]], Http::STATUS_INTERNAL_SERVER_ERROR);
}
Expand Down Expand Up @@ -515,27 +500,36 @@ public function getAppLogs(string $appId, string $tail = 'all'): DataDownloadRes
}
}

/**
* Returns ExApp Advanced deploy options (only for docker-install), container environment variables, mounts and ports
*/
public function getAppDeployOptions(string $appId) {
$exApp = $this->exAppService->getExApp($appId);
if (is_null($exApp)) {
return new JSONResponse(['error' => $this->l10n->t('ExApp not found, failed to get deploy options')], Http::STATUS_NOT_FOUND);
}
$daemonConfig = $this->daemonConfigService->getDaemonConfigByName($exApp->getDaemonConfigName());
if ($daemonConfig->getAcceptsDeployId() !== $this->dockerActions->getAcceptsDeployId()) {
return new JSONResponse(['error' => $this->l10n->t('ExApp deploy options are not supported by the daemon')], Http::STATUS_NOT_IMPLEMENTED);
}

$this->dockerActions->initGuzzleClient($daemonConfig);
$deployOptions = $this->dockerActions->getDeployOptions($appId, $daemonConfig);
$deployOptions = $this->exAppDeployOptionsService->formatDeployOptions(
$this->exAppDeployOptionsService->getDeployOptions($appId)
);

$envs = [];
if (isset($deployOptions['environment_variables'])) {
$envs = $deployOptions['environment_variables'];
}

if (isset($deployOptions['error'])) {
return new JSONResponse(['error' => $deployOptions['error']], Http::STATUS_INTERNAL_SERVER_ERROR);
$mounts = [];
if (isset($deployOptions['mounts'])) {
foreach ($deployOptions['mounts'] as $mount) {
$mounts[] = [
'hostPath' => $mount['source'],
'containerPath' => $mount['target'],
'readonly' => $mount['mode'] === 'ro'
];
}
}

return new JSONResponse($deployOptions);
return new JSONResponse([
'environment_variables' => $envs,
'mounts' => $mounts,
]);
}

/**
Expand Down
64 changes: 64 additions & 0 deletions lib/Db/ExAppDeployOption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\AppAPI\Db;

use JsonSerializable;
use OCP\AppFramework\Db\Entity;

/**
* Class ExAppDeployOption
*
* @package OCA\AppAPI\Db
*
* @method string getAppid()
* @method string getType()
* @method array getValue()
* @method void setAppid(string $appid)
* @method void setName(string $name)
* @method void setType(string $type)
* @method void setValue(array $value)
*/
class ExAppDeployOption extends Entity implements JsonSerializable {
protected $appid;
protected $type;
protected $value;

/**
* @param array $params
*/
public function __construct(array $params = []) {
$this->addType('appid', 'string');
$this->addType('name', 'string');
$this->addType('type', 'string');
$this->addType('value', 'json');

if (isset($params['id'])) {
$this->setId($params['id']);
}
if (isset($params['appid'])) {
$this->setAppid($params['appid']);
}
if (isset($params['type'])) {
$this->setType($params['type']);
}
if (isset($params['value'])) {
$this->setValue($params['value']);
}
}

public function jsonSerialize(): array {
return [
'id' => $this->getId(),
'appid' => $this->getAppid(),
'type' => $this->getType(),
'value' => $this->getValue(),
];
}
}
47 changes: 47 additions & 0 deletions lib/Db/ExAppDeployOptionsMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\AppAPI\Db;

use OCP\AppFramework\Db\QBMapper;
use OCP\DB\Exception;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;

/**
* @template-extends QBMapper<ExAppDeployOption>
*/
class ExAppDeployOptionsMapper extends QBMapper {
public function __construct(IDBConnection $db) {
parent::__construct($db, 'ex_deploy_options');
}

/**
* @throws Exception
*/
public function findAll(): array {
$qb = $this->db->getQueryBuilder();
$result = $qb->select('exs.*')
->from($this->tableName, 'exs')
->executeQuery();
return $result->fetchAll();
}

/**
* @throws Exception
*/
public function removeAllByAppId(string $appId): int {
$qb = $this->db->getQueryBuilder();
$qb->delete($this->tableName)
->where(
$qb->expr()->eq('appid', $qb->createNamedParameter($appId, IQueryBuilder::PARAM_STR))
);
return $qb->executeStatement();
}
}
Loading

0 comments on commit eee8e6d

Please sign in to comment.