1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheAentMachine\AentDockerCompose\Command; |
4
|
|
|
|
5
|
|
|
use TheAentMachine\AentDockerCompose\DockerCompose\DockerComposeService; |
6
|
|
|
use TheAentMachine\Aenthill\Aenthill; |
7
|
|
|
use TheAentMachine\Aenthill\Manifest; |
8
|
|
|
use TheAentMachine\Aenthill\Metadata; |
9
|
|
|
use TheAentMachine\Aenthill\Pheromone; |
10
|
|
|
use TheAentMachine\Command\JsonEventCommand; |
11
|
|
|
use TheAentMachine\Exception\ManifestException; |
12
|
|
|
use TheAentMachine\Exception\MissingEnvironmentVariableException; |
13
|
|
|
use TheAentMachine\Service\Service; |
14
|
|
|
|
15
|
|
|
class NewServiceEventCommand extends JsonEventCommand |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
protected function getEventName(): string |
19
|
|
|
{ |
20
|
|
|
return 'NEW_SERVICE'; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param array $payload |
25
|
|
|
* @return array|null |
26
|
|
|
* @throws ManifestException |
27
|
|
|
* @throws MissingEnvironmentVariableException |
28
|
|
|
* @throws \TheAentMachine\Service\Exception\ServiceException |
29
|
|
|
*/ |
30
|
|
|
protected function executeJsonEvent(array $payload): ?array |
31
|
|
|
{ |
32
|
|
|
$fileName = Manifest::getMetadata(Metadata::DOCKER_COMPOSE_FILENAME_KEY); |
33
|
|
|
$this->getAentHelper()->title($fileName); |
34
|
|
|
|
35
|
|
|
$service = Service::parsePayload($payload); |
36
|
|
|
$serviceName = $service->getServiceName(); |
37
|
|
|
$formattedPayload = DockerComposeService::dockerComposeServiceSerialize($service); |
38
|
|
|
$this->log->debug(\GuzzleHttp\json_encode($formattedPayload, JSON_PRETTY_PRINT)); |
39
|
|
|
|
40
|
|
|
// docker-compose |
41
|
|
|
$dockerComposePath = Pheromone::getContainerProjectDirectory() . '/' . $fileName; |
42
|
|
|
DockerComposeService::mergeContentInDockerComposeFile($formattedPayload, $dockerComposePath, true); |
43
|
|
|
|
44
|
|
|
// Virtual Host |
45
|
|
|
if ($service->getNeedVirtualHost()) { |
46
|
|
|
$reverseProxyKey = Manifest::getDependencyOrNull(Metadata::REVERSE_PROXY_KEY); |
47
|
|
|
if ($reverseProxyKey === null) { |
48
|
|
|
$this->log->info('Adding aent-treafik (a reverse proxy service which can handles virtual hosts)'); |
49
|
|
|
Manifest::addDependency('theaentmachine/aent-traefik:snapshot', Metadata::REVERSE_PROXY_KEY, [ |
50
|
|
|
Metadata::ENV_NAME_KEY => Manifest::getMetadata(Metadata::ENV_NAME_KEY), |
51
|
|
|
Metadata::ENV_TYPE_KEY => Manifest::getMetadata(Metadata::ENV_TYPE_KEY), |
52
|
|
|
]); |
53
|
|
|
$this->addAentTraefik($dockerComposePath); |
54
|
|
|
} |
55
|
|
|
$this->newVirtualHost($dockerComposePath, $serviceName); |
56
|
|
|
} |
57
|
|
|
$this->output->writeln("Service <info>$serviceName</info> has been successfully added in <info>$fileName</info>!"); |
58
|
|
|
return null; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @throws ManifestException |
63
|
|
|
* @throws MissingEnvironmentVariableException |
64
|
|
|
* @throws \TheAentMachine\Service\Exception\ServiceException |
65
|
|
|
*/ |
66
|
|
|
private function addAentTraefik(string $dockerComposePath): void |
67
|
|
|
{ |
68
|
|
|
$reverseProxyKey = Manifest::getDependency(Metadata::REVERSE_PROXY_KEY); |
69
|
|
|
$repliedPayloads = Aenthill::runJson($reverseProxyKey, 'ADD', []); |
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
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @throws ManifestException |
78
|
|
|
* @throws MissingEnvironmentVariableException |
79
|
|
|
* @throws \TheAentMachine\Service\Exception\ServiceException |
80
|
|
|
*/ |
81
|
|
|
private function newVirtualHost(string $dockerComposePath, string $serviceName, int $virtualPort = 80, string $virtualHost = null): void |
82
|
|
|
{ |
83
|
|
|
$message = [ |
84
|
|
|
'service' => $serviceName, |
85
|
|
|
'virtualPort' => $virtualPort |
86
|
|
|
]; |
87
|
|
|
if ($virtualHost !== null) { |
88
|
|
|
$message['virtualHost'] = $virtualHost; |
89
|
|
|
} |
90
|
|
|
$reverseProxyKey = Manifest::getDependency(Metadata::REVERSE_PROXY_KEY); |
91
|
|
|
$repliedPayloads = Aenthill::runJson($reverseProxyKey, 'NEW_VIRTUAL_HOST', $message); |
92
|
|
|
$payload = \GuzzleHttp\json_decode($repliedPayloads[0], true); |
93
|
|
|
$service = Service::parsePayload($payload); |
94
|
|
|
$formattedPayload = DockerComposeService::dockerComposeServiceSerialize($service); |
95
|
|
|
DockerComposeService::mergeContentInDockerComposeFile($formattedPayload, $dockerComposePath, true); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|