Test Failed
Push — test ( bcdd58...910c7a )
by Tom
02:33
created

StepContainer::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\Runner\Containers;
6
7
use Ktomk\Pipelines\Cli\Docker;
8
use Ktomk\Pipelines\File\Pipeline\Step;
9
use Ktomk\Pipelines\LibFs;
10
use Ktomk\Pipelines\LibFsPath;
11
use Ktomk\Pipelines\Runner\Containers;
12
use Ktomk\Pipelines\Runner\Runner;
13
14
/**
15
 * Class StepContainer
16
 *
17
 * @package Ktomk\Pipelines\Runner
18
 */
19
class StepContainer
20
{
21
    /**
22
     * @var null|string id of the (running) container
23
     */
24
    private $id;
25
26
    /**
27
     * @var null|string name of the container
28
     */
29
    private $name;
30
31
    /**
32
     * @var Step
33
     */
34
    private $step;
35
36
    /**
37
     * @var Runner
38
     */
39
    private $runner;
40
41
    /**
42
     * @var StepServiceContainers
43
     */
44
    private $serviceContainers;
45
46
    /**
47
     * StepContainer constructor.
48
     *
49
     * @param string $name
50
     * @param Step $step
51
     * @param Runner $runner
52
     */
53
    public function __construct($name, Step $step, Runner $runner)
54
    {
55
        $this->name = $name;
56
        $this->step = $step;
57
        $this->runner = $runner;
58
    }
59
60
    /**
61
     * the display id
62
     *
63
     *   side-effect: if id is null, this signals a dry-run which is made
64
     * visible by the string "*dry-run*"
65
     *
66
     * @return string
67
     */
68
    public function getDisplayId()
69
    {
70
        return isset($this->id) ? $this->id : '*dry-run*';
71
    }
72
73
    /**
74
     * @return null|string ID of (once) running container or null if not yet running
75
     */
76
    public function getId()
77
    {
78
        return $this->id;
79
    }
80
81
    /**
82
     * @return null|string name of the container, NULL if no name generated yet
83
     */
84
    public function getName()
85
    {
86
        return $this->name;
87
    }
88
89
    /**
90
     * @return StepServiceContainers
91
     */
92
    public function getServiceContainers()
93
    {
94
        return $this->serviceContainers
95
            = $this->serviceContainers ?: new StepServiceContainers($this->step, $this->runner);
96
    }
97
98
    /**
99
     * @param null|bool $keep a container on true, kill on false (if it exists)
100
     *
101
     * @return null|string
102
     */
103
    public function keepOrKill($keep = null)
104
    {
105
        $keep = null === $keep ? $this->runner->getFlags()->reuseContainer() : $keep;
106
107
        $name = $this->name;
108
        if (null === $name) {
109
            throw new \BadMethodCallException('Container has no name yet');
110
        }
111
112
        $processManager = Docker::create($this->runner->getExec())->getProcessManager();
113
114
        if (false === $keep) {
115
            $processManager->zapContainersByName($name);
116
117
            return $this->id = null;
118
        }
119
120
        return $this->id = $processManager->findContainerIdByName($name);
121
    }
122
123
    /**
124
     * @param bool $kill
125
     * @param bool $remove
126
     *
127
     * @return void
128
     */
129
    public function killAndRemove($kill, $remove)
130
    {
131
        $id = $this->getDisplayId();
132
133
        Containers::execKillAndRemove($this->runner->getExec(), $id, $kill, $remove);
134
    }
135
136
    /**
137
     * @param array $args
138
     *
139
     * @return array array(int $status, string $out, string $err)
140
     */
141
    public function run(array $args)
142
    {
143
        $execRun = Containers::execRun($this->runner->getExec(), $args);
144
        $this->id = array_pop($execRun);
145
146
        return $execRun;
147
    }
148
149
    /**
150
     * @param int $status
151
     *
152
     * @return void
153
     */
154
    public function shutdown($status)
155
    {
156
        $id = $this->getDisplayId();
157
158
        $message = sprintf(
159
            'keeping container id %s',
160
            (string)substr($id, 0, 12)
161
        );
162
163
        Containers::execShutdownContainer(
164
            $this->runner->getExec(),
165
            $this->runner->getStreams(),
166
            $id,
167
            $status,
168
            $this->runner->getFlags(),
169
            $message
170
        );
171
172
        $this->getServiceContainers()->shutdown($status);
173
    }
174
175
    /** docker run args mapping methods */
176
177
    /**
178
     * @param null|string $user
179
     *
180
     * @return array
181
     */
182
    public function obtainUserOptions()
183
    {
184
        $user = $this->runner->getRunOpts()->getUser();
185
186
        $userOpts = array();
187
188
        if (null === $user) {
189
            return $userOpts;
190
        }
191
192
        $userOpts = array('--user', $user);
193
194
        if (LibFs::isReadableFile('/etc/passwd') && LibFs::isReadableFile('/etc/group')) {
195
            $userOpts[] = '-v';
196
            $userOpts[] = '/etc/passwd:/etc/passwd:ro';
197
            $userOpts[] = '-v';
198
            $userOpts[] = '/etc/group:/etc/group:ro';
199
        }
200
201
        return $userOpts;
202
    }
203
204
    public function obtainSshOptions()
205
    {
206
        $ssh = $this->runner->getRunOpts()->getSsh();
207
        $env = $this->runner->getEnv();
208
209
        $sshOpts = array();
210
        if (
211
            null === $ssh
212
            || (null === $sshAuthSock = $env->getInheritValue('SSH_AUTH_SOCK'))
213
            || '' === trim($sshAuthSock)
214
            || !is_writable($sshAuthSock)
215
        ) {
216
            return $sshOpts;
217
        }
218
219
        $sshOpts = array(
220
            '-v',
221
            LibFsPath::gateAbsolutePortable($sshAuthSock) . ':/ssh-auth.sock',
222
            '-e',
223
            'SSH_AUTH_SOCK=/ssh-auth.sock',
224
        );
225
226
        return $sshOpts;
227
    }
228
}
229