1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheAentMachine\Aent\Event\Bootstrap; |
4
|
|
|
|
5
|
|
|
use Safe\Exceptions\ArrayException; |
6
|
|
|
use Safe\Exceptions\StringsException; |
7
|
|
|
use TheAentMachine\Aent\Context\BaseOrchestratorContext; |
8
|
|
|
use TheAentMachine\Aent\Context\Context; |
9
|
|
|
use TheAentMachine\Aent\Event\Bootstrap\Model\CIBootstrap; |
10
|
|
|
use TheAentMachine\Aent\Event\Bootstrap\Model\OrchestratorBootstrap; |
11
|
|
|
use TheAentMachine\Aent\Payload\Bootstrap\BootstrapPayload; |
12
|
|
|
use TheAentMachine\Aent\Registry\ColonyRegistry; |
13
|
|
|
use TheAentMachine\Aent\Registry\Exception\ColonyRegistryException; |
14
|
|
|
use TheAentMachine\Aenthill\Aenthill; |
15
|
|
|
use TheAentMachine\Aent\Event\AbstractEvent; |
16
|
|
|
use function Safe\sprintf; |
17
|
|
|
|
18
|
|
|
abstract class AbstractBootstrapAddEvent extends AbstractEvent |
19
|
|
|
{ |
20
|
|
|
/** @var OrchestratorBootstrap[] */ |
21
|
|
|
private $orchestratorsBootstraps; |
22
|
|
|
|
23
|
|
|
/** @var null|CIBootstrap */ |
24
|
|
|
private $CIBootstrap; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return OrchestratorBootstrap[] |
28
|
|
|
*/ |
29
|
|
|
abstract protected function getOrchestratorsBootstraps(): array; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
protected function getEventName(): string |
35
|
|
|
{ |
36
|
|
|
return 'ADD'; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return bool |
41
|
|
|
*/ |
42
|
|
|
protected function shouldRegisterEvents(): bool |
43
|
|
|
{ |
44
|
|
|
return false; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return void |
49
|
|
|
* @throws StringsException |
50
|
|
|
*/ |
51
|
|
|
protected function beforeExecute(): void |
52
|
|
|
{ |
53
|
|
|
$this->output->writeln(sprintf("š Hello! I'm the aent <info>%s</info> and I'll help you bootstrapping a Docker project for your web application.", $this->getAentName())); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param null|string $payload |
58
|
|
|
* @return null|string |
59
|
|
|
* @throws ColonyRegistryException |
60
|
|
|
* @throws StringsException |
61
|
|
|
* @throws ArrayException |
62
|
|
|
*/ |
63
|
|
|
protected function executeEvent(?string $payload): ?string |
64
|
|
|
{ |
65
|
|
|
$this->orchestratorsBootstraps = $this->getOrchestratorsBootstraps(); |
66
|
|
|
$this->CIBootstrap = $this->hasOrchestratorsForRemoteEnvironments() ? $this->getCIBootstrap() : null; |
67
|
|
|
$this->output->writeln("\nš I'm going to wake up some aents, see you later!"); |
68
|
|
|
$this->printSummary($this->orchestratorsBootstraps); |
69
|
|
|
foreach ($this->orchestratorsBootstraps as $bootstrap) { |
70
|
|
|
$this->addOrchestrator($bootstrap); |
71
|
|
|
} |
72
|
|
|
$this->prompt->printBlock('Setup done.'); |
73
|
|
|
return null; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return void |
78
|
|
|
* @throws StringsException |
79
|
|
|
*/ |
80
|
|
|
protected function afterExecute(): void |
81
|
|
|
{ |
82
|
|
|
$this->output->writeln(sprintf("\nš Hello again! This is the aent <info>%s</info> and we have finished your project setup.", $this->getAentName())); |
83
|
|
|
$this->printSummary($this->orchestratorsBootstraps); |
84
|
|
|
$this->output->writeln("\nYou may now start adding services with <info>aenthill add [image]</info>. See https://aenthill.github.io/ for the complete documentation!"); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return bool |
89
|
|
|
*/ |
90
|
|
|
private function hasOrchestratorsForRemoteEnvironments(): bool |
91
|
|
|
{ |
92
|
|
|
foreach ($this->orchestratorsBootstraps as $orchestratorBootstrap) { |
93
|
|
|
if ($orchestratorBootstrap->getEnvironmentType() !== Context::DEV) { |
94
|
|
|
return true; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
return false; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return CIBootstrap |
102
|
|
|
* @throws ColonyRegistryException |
103
|
|
|
* @throws StringsException |
104
|
|
|
* @throws ArrayException |
105
|
|
|
*/ |
106
|
|
|
private function getCIBootstrap(): CIBootstrap |
107
|
|
|
{ |
108
|
|
|
$this->output->writeln("\nHey, you have defined at least one remote environment, so let's configure a <info>CI/CD provider</info>!"); |
109
|
|
|
$text = "\nYour CI/CD provider"; |
110
|
|
|
$helpText = "A CI provider will automatically build the images of your containers and deploy them in your remote environment(s)"; |
111
|
|
|
$aent = $this->prompt->getPromptHelper()->getFromColonyRegistry(ColonyRegistry::CIRegistry(), $text, $helpText); |
112
|
|
|
$this->output->writeln(sprintf("\nš Alright, I'm going to wake up the aent <info>%s</info>!", $aent->getName())); |
113
|
|
|
$response = Aenthill::runJson($aent->getImage(), 'CONFIGURE_CI', []); |
114
|
|
|
$metadata = \GuzzleHttp\json_decode($response[0], true); |
115
|
|
|
$this->output->writeln(sprintf( |
116
|
|
|
"\nš Hello again! This is the aent <info>%s</info> and I've received the configuration of the aent <info>%s</info>.", |
117
|
|
|
$this->getAentName(), |
118
|
|
|
$aent->getName() |
119
|
|
|
)); |
120
|
|
|
$bootstrap = new CIBootstrap(); |
121
|
|
|
$bootstrap |
122
|
|
|
->setAent($aent) |
123
|
|
|
->setMetadata($metadata); |
124
|
|
|
return $bootstrap; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param OrchestratorBootstrap $bootstrap |
129
|
|
|
* @return void |
130
|
|
|
* @throws StringsException |
131
|
|
|
*/ |
132
|
|
|
private function addOrchestrator(OrchestratorBootstrap $bootstrap): void |
133
|
|
|
{ |
134
|
|
|
$message = sprintf( |
135
|
|
|
"Setting up %s for %s environment %s.", |
136
|
|
|
$bootstrap->getAent()->getName(), |
137
|
|
|
$bootstrap->getEnvironmentType(), |
138
|
|
|
$bootstrap->getEnvironmentName() |
139
|
|
|
); |
140
|
|
|
$this->prompt->printBlock($message); |
141
|
|
|
$key = \uniqid(); |
142
|
|
|
$context = new BaseOrchestratorContext($bootstrap->getEnvironmentType(), $bootstrap->getEnvironmentName(), $bootstrap->getBaseVirtualHost()); |
143
|
|
|
Aenthill::register($bootstrap->getAent()->getImage(), $key, $context->toArray()); |
144
|
|
|
if (!$context->isDevelopment() && !empty($this->CIBootstrap)) { |
145
|
|
|
$payload = new BootstrapPayload($this->CIBootstrap->getAent(), $this->CIBootstrap->getMetadata()); |
146
|
|
|
} |
147
|
|
|
Aenthill::runJson($key, 'ADD_ORCHESTRATOR', !empty($payload) ? $payload->toArray() : []); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param OrchestratorBootstrap[] $orchestratorsBootstraps |
152
|
|
|
* @return void |
153
|
|
|
* @throws StringsException |
154
|
|
|
*/ |
155
|
|
|
protected function printSummary(array $orchestratorsBootstraps): void |
156
|
|
|
{ |
157
|
|
|
$this->output->writeln("\nSetup summary:"); |
158
|
|
|
foreach ($orchestratorsBootstraps as $orchestratorBootstrap) { |
159
|
|
|
$message = sprintf( |
160
|
|
|
" - a <info>%s</info> environment <info>%s</info> with the base virtual host <info>%s</info> and with <info>%s</info> as orchestrator", |
161
|
|
|
$orchestratorBootstrap->getEnvironmentType(), |
162
|
|
|
$orchestratorBootstrap->getEnvironmentName(), |
163
|
|
|
$orchestratorBootstrap->getBaseVirtualHost(), |
164
|
|
|
$orchestratorBootstrap->getAent()->getName() |
165
|
|
|
); |
166
|
|
|
$this->output->writeln($message); |
167
|
|
|
} |
168
|
|
|
if (!empty($this->CIBootstrap)) { |
169
|
|
|
$this->output->writeln(sprintf(" - <info>%s</info> as CI/CD provider for your remote environment(s)", $this->CIBootstrap->getAent()->getName())); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|