-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathExAppService.php
More file actions
462 lines (433 loc) · 16.3 KB
/
Copy pathExAppService.php
File metadata and controls
462 lines (433 loc) · 16.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\AppAPI\Service;
use InvalidArgumentException;
use OCA\AppAPI\AppInfo\Application;
use OCA\AppAPI\Db\ExApp;
use OCA\AppAPI\Db\ExAppMapper;
use OCA\AppAPI\Fetcher\ExAppArchiveFetcher;
use OCA\AppAPI\Fetcher\ExAppFetcher;
use OCA\AppAPI\Service\ProvidersAI\TaskProcessingService;
use OCA\AppAPI\Service\UI\FilesActionsMenuService;
use OCA\AppAPI\Service\UI\InitialStateService;
use OCA\AppAPI\Service\UI\ScriptsService;
use OCA\AppAPI\Service\UI\SettingsService;
use OCA\AppAPI\Service\UI\StylesService;
use OCA\AppAPI\Service\UI\TopMenuService;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\DB\Exception;
use OCP\ICache;
use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\IUser;
use OCP\IUserManager;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Log\LoggerInterface;
use SimpleXMLElement;
class ExAppService {
private ?ICache $cache = null;
private AppAPIService $appAPIService;
public function __construct(
private readonly LoggerInterface $logger,
ICacheFactory $cacheFactory,
private readonly IUserManager $userManager,
private readonly ExAppFetcher $exAppFetcher,
private readonly ExAppArchiveFetcher $exAppArchiveFetcher,
private readonly ExAppMapper $exAppMapper,
private readonly TopMenuService $topMenuService,
private readonly InitialStateService $initialStateService,
private readonly ScriptsService $scriptsService,
private readonly StylesService $stylesService,
private readonly FilesActionsMenuService $filesActionsMenuService,
private readonly TaskProcessingService $taskProcessingService,
private readonly TalkBotsService $talkBotsService,
private readonly SettingsService $settingsService,
private readonly ExAppOccService $occService,
private readonly ExAppDeployOptionsService $deployOptionsService,
private readonly IConfig $config,
) {
if ($cacheFactory->isAvailable()) {
$distributedCacheClass = ltrim($config->getSystemValueString('memcache.distributed', ''), '\\');
$localCacheClass = ltrim($config->getSystemValueString('memcache.local', ''), '\\');
if (
($distributedCacheClass === '' && $localCacheClass !== \OC\Memcache\APCu::class)
|| ($distributedCacheClass !== '' && $distributedCacheClass !== \OC\Memcache\APCu::class)
) {
$this->cache = $cacheFactory->createDistributed(Application::APP_ID . '/service');
}
}
$this->taskProcessingService->setExAppService($this);
}
public function getExApp(string $appId): ?ExApp {
foreach ($this->getExApps() as $exApp) {
if ($exApp->getAppid() === $appId) {
return $exApp;
}
}
$this->logger->debug(sprintf('ExApp "%s" not found.', $appId));
return null;
}
public function registerExApp(array $appInfo): ?ExApp {
$exApp = new ExApp([
'appid' => $appInfo['id'],
'version' => $appInfo['version'],
'name' => $appInfo['name'],
'daemon_config_name' => $appInfo['daemon_config_name'],
'port' => $appInfo['port'],
'secret' => $appInfo['secret'],
'status' => json_encode(['deploy' => 0, 'init' => 0, 'action' => '', 'type' => 'install', 'error' => '']),
'created_time' => time(),
]);
try {
$this->exAppMapper->insert($exApp);
$exApp = $this->exAppMapper->findByAppId($appInfo['id']);
$this->cache?->remove('/ex_apps');
if (isset($appInfo['external-app']['routes'])) {
$exApp->setRoutes($this->registerExAppRoutes($exApp, $appInfo['external-app']['routes'])->getRoutes() ?? []);
}
return $exApp;
} catch (Exception|MultipleObjectsReturnedException|DoesNotExistException $e) {
$this->logger->error(sprintf('Error while registering ExApp %s: %s', $appInfo['id'], $e->getMessage()));
return null;
}
}
public function unregisterExApp(string $appId): bool {
$exApp = $this->getExApp($appId);
if ($exApp === null) {
return false;
}
$this->talkBotsService->unregisterExAppTalkBots($exApp); // TODO: Think about internal Events for clean and flexible unregister ExApp callbacks
$this->filesActionsMenuService->unregisterExAppFileActions($appId);
$this->topMenuService->unregisterExAppMenuEntries($appId);
$this->initialStateService->deleteExAppInitialStates($appId);
$this->scriptsService->deleteExAppScripts($appId);
$this->stylesService->deleteExAppStyles($appId);
$this->taskProcessingService->unregisterExAppTaskProcessingProviders($appId);
$this->settingsService->unregisterExAppForms($appId);
$this->exAppArchiveFetcher->removeExAppFolder($appId);
$this->occService->unregisterExAppOccCommands($appId);
$this->deployOptionsService->removeExAppDeployOptions($appId);
$this->unregisterExAppWebhooks($appId);
$r = $this->exAppMapper->deleteExApp($appId);
if ($r !== 1) {
$this->logger->error(sprintf('Error while unregistering %s ExApp from the database.', $appId));
}
$rmRoutes = $this->removeExAppRoutes($exApp);
if ($rmRoutes === null) {
$this->logger->error(sprintf('Error while unregistering %s ExApp routes from the database.', $appId));
}
$this->cache?->remove('/ex_apps');
return $r === 1 && $rmRoutes !== null;
}
public function getExAppFreePort(): int {
try {
$ports = $this->exAppMapper->getUsedPorts();
for ($port = 23000; $port <= 23999; $port++) {
if (!in_array($port, $ports)) {
return $port;
}
}
} catch (Exception) {
}
return 0;
}
public function enableExAppInternal(ExApp $exApp): bool {
$exApp->setEnabled(1);
$status = $exApp->getStatus();
$status['error'] = '';
$exApp->setStatus($status);
return $this->updateExApp($exApp, ['enabled', 'status']);
}
public function disableExAppInternal(ExApp $exApp): void {
$exApp->setEnabled(0);
$this->updateExApp($exApp, ['enabled']);
}
public function getExAppsByDaemonName(string $daemonName): array {
try {
return array_filter($this->exAppMapper->findAll(), function (ExApp $exApp) use ($daemonName) {
return $exApp->getDaemonConfigName() === $daemonName;
});
} catch (Exception $e) {
$this->logger->error(sprintf('Error while getting ExApps list. Error: %s', $e->getMessage()), ['exception' => $e]);
return [];
}
}
public function getExAppsList(string $list = 'enabled'): array {
$exApps = $this->getExApps();
if ($list === 'enabled') {
$exApps = array_values(array_filter($exApps, function (ExApp $exApp) {
return $exApp->getEnabled() === 1;
}));
}
return array_map(function (ExApp $exApp) {
return $this->formatExAppInfo($exApp);
}, $exApps);
}
public function formatExAppInfo(ExApp $exApp): array {
return [
'id' => $exApp->getAppid(),
'name' => $exApp->getName(),
'version' => $exApp->getVersion(),
'enabled' => filter_var($exApp->getEnabled(), FILTER_VALIDATE_BOOLEAN),
'status' => $exApp->getStatus(),
];
}
public function getNCUsersList(): ?array {
return array_map(function (IUser $user) {
return $user->getUID();
}, $this->userManager->searchDisplayName(''));
}
public function updateExAppInfo(ExApp $exApp, array $appInfo): bool {
$exApp->setVersion($appInfo['version']);
$exApp->setName($appInfo['name']);
if (!$this->updateExApp($exApp, ['version', 'name'])) {
return false;
}
return true;
}
public function updateExApp(ExApp $exApp, array $fields = ['version', 'name', 'port', 'status', 'enabled']): bool {
try {
$this->exAppMapper->updateExApp($exApp, $fields);
$this->cache?->remove('/ex_apps');
if (in_array('enabled', $fields) || in_array('version', $fields)) {
$this->resetCaches();
}
return true;
} catch (Exception $e) {
$this->logger->error(sprintf('Failed to update "%s" ExApp info.', $exApp->getAppid()), ['exception' => $e]);
$this->cache?->remove('/ex_apps');
$this->resetCaches();
}
return false;
}
public function getLatestExAppInfoFromAppstore(string $appId, string &$extractedDir): ?SimpleXMLElement {
$exApps = $this->exAppFetcher->get();
$exAppAppstoreData = array_filter($exApps, function (array $exAppItem) use ($appId) {
return $exAppItem['id'] === $appId && count($exAppItem['releases']) > 0;
});
if (empty($exAppAppstoreData)) {
return null;
}
$exAppAppstoreData = end($exAppAppstoreData);
$exAppReleaseInfo = end($exAppAppstoreData['releases']);
if ($exAppReleaseInfo !== false) {
return $this->exAppArchiveFetcher->downloadInfoXml($exAppAppstoreData, $extractedDir);
}
return null;
}
private function resetCaches(): void {
$this->topMenuService->resetCacheEnabled();
$this->filesActionsMenuService->resetCacheEnabled();
$this->settingsService->resetCacheEnabled();
$this->occService->resetCacheEnabled();
$this->deployOptionsService->resetCache();
}
public function getAppInfo(string $appId, ?string $infoXml, ?string $jsonInfo, ?array $deployOptions = null): array {
$extractedDir = '';
if ($jsonInfo !== null) {
$appInfo = json_decode($jsonInfo, true);
if (!$appInfo) {
return ['error' => 'Invalid app info provided in JSON format'];
}
# fill 'id' if it is missing(this field was called `appid` in previous versions in json)
$appInfo['id'] = $appInfo['id'] ?? $appId;
# during manual install JSON can have all values at root level
foreach (['docker-install', 'translations_folder', 'routes', 'k8s-service-roles'] as $key) {
if (isset($appInfo[$key])) {
$appInfo['external-app'][$key] = $appInfo[$key];
unset($appInfo[$key]);
}
}
} else {
if ($infoXml !== null) {
$infoXmlContents = file_get_contents($infoXml);
if ($infoXmlContents === false) {
return ['error' => sprintf('Failed to read info.xml from %s', $infoXml)];
}
$xmlAppInfo = simplexml_load_string($infoXmlContents);
if ($xmlAppInfo === false) {
return ['error' => sprintf('Failed to load info.xml from %s', $infoXml)];
}
} else {
$xmlAppInfo = $this->getLatestExAppInfoFromAppstore($appId, $extractedDir);
if (empty($xmlAppInfo)) {
return ['error' => sprintf('Failed to get app info for `%s` from the Appstore', $appId)];
}
}
$appInfo = json_decode(json_encode((array)$xmlAppInfo), true);
if (isset($appInfo['external-app']['routes']['route'])) {
if (isset($appInfo['external-app']['routes']['route'][0])) {
$appInfo['external-app']['routes'] = $appInfo['external-app']['routes']['route'];
} else {
$appInfo['external-app']['routes'] = [$appInfo['external-app']['routes']['route']];
}
}
// Advanced deploy options
if (isset($appInfo['external-app']['environment-variables']['variable'])) {
$envVars = [];
if (!isset($appInfo['external-app']['environment-variables']['variable'][0])) {
$appInfo['external-app']['environment-variables']['variable'] = [$appInfo['external-app']['environment-variables']['variable']];
}
foreach ($appInfo['external-app']['environment-variables']['variable'] as $envVar) {
$envVars[$envVar['name']] = [
'name' => $envVar['name'],
'displayName' => $envVar['display-name'] ?? '',
'description' => $envVar['description'] ?? '',
'default' => $envVar['default'] ?? '',
'value' => $envVar['default'] ?? '',
];
}
if (isset($deployOptions['environment_variables']) && count(array_keys($deployOptions['environment_variables'])) > 0) {
// override with given deploy options values
foreach ($deployOptions['environment_variables'] as $key => $value) {
if (array_key_exists($key, $envVars)) {
$envVars[$key]['value'] = $value['value'] ?? $value ?? '';
}
}
}
$envVars = array_filter($envVars, function ($envVar) {
return $envVar['value'] !== '';
});
$appInfo['external-app']['environment-variables'] = $envVars;
}
if (isset($appInfo['external-app']['k8s-service-roles']['role'])) {
$roles = $appInfo['external-app']['k8s-service-roles']['role'];
if (!isset($roles[0])) {
$roles = [$roles];
}
$appInfo['external-app']['k8s-service-roles'] = array_map(function ($role) {
return [
'name' => (string)($role['name'] ?? ''),
'display-name' => (string)($role['display-name'] ?? $role['name'] ?? ''),
'env' => (string)($role['env'] ?? ''),
'expose' => filter_var($role['expose'] ?? 'false', FILTER_VALIDATE_BOOLEAN),
];
}, $roles);
}
if (isset($deployOptions['mounts'])) {
$appInfo['external-app']['mounts'] = $deployOptions['mounts'];
}
if ($extractedDir) {
if (file_exists($extractedDir . '/l10n')) {
$appInfo['translations_folder'] = $extractedDir . '/l10n';
} else {
$this->logger->info(sprintf('Application %s does not support translations', $appId));
}
}
}
if (isset($appInfo['external-app']['routes'])) {
if (!is_array($appInfo['external-app']['routes'])) {
return ['error' => sprintf("ExApp '%s' has invalid route definition. 'routes' must be a list of route objects, got %s", $appId, get_debug_type($appInfo['external-app']['routes']))];
}
try {
$appInfo['external-app']['routes'] = ExAppRouteHelper::normalizeAndValidate($appInfo['external-app']['routes']);
} catch (InvalidArgumentException $e) {
return ['error' => sprintf("ExApp '%s' has invalid route definition. %s", $appId, $e->getMessage())];
}
}
return $appInfo;
}
public function setAppDeployProgress(ExApp $exApp, int $progress, string $error = ''): void {
if ($progress < 0 || $progress > 100) {
throw new InvalidArgumentException('Invalid ExApp deploy status progress value');
}
$status = $exApp->getStatus();
if ($progress !== 0 && isset($status['deploy']) && $status['deploy'] === 100) {
return;
}
if ($error !== '') {
$this->logger->error(sprintf('ExApp %s deploying failed. Error: %s', $exApp->getAppid(), $error));
$status['error'] = $error;
} else {
if ($progress === 0) {
$status['action'] = 'deploy';
$status['deploy_start_time'] = time();
$status['error'] = '';
}
$status['deploy'] = $progress;
}
if ($progress === 100) {
$status['action'] = 'healthcheck';
}
$exApp->setStatus($status);
$this->updateExApp($exApp, ['status']);
}
public function waitInitStepFinish(string $appId): string {
do {
$exApp = $this->getExApp($appId);
$status = $exApp->getStatus();
if (isset($status['error']) && $status['error'] !== '') {
return sprintf('ExApp %s initialization step failed. Error: %s', $appId, $status['error']);
}
usleep(100000); // 0.1s
} while ($status['init'] !== 100);
return '';
}
public function setStatusError(ExApp $exApp, string $error): void {
$status = $exApp->getStatus();
$status['error'] = $error;
$exApp->setStatus($status);
$this->updateExApp($exApp, ['status']);
}
/**
* Get list of registered ExApps
*
* @return ExApp[]
*/
public function getExApps(): array {
try {
$cacheKey = '/ex_apps';
$records = $this->cache?->get($cacheKey);
if ($records !== null) {
return array_map(function ($record) {
return $record instanceof ExApp ? $record : new ExApp($record);
}, $records);
}
$records = $this->exAppMapper->findAll();
$this->cache?->set($cacheKey, $records);
return $records;
} catch (Exception) {
return [];
}
}
public function registerExAppRoutes(ExApp $exApp, array $routes): ?ExApp {
try {
$this->exAppMapper->registerExAppRoutes($exApp, $routes);
return $this->exAppMapper->findByAppId($exApp->getAppid());
} catch (Exception|MultipleObjectsReturnedException|DoesNotExistException $e) {
$this->logger->error(sprintf('Error while registering ExApp %s routes: %s. Routes: %s', $exApp->getAppid(), $e->getMessage(), json_encode($routes)));
return null;
}
}
public function removeExAppRoutes(ExApp $exApp): ?ExApp {
try {
$this->exAppMapper->removeExAppRoutes($exApp);
$exApp->setRoutes([]);
return $exApp;
} catch (Exception) {
return null;
}
}
/**
* @psalm-suppress UndefinedClass
*/
private function unregisterExAppWebhooks(string $appId): void {
try {
$webhookListenerMapper = \OCP\Server::get(\OCA\WebhookListeners\Db\WebhookListenerMapper::class);
$webhookListenerMapper->deleteByAppId($appId);
} catch (ContainerExceptionInterface|NotFoundExceptionInterface $e) {
} catch (Exception $e) {
$this->logger->debug(sprintf('Error while unregistering ExApp %s webhooks: %s', $appId, $e->getMessage()));
}
}
public function setAppAPIService(AppAPIService $appAPIService): void {
$this->appAPIService = $appAPIService;
$this->taskProcessingService->setAppAPIService($this->appAPIService);
}
}