Test Failed
Push — test ( a53f3f...e32fe9 )
by Tom
02:31
created

BinaryRepository::asPackageArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 10
c 1
b 0
f 1
nc 2
nop 0
dl 0
loc 15
rs 9.9332
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\Runner\Docker;
6
7
use Ktomk\Pipelines\Cli\Exec;
8
use Ktomk\Pipelines\Lib;
9
use Ktomk\Pipelines\Runner\Directories;
10
11
/**
12
 * Class DockerBinaryRepository
13
 *
14
 * Docker provides a static binary which is useful to have in containers when
15
 * in the container itself docker should be available (e.g. docker service).
16
 *
17
 * It "comes" out of this binary repository. In the background there is a
18
 * BinaryUnPackager taking care of network interaction and file verification
19
 * as it is technically possible to have different kind of static docker
20
 * binaries also from different packages.
21
 *
22
 * @package Ktomk\Pipelines\Runner\Docker
23
 */
24
class BinaryRepository implements BinaryPackageInterface
25
{
26
    /**
27
     * @var Exec
28
     */
29
    private $exec;
30
31
    /**
32
     * @var Directories
33
     */
34
    private $directories;
35
36
    /**
37
     * @var array package definition data
38
     */
39
    private $package;
40
41
    /**
42
     * BinaryRepository constructor.
43
     * @param Exec $exec
44
     * @param Directories $directories
45
     * @param array $package [optional]
46
     */
47
    public function __construct(Exec $exec, Directories $directories, array $package = null)
48
    {
49
        $this->exec = $exec;
50
        $this->directories = $directories;
51
        $this->package = $package;
52
    }
53
54
    /**
55
     * Resolve a binary package name in this repository itself
56
     *
57
     * @param string $packageName
58
     * @return BinaryRepository
59
     */
60
    public function resolve($packageName)
61
    {
62
        $packageDir = __DIR__ . '/../../../lib/package';
63
        $reader = new BinaryPackageYamlFileReader(
64
            sprintf('%s/%s.yml', $packageDir, $packageName)
65
        );
66
        $this->package = $reader->asPackageArray();
67
68
        return $this;
69
    }
70
71
    /**
72
     * @param $containerId
73
     */
74
    public function inject($containerId)
75
    {
76
        $package = $this->asPackageArray();
77
78
        $local = BinaryUnPackager::fromDirectories($this->exec, $this->directories)->getLocalBinary($package);
79
80
        $this->exec->pass(
81
            sprintf('2>&1 < %s docker', lib::quoteArg($local)),
82
            array('exec', '-i', $containerId, '/bin/sh', '-c', 'mkdir -p /usr/bin && cat - > /usr/bin/docker; chmod +x /usr/bin/docker; docker --version')
83
        );
84
    }
85
86
    /**
87
     * @inheritDoc
88
     */
89
    public function asPackageArray()
90
    {
91
        $package =  $this->package;
92
        if (null === $package) {
0 ignored issues
show
introduced by
The condition null === $package is always false.
Loading history...
93
            $package = array(
94
                'name' => 'docker-19.03.1-linux-static-x86_64',
95
                'uri' => 'https://download.docker.com/linux/static/stable/x86_64/docker-19.03.1.tgz',
96
                'basename' => 'docker-19.03.1',
97
                'sha256' => '6e7d8e24ee46b13d7547d751696d01607d19c8224c1b2c867acc8c779e77734b',
98
                'binary' => 'docker/docker',
99
                'binary_sha256' => '9b6191f64cb89e706dc29390408260d643e88f7f853d8878b2fb0360186b2ac3',
100
            );
101
        }
102
103
        return $package;
104
    }
105
}
106