|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace TheAentMachine; |
|
5
|
|
|
|
|
6
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
9
|
|
|
use Symfony\Component\Console\Question\Question; |
|
10
|
|
|
use TheAentMachine\Exception\CannotHandleEventException; |
|
11
|
|
|
use TheAentMachine\Service\Service; |
|
12
|
|
|
|
|
13
|
|
|
class CommonEvents |
|
14
|
|
|
{ |
|
15
|
|
|
private const NEW_SERVICE = 'NEW_SERVICE'; |
|
16
|
|
|
private const NEW_VIRTUAL_HOST = 'NEW_VIRTUAL_HOST'; |
|
17
|
|
|
private const NEW_IMAGE = 'NEW_IMAGE'; |
|
18
|
|
|
|
|
19
|
|
|
/** @var AentHelper */ |
|
20
|
|
|
private $aentHelper; |
|
21
|
|
|
/** @var OutputInterface */ |
|
22
|
|
|
private $output; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* CommonEvents constructor. |
|
26
|
|
|
* @param AentHelper $aentHelper |
|
27
|
|
|
* @param OutputInterface $output |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct(AentHelper $aentHelper, OutputInterface $output) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->aentHelper = $aentHelper; |
|
32
|
|
|
$this->output = $output; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @throws CannotHandleEventException |
|
37
|
|
|
*/ |
|
38
|
|
|
public function dispatchService(Service $service): void |
|
39
|
|
|
{ |
|
40
|
|
|
$this->canDispatchServiceOrFail(); |
|
41
|
|
|
Hermes::dispatchJson(self::NEW_SERVICE, $service); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @throws CannotHandleEventException |
|
46
|
|
|
*/ |
|
47
|
|
|
public function canDispatchServiceOrFail(): void |
|
48
|
|
|
{ |
|
49
|
|
|
$canHandle = Hermes::canHandleEvent(self::NEW_SERVICE); |
|
50
|
|
|
|
|
51
|
|
|
if (!$canHandle) { |
|
52
|
|
|
$this->output->writeln('<error>Heads up!</error>'); |
|
53
|
|
|
$this->output->writeln('It seems that Aenthill does not know how or where to store this new service. You need to install a dedicated Aent for this.'); |
|
54
|
|
|
$this->output->writeln('Most of the time, you want to put this service in a docker-compose.yml file. We have a pretty good Aent for this: <info>theaentmachine/aent-docker-compose</info>'); |
|
55
|
|
|
|
|
56
|
|
|
$answer = $this->aentHelper |
|
57
|
|
|
->question('Do you want me to add this Aent for you?') |
|
58
|
|
|
->yesNoQuestion() |
|
59
|
|
|
->setDefault('y') |
|
60
|
|
|
->ask(); |
|
61
|
|
|
|
|
62
|
|
|
if ($answer === 'y') { |
|
63
|
|
|
Hermes::setDependencies(['theaentmachine/aent-docker-compose']); |
|
64
|
|
|
} else { |
|
65
|
|
|
throw CannotHandleEventException::cannotHandleEvent(self::NEW_SERVICE); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @throws CannotHandleEventException |
|
72
|
|
|
* @return array[] Returns the responses |
|
73
|
|
|
*/ |
|
74
|
|
|
public function dispatchNewVirtualHost(string $serviceName, int $virtualPort = 80, string $virtualHost = null): ?array |
|
75
|
|
|
{ |
|
76
|
|
|
$this->canDispatchVirtualHostOrFail(); |
|
77
|
|
|
|
|
78
|
|
|
$message = [ |
|
79
|
|
|
'service' => $serviceName, |
|
80
|
|
|
'virtualPort' => $virtualPort |
|
81
|
|
|
]; |
|
82
|
|
|
if ($virtualHost !== null) { |
|
83
|
|
|
$message['virtualHost'] = $virtualHost; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return Hermes::dispatchJson(self::NEW_VIRTUAL_HOST, $message); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @throws CannotHandleEventException |
|
91
|
|
|
*/ |
|
92
|
|
|
public function canDispatchVirtualHostOrFail(): void |
|
93
|
|
|
{ |
|
94
|
|
|
$canHandle = Hermes::canHandleEvent(self::NEW_VIRTUAL_HOST); |
|
95
|
|
|
|
|
96
|
|
|
if (!$canHandle) { |
|
97
|
|
|
$this->output->writeln('<error>Heads up!</error>'); |
|
98
|
|
|
$this->output->writeln('It seems that Aenthill does not know how to bind your container to a domain name. You need to install a reverse proxy for this.'); |
|
99
|
|
|
$this->output->writeln('Traefik is a good reverse proxy. We have an Aent to add Traefik to your project: <info>theaentmachine/aent-traefik</info>.'); |
|
100
|
|
|
|
|
101
|
|
|
$answer = $this->aentHelper |
|
102
|
|
|
->question('Do you want me to add this Aent for you?') |
|
103
|
|
|
->yesNoQuestion() |
|
104
|
|
|
->setDefault('y') |
|
105
|
|
|
->ask(); |
|
106
|
|
|
|
|
107
|
|
|
if ($answer === 'y') { |
|
108
|
|
|
Hermes::setDependencies(['theaentmachine/aent-traefik']); |
|
109
|
|
|
} else { |
|
110
|
|
|
throw CannotHandleEventException::cannotHandleEvent(self::NEW_VIRTUAL_HOST); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @throws CannotHandleEventException |
|
117
|
|
|
*/ |
|
118
|
|
|
public function dispatchImage(Service $service): void |
|
119
|
|
|
{ |
|
120
|
|
|
$this->canDispatchImageOrFail(); |
|
121
|
|
|
|
|
122
|
|
|
Hermes::dispatchJson(self::NEW_IMAGE, $service); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @throws CannotHandleEventException |
|
127
|
|
|
*/ |
|
128
|
|
|
public function canDispatchImageOrFail(): void |
|
129
|
|
|
{ |
|
130
|
|
|
$canHandle = Hermes::canHandleEvent(self::NEW_IMAGE); |
|
131
|
|
|
|
|
132
|
|
|
if (!$canHandle) { |
|
133
|
|
|
$this->output->writeln('<error>Heads up!</error>'); |
|
134
|
|
|
$this->output->writeln('It seems that Aenthill does not know how to handle the creation of a new image. You need to install a dedicated Aent for this.'); |
|
135
|
|
|
$this->output->writeln('Most of the time, you want to put the instructions in a Dockerfile. We have a pretty good Aent for this: <info>theaentmachine/aent-dockerfile</info>.'); |
|
136
|
|
|
|
|
137
|
|
|
$answer = $this->aentHelper |
|
138
|
|
|
->question('Do you want me to add this Aent for you?') |
|
139
|
|
|
->yesNoQuestion() |
|
140
|
|
|
->setDefault('y') |
|
141
|
|
|
->ask(); |
|
142
|
|
|
|
|
143
|
|
|
if ($answer === 'y') { |
|
144
|
|
|
Hermes::setDependencies(['theaentmachine/aent-dockerfile']); |
|
145
|
|
|
} else { |
|
146
|
|
|
throw CannotHandleEventException::cannotHandleEvent(self::NEW_IMAGE); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|