1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheAentMachine\AentDockerCompose\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Command\Command; |
6
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
use Symfony\Component\Console\Question\ChoiceQuestion; |
10
|
|
|
use Symfony\Component\Yaml\Yaml; |
11
|
|
|
|
12
|
|
|
class DeleteDockerServiceCommand extends Command |
13
|
|
|
{ |
14
|
|
|
protected function configure() |
15
|
|
|
{ |
16
|
|
|
$this |
17
|
|
|
->setName(Cst::DELETE_DOCKER_SERVICE_EVENT) |
18
|
|
|
->setDescription('Delete a docker service from the docker-compose.yml') |
19
|
|
|
->setHelp('TODO') |
20
|
|
|
->addArgument('payload', InputArgument::OPTIONAL, 'The payload of the event'); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
24
|
|
|
{ |
25
|
|
|
$payload = json_decode($input->getArgument('payload'), true); |
26
|
|
|
|
27
|
|
|
// $dockerComposePath = getenv('PHEROMONE_CONTAINER_PROJECT_DIR') . '/docker-compose.yml'; |
28
|
|
|
$dockerComposePath = getenv('PHEROMONE_CONTAINER_PROJECT_DIR') . '/aenthill/docker-compose.yml'; |
29
|
|
|
|
30
|
|
|
if (empty($payload)) { |
31
|
|
|
$yml = Yaml::parseFile($dockerComposePath); |
32
|
|
|
//$service = array_search('service', array_column($yml, 'service')); |
33
|
|
|
$services = array_keys($yml['services']); |
34
|
|
|
// $volumes = $yml['volumes']; |
35
|
|
|
$helper = $this->getHelper('question'); |
36
|
|
|
$question = new ChoiceQuestion( |
37
|
|
|
'Please choose the services you want to detele, if any :', |
38
|
|
|
$services |
39
|
|
|
); |
40
|
|
|
$question->setMultiselect(true); |
41
|
|
|
$servicesToDelete = $helper->ask($input, $output, $question); |
42
|
|
|
print_r($servicesToDelete); |
43
|
|
|
foreach ($servicesToDelete as $s) { |
44
|
|
|
$this->deleteElementInDockerCompose('services.' . $s, $dockerComposePath, $output); |
45
|
|
|
} |
46
|
|
|
} else { |
47
|
|
|
$elemToDelete = "services." . $payload[Cst::SERVICE_NAME_KEY]; |
48
|
|
|
$this->deleteElementInDockerCompose($elemToDelete, $dockerComposePath, $output); |
49
|
|
|
|
50
|
|
|
foreach ($payload[Cst::NAMED_VOLUMES_KEY] as $v) { |
51
|
|
|
$elemToDelete = "volumes." . $v; |
52
|
|
|
$this->deleteElementInDockerCompose($elemToDelete, $dockerComposePath, $output); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $element |
59
|
|
|
* @param string $file |
60
|
|
|
* @param OutputInterface $output |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
|
|
protected function deleteElementInDockerCompose(string $element, string $file, OutputInterface $output) |
64
|
|
|
{ |
65
|
|
|
$cmd = array("yaml-tools", "delete", $element, "-i", $file, "-o", $file); |
66
|
|
|
|
67
|
|
|
$process = Utils::runAndGetProcess($cmd, $output); |
68
|
|
|
if (!$process->isSuccessful()) { |
69
|
|
|
exit($process->getExitCode()); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.