|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TheAentMachine\AentDockerfile\Event; |
|
4
|
|
|
|
|
5
|
|
|
use Safe\Exceptions\FilesystemException; |
|
6
|
|
|
use Safe\Exceptions\StringsException; |
|
7
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
8
|
|
|
use TheAentMachine\Aent\Context\Context; |
|
9
|
|
|
use TheAentMachine\Aent\Event\Builder\AbstractNewImageEvent; |
|
10
|
|
|
use TheAentMachine\Aent\Payload\Builder\NewImageReplyPayload; |
|
11
|
|
|
use TheAentMachine\Aenthill\Pheromone; |
|
12
|
|
|
use TheAentMachine\Exception\MissingEnvironmentVariableException; |
|
13
|
|
|
use TheAentMachine\Service\Service; |
|
14
|
|
|
use function \Safe\sprintf; |
|
15
|
|
|
use function \Safe\chown; |
|
16
|
|
|
use function \Safe\chgrp; |
|
17
|
|
|
|
|
18
|
|
|
final class NewImageEvent extends AbstractNewImageEvent |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @param Service $service |
|
22
|
|
|
* @return NewImageReplyPayload |
|
23
|
|
|
* @throws FilesystemException |
|
24
|
|
|
* @throws MissingEnvironmentVariableException |
|
25
|
|
|
* @throws StringsException |
|
26
|
|
|
*/ |
|
27
|
|
|
protected function createDockerfile(Service $service): NewImageReplyPayload |
|
28
|
|
|
{ |
|
29
|
|
|
/** @var Context $context */ |
|
30
|
|
|
$context = Context::fromMetadata(); |
|
31
|
|
|
$commands = \implode(PHP_EOL, $service->getDockerfileCommands()) . PHP_EOL; |
|
32
|
|
|
$dockerfileName = sprintf("Dockerfile.%s.%s", $context->getEnvironmentName(), $service->getServiceName()); |
|
33
|
|
|
$dockerfilePath = Pheromone::getContainerProjectDirectory() . "/$dockerfileName"; |
|
34
|
|
|
$fileSystem = new Filesystem(); |
|
35
|
|
|
$fileSystem->dumpFile($dockerfilePath, $commands); |
|
36
|
|
|
$dirInfo = new \SplFileInfo(\dirname($dockerfilePath)); |
|
37
|
|
|
chown($dockerfilePath, $dirInfo->getOwner()); |
|
38
|
|
|
chgrp($dockerfilePath, $dirInfo->getGroup()); |
|
39
|
|
|
return new NewImageReplyPayload($dockerfileName); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|