-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathUnregister.php
More file actions
157 lines (140 loc) · 4.71 KB
/
Copy pathUnregister.php
File metadata and controls
157 lines (140 loc) · 4.71 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
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\AppAPI\Command\ExApp;
use OCA\AppAPI\DeployActions\DockerActions;
use OCA\AppAPI\Service\AppAPIService;
use OCA\AppAPI\Service\DaemonConfigService;
use OCA\AppAPI\Service\ExAppService;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class Unregister extends Command {
public function __construct(
private readonly AppAPIService $service,
private readonly DaemonConfigService $daemonConfigService,
private readonly DockerActions $dockerActions,
private readonly ExAppService $exAppService,
) {
parent::__construct();
}
protected function configure(): void {
$this->setName('app_api:app:unregister');
$this->setDescription('Unregister external app');
$this->addArgument('appid', InputArgument::REQUIRED);
$this->addOption(
'silent',
null,
InputOption::VALUE_NONE,
'Print only minimum and only errors.');
$this->addOption(
'force',
null,
InputOption::VALUE_NONE,
'Continue removal even if errors.');
$this->addOption('keep-data', null, InputOption::VALUE_NONE, 'Keep ExApp data (volume) [deprecated, data is kept by default].');
$this->addOption('rm-data', null, InputOption::VALUE_NONE, 'Remove ExApp data (persistent storage volume).');
$this->addUsage('test_app');
$this->addUsage('test_app --silent');
$this->addUsage('test_app --rm-data');
$this->addUsage('test_app --silent --force --rm-data');
}
protected function execute(InputInterface $input, OutputInterface $output): int {
$appId = $input->getArgument('appid');
$silent = $input->getOption('silent');
$force = $input->getOption('force');
$rmData = $input->getOption('rm-data');
$exApp = $this->exAppService->getExApp($appId);
if ($exApp === null) {
if ($silent) {
return 0;
}
$output->writeln(sprintf('ExApp %s not found. Failed to unregister.', $appId));
return 1;
}
if ($exApp->getEnabled()) {
if (!$this->service->disableExApp($exApp)) {
if (!$silent) {
$output->writeln(sprintf('Error during disabling %s ExApp.', $appId));
}
if (!$force) {
return 1;
}
} elseif (!$silent) {
$output->writeln(sprintf('ExApp %s successfully disabled.', $appId));
}
}
$daemonConfig = $this->daemonConfigService->getDaemonConfigByName($exApp->getDaemonConfigName());
if ($daemonConfig === null) {
if (!$silent) {
$output->writeln(
sprintf('Failed to get ExApp %s DaemonConfig by name %s', $appId, $exApp->getDaemonConfigName())
);
}
if (!$force) {
return 1;
}
}
if ($daemonConfig->getAcceptsDeployId() === $this->dockerActions->getAcceptsDeployId()) {
$this->dockerActions->initGuzzleClient($daemonConfig);
if (boolval($exApp->getDeployConfig()['harp'] ?? false)) {
if ($this->dockerActions->removeExApp($this->dockerActions->buildDockerUrl($daemonConfig), $exApp->getAppid(), removeData: $rmData)) {
if (!$silent) {
$output->writeln(sprintf('Failed to remove ExApp %s', $appId));
}
if (!$force) {
return 1;
}
} else {
if (!$silent) {
$output->writeln(sprintf('ExApp %s successfully removed', $appId));
}
}
} else {
$removeResult = $this->dockerActions->removeContainer(
$this->dockerActions->buildDockerUrl($daemonConfig), $this->dockerActions->buildExAppContainerName($appId)
);
if ($removeResult) {
if (!$silent) {
$output->writeln(sprintf('Failed to remove ExApp %s container', $appId));
}
if (!$force) {
return 1;
}
} elseif (!$silent) {
$output->writeln(sprintf('ExApp %s container successfully removed', $appId));
}
if ($rmData) {
$volumeName = $this->dockerActions->buildExAppVolumeName($appId);
$removeVolumeResult = $this->dockerActions->removeVolume(
$this->dockerActions->buildDockerUrl($daemonConfig), $volumeName
);
if (!$silent) {
if (isset($removeVolumeResult['error'])) {
$output->writeln(sprintf('Failed to remove ExApp %s volume: %s', $appId, $volumeName));
} else {
$output->writeln(sprintf('ExApp %s data volume successfully removed', $appId));
}
}
}
}
}
if (!$this->exAppService->unregisterExApp($appId)) {
if (!$silent) {
$output->writeln(sprintf('Failed to unregister ExApp %s.', $appId));
}
if (!$force) {
return 1;
}
}
if (!$silent) {
$output->writeln(sprintf('ExApp %s successfully unregistered.', $appId));
}
return 0;
}
}