Passed
Push — test ( c65f5a...b51793 )
by Tom
07:13
created

StepContainer::shutdown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 12
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 19
ccs 13
cts 13
cp 1
crap 1
rs 9.8666
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\Cli\Exec;
9
use Ktomk\Pipelines\File\Pipeline\Step;
10
use Ktomk\Pipelines\Runner\Containers;
11
use Ktomk\Pipelines\Runner\Runner;
12
13
/**
14
 * Class StepContainer
15
 *
16
 * @package Ktomk\Pipelines\Runner
17
 */
18
class StepContainer
19
{
20
    /**
21
     * @var null|string id of the (running) container
22
     */
23
    private $id;
24
25
    /**
26
     * @var null|string name of the container
27
     */
28
    private $name;
29
30
    /**
31
     * @var Step
32
     */
33
    private $step;
34
35
    /**
36
     * @var Runner
37
     */
38
    private $runner;
39
40
    /**
41
     * @var StepServiceContainers
42
     */
43
    private $serviceContainers;
44
45
    /**
46
     * StepContainer constructor.
47
     *
48
     * @param string $name
49
     * @param Step $step
50
     * @param Runner $runner
51
     */
52 12
    public function __construct($name, Step $step, Runner $runner)
53
    {
54 12
        $this->name = $name;
55 12
        $this->step = $step;
56 12
        $this->runner = $runner;
57 12
    }
58
59
    /**
60
     * the display id
61
     *
62
     *   side-effect: if id is null, this signals a dry-run which is made
63
     * visible by the string "*dry-run*"
64
     *
65
     * @return string
66
     */
67 8
    public function getDisplayId()
68
    {
69 8
        return isset($this->id) ? $this->id : '*dry-run*';
70
    }
71
72
    /**
73
     * @return null|string ID of (once) running container or null if not yet running
74
     */
75 3
    public function getId()
76
    {
77 3
        return $this->id;
78
    }
79
80
    /**
81
     * @return null|string name of the container, NULL if no name generated yet
82
     */
83 5
    public function getName()
84
    {
85 5
        return $this->name;
86
    }
87
88
    /**
89
     * @return StepServiceContainers
90
     */
91 4
    public function getServiceContainers()
92
    {
93 4
        return $this->serviceContainers
94 4
            = $this->serviceContainers ?: new StepServiceContainers($this->step, $this->runner);
95
    }
96
97
    /**
98
     * @param null|bool $keep a container on true, kill on false (if it exists)
99
     *
100
     * @return null|string
101
     */
102 7
    public function keepOrKill($keep = null)
103
    {
104 7
        $keep = null === $keep ? $this->runner->getFlags()->reuseContainer() : $keep;
105
106 7
        $name = $this->name;
107 7
        if (null === $name) {
108 1
            throw new \BadMethodCallException('Container has no name yet');
109
        }
110
111 6
        $processManager = Docker::create($this->runner->getExec())->getProcessManager();
112
113 6
        if (false === $keep) {
114 2
            $processManager->zapContainersByName($name);
115
116 2
            return $this->id = null;
117
        }
118
119 5
        return $this->id = $processManager->findContainerIdByName($name);
120
    }
121
122
    /**
123
     * @param bool $kill
124
     * @param bool $remove
125
     *
126
     * @return void
127
     */
128 2
    public function killAndRemove($kill, $remove)
129
    {
130 2
        $id = $this->getDisplayId();
131
132 2
        Containers::execKillAndRemove($this->runner->getExec(), $id, $kill, $remove);
133 2
    }
134
135
    /**
136
     * @param array $args
137
     *
138
     * @return array array(int $status, string $out, string $err)
139
     */
140 4
    public function run(array $args)
141
    {
142 4
        $execRun = Containers::execRun($this->runner->getExec(), $args);
143 4
        $this->id = array_pop($execRun);
144
145 4
        return $execRun;
146
    }
147
148
    /**
149
     * @param int $status
150
     *
151
     * @return void
152
     */
153 4
    public function shutdown($status)
154
    {
155 4
        $id = $this->getDisplayId();
156
157 4
        $message = sprintf(
158 4
            'keeping container id %s',
159 4
            (string)substr($id, 0, 12)
160
        );
161
162 4
        Containers::execShutdownContainer(
163 4
            $this->runner->getExec(),
164 4
            $this->runner->getStreams(),
165 4
            $id,
166 4
            $status,
167 4
            $this->runner->getFlags(),
168 4
            $message
169
        );
170
171 4
        $this->getServiceContainers()->shutdown($status);
172 4
    }
173
}
174