aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorMaxence Lange <maxence@artificial-owl.com>2024-07-18 10:35:48 -0100
committerMaxence Lange <maxence@artificial-owl.com>2024-07-29 12:44:52 -0100
commit79e60148799eee50b08c5edf07b91ba8428642fd (patch)
tree8ce10b3bc67ebeadab7af3d43bab3970fbab08ec /lib
parent88cfab4f329c7ee1f360be1ad7d90cf767da3720 (diff)
downloadnextcloud-server-79e60148799eee50b08c5edf07b91ba8428642fd.tar.gz
nextcloud-server-79e60148799eee50b08c5edf07b91ba8428642fd.zip
feat(upgrade): release metadata
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/private/Migration/MetadataManager.php153
-rw-r--r--lib/private/Updater/Exceptions/ReleaseMetadataException.php17
-rw-r--r--lib/private/Updater/ReleaseMetadata.php80
3 files changed, 250 insertions, 0 deletions
diff --git a/lib/private/Migration/MetadataManager.php b/lib/private/Migration/MetadataManager.php
new file mode 100644
index 00000000000..c5061b6fe0c
--- /dev/null
+++ b/lib/private/Migration/MetadataManager.php
@@ -0,0 +1,153 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-only
+ */
+namespace OC\Migration;
+
+use OC\DB\Connection;
+use OC\DB\MigrationService;
+use OCP\App\IAppManager;
+use OCP\Migration\Attributes\GenericMigrationAttribute;
+use OCP\Migration\Attributes\MigrationAttribute;
+use OCP\Migration\Exceptions\AttributeException;
+use Psr\Log\LoggerInterface;
+use ReflectionClass;
+
+/**
+ * Helps managing DB Migrations' Metadata
+ *
+ * @since 30.0.0
+ */
+class MetadataManager {
+ public function __construct(
+ private readonly IAppManager $appManager,
+ private readonly Connection $connection,
+ private readonly LoggerInterface $logger,
+ ) {
+ }
+
+ /**
+ * We get all migrations from an app (or 'core'), and
+ * for each migration files we extract its attributes
+ *
+ * @param string $appId
+ *
+ * @return array
+ * @since 30.0.0
+ */
+ public function extractMigrationAttributes(string $appId): array {
+ $ms = new MigrationService($appId, $this->connection);
+
+ $metadata = [];
+ foreach($ms->getAvailableVersions() as $version) {
+ $metadata[$version] = [];
+ $class = new ReflectionClass($ms->createInstance($version));
+ $attributes = $class->getAttributes();
+ foreach ($attributes as $attribute) {
+ $metadata[$version][] = $attribute->newInstance();
+ }
+ }
+
+ return $metadata;
+ }
+
+ /**
+ * convert direct data from release metadata into a list of Migrations' Attribute
+ *
+ * @param array $metadata
+ * @param bool $filterKnownMigrations ignore metadata already done in local instance
+ *
+ * @return array
+ * @since 30.0.0
+ */
+ public function getMigrationsAttributesFromReleaseMetadata(
+ array $metadata,
+ bool $filterKnownMigrations = false
+ ): array {
+ $appsAttributes = [];
+ foreach (array_keys($metadata['apps']) as $appId) {
+ if ($filterKnownMigrations && !$this->appManager->isInstalled($appId)) {
+ continue; // if not interested and app is not installed
+ }
+ $done = ($filterKnownMigrations) ? $this->getKnownMigrations($appId) : [];
+ $appsAttributes[$appId] = $this->parseMigrations($metadata['apps'][$appId] ?? [], $done);
+ }
+
+ $done = ($filterKnownMigrations) ? $this->getKnownMigrations('core') : [];
+ return [
+ 'core' => $this->parseMigrations($metadata['core'] ?? [], $done),
+ 'apps' => $appsAttributes
+ ];
+ }
+
+ /**
+ * convert raw data to a list of MigrationAttribute
+ *
+ * @param array $migrations
+ * @param array $ignoreMigrations
+ *
+ * @return array<string, MigrationAttribute[]>
+ */
+ private function parseMigrations(array $migrations, array $ignoreMigrations = []): array {
+ $parsed = [];
+ foreach (array_keys($migrations) as $entry) {
+ if (in_array($entry, $ignoreMigrations)) {
+ continue;
+ }
+
+ $parsed[$entry] = [];
+ foreach ($migrations[$entry] as $item) {
+ try {
+ $parsed[$entry][] = $this->createAttribute($item);
+ } catch (AttributeException $e) {
+ $this->logger->warning('exception while trying to create attribute', ['exception' => $e, 'item' => json_encode($item)]);
+ $parsed[$entry][] = new GenericMigrationAttribute($item);
+ }
+ }
+ }
+
+ return $parsed;
+ }
+
+ /**
+ * returns migrations already done
+ *
+ * @param string $appId
+ *
+ * @return array
+ * @throws \Exception
+ */
+ private function getKnownMigrations(string $appId): array {
+ $ms = new MigrationService($appId, $this->connection);
+ return $ms->getMigratedVersions();
+ }
+
+
+ /**
+ * generate (deserialize) a MigrationAttribute from a serialized version
+ *
+ * @param array $item
+ *
+ * @return MigrationAttribute
+ * @throws AttributeException
+ */
+ private function createAttribute(array $item): MigrationAttribute {
+ $class = $item['class'] ?? '';
+ $namespace = 'OCP\Migration\Attributes\\';
+ if (!str_starts_with($class, $namespace)
+ || !ctype_alpha(substr($class, strlen($namespace)))) {
+ throw new AttributeException('class name does not looks valid');
+ }
+
+ try {
+ $attribute = new $class();
+ return $attribute->import($item);
+ } catch (\Error) {
+ throw new AttributeException('cannot import Attribute');
+ }
+ }
+}
diff --git a/lib/private/Updater/Exceptions/ReleaseMetadataException.php b/lib/private/Updater/Exceptions/ReleaseMetadataException.php
new file mode 100644
index 00000000000..bc82e4e03df
--- /dev/null
+++ b/lib/private/Updater/Exceptions/ReleaseMetadataException.php
@@ -0,0 +1,17 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+namespace OC\Updater\Exceptions;
+
+use Exception;
+
+/**
+ * @since 30.0.0
+ */
+class ReleaseMetadataException extends Exception {
+}
diff --git a/lib/private/Updater/ReleaseMetadata.php b/lib/private/Updater/ReleaseMetadata.php
new file mode 100644
index 00000000000..22d8c843412
--- /dev/null
+++ b/lib/private/Updater/ReleaseMetadata.php
@@ -0,0 +1,80 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+namespace OC\Updater;
+
+use Exception;
+use JsonException;
+use OC\Updater\Exceptions\ReleaseMetadataException;
+use OCP\Http\Client\IClientService;
+
+/** retrieve releases metadata from official servers
+ *
+ * @since 30.0.0
+ */
+class ReleaseMetadata {
+ public function __construct(
+ private readonly IClientService $clientService,
+ ) {
+ }
+
+ /**
+ * returns metadata based on release version
+ *
+ * - version is a stable release, metadata is downloaded from official releases folder
+ * - version is not a table release, metadata is downloaded from official prereleases folder
+ * - version is a major version (30, 31, 32, ...), latest metadata are downloaded
+ *
+ * @param string $version
+ *
+ * @return array
+ * @throws ReleaseMetadataException
+ * @since 30.0.0
+ */
+ public function getMetadata(string $version): array {
+ if (!str_contains($version, '.')) {
+ $url = 'https://download.nextcloud.com/server/releases/latest-' . $version . '.metadata';
+ } else {
+ [,,$minor] = explode('.', $version);
+ if (ctype_digit($minor)) {
+ $url = 'https://download.nextcloud.com/server/releases/nextcloud-' . $version . '.metadata';
+ } else {
+ $url = 'https://download.nextcloud.com/server/prereleases/nextcloud-' . $version . '.metadata';
+ }
+ }
+ return $this->downloadMetadata($url);
+ }
+
+ /**
+ * download Metadata from a link
+ *
+ * @param string $url
+ *
+ * @return array
+ * @throws ReleaseMetadataException
+ * @since 30.0.0
+ */
+ public function downloadMetadata(string $url): array {
+ $client = $this->clientService->newClient();
+ try {
+ $response = $client->get($url, [
+ 'timeout' => 10,
+ 'connect_timeout' => 10,
+ 'verify' => false,
+ ]);
+ } catch (Exception $e) {
+ throw new ReleaseMetadataException('could not reach metadata at ' . $url, previous: $e);
+ }
+
+ try {
+ return json_decode($response->getBody(), true, flags: JSON_THROW_ON_ERROR);
+ } catch (JsonException) {
+ throw new ReleaseMetadataException('remote document is not valid');
+ }
+ }
+}