|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TheAentMachine\Aent\Event\Orchestrator; |
|
4
|
|
|
|
|
5
|
|
|
use Safe\Exceptions\FilesystemException; |
|
6
|
|
|
use Safe\Exceptions\StringsException; |
|
7
|
|
|
use TheAentMachine\Aent\Context\BaseOrchestratorContext; |
|
8
|
|
|
use TheAentMachine\Aent\Context\Context; |
|
9
|
|
|
use TheAentMachine\Aent\Event\AbstractJsonEvent; |
|
10
|
|
|
use TheAentMachine\Aent\Payload\Builder\NewImageReplyPayload; |
|
11
|
|
|
use TheAentMachine\Aent\Payload\CI\CINewImagePayload; |
|
12
|
|
|
use TheAentMachine\Aent\Payload\CI\CINewImageReplyPayload; |
|
13
|
|
|
use TheAentMachine\Aenthill\Aenthill; |
|
14
|
|
|
use TheAentMachine\Service\Exception\ServiceException; |
|
15
|
|
|
use TheAentMachine\Service\Service; |
|
16
|
|
|
use function Safe\sprintf; |
|
17
|
|
|
|
|
18
|
|
|
abstract class AbstractOrchestratorNewServiceEvent extends AbstractJsonEvent |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @param Service $service |
|
22
|
|
|
* @return void |
|
23
|
|
|
*/ |
|
24
|
|
|
abstract protected function finalizeService(Service $service): void; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @return string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected function getEventName(): string |
|
30
|
|
|
{ |
|
31
|
|
|
return 'NEW_SERVICE'; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return bool |
|
36
|
|
|
*/ |
|
37
|
|
|
protected function shouldRegisterEvents(): bool |
|
38
|
|
|
{ |
|
39
|
|
|
return false; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @return void |
|
44
|
|
|
* @throws StringsException |
|
45
|
|
|
*/ |
|
46
|
|
|
protected function beforeExecute(): void |
|
47
|
|
|
{ |
|
48
|
|
|
/** @var Context $context */ |
|
49
|
|
|
$context = Context::fromMetadata(); |
|
50
|
|
|
$this->output->writeln(sprintf( |
|
51
|
|
|
"\n👋 Hello! I'm the aent <info>%s</info> and I'm going to add a new service for <info>%s</info> environment <info>%s</info>.", |
|
52
|
|
|
$this->getAentName(), |
|
53
|
|
|
$context->getEnvironmentType(), |
|
54
|
|
|
$context->getEnvironmentName() |
|
55
|
|
|
)); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param mixed[] $payload |
|
60
|
|
|
* @return mixed[]|null |
|
61
|
|
|
* @throws ServiceException |
|
62
|
|
|
* @throws StringsException |
|
63
|
|
|
* @throws FilesystemException |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function executeJsonEvent(array $payload): ?array |
|
66
|
|
|
{ |
|
67
|
|
|
$service = Service::parsePayload($payload); |
|
68
|
|
|
$service = $this->createDockerFileAndBuild($service); |
|
69
|
|
|
$this->prompt->printAltBlock(sprintf("%s: finalizing service...", $this->getAentName())); |
|
70
|
|
|
$this->finalizeService($service); |
|
71
|
|
|
return null; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return void |
|
76
|
|
|
* @throws StringsException |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function afterExecute(): void |
|
79
|
|
|
{ |
|
80
|
|
|
/** @var Context $context */ |
|
81
|
|
|
$context = Context::fromMetadata(); |
|
82
|
|
|
$this->output->writeln( |
|
83
|
|
|
sprintf( |
|
84
|
|
|
"\nI've successfully added the service on your <info>%s</info> environment <info>%s</info>. See you later!", |
|
85
|
|
|
$context->getEnvironmentType(), |
|
86
|
|
|
$context->getEnvironmentName() |
|
87
|
|
|
) |
|
88
|
|
|
); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param Service $service |
|
93
|
|
|
* @return Service |
|
94
|
|
|
* @throws ServiceException |
|
95
|
|
|
* @throws StringsException |
|
96
|
|
|
*/ |
|
97
|
|
|
private function createDockerFileAndBuild(Service $service): Service |
|
98
|
|
|
{ |
|
99
|
|
|
/** @var Context $context */ |
|
100
|
|
|
$context = Context::fromMetadata(); |
|
101
|
|
|
if ($context->isDevelopment() || empty($service->getNeedBuild())) { |
|
102
|
|
|
return $service; |
|
103
|
|
|
} |
|
104
|
|
|
$this->prompt->printAltBlock(sprintf("%s: creating Dockerfile...", $this->getAentName())); |
|
105
|
|
|
$response = Aenthill::runJson(BaseOrchestratorContext::BUILDER_DEPENDENCY_KEY, 'NEW_IMAGE', $service->jsonSerialize()); |
|
106
|
|
|
$assoc = \GuzzleHttp\json_decode($response[0], true); |
|
107
|
|
|
$replyPayload = NewImageReplyPayload::fromArray($assoc); |
|
108
|
|
|
return $this->addBuildJobInCI($service, $replyPayload->getDockerfileName()); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param Service $service |
|
113
|
|
|
* @param string $dockerfileName |
|
114
|
|
|
* @return Service |
|
115
|
|
|
* @throws StringsException |
|
116
|
|
|
*/ |
|
117
|
|
|
private function addBuildJobInCI(Service $service, string $dockerfileName): Service |
|
118
|
|
|
{ |
|
119
|
|
|
$this->prompt->printAltBlock(sprintf("%s: adding build job in CI/CD...", $this->getAentName())); |
|
120
|
|
|
$payload = new CINewImagePayload($service->getServiceName(), $dockerfileName); |
|
121
|
|
|
$response = Aenthill::runJson(BaseOrchestratorContext::CI_DEPENDENCY_KEY, 'BUILD_JOB', $payload->toArray()); |
|
122
|
|
|
$assoc = \GuzzleHttp\json_decode($response[0], true); |
|
123
|
|
|
$replyPayload = CINewImageReplyPayload::fromArray($assoc); |
|
124
|
|
|
$service->setImage($replyPayload->getImageName()); |
|
125
|
|
|
return $service; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|