public
nobgit
read
NobMail
Based on mailcow: dockerized
Languages
Repository composition by tracked source files.
PHP
49%
JavaScript
35%
HTML
9%
CSS
4%
Shell
2%
Python
1%
Lua
0%
Perl
0%
Ruby
0%
SCSS
0%
Create file
Wiki Documentation
Clone
https://nobgit.com/orgs/nobgit/nobmail.git
ssh://[email protected]:2222/orgs/nobgit/nobmail.git
Trace
data/web/inc/functions.docker.inc.php
Trace helps you understand code history line by line. See who changed each line, when it changed, and which commit introduced it.
Author
Date
Commit
Line
Code
1
<?php
2
function docker($action, $service_name = null, $attr1 = null, $attr2 = null, $extra_headers = null) {
3
global $DOCKER_TIMEOUT;
4
global $redis;
5
$curl = curl_init();
6
curl_setopt($curl, CURLOPT_HTTPHEADER,array('Content-Type: application/json' ));
7
// We are using our mail certificates for dockerapi, the names will not match, the certs are trusted anyway
8
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
9
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
10
switch($action) {
11
case 'get_id':
12
curl_setopt($curl, CURLOPT_URL, 'https://dockerapi:443/containers/json');
13
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
14
curl_setopt($curl, CURLOPT_POST, 0);
15
curl_setopt($curl, CURLOPT_TIMEOUT, $DOCKER_TIMEOUT);
16
$response = curl_exec($curl);
17
if ($response === false) {
18
$err = curl_error($curl);
19
curl_close($curl);
20
return $err;
21
}
22
else {
23
curl_close($curl);
24
$containers = json_decode($response, true);
25
if (!empty($containers)) {
26
foreach ($containers as $container) {
27
if (isset($container['Config']['Labels']['com.docker.compose.service'])
28
&& $container['Config']['Labels']['com.docker.compose.service'] == $service_name
29
&& strtolower($container['Config']['Labels']['com.docker.compose.project']) == strtolower(getenv('COMPOSE_PROJECT_NAME'))) {
30
return trim($container['Id']);
31
}
32
}
33
}
34
}
35
return false;
36
break;
37
case 'containers':
38
curl_setopt($curl, CURLOPT_URL, 'https://dockerapi:443/containers/json');
39
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
40
curl_setopt($curl, CURLOPT_POST, 0);
41
curl_setopt($curl, CURLOPT_TIMEOUT, $DOCKER_TIMEOUT);
42
$response = curl_exec($curl);
43
if ($response === false) {
44
$err = curl_error($curl);
45
curl_close($curl);
46
return $err;
47
}
48
else {
49
curl_close($curl);
50
$containers = json_decode($response, true);
51
if (!empty($containers)) {
52
foreach ($containers as $container) {
53
if (strtolower($container['Config']['Labels']['com.docker.compose.project']) == strtolower(getenv('COMPOSE_PROJECT_NAME'))) {
54
$out[$container['Config']['Labels']['com.docker.compose.service']]['State'] = $container['State'];
55
$out[$container['Config']['Labels']['com.docker.compose.service']]['Config'] = $container['Config'];
56
$out[$container['Config']['Labels']['com.docker.compose.service']]['Id'] = trim($container['Id']);
57
}
58
}
59
}
60
return (!empty($out)) ? $out : false;
61
}
62
return false;
63
break;
64
case 'info':
65
if (empty($service_name)) {
66
curl_setopt($curl, CURLOPT_URL, 'https://dockerapi:443/containers/json?all=true');
67
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
68
curl_setopt($curl, CURLOPT_POST, 0);
69
curl_setopt($curl, CURLOPT_TIMEOUT, $DOCKER_TIMEOUT);
70
}
71
else {
72
$container_id = docker('get_id', $service_name);
73
if (ctype_xdigit($container_id)) {
74
curl_setopt($curl, CURLOPT_URL, 'https://dockerapi:443/containers/' . $container_id . '/json');
75
}
76
else {
77
return false;
78
}
79
}
80
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
81
curl_setopt($curl, CURLOPT_POST, 0);
82
curl_setopt($curl, CURLOPT_TIMEOUT, $DOCKER_TIMEOUT);
83
$response = curl_exec($curl);
84
if ($response === false) {
85
$err = curl_error($curl);
86
curl_close($curl);
87
return $err;
88
}
89
else {
90
curl_close($curl);
91
$decoded_response = json_decode($response, true);
92
if (!empty($decoded_response)) {
93
if (empty($service_name)) {
94
foreach ($decoded_response as $container) {
95
if (isset($container['Config']['Labels']['com.docker.compose.project'])
96
&& strtolower($container['Config']['Labels']['com.docker.compose.project']) == strtolower(getenv('COMPOSE_PROJECT_NAME'))) {
97
unset($container['Config']['Env']);
98
$out[$container['Config']['Labels']['com.docker.compose.service']]['State'] = $container['State'];
99
$out[$container['Config']['Labels']['com.docker.compose.service']]['Config'] = $container['Config'];
100
$out[$container['Config']['Labels']['com.docker.compose.service']]['Id'] = trim($container['Id']);
101
}
102
}
103
}
104
else {
105
if (isset($decoded_response['Config']['Labels']['com.docker.compose.project'])
106
&& strtolower($decoded_response['Config']['Labels']['com.docker.compose.project']) == strtolower(getenv('COMPOSE_PROJECT_NAME'))) {
107
unset($container['Config']['Env']);
108
$out[$decoded_response['Config']['Labels']['com.docker.compose.service']]['State'] = $decoded_response['State'];
109
$out[$decoded_response['Config']['Labels']['com.docker.compose.service']]['Config'] = $decoded_response['Config'];
110
$out[$decoded_response['Config']['Labels']['com.docker.compose.service']]['Id'] = trim($decoded_response['Id']);
111
}
112
}
113
}
114
if (empty($response)) {
115
return true;
116
}
117
else {
118
return (!empty($out)) ? $out : false;
119
}
120
}
121
break;
122
case 'post':
123
if (!empty($attr1)) {
124
$container_id = docker('get_id', $service_name);
125
if (ctype_xdigit($container_id) && ctype_alnum($attr1)) {
126
curl_setopt($curl, CURLOPT_URL, 'https://dockerapi:443/containers/' . $container_id . '/' . $attr1);
127
curl_setopt($curl, CURLOPT_POST, 1);
128
curl_setopt($curl, CURLOPT_TIMEOUT, $DOCKER_TIMEOUT);
129
if (!empty($attr2)) {
130
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($attr2));
131
}
132
if (!empty($extra_headers) && is_array($extra_headers)) {
133
curl_setopt($curl, CURLOPT_HTTPHEADER, $extra_headers);
134
}
135
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
136
$response = curl_exec($curl);
137
if ($response === false) {
138
$err = curl_error($curl);
139
curl_close($curl);
140
return $err;
141
}
142
else {
143
curl_close($curl);
144
if (empty($response)) {
145
return true;
146
}
147
else {
148
return $response;
149
}
150
}
151
}
152
}
153
break;
154
case 'container_stats':
155
if (empty($service_name)){
156
return false;
157
}
159
$container_id = $service_name;
160
curl_setopt($curl, CURLOPT_URL, 'https://dockerapi:443/container/' . $container_id . '/stats/update');
161
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
162
curl_setopt($curl, CURLOPT_POST, 1);
163
curl_setopt($curl, CURLOPT_TIMEOUT, $DOCKER_TIMEOUT);
164
$response = curl_exec($curl);
165
if ($response === false) {
166
$err = curl_error($curl);
167
curl_close($curl);
168
return $err;
169
}
170
else {
171
curl_close($curl);
172
$stats = json_decode($response, true);
173
if (!empty($stats)) return $stats;
174
}
175
return false;
176
break;
177
case 'host_stats':
178
curl_setopt($curl, CURLOPT_URL, 'https://dockerapi:443/host/stats');
179
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
180
curl_setopt($curl, CURLOPT_POST, 0);
181
curl_setopt($curl, CURLOPT_TIMEOUT, $DOCKER_TIMEOUT);
182
$response = curl_exec($curl);
183
if ($response === false) {
184
$err = curl_error($curl);
185
curl_close($curl);
186
return $err;
187
}
188
else {
189
curl_close($curl);
190
$stats = json_decode($response, true);
191
if (!empty($stats)) return $stats;
192
}
193
return false;
194
break;
195
case 'broadcast':
196
$request = array(
197
"api_call" => "container_post",
198
"container_name" => $service_name,
199
"post_action" => $attr1,
200
"request" => $attr2
201
);
203
$redis->publish("MC_CHANNEL", json_encode($request));
204
return true;
205
break;
206
}
207
}