|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Support\Images\Organiser; |
|
4
|
|
|
|
|
5
|
|
|
use App\Support\Contracts\Cli; |
|
6
|
|
|
use App\Support\Contracts\ImageRepository; |
|
7
|
|
|
use App\Support\Images\Image; |
|
8
|
|
|
|
|
9
|
|
|
class Organiser |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var ImageRepository */ |
|
12
|
|
|
protected $repository; |
|
13
|
|
|
|
|
14
|
|
|
/** @var Cli */ |
|
15
|
|
|
protected $cli; |
|
16
|
|
|
|
|
17
|
9 |
|
public function __construct(ImageRepository $repository, Cli $cli) |
|
18
|
|
|
{ |
|
19
|
9 |
|
$this->repository = $repository; |
|
20
|
9 |
|
$this->cli = $cli; |
|
21
|
9 |
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Build the current images. |
|
25
|
|
|
* |
|
26
|
|
|
* @param string|null $service |
|
27
|
|
|
* @throws \Exception |
|
28
|
|
|
*/ |
|
29
|
3 |
|
public function buildImages($service = null) |
|
30
|
|
|
{ |
|
31
|
3 |
|
foreach ($this->repository->findByServiceName($service, $firstPartyOnly = true) as $image) { |
|
32
|
|
|
/* @var Image $image */ |
|
33
|
2 |
|
$this->cli->passthru("docker build -t {$image->getName()} --rm {$image->getLocalPath()} --"); |
|
34
|
|
|
} |
|
35
|
3 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Push the current images. |
|
39
|
|
|
* |
|
40
|
|
|
* @param string|null $service |
|
41
|
|
|
* @throws \Exception |
|
42
|
|
|
*/ |
|
43
|
3 |
|
public function pushImages($service = null) |
|
44
|
|
|
{ |
|
45
|
3 |
|
foreach ($this->repository->findByServiceName($service, $firstPartyOnly = true) as $image) { |
|
46
|
|
|
/* @var Image $image */ |
|
47
|
2 |
|
$this->cli->passthru("docker push {$image->getName()}"); |
|
48
|
|
|
} |
|
49
|
3 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Pull our docker images. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string|null $service |
|
55
|
|
|
* @throws \Exception |
|
56
|
|
|
*/ |
|
57
|
3 |
|
public function pullImages($service = null) |
|
58
|
|
|
{ |
|
59
|
3 |
|
foreach ($this->repository->findByServiceName($service) as $image) { |
|
60
|
|
|
/** @var Image $image */ |
|
61
|
3 |
|
if (running_tests() && $this->hasImage($image)) { |
|
62
|
|
|
continue; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
3 |
|
$this->cli->passthru("docker pull {$image->getName()}"); |
|
66
|
|
|
} |
|
67
|
3 |
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Check if we already have the image. |
|
71
|
|
|
* |
|
72
|
|
|
* @param Image $image |
|
73
|
|
|
* |
|
74
|
|
|
* @return bool |
|
75
|
|
|
*/ |
|
76
|
3 |
|
public function hasImage(Image $image) |
|
77
|
|
|
{ |
|
78
|
3 |
|
$output = $this->cli->exec("docker image inspect {$image->getName()}"); |
|
79
|
|
|
|
|
80
|
3 |
|
return strpos($output, "Error: No such image: {$image->getName()}") === false; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|