1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheAentMachine\AentDockerCompose\Command; |
4
|
|
|
|
5
|
|
|
use TheAentMachine\AentDockerCompose\DockerCompose\DockerComposeService; |
6
|
|
|
use TheAentMachine\Aenthill\Aenthill; |
7
|
|
|
use TheAentMachine\Aenthill\CommonDependencies; |
8
|
|
|
use TheAentMachine\Aenthill\CommonEvents; |
9
|
|
|
use TheAentMachine\Aenthill\CommonMetadata; |
10
|
|
|
use TheAentMachine\Aenthill\Manifest; |
11
|
|
|
use TheAentMachine\Aenthill\Pheromone; |
12
|
|
|
use TheAentMachine\Command\AbstractJsonEventCommand; |
13
|
|
|
use TheAentMachine\Exception\ManifestException; |
14
|
|
|
use TheAentMachine\Exception\MissingEnvironmentVariableException; |
15
|
|
|
use TheAentMachine\Service\Service; |
16
|
|
|
|
17
|
|
|
class NewServiceEventCommand extends AbstractJsonEventCommand |
18
|
|
|
{ |
19
|
|
|
protected function getEventName(): string |
20
|
|
|
{ |
21
|
|
|
return 'NEW_SERVICE'; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param array $payload |
26
|
|
|
* @return array|null |
27
|
|
|
* @throws ManifestException |
28
|
|
|
* @throws MissingEnvironmentVariableException |
29
|
|
|
* @throws \TheAentMachine\Service\Exception\ServiceException |
30
|
|
|
* @throws \TheAentMachine\Exception\CommonAentsException |
31
|
|
|
*/ |
32
|
|
|
protected function executeJsonEvent(array $payload): ?array |
33
|
|
|
{ |
34
|
|
|
$service = Service::parsePayload($payload); |
35
|
|
|
if (!$service->isForMyEnvType()) { |
36
|
|
|
return null; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$fileName = Manifest::mustGetMetadata(CommonMetadata::DOCKER_COMPOSE_FILENAME_KEY); |
40
|
|
|
$this->getAentHelper()->title($fileName); |
41
|
|
|
|
42
|
|
|
$serviceName = $service->getServiceName(); |
43
|
|
|
$formattedPayload = DockerComposeService::dockerComposeServiceSerialize($service); |
44
|
|
|
$this->log->debug(\GuzzleHttp\json_encode($formattedPayload, JSON_PRETTY_PRINT)); |
45
|
|
|
|
46
|
|
|
// docker-compose |
47
|
|
|
$dockerComposePath = Pheromone::getContainerProjectDirectory() . '/' . $fileName; |
48
|
|
|
DockerComposeService::mergeContentInDockerComposeFile($formattedPayload, $dockerComposePath, true); |
49
|
|
|
|
50
|
|
|
// Virtual Host |
51
|
|
|
if ($service->getNeedVirtualHost()) { |
52
|
|
|
if (null === Manifest::getDependency(CommonDependencies::REVERSE_PROXY_KEY)) { |
53
|
|
|
$this->getAentHelper()->getCommonQuestions()->askForReverseProxy(); |
54
|
|
|
$this->runAddReverseProxy($dockerComposePath); |
55
|
|
|
} |
56
|
|
|
$this->newVirtualHost($dockerComposePath, $serviceName); |
57
|
|
|
} |
58
|
|
|
$this->output->writeln("Service <info>$serviceName</info> has been successfully added in <info>$fileName</info>!"); |
59
|
|
|
return null; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @throws ManifestException |
64
|
|
|
* @throws \TheAentMachine\Service\Exception\ServiceException |
65
|
|
|
*/ |
66
|
|
|
private function runAddReverseProxy(string $dockerComposePath): void |
67
|
|
|
{ |
68
|
|
|
$reverseProxyKey = Manifest::mustGetDependency(CommonDependencies::REVERSE_PROXY_KEY); |
69
|
|
|
$repliedPayloads = Aenthill::runJson($reverseProxyKey, CommonEvents::ADD_EVENT, []); |
70
|
|
|
$payload = \GuzzleHttp\json_decode($repliedPayloads[0], true); |
71
|
|
|
$service = Service::parsePayload($payload); |
72
|
|
|
$formattedPayload = DockerComposeService::dockerComposeServiceSerialize($service); |
73
|
|
|
DockerComposeService::mergeContentInDockerComposeFile($formattedPayload, $dockerComposePath, true); |
74
|
|
|
|
75
|
|
|
$serviceName = $service->getServiceName(); |
76
|
|
|
$fileName = Manifest::mustGetMetadata(CommonMetadata::DOCKER_COMPOSE_FILENAME_KEY); |
77
|
|
|
$this->output->writeln("Reverse proxy <info>$serviceName</info> has been successfully added in <info>$fileName</info>!"); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @throws ManifestException |
82
|
|
|
* @throws \TheAentMachine\Service\Exception\ServiceException |
83
|
|
|
*/ |
84
|
|
|
private function newVirtualHost(string $dockerComposePath, string $serviceName, int $virtualPort = 80, string $virtualHost = null): void |
85
|
|
|
{ |
86
|
|
|
$message = [ |
87
|
|
|
'service' => $serviceName, |
88
|
|
|
'virtualPort' => $virtualPort |
89
|
|
|
]; |
90
|
|
|
if ($virtualHost !== null) { |
91
|
|
|
$message['virtualHost'] = $virtualHost; |
92
|
|
|
} |
93
|
|
|
$reverseProxyKey = Manifest::mustGetDependency(CommonDependencies::REVERSE_PROXY_KEY); |
94
|
|
|
$repliedPayloads = Aenthill::runJson($reverseProxyKey, CommonEvents::NEW_VIRTUAL_HOST_EVENT, $message); |
95
|
|
|
$payload = \GuzzleHttp\json_decode($repliedPayloads[0], true); |
96
|
|
|
$service = Service::parsePayload($payload); |
97
|
|
|
$formattedPayload = DockerComposeService::dockerComposeServiceSerialize($service); |
98
|
|
|
DockerComposeService::mergeContentInDockerComposeFile($formattedPayload, $dockerComposePath, true); |
99
|
|
|
|
100
|
|
|
$serviceName = $service->getServiceName(); |
101
|
|
|
$fileName = Manifest::mustGetMetadata(CommonMetadata::DOCKER_COMPOSE_FILENAME_KEY); |
102
|
|
|
$this->output->writeln("A new virtual host has been successfully added for <info>$serviceName</info> in <info>$fileName</info>!"); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|