blob: e47f5e2f2f8605ce2f9c4bbc9d65e10a7ec650fe [file] [log] [blame]
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001<?php
2function docker($action, $service_name = null, $attr1 = null, $attr2 = null, $extra_headers = null) {
3 global $DOCKER_TIMEOUT;
4 $curl = curl_init();
5 curl_setopt($curl, CURLOPT_HTTPHEADER,array('Content-Type: application/json' ));
6 // We are using our mail certificates for dockerapi, the names will not match, the certs are trusted anyway
7 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
8 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
9 switch($action) {
10 case 'get_id':
11 curl_setopt($curl, CURLOPT_URL, 'https://dockerapi:443/containers/json');
12 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
13 curl_setopt($curl, CURLOPT_POST, 0);
14 curl_setopt($curl, CURLOPT_TIMEOUT, $DOCKER_TIMEOUT);
15 $response = curl_exec($curl);
16 if ($response === false) {
17 $err = curl_error($curl);
18 curl_close($curl);
19 return $err;
20 }
21 else {
22 curl_close($curl);
23 $containers = json_decode($response, true);
24 if (!empty($containers)) {
25 foreach ($containers as $container) {
26 if (isset($container['Config']['Labels']['com.docker.compose.service'])
27 && $container['Config']['Labels']['com.docker.compose.service'] == $service_name
28 && strtolower($container['Config']['Labels']['com.docker.compose.project']) == strtolower(getenv('COMPOSE_PROJECT_NAME'))) {
29 return trim($container['Id']);
30 }
31 }
32 }
33 }
34 return false;
35 case 'containers':
36 curl_setopt($curl, CURLOPT_URL, 'https://dockerapi:443/containers/json');
37 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
38 curl_setopt($curl, CURLOPT_POST, 0);
39 curl_setopt($curl, CURLOPT_TIMEOUT, $DOCKER_TIMEOUT);
40 $response = curl_exec($curl);
41 if ($response === false) {
42 $err = curl_error($curl);
43 curl_close($curl);
44 return $err;
45 }
46 else {
47 curl_close($curl);
48 $containers = json_decode($response, true);
49 if (!empty($containers)) {
50 foreach ($containers as $container) {
51 if (strtolower($container['Config']['Labels']['com.docker.compose.project']) == strtolower(getenv('COMPOSE_PROJECT_NAME'))) {
52 $out[$container['Config']['Labels']['com.docker.compose.service']]['State'] = $container['State'];
53 $out[$container['Config']['Labels']['com.docker.compose.service']]['Config'] = $container['Config'];
54 }
55 }
56 }
57 return (!empty($out)) ? $out : false;
58 }
59 return false;
60 break;
61 case 'info':
62 if (empty($service_name)) {
63 curl_setopt($curl, CURLOPT_URL, 'https://dockerapi:443/containers/json');
64 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
65 curl_setopt($curl, CURLOPT_POST, 0);
66 curl_setopt($curl, CURLOPT_TIMEOUT, $DOCKER_TIMEOUT);
67 }
68 else {
69 $container_id = docker('get_id', $service_name);
70 if (ctype_xdigit($container_id)) {
71 curl_setopt($curl, CURLOPT_URL, 'https://dockerapi:443/containers/' . $container_id . '/json');
72 }
73 else {
74 return false;
75 }
76 }
77 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
78 curl_setopt($curl, CURLOPT_POST, 0);
79 curl_setopt($curl, CURLOPT_TIMEOUT, $DOCKER_TIMEOUT);
80 $response = curl_exec($curl);
81 if ($response === false) {
82 $err = curl_error($curl);
83 curl_close($curl);
84 return $err;
85 }
86 else {
87 curl_close($curl);
88 $decoded_response = json_decode($response, true);
89 if (!empty($decoded_response)) {
90 if (empty($service_name)) {
91 foreach ($decoded_response as $container) {
92 if (isset($container['Config']['Labels']['com.docker.compose.project'])
93 && strtolower($container['Config']['Labels']['com.docker.compose.project']) == strtolower(getenv('COMPOSE_PROJECT_NAME'))) {
94 unset($container['Config']['Env']);
95 $out[$container['Config']['Labels']['com.docker.compose.service']]['State'] = $container['State'];
96 $out[$container['Config']['Labels']['com.docker.compose.service']]['Config'] = $container['Config'];
97 }
98 }
99 }
100 else {
101 if (isset($decoded_response['Config']['Labels']['com.docker.compose.project'])
102 && strtolower($decoded_response['Config']['Labels']['com.docker.compose.project']) == strtolower(getenv('COMPOSE_PROJECT_NAME'))) {
103 unset($container['Config']['Env']);
104 $out[$decoded_response['Config']['Labels']['com.docker.compose.service']]['State'] = $decoded_response['State'];
105 $out[$decoded_response['Config']['Labels']['com.docker.compose.service']]['Config'] = $decoded_response['Config'];
106 }
107 }
108 }
109 if (empty($response)) {
110 return true;
111 }
112 else {
113 return (!empty($out)) ? $out : false;
114 }
115 }
116 break;
117 case 'post':
118 if (!empty($attr1)) {
119 $container_id = docker('get_id', $service_name);
120 if (ctype_xdigit($container_id) && ctype_alnum($attr1)) {
121 curl_setopt($curl, CURLOPT_URL, 'https://dockerapi:443/containers/' . $container_id . '/' . $attr1);
122 curl_setopt($curl, CURLOPT_POST, 1);
123 curl_setopt($curl, CURLOPT_TIMEOUT, $DOCKER_TIMEOUT);
124 if (!empty($attr2)) {
125 curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($attr2));
126 }
127 if (!empty($extra_headers) && is_array($extra_headers)) {
128 curl_setopt($curl, CURLOPT_HTTPHEADER, $extra_headers);
129 }
130 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
131 $response = curl_exec($curl);
132 if ($response === false) {
133 $err = curl_error($curl);
134 curl_close($curl);
135 return $err;
136 }
137 else {
138 curl_close($curl);
139 if (empty($response)) {
140 return true;
141 }
142 else {
143 return $response;
144 }
145 }
146 }
147 }
148 break;
149 }
150}