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
|
7 |
|
} |
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
|
1 |
|
'docker', |
37
|
|
|
array( |
38
|
1 |
|
'ps', '--no-trunc', '-qa', '--filter', |
39
|
1 |
|
"name=^/\\Q${name}\\E$", |
40
|
|
|
), |
41
|
|
|
$result |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
$status || $ids = Lib::lines($result); |
45
|
|
|
|
46
|
|
|
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
|
|
|
public function findAllContainerIdsByNamePrefix($prefix) |
57
|
|
|
{ |
58
|
2 |
|
return $this->psPrefixImpl($prefix, true); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* container ids by name prefix of running containers |
63
|
|
|
* |
64
|
|
|
* @param string $prefix |
65
|
|
|
* |
66
|
|
|
* @return null|array of ids, null if an internal error occurred |
67
|
|
|
*/ |
68
|
|
|
public function findRunningContainerIdsByNamePrefix($prefix) |
69
|
|
|
{ |
70
|
1 |
|
return $this->psPrefixImpl($prefix); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string|string[] $idOrIds |
75
|
|
|
* |
76
|
|
|
* @return int |
77
|
|
|
*/ |
78
|
|
|
public function kill($idOrIds) |
79
|
|
|
{ |
80
|
1 |
|
return $this->exec->capture('docker', Lib::merge('kill', func_get_args())); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string|string[] $idOrIds |
85
|
|
|
* |
86
|
|
|
* @return int |
87
|
|
|
*/ |
88
|
|
|
public function remove($idOrIds) |
89
|
|
|
{ |
90
|
1 |
|
return $this->exec->capture('docker', Lib::merge('rm', func_get_args())); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $name |
95
|
|
|
*/ |
96
|
|
|
public function zapContainersByName($name) |
97
|
|
|
{ |
98
|
2 |
|
$ids = $this->findAllContainerIdsByName($name); |
99
|
2 |
|
if (!$ids) { |
100
|
1 |
|
return; |
101
|
|
|
} |
102
|
1 |
|
$this->zap($ids); |
103
|
1 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string|string[] $idOrIds |
107
|
|
|
*/ |
108
|
|
|
public function zap($idOrIds) |
109
|
|
|
{ |
110
|
1 |
|
$this->kill($idOrIds); |
111
|
1 |
|
$this->remove($idOrIds); |
112
|
1 |
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string $prefix |
116
|
|
|
* @param bool $all |
117
|
|
|
* |
118
|
|
|
* @return null|array |
119
|
|
|
*/ |
120
|
|
|
private function psPrefixImpl($prefix, $all = false) |
121
|
|
|
{ |
122
|
3 |
|
$ids = null; |
123
|
|
|
|
124
|
3 |
|
if (0 === preg_match('(^[a-zA-Z0-9][a-zA-Z0-9_.-]+$)', $prefix)) { |
125
|
1 |
|
throw new \InvalidArgumentException( |
126
|
1 |
|
sprintf('Invalid container name prefix "%s"', $prefix) |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
2 |
|
$status = $this->exec->capture( |
131
|
2 |
|
'docker', |
132
|
|
|
array( |
133
|
2 |
|
'ps', '-q' . ($all ? 'a' : ''), '--no-trunc', '--filter', |
134
|
2 |
|
"name=^/${prefix}", |
135
|
|
|
), |
136
|
2 |
|
$result |
137
|
|
|
); |
138
|
|
|
|
139
|
2 |
|
$status || $ids = Lib::lines($result); |
140
|
|
|
|
141
|
2 |
|
return $ids; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|