theaentmachine /
aent-console
| 1 | <?php |
||
| 2 | namespace TheAentMachine\Docker; |
||
| 3 | |||
| 4 | use Docker\API\Client; |
||
| 5 | use Docker\API\Exception\ImageInspectNotFoundException; |
||
| 6 | use Docker\API\Model\ContainerConfig; |
||
| 7 | use Docker\API\Model\CreateImageInfo; |
||
| 8 | use Docker\Docker; |
||
| 9 | use Docker\Stream\CreateImageStream; |
||
| 10 | use Psr\Log\LoggerInterface; |
||
| 11 | use TheAentMachine\Exception\AenthillException; |
||
| 12 | |||
| 13 | class ImageService |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var LoggerInterface |
||
| 17 | */ |
||
| 18 | private $logger; |
||
| 19 | /** |
||
| 20 | * @var Docker |
||
| 21 | */ |
||
| 22 | private $docker; |
||
| 23 | |||
| 24 | public function __construct(LoggerInterface $logger) |
||
| 25 | { |
||
| 26 | $this->docker = Docker::create(); |
||
| 27 | $this->logger = $logger; |
||
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @return int[] |
||
| 32 | */ |
||
| 33 | public function getInternalPorts(string $imageName) : array |
||
| 34 | { |
||
| 35 | $ports = $this->getInspection($imageName)->getExposedPorts(); |
||
| 36 | if ($ports === null) { |
||
| 37 | return []; |
||
| 38 | } |
||
| 39 | |||
| 40 | $finalPorts = []; |
||
| 41 | foreach ($ports as $portStr => $obj) { |
||
| 42 | // $portStr = "80/tcp". Let's remove the string by casting. |
||
| 43 | $finalPorts[] = (int) $portStr; |
||
| 44 | } |
||
| 45 | return $finalPorts; |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @return string[] |
||
| 50 | */ |
||
| 51 | public function getVolumes(string $imageName) : array |
||
| 52 | { |
||
| 53 | $volumes = $this->getInspection($imageName)->getVolumes(); |
||
| 54 | if ($volumes === null) { |
||
| 55 | return []; |
||
| 56 | } |
||
| 57 | |||
| 58 | return \array_keys($volumes->getArrayCopy()); |
||
| 59 | } |
||
| 60 | |||
| 61 | private function getInspection(string $imageName) : ContainerConfig |
||
| 62 | { |
||
| 63 | try { |
||
| 64 | $config = $this->docker->imageInspect($imageName)->getConfig(); |
||
|
0 ignored issues
–
show
|
|||
| 65 | } catch (ImageInspectNotFoundException $e) { |
||
| 66 | $this->pull($imageName); |
||
| 67 | $config = $this->docker->imageInspect($imageName)->getConfig(); |
||
| 68 | } |
||
| 69 | if ($config === null) { |
||
| 70 | throw new AenthillException('Cannot inspect container '.$imageName.'. Missing config key.'); |
||
| 71 | } |
||
| 72 | return $config; |
||
| 73 | } |
||
| 74 | |||
| 75 | public function pullIfNotAvailable(string $imageName): void |
||
| 76 | { |
||
| 77 | try { |
||
| 78 | $this->docker->imageInspect($imageName); |
||
| 79 | } catch (ImageInspectNotFoundException $e) { |
||
| 80 | $this->pull($imageName); |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | public function pull(string $imageName): void |
||
| 85 | { |
||
| 86 | /** @var CreateImageStream $result */ |
||
| 87 | $result = $this->docker->imageCreate($imageName, [ |
||
| 88 | 'fromImage' => $imageName |
||
| 89 | ]); |
||
| 90 | |||
| 91 | $result->onFrame(function (CreateImageInfo $frame) { |
||
| 92 | $this->logger->info($frame->getStatus()); |
||
| 93 | }); |
||
| 94 | $result->wait(); |
||
| 95 | } |
||
| 96 | |||
| 97 | public function rmi(string $imageName): void |
||
| 98 | { |
||
| 99 | $this->docker->imageDelete($imageName); |
||
| 100 | } |
||
| 101 | } |
||
| 102 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.