|
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
|
|
|
use Illuminate\Contracts\Filesystem\FileNotFoundException; |
|
9
|
|
|
use Illuminate\Filesystem\Filesystem; |
|
10
|
|
|
|
|
11
|
|
|
class Organiser |
|
12
|
|
|
{ |
|
13
|
|
|
/** @var ImageRepository */ |
|
14
|
|
|
protected $repository; |
|
15
|
|
|
|
|
16
|
|
|
/** @var Cli */ |
|
17
|
|
|
protected $cli; |
|
18
|
|
|
|
|
19
|
|
|
/** @var Filesystem */ |
|
20
|
|
|
protected $filesystem; |
|
21
|
|
|
|
|
22
|
13 |
|
public function __construct(ImageRepository $repository, Cli $cli, Filesystem $filesystem) |
|
23
|
|
|
{ |
|
24
|
13 |
|
$this->repository = $repository; |
|
25
|
13 |
|
$this->cli = $cli; |
|
26
|
13 |
|
$this->filesystem = $filesystem; |
|
27
|
13 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Build the current images. |
|
31
|
|
|
* |
|
32
|
|
|
* @param string|null $service |
|
33
|
|
|
* |
|
34
|
|
|
* @throws \Exception |
|
35
|
|
|
*/ |
|
36
|
3 |
|
public function buildImages($service = null) |
|
37
|
|
|
{ |
|
38
|
3 |
|
foreach ($this->repository->findByServiceName($service, $firstPartyOnly = true) as $image) { |
|
39
|
|
|
/** @var Image $image */ |
|
40
|
2 |
|
$version = $this->findBuildVersion($image); |
|
41
|
2 |
|
$name = $image->getUnVersionedName(); |
|
42
|
|
|
|
|
43
|
2 |
|
$this->cli->passthru("docker build -t {$name}:{$version} -t {$name}:latest --rm {$image->getLocalPath()} --"); |
|
44
|
|
|
|
|
45
|
2 |
|
$this->updateConfigVersionForImage($image, $version); |
|
46
|
|
|
} |
|
47
|
3 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Find the build version for an image. |
|
51
|
|
|
* Look in the first line of the Dockerfile. |
|
52
|
|
|
* |
|
53
|
|
|
* @param Image $image |
|
54
|
|
|
* |
|
55
|
|
|
* @throws FileNotFoundException |
|
56
|
|
|
* |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
3 |
|
public function findBuildVersion(Image $image) |
|
60
|
|
|
{ |
|
61
|
3 |
|
$dockerFile = $this->filesystem->get($image->getLocalPath().'/Dockerfile'); |
|
62
|
|
|
|
|
63
|
3 |
|
return (string) $this->readVersion(strtok($dockerFile, "\n")); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Read the version string, validate and return version. |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $versionString |
|
70
|
|
|
* |
|
71
|
|
|
* @throws \Exception |
|
72
|
|
|
* |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
5 |
|
public function readVersion($versionString) |
|
76
|
|
|
{ |
|
77
|
5 |
|
if (! preg_match('/#VERSION:\s{0,}\d+\.\d+\.\d+/', $versionString)) { |
|
78
|
1 |
|
throw new \Exception( |
|
79
|
1 |
|
"The version must be the first line of the Dockerfile and in the format '#VERSION: x.y.z'" |
|
80
|
|
|
); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
4 |
|
return trim(str_replace('#VERSION:', '', $versionString)); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Update the version stored in the config.json file. |
|
88
|
|
|
* |
|
89
|
|
|
* @param Image $image |
|
90
|
|
|
* @param $version |
|
91
|
|
|
* @throws FileNotFoundException |
|
92
|
|
|
*/ |
|
93
|
3 |
|
public function updateConfigVersionForImage(Image $image, $version) |
|
94
|
|
|
{ |
|
95
|
3 |
|
$configFile = $this->repository->getPath().'/config.json'; |
|
96
|
3 |
|
$config = json_decode($this->filesystem->get($configFile), true); |
|
97
|
|
|
|
|
98
|
3 |
|
$serviceName = substr($image->getUnVersionedName(), strlen($config['name']) + 1); |
|
99
|
|
|
|
|
100
|
3 |
|
$config['firstParty'][$serviceName] = $version; |
|
101
|
|
|
|
|
102
|
3 |
|
$this->filesystem->put($configFile, json_encode($config, JSON_PRETTY_PRINT)); |
|
103
|
3 |
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Push the current images. |
|
107
|
|
|
* |
|
108
|
|
|
* @param string|null $service |
|
109
|
|
|
* |
|
110
|
|
|
* @throws \Exception |
|
111
|
|
|
*/ |
|
112
|
3 |
|
public function pushImages($service = null) |
|
113
|
|
|
{ |
|
114
|
3 |
|
foreach ($this->repository->findByServiceName($service, $firstPartyOnly = true) as $image) { |
|
115
|
|
|
/* @var Image $image */ |
|
116
|
2 |
|
$this->cli->passthru("docker push {$image->getName()}"); |
|
117
|
|
|
} |
|
118
|
3 |
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Pull our docker images. |
|
122
|
|
|
* |
|
123
|
|
|
* @param string|null $service |
|
124
|
|
|
* |
|
125
|
|
|
* @throws \Exception |
|
126
|
|
|
*/ |
|
127
|
3 |
|
public function pullImages($service = null) |
|
128
|
|
|
{ |
|
129
|
3 |
|
foreach ($this->repository->findByServiceName($service) as $image) { |
|
130
|
|
|
/** @var Image $image */ |
|
131
|
3 |
|
if (running_tests() && $this->hasImage($image)) { |
|
132
|
|
|
continue; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
3 |
|
$this->cli->passthru("docker pull {$image->getName()}"); |
|
136
|
|
|
} |
|
137
|
3 |
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Check if we already have the image. |
|
141
|
|
|
* |
|
142
|
|
|
* @param Image $image |
|
143
|
|
|
* |
|
144
|
|
|
* @return bool |
|
145
|
|
|
*/ |
|
146
|
3 |
|
public function hasImage(Image $image) |
|
147
|
|
|
{ |
|
148
|
3 |
|
$output = $this->cli->exec("docker image inspect {$image->getName()}"); |
|
149
|
|
|
|
|
150
|
3 |
|
return strpos($output, "Error: No such image: {$image->getName()}") === false; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|