ProcessManager::kill()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
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\Cli\Docker;
6
7
use Ktomk\Pipelines\Cli\Exec;
8
use Ktomk\Pipelines\Lib;
9
10
/**
11
 * Process manager for docker container
12
 */
13
class ProcessManager
14
{
15
    /**
16
     * @var Exec
17
     */
18
    private $exec;
19
20 7
    public function __construct(Exec $exec)
21
    {
22 7
        $this->exec = $exec;
23
    }
24
25
    /**
26
     * @param string $name
27
     *
28
     * @return null|array|string[] 0 or more ids, null if subsystem
29
     *         unavailable, subsystem error or unsupported parameter
30
     */
31 1
    public function findAllContainerIdsByName($name)
32
    {
33 1
        $ids = null;
34
35 1
        $status = $this->exec->capture(
36
            'docker',
37
            array(
38 1
                'ps', '--no-trunc', '-qa', '--filter',
39 1
                "name=^/\\Q{$name}\\E$",
40
            ),
41
            $result
42
        );
43
44 1
        $status || $ids = Lib::lines($result);
45
46 1
        return $ids;
47
    }
48
49
    /**
50
     * container ids by name prefix of running and stopped containers
51
     *
52
     * @param string $prefix
53
     *
54
     * @return null|array of ids, null if an internal error occurred
55
     */
56 2
    public function findAllContainerIdsByNamePrefix($prefix)
57
    {
58 2
        return $this->psPrefixImpl($prefix, true);
59
    }
60
61
    /**
62
     * @param string $name
63
     *
64
     * @return null|string
65
     */
66 4
    public function findContainerIdByName($name)
67
    {
68 4
        $ids = $this->findAllContainerIdsByName($name);
69 4
        if (null === $ids || 1 !== count($ids)) {
70 3
            return null;
71
        }
72
73 1
        return $ids[0];
74
    }
75
76
    /**
77
     * container ids by name prefix of running containers
78
     *
79
     * @param string $prefix
80
     *
81
     * @return null|array of ids, null if an internal error occurred
82
     */
83 1
    public function findRunningContainerIdsByNamePrefix($prefix)
84
    {
85 1
        return $this->psPrefixImpl($prefix);
86
    }
87
88
    /**
89
     * @param string|string[] $idOrIds
90
     *
91
     * @return int
92
     */
93 1
    public function kill($idOrIds)
94
    {
95 1
        return $this->exec->capture('docker', Lib::merge('kill', func_get_args()));
96
    }
97
98
    /**
99
     * @param string|string[] $idOrIds
100
     *
101
     * @return int
102
     */
103 1
    public function remove($idOrIds)
104
    {
105 1
        return $this->exec->capture('docker', Lib::merge('rm', func_get_args()));
106
    }
107
108
    /**
109
     * @param string $name
110
     *
111
     * @return void
112
     */
113 2
    public function zapContainersByName($name)
114
    {
115 2
        $ids = $this->findAllContainerIdsByName($name);
116 2
        if (!$ids) {
117 1
            return;
118
        }
119 1
        $this->zap($ids);
120
    }
121
122
    /**
123
     * @param string|string[] $idOrIds
124
     *
125
     * @return void
126
     */
127 1
    public function zap($idOrIds)
128
    {
129 1
        $this->kill($idOrIds);
130 1
        $this->remove($idOrIds);
131
    }
132
133
    /**
134
     * @param string $prefix beginning of container name
135
     * @param bool $all
136
     *
137
     * @return null|array
138
     */
139 3
    private function psPrefixImpl($prefix, $all = false)
140
    {
141 3
        $ids = null;
142
143 3
        if (0 === preg_match('(^[a-z}{3,}[a-zA-Z0-9_.-]+$)', $prefix)) {
144 1
            throw new \InvalidArgumentException(
145 1
                sprintf('Invalid container name prefix "%s"', $prefix)
146
            );
147
        }
148
149 2
        $status = $this->exec->capture(
150
            'docker',
151
            array(
152 2
                'ps', '-q' . ($all ? 'a' : ''), '--no-trunc', '--filter',
153 2
                "name=^/{$prefix}[-.]",
154
            ),
155
            $result
156
        );
157
158 2
        $status || $ids = Lib::lines($result);
159
160 2
        return $ids;
161
    }
162
}
163