|
3 | 3 | namespace DockerHostManager; |
4 | 4 |
|
5 | 5 | use Docker\Container; |
| 6 | +use Docker\Exception\APIException; |
6 | 7 | use Docker\Exception\ContainerNotFoundException; |
7 | 8 | use DockerHostManager\Docker\Docker; |
8 | 9 | use DockerHostManager\Docker\Event; |
9 | | -use Docker\Http\DockerClient; |
10 | 10 |
|
11 | | -class Application |
| 11 | +class Synchronizer |
12 | 12 | { |
13 | 13 | const START_TAG = '## docker-hostmanager-start'; |
14 | 14 | const END_TAG = '## docker-hostmanager-end'; |
15 | 15 |
|
16 | | - /** @var string */ |
17 | | - private $entrypoint; |
| 16 | + /** @var Docker */ |
| 17 | + private $docker; |
18 | 18 | /** @var string */ |
19 | 19 | private $hostsFile; |
20 | 20 | /** @var string */ |
21 | 21 | private $tld; |
22 | 22 |
|
23 | | - /** @var Docker */ |
24 | | - private $docker; |
25 | 23 | /** @var array Container */ |
26 | 24 | private $activeContainers = []; |
27 | 25 |
|
28 | 26 | /** |
29 | | - * @param string $entrypoint |
| 27 | + * @param Docker $docker |
30 | 28 | * @param string $hostsFile |
31 | 29 | * @param string $tld |
32 | 30 | */ |
33 | | - public function __construct($entrypoint, $hostsFile, $tld) |
| 31 | + public function __construct(Docker $docker, $hostsFile, $tld) |
34 | 32 | { |
35 | | - $this->entrypoint = $entrypoint; |
| 33 | + $this->docker = $docker; |
36 | 34 | $this->hostsFile = $hostsFile; |
37 | 35 | $this->tld = $tld; |
38 | | - $client = new DockerClient([], $this->entrypoint); |
39 | | - $this->docker = new Docker($client); |
40 | 36 | } |
41 | 37 |
|
42 | 38 | public function run() |
43 | 39 | { |
| 40 | + if (!is_writable($this->hostsFile)) { |
| 41 | + throw new \RuntimeException(sprintf('File "%s" is not writable.', $this->hostsFile)); |
| 42 | + } |
| 43 | + |
44 | 44 | $this->init(); |
45 | 45 | $this->listen(); |
46 | 46 | } |
47 | 47 |
|
48 | 48 | private function init() |
49 | 49 | { |
50 | | - $this->activeContainers = array_filter($this->docker->getContainerManager()->findAll(), function (Container $container) { |
51 | | - $this->docker->getContainerManager()->inspect($container); |
| 50 | + foreach ($this->docker->getContainerManager()->findAll() as $container) { |
| 51 | + if ($this->isExposed($container)) { |
| 52 | + $this->activeContainers[$container->getId()] = $container; |
| 53 | + } |
| 54 | + } |
52 | 55 |
|
53 | | - return $this->isExposed($container); |
54 | | - }); |
55 | 56 | $this->write(); |
56 | 57 | } |
57 | 58 |
|
58 | 59 | private function listen() |
59 | 60 | { |
60 | 61 | $this->docker->listenEvents(function (Event $event) { |
61 | 62 | $container = $this->docker->getContainerManager()->find($event->getId()); |
62 | | - $this->docker->getContainerManager()->inspect($container); |
| 63 | + if (null === $container) { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
63 | 67 | if ($this->isExposed($container)) { |
64 | | - $this->addActiveContainer($container); |
| 68 | + $this->activeContainers[$container->getId()] = $container; |
65 | 69 | } else { |
66 | | - $this->removeActiveContainer($container); |
| 70 | + unset($this->activeContainers[$container->getId()]); |
67 | 71 | } |
68 | | - $this->write(); |
69 | | - }); |
70 | | - } |
71 | 72 |
|
72 | | - /** |
73 | | - * @param Container $container |
74 | | - */ |
75 | | - private function addActiveContainer(Container $container) |
76 | | - { |
77 | | - $id = $container->getId(); |
78 | | - if (!empty($this->activeContainers[$id])) { |
79 | | - return; |
80 | | - } |
81 | | - $this->activeContainers[$id] = $container; |
82 | | - } |
83 | | - |
84 | | - /** |
85 | | - * @param Container $container |
86 | | - */ |
87 | | - private function removeActiveContainer(Container $container) |
88 | | - { |
89 | | - $this->activeContainers = array_filter($this->activeContainers, function (Container $c) use ($container) { |
90 | | - return $c->getId() !== $container->getId(); |
| 73 | + $this->write(); |
91 | 74 | }); |
92 | 75 | } |
93 | 76 |
|
@@ -121,8 +104,6 @@ function (Container $container) { |
121 | 104 | */ |
122 | 105 | private function getHostsLines(Container $container) |
123 | 106 | { |
124 | | - $this->docker->getContainerManager()->inspect($container); |
125 | | - |
126 | 107 | $lines = []; |
127 | 108 | $hosts = $this->getContainerHosts($container); |
128 | 109 | foreach ($this->getContainerIps($container) as $ip) { |
@@ -183,6 +164,13 @@ private function getContainerHosts(Container $container) |
183 | 164 | */ |
184 | 165 | private function isExposed(Container $container) |
185 | 166 | { |
| 167 | + try { |
| 168 | + $this->docker->getContainerManager()->inspect($container); |
| 169 | + } catch (APIException $e) { |
| 170 | + // Happen on "docker build" |
| 171 | + return false; |
| 172 | + } |
| 173 | + |
186 | 174 | $inspection = $container->getRuntimeInformations(); |
187 | 175 | if (empty($inspection['NetworkSettings']['Ports']) || empty($inspection['State']['Running'])) { |
188 | 176 | return false; |
|
0 commit comments