-
Notifications
You must be signed in to change notification settings - Fork 3
/
DbDockerCommand.php
299 lines (264 loc) · 10.7 KB
/
DbDockerCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
namespace Axelerant\DbDocker;
use Composer\Command\BaseCommand;
use GitElephant\Repository;
use Symfony\Component\Console\Exception\InvalidOptionException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\ProcessBuilder;
class DbDockerCommand extends BaseCommand
{
/**
* @var InputInterface
*/
protected $input;
/**
* @var OutputInterface
*/
protected $output;
/**
* @var OptionsProvider
*/
protected $options;
/**
* {@inheritDoc}
*/
protected function configure()
{
$this->setName('db-docker')
->setDescription('Generate a Docker image for the database.')
->addOption(
'docker-tag',
't',
InputOption::VALUE_OPTIONAL,
'The Docker tag to build'
)
->addOption(
'git-remote',
'r',
InputOption::VALUE_OPTIONAL,
'The git remote to use to determine the image name'
)
->addOption(
'db-source',
's',
InputOption::VALUE_OPTIONAL,
'Source of the database ("lando", "drush", or "file")'
)
->addOption(
'db-file',
'f',
InputOption::VALUE_OPTIONAL,
'The path to the database file (required if db-source is set to file)'
)
->addOption(
'no-push',
null,
InputOption::VALUE_NONE,
'Set to not push the image after building'
);
}
/**
* {@inheritDoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->input = $input;
$this->output = $output;
$this->options = new OptionsProvider($input, $this->getComposer()->getPackage());
$imageId = $this->getImageId();
$sqlFile = $this->getDbFile();
$baseDetails = $this->options->getDockerBaseDetails();
$this->verifyBaseImage($baseDetails);
$this->buildImage($imageId, $sqlFile, $baseDetails);
if ($this->options->getPush()) {
$this->output->writeln("<info>Pushing image...</info>");
$this->execCmd(['docker', 'push', $imageId]);
} else {
$this->output->writeln(sprintf("<info>To push image, run '%s'</info>", "docker push " . $imageId));
}
}
/**
* Get the complete image name (with tag) based on given options.
*/
protected function getImageId(): string
{
// We can safely use `getcwd()` even in a subdirectory.
$git = new Repository(getcwd());
$tag = $this->options->getDockerTag();
if (!$tag || $tag == 'auto') {
$tag = $git->getMainBranch()->getName();
$this->output->writeln("<info>Docker tag not specified. Using current branch name: {$tag}</info>");
// We should be using the tag 'latest' if the current branch is 'master'.
if ($tag == 'master') {
$tag = 'latest';
$this->output->writeln(
"<info>Using Docker tag 'latest' for branch 'master'.</info>",
OutputInterface::VERBOSITY_VERBOSE
);
}
}
$imageName = $this->options->getDockerImageName();
if (!$imageName || $imageName == "auto") {
// Throws an exception if the remote not found, so we don't have to.
$remote = $git->getRemote($this->options->getGitRemote(), false);
$imageName = $this->getImageNameFromRepoUrl($remote->getFetchURL());
$this->output->writeln("<info>Docker image not specified. Using from git repository: {$imageName}</info>");
}
// Determine the image name (path) from the git remote URL.
return sprintf("%s:%s", $imageName, $tag);
}
/**
* Determine the Docker image name from the repo URL.
*
* @param string $url
*
* @return string
*/
protected function getImageNameFromRepoUrl(string $url): string
{
if (!preg_match('/^[^@]*@([^:]*):(.*)\.git$/', $url, $matches)) {
throw new InvalidOptionException("The specified git remote URL couldn't be parsed");
}
$host = $matches[1];
$path = $matches[2];
switch ($host) {
case 'gitlab.axl8.xyz':
$registryDomain = 'registry.axl8.xyz';
break;
case 'gitorious.xyz':
case 'code.axelerant.com':
$registryDomain = 'registry.gitorious.xyz';
break;
default:
throw new InvalidOptionException("The specified git remote URL isn't supported");
}
return sprintf("%s/%s/db", $registryDomain, strtolower($path));
}
/**
* Get the database file path based on the source.
*/
protected function getDbFile(): string
{
$src = $this->options->getDbSource() ?: $this->guessSource();
if ($src != 'lando' && $src != 'drush' && $src != 'file') {
throw new InvalidOptionException("db-source can only be 'lando', 'drush', or 'file'");
}
$this->output->writeln("<info>Getting SQL file from source '{$src}'</info>");
if ($src == 'file') {
if (!$this->input->getOption('db-file')) {
throw new InvalidOptionException("db-file is required if db-source is set to 'file'");
}
return realpath($this->input->getOption('db-file'));
}
// Get SQL from Lando or Drush.
$sqlFileName = tempnam(sys_get_temp_dir(), 'axldb');
$drushCmd = 'drush sql:dump > ' . $sqlFileName;
if ($src == 'lando') {
$drushCmd = 'lando ' . $drushCmd;
}
$this->execCmd($drushCmd);
return $sqlFileName;
}
/**
* Guess the best database source.
*/
protected function guessSource(): string
{
// If there is a file called '.lando.yml', there is a good chance
// that the project uses lando and we should use that for the source.
if (file_exists('.lando.yml')) {
// If we are running inside Lando, just use 'drush'.
return getenv('LANDO') == 'ON' ? 'drush' : 'lando';
}
return 'drush';
}
/**
* Show warnings if the image doesn't appear to be a MariaDB image.
*
* @param array $baseImageDetails
*/
protected function verifyBaseImage(array $baseImageDetails): void
{
$image = $baseImageDetails['image'];
if (!preg_match("/^([^:]+)(:.+)?$/", $image, $matches)) {
throw new \UnexpectedValueException("Cannot parse Docker image name: '{$image}.'");
}
$imageName = $matches[1];
$allowedImages = ['bitnami/mariadb', 'mariadb'];
if (!in_array(strtolower($imageName), $allowedImages)) {
$this->output->writeln(
"<comment>Cannot recognize image name '{$imageName}'. Use at your own risk.</comment>"
);
}
}
/**
* Build the image using our Dockerfile and SQL scripts
*
* @param string $imageId
* @param string $sqlFile
* @param array $baseDetails
*/
protected function buildImage(string $imageId, string $sqlFile, array $baseDetails): void
{
$tempDir = realpath(sys_get_temp_dir());
$tempPath = sprintf('%s%s%s', $tempDir, DIRECTORY_SEPARATOR, sha1(uniqid())) . '/';
$assetPath = realpath(__DIR__ . '/../assets/dockerize-db') . '/';
mkdir($tempPath);
mkdir($tempPath . 'dumps');
copy($assetPath . 'Dockerfile', $tempPath . 'Dockerfile');
copy($sqlFile, $tempPath . "/dumps/db.sql");
copy($assetPath . "zzzz-truncate-caches.sql", $tempPath . "zzzz-truncate-caches.sql");
$this->output->writeln("<info>Building image '{$imageId}'</info>");
$dockerCmd = ['docker', 'build', '-t', $imageId];
$dockerCmd[] = '--build-arg';
$dockerCmd[] = sprintf("BASE_IMAGE=%s", $baseDetails['image']);
$dockerCmd[] = '--build-arg';
$dockerCmd[] = sprintf("BASE_IMAGE_USER=%s", $baseDetails['user']);
$dockerCmd[] = '--build-arg';
$dockerCmd[] = sprintf("BASE_IMAGE_PASSWORD=%s", $baseDetails['password']);
$dockerCmd[] = '--build-arg';
$dockerCmd[] = sprintf("BASE_IMAGE_DATABASE=%s", $baseDetails['database']);
$dockerCmd[] = $tempPath;
$this->execCmd($dockerCmd);
}
protected function execCmd($cmd): void
{
$this->output->writeln(sprintf(
"<info>Running '%s'</info>",
is_array($cmd) ? implode(" ", $cmd) : $cmd
), OutputInterface::VERBOSITY_VERBOSE);
if (is_array($cmd) && PHP_VERSION_ID < 70400 && class_exists(ProcessBuilder::class)) {
// Composer embeds an old version of symfony/process, and we need to
// target that. This version (2.8.52 as of this writing) does not
// support command line as an array which results in an error with
// proc_open before PHP 7.4. This is the case we should target.
// See https://github.com/axelerant/db-docker/issues/12.
$p = (new ProcessBuilder($cmd))->getProcess();
} elseif (is_string($cmd) && method_exists(Process::class, 'fromShellCommandline')) {
// BC for symfony/process < 4.2.
// The method fromShellCommandline is new in 4.2 and it deprecated
// using strings for constructor (and was removed in 5). Since we are
// trying to support a variety of versions, this check is necessary when
// the command is a string.
// This code is only left here in case composer updates its version of
// symfony/process one day. In most cases, this line of code will not
// be executed when using composer phar file.
$p = Process::fromShellCommandline($cmd);
} else {
// Versions of symfony/process from 3.3 to before 5.0 supported
// constructor with string AND array parameters.
$p = new Process($cmd);
}
$p->setTimeout(600);
$code = $p->run();
$this->output->writeln($p->getOutput(), OutputInterface::OUTPUT_RAW | OutputInterface::VERBOSITY_VERBOSE);
if (!$p->isSuccessful()) {
$this->output->writeln("<error>Command returned exit code '{$code}'</error>");
throw new ProcessFailedException($p);
}
}
}