Passed
Push — test ( 0afaf2...0ffdf8 )
by Tom
02:36
created

StepRunner::zapContainerByName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\Runner;
6
7
use Ktomk\Pipelines\Cli\Docker;
8
use Ktomk\Pipelines\Cli\Exec;
9
use Ktomk\Pipelines\Cli\Streams;
10
use Ktomk\Pipelines\DestructibleString;
11
use Ktomk\Pipelines\File\Image;
12
use Ktomk\Pipelines\File\Step;
13
use Ktomk\Pipelines\Lib;
14
use Ktomk\Pipelines\LibFs;
15
use Ktomk\Pipelines\LibTmp;
16
use Ktomk\Pipelines\Runner\Docker\ArtifactSource;
17
use Ktomk\Pipelines\Runner\Docker\Binary\Repository;
18
use Ktomk\Pipelines\Runner\Docker\ImageLogin;
19
20
/**
21
 * Runner for a single step of a pipeline
22
 */
23
class StepRunner
24
{
25
    /**
26
     * @var RunOpts
27
     */
28
    private $runOpts;
29
30
    /**
31
     * @var Directories
32
     */
33
    private $directories;
34
35
    /**
36
     * @var Exec
37
     */
38
    private $exec;
39
40
    /**
41
     * @var Flags
42
     */
43
    private $flags;
44
45
    /**
46
     * @var Env
47
     */
48
    private $env;
49
50
    /**
51
     * @var Streams
52
     */
53
    private $streams;
54
55
    /**
56
     * list of temporary directory destructible markers
57
     *
58
     * @var array
59
     */
60
    private $temporaryDirectories = array();
61
62
    /**
63
     * DockerSession constructor.
64
     *
65
     * @param RunOpts $runOpts
66
     * @param Directories $directories source repository root directory based directories object
67
     * @param Exec $exec
68
     * @param Flags $flags
69
     * @param Env $env
70
     * @param Streams $streams
71
     */
72 18
    public function __construct(
73
        RunOpts $runOpts,
74
        Directories $directories,
75
        Exec $exec,
76
        Flags $flags,
77
        Env $env,
78
        Streams $streams
79
    )
80
    {
81 18
        $this->runOpts = $runOpts;
82 18
        $this->directories = $directories;
83 18
        $this->exec = $exec;
84 18
        $this->flags = $flags;
85 18
        $this->env = $env;
86 18
        $this->streams = $streams;
87 18
    }
88
89
    /**
90
     * @param Step $step
91
     * @return null|int exist status of step script or null if the run operation failed
92
     */
93 17
    public function runStep(Step $step)
94
    {
95 17
        $dir = $this->directories->getProjectDirectory();
96 17
        $env = $this->env;
97 17
        $exec = $this->exec;
98 17
        $streams = $this->streams;
99
100 17
        $env->setPipelinesProjectPath($dir);
101
102 17
        $container = StepContainer::create($step, $exec);
103
104 17
        $name = $container->generateName($this->runOpts->getPrefix(), $this->directories->getName());
105 17
        $env->setContainerName($name);
106
107 17
        $image = $step->getImage();
108
109
        # launch container
110 17
        $streams->out(sprintf(
111 17
            "\x1D+++ step #%d\n\n    name...........: %s\n    effective-image: %s\n    container......: %s\n",
112 17
            $step->getIndex() + 1,
113 17
            $step->getName() ? '"' . $step->getName() . '"' : '(unnamed)',
114 17
            $image->getName(),
115 17
            $name
116
        ));
117
118 17
        $id = $container->keepOrKill($this->flags->reuseContainer());
119
120 17
        $deployCopy = $this->flags->deployCopy();
121
122 17
        if (null === $id) {
123 15
            list($id, $status) = $this->runNewContainer($container, $dir, $deployCopy, $step);
124 15
            if (null === $id) {
125 2
                return $status;
126
            }
127
        }
128
129 15
        $streams->out(sprintf("    container-id...: %s\n\n", substr($id, 0, 12)));
130
131
        # TODO: different deployments, mount (default), mount-ro, copy
132 15
        if (null !== $result = $this->deployCopy($deployCopy, $id, $dir)) {
133 2
            return $result;
134
        }
135
136 13
        list($status, $message) = $this->deployDockerClient($step, $id);
137 12
        if (0 !== $status) {
138 1
            $this->streams->err(rtrim($message, "\n") . "\n");
139
140 1
            return $status;
141
        }
142
143 11
        $status = $this->runStepScript($step, $streams, $exec, $name);
144
145 11
        $this->captureStepArtifacts($step, $deployCopy && 0 === $status, $id, $dir);
146
147 11
        $this->shutdownStepContainer($status, $id);
148
149 11
        return $status;
150
    }
151
152
    /**
153
     * method to wrap new to have a test-point
154
     *
155
     * @return Repository
156
     */
157 2
    public function getDockerBinaryRepository()
158
    {
159 2
        $repo = Repository::create($this->exec, $this->directories);
160 2
        $repo->resolve($this->runOpts->getBinaryPackage());
161
162 1
        return $repo;
163
    }
164
165
    /**
166
     * @param Step $step
167
     * @param bool $copy
168
     * @param string $id container id
169
     * @param string $dir to put artifacts in (project directory)
170
     * @throws \RuntimeException
171
     */
172 11
    private function captureStepArtifacts(Step $step, $copy, $id, $dir)
173
    {
174
        # capturing artifacts is only supported for deploy copy
175 11
        if (!$copy) {
176 6
            return;
177
        }
178
179 5
        $artifacts = $step->getArtifacts();
180
181 5
        if (null === $artifacts) {
182 2
            return;
183
        }
184
185 3
        $exec = $this->exec;
186 3
        $streams = $this->streams;
187
188 3
        $streams->out("\x1D+++ copying artifacts from container...\n");
189
190 3
        $source = new ArtifactSource($exec, $id, $dir);
191
192 3
        $patterns = $artifacts->getPatterns();
193 3
        foreach ($patterns as $pattern) {
194 3
            $this->captureArtifactPattern($source, $pattern, $dir);
195
        }
196
197 3
        $streams('');
198 3
    }
199
200
    /**
201
     * @see Runner::captureStepArtifacts()
202
     *
203
     * @param ArtifactSource $source
204
     * @param string $pattern
205
     * @param string $dir
206
     * @throws \RuntimeException
207
     */
208 3
    private function captureArtifactPattern(ArtifactSource $source, $pattern, $dir)
209
    {
210 3
        $exec = $this->exec;
211 3
        $streams = $this->streams;
212
213 3
        $id = $source->getId();
214 3
        $paths = $source->findByPattern($pattern);
215 3
        if (empty($paths)) {
216 1
            return;
217
        }
218
219 2
        $chunks = Lib::arrayChunkByStringLength($paths, 131072, 4);
220
221 2
        foreach ($chunks as $paths) {
222 2
            $docker = Lib::cmd('docker', array('exec', '-w', '/app', $id));
223 2
            $tar = Lib::cmd('tar', array('c', '-f', '-', $paths));
224 2
            $unTar = Lib::cmd('tar', array('x', '-f', '-', '-C', $dir));
225
226 2
            $command = $docker . ' ' . $tar . ' | ' . $unTar;
227 2
            $status = $exec->pass($command, array());
228
229 2
            if (0 !== $status) {
230 1
                $streams->err(sprintf(
231 1
                    "pipelines: Artifact failure: '%s' (%d, %d paths, %d bytes)\n",
232 1
                    $pattern,
233 1
                    $status,
234 1
                    count($paths),
235 1
                    strlen($command)
236
                ));
237
            }
238
        }
239 2
    }
240
241
    /**
242
     * @param bool $copy
243
     * @param string $id container id
244
     * @param string $dir directory to copy contents into container
245
     * @throws \RuntimeException
246
     * @return null|int null if all clear, integer for exit status
247
     */
248 15
    private function deployCopy($copy, $id, $dir)
249
    {
250 15
        if (!$copy) {
251 8
            return null;
252
        }
253
254 7
        $streams = $this->streams;
255 7
        $exec = $this->exec;
256
257 7
        $streams->out("\x1D+++ copying files into container...\n");
258
259 7
        $tmpDir = LibTmp::tmpDir('pipelines-cp.');
260 7
        $this->temporaryDirectories[] = DestructibleString::rmDir($tmpDir);
261 7
        LibFs::symlink($dir, $tmpDir . '/app');
262 7
        $cd = Lib::cmd('cd', array($tmpDir . '/.'));
263 7
        $tar = Lib::cmd('tar', array('c', '-h', '-f', '-', '--no-recursion', 'app'));
264 7
        $dockerCp = Lib::cmd('docker ', array('cp', '-', $id . ':/.'));
265
        $status = $exec->pass("${cd} && echo 'app' | ${tar} | ${dockerCp}", array());
266
        LibFs::unlink($tmpDir . '/app');
267 7
        if (0 !== $status) {
268 1
            $streams->err('pipelines: deploy copy failure\n');
269
270 1
            return $status;
271
        }
272
273 6
        $cd = Lib::cmd('cd', array($dir . '/.'));
274 6
        $tar = Lib::cmd('tar', array('c', '-f', '-', '.'));
275 6
        $dockerCp = Lib::cmd('docker ', array('cp', '-', $id . ':/app'));
276 6
        $status = $exec->pass("${cd} && ${tar} | ${dockerCp}", array());
277 6
        if (0 !== $status) {
278 1
            $streams->err('pipelines: deploy copy failure\n');
279
280 1
            return $status;
281
        }
282
283 5
        $streams('');
284
285 5
        return null;
286
    }
287
288
    /**
289
     * if there is the docker service in the step, deploy the
290
     * docker client
291
     *
292
     * @param Step $step
293
     * @param string $id
294
     * @throws
295
     * @return array array(int $status, string $message)
296
     */
297
    private function deployDockerClient(Step $step, $id)
298
    {
299 13
        if (!$step->getServices()->has('docker')) {
300 10
            return array(0, '');
301
        }
302
303 3
        $this->streams->out(' +++ docker client install...: ');
304
305
        try {
306 3
            list($status, $message) = $this->getDockerBinaryRepository()->inject($id);
307 1
        } catch (\Exception $e) {
308 1
            $this->streams->out("pipelines internal failure.\n");
309
310 1
            throw new \InvalidArgumentException('inject docker client failed: ' . $e->getMessage(), 1, $e);
311
        }
312
313 2
        $this->streams->out("${message}\n");
314
315 2
        return array($status, $message);
316
    }
317
318
    /**
319
     * @param Image $image
320
     * @throws \RuntimeException
321
     * @throws \InvalidArgumentException
322
     */
323
    private function imageLogin(Image $image)
324
    {
325 15
        $login = new ImageLogin($this->exec, $this->env->getResolver());
326 15
        $login->byImage($image);
327 15
    }
328
329
    /**
330
     * @param StepContainer $container
331
     * @param string $dir
332
     * @param bool $copy
333
     * @param Step $step
334
     *
335
     * @return array array(string|null $id, int $status)
336
     */
337
    private function runNewContainer(StepContainer $container, $dir, $copy, Step $step)
338
    {
339 15
        $env = $this->env;
340 15
        $exec = $this->exec;
341 15
        $streams = $this->streams;
342
343 15
        $image = $step->getImage();
344
345
        # process docker login if image demands so, but continue on failure
346 15
        $this->imageLogin($image);
347
348
        // enable docker client inside docker by mounting docker socket
349
        // FIXME give controlling options, this is serious /!\
350 15
        $mountDockerSock = array();
351 15
        $pathDockerSock = $this->runOpts->getOption('docker.socket.path');
352 15
        if ($this->flags->useDockerSocket() && file_exists($pathDockerSock)) {
353
            $mountDockerSock = array(
354 12
                '-v', sprintf('%s:%s', $pathDockerSock, $pathDockerSock),
355
            );
356
        }
357
358 15
        $parentName = $env->getValue('PIPELINES_PARENT_CONTAINER_NAME');
359 15
        $checkMount = $mountDockerSock && null !== $parentName;
360 15
        $deviceDir = $dir;
361 15
        if ($checkMount && '/app' === $dir) { // FIXME(tk): hard encoded /app
362 1
            $docker = new Docker($exec);
363 1
            $deviceDir = $docker->hostDevice($parentName, $dir);
364 1
            unset($docker);
365 1
            if ($deviceDir === $dir) {
366 1
                $deviceDir = $env->getPipelinesProjectPath($deviceDir);
367
            }
368 1
            if ($deviceDir === $dir) {
369 1
                $streams->err("pipelines: fatal: can not detect ${dir} mount point. preventing new container.\n");
370
371 1
                return array(null, 1);
372
            }
373
        }
374
375 14
        $mountWorkingDirectory = $copy
376 7
            ? array()
377
            // FIXME(tk): Never mount anything not matching /home/[a-zA-Z][a-zA-Z0-9]*/[^.].*/...
378
            //   + do realpath checking
379
            //   + prevent dot path injections (logical fix first)
380 14
            : array('--volume', "${deviceDir}:/app"); // FIXME(tk): hard encoded /app
381
382 14
        $status = $exec->capture('docker', array(
383 14
            'run', '-i', '--name', $container->getName(),
384 14
            $env->getArgs('-e'),
385 14
            $mountWorkingDirectory, '-e', 'BITBUCKET_CLONE_DIR=/app',
386 14
            $mountDockerSock,
387 14
            '--workdir', '/app', '--detach', '--entrypoint=/bin/sh', $image->getName()
388 14
        ), $out, $err);
389 14
        if (0 !== $status) {
390 1
            $streams->out("    container-id...: *failure*\n\n");
391 1
            $streams->err("pipelines: setting up the container failed\n");
392 1
            $streams->err("${err}\n");
393 1
            $streams->out("${out}\n");
394 1
            $streams->out(sprintf("exit status: %d\n", $status));
395
396 1
            return array(null, $status);
397
        }
398 13
        $id = rtrim($out) ?: '*dry-run*'; # side-effect: internal exploit of no output with true exit status
399
400 13
        return array($id, 0);
401
    }
402
403
    /**
404
     * @param Step $step
405
     * @param Streams $streams
406
     * @param Exec $exec
407
     * @param string $name container name
408
     * @return null|int should never be null, status, non-zero if a command failed
409
     */
410
    private function runStepScript(Step $step, Streams $streams, Exec $exec, $name)
411
    {
412 11
        $script = $step->getScript();
413
414 11
        $buffer = Lib::cmd("<<'SCRIPT' docker", array(
415 11
            'exec', '-i', $name, '/bin/sh'
416
        ));
417 11
        $buffer .= "\n# this /bin/sh script is generated from a pipelines pipeline:\n";
418 11
        $buffer .= "set -e\n";
419 11
        foreach ($script as $line => $command) {
420 11
            $line && $buffer .= 'printf \'\\n\'' . "\n";
421 11
            $buffer .= 'printf \'\\035+ %s\\n\' ' . Lib::quoteArg($command) . "\n";
422 11
            $buffer .= $command . "\n";
423
        }
424 11
        $buffer .= "SCRIPT\n";
425
426 11
        $status = $exec->pass($buffer, array());
427
428 11
        if (0 !== $status) {
429 2
            $streams->err(sprintf("script non-zero exit status: %d\n", $status));
430
        }
431
432 11
        return $status;
433
    }
434
435
    /**
436
     * @param int $status
437
     * @param string $id container id
438
     * @throws \RuntimeException
439
     */
440
    private function shutdownStepContainer($status, $id)
441
    {
442 11
        $flags = $this->flags;
443
444
        # keep container on error
445 11
        if (0 !== $status && $flags->keepOnError()) {
446 2
            $this->streams->err(sprintf(
447 2
                "error, keeping container id %s\n",
448 2
                substr($id, 0, 12)
449
            ));
450
451 2
            return;
452
        }
453
454
        # keep or remove container
455 9
        if ($flags->killContainer()) {
456 8
            Docker::create($this->exec)->getProcessManager()->kill($id);
457
        }
458
459 9
        if ($flags->removeContainer()) {
460 8
            Docker::create($this->exec)->getProcessManager()->remove($id);
461
        }
462
463 9
        if ($flags->keep()) {
464 1
            $this->streams->out(sprintf(
465 1
                "keeping container id %s\n",
466 1
                substr($id, 0, 12)
467
            ));
468
        }
469 9
    }
470
}
471