Page Menu
Home
WMGMC Issues
搜索
Configure Global Search
登录
Files
F16256
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
订阅
标记用于日后
授予令牌
Size
8 KB
Referenced Files
None
订阅者
None
View Options
diff --git a/data/Dockerfiles/dockerapi/server.py b/data/Dockerfiles/dockerapi/server.py
index b4f7d287..22eb8508 100644
--- a/data/Dockerfiles/dockerapi/server.py
+++ b/data/Dockerfiles/dockerapi/server.py
@@ -1,41 +1,54 @@
from flask import Flask
from flask_restful import Resource, Api
-from docker import APIClient
+from flask import jsonify
+import docker
-dockercli = APIClient(base_url='unix://var/run/docker.sock')
+docker_client = docker.DockerClient(base_url='unix://var/run/docker.sock')
app = Flask(__name__)
api = Api(app)
-class Containers(Resource):
+class containers_get(Resource):
def get(self):
- return dockercli.containers(all=True)
+ containers = {}
+ for container in docker_client.containers.list(all=True):
+ containers.update({container.attrs['Id']: container.attrs})
+ return containers
-class ContainerInfo(Resource):
+class container_get(Resource):
def get(self, container_id):
- return dockercli.containers(all=True, filters={"id": container_id})
-
-class ContainerStart(Resource):
- def post(self, container_id):
- try:
- dockercli.start(container_id);
- except:
- return 'Error'
+ if container_id and container_id.isalnum():
+ for container in docker_client.containers.list(all=True, filters={"id": container_id}):
+ return container.attrs
else:
- return 'OK'
+ return jsonify(message='No or invalid id defined')
-class ContainerStop(Resource):
- def post(self, container_id):
- try:
- dockercli.stop(container_id);
- except:
- return 'Error'
+class container_post(Resource):
+ def post(self, container_id, post_action):
+ if container_id and container_id.isalnum() and post_action:
+ if post_action == 'stop':
+ try:
+ for container in docker_client.containers.list(all=True, filters={"id": container_id}):
+ container.stop()
+ except:
+ return 'Error'
+ else:
+ return 'OK'
+ elif post_action == 'start':
+ try:
+ for container in docker_client.containers.list(all=True, filters={"id": container_id}):
+ container.start()
+ except:
+ return 'Error'
+ else:
+ return 'OK'
+ else:
+ return jsonify(message='Invalid action')
else:
- return 'OK'
+ return jsonify(message='Invalid container id or missing action')
-api.add_resource(Containers, '/info/container/all')
-api.add_resource(ContainerInfo, '/info/container/<string:container_id>')
-api.add_resource(ContainerStop, '/stop/container/<string:container_id>')
-api.add_resource(ContainerStart, '/start/container/<string:container_id>')
+api.add_resource(containers_get, '/containers/json')
+api.add_resource(container_get, '/containers/<string:container_id>/json')
+api.add_resource(container_post, '/containers/<string:container_id>/<string:post_action>')
if __name__ == '__main__':
app.run(debug=False, host='0.0.0.0', port='8080')
diff --git a/data/web/inc/call_sogo_ctrl.php b/data/web/inc/call_sogo_ctrl.php
index 88f50419..c54a0a5a 100644
--- a/data/web/inc/call_sogo_ctrl.php
+++ b/data/web/inc/call_sogo_ctrl.php
@@ -1,121 +1,121 @@
<?php
session_start();
$AuthUsers = array("admin");
if (!isset($_SESSION['mailcow_cc_role']) OR !in_array($_SESSION['mailcow_cc_role'], $AuthUsers)) {
echo "Not allowed." . PHP_EOL;
exit();
}
function docker($service_name, $action, $post_action = null, $post_fields = null) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPHEADER,array( 'Content-Type: application/json' ));
switch($action) {
case 'get_id':
- curl_setopt($curl, CURLOPT_URL, 'http://dockerapi:8080/info/container/all');
+ curl_setopt($curl, CURLOPT_URL, 'http://dockerapi:8080/containers/json');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 0);
$response = curl_exec($curl);
if ($response === false) {
$err = curl_error($curl);
curl_close($curl);
return $err;
}
else {
curl_close($curl);
$containers = json_decode($response, true);
if (!empty($containers)) {
foreach ($containers as $container) {
- if ($container['Labels']['com.docker.compose.service'] == $service_name) {
+ if ($container['Config']['Labels']['com.docker.compose.service'] == $service_name) {
return trim($container['Id']);
}
}
}
}
return false;
break;
case 'info':
$container_id = docker($service_name, 'get_id');
if (ctype_xdigit($container_id)) {
- curl_setopt($curl, CURLOPT_URL, 'http://dockerapi:8080/info/container/' . $container_id);
+ curl_setopt($curl, CURLOPT_URL, 'http://dockerapi:8080/containers/' . $container_id . '/json');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 0);
$response = curl_exec($curl);
if ($response === false) {
$err = curl_error($curl);
curl_close($curl);
return $err;
}
else {
curl_close($curl);
if (empty($response)) {
return true;
}
else {
return json_decode($response, true);
}
}
}
else {
return false;
}
break;
case 'post':
if (!empty($post_action)) {
$container_id = docker($service_name, 'get_id');
if (ctype_xdigit($container_id) && ctype_alnum($post_action)) {
- curl_setopt($curl, CURLOPT_URL, 'http://dockerapi:8080/' . $post_action . '/container/' . $container_id);
+ curl_setopt($curl, CURLOPT_URL, 'http://dockerapi:8080/containers/' . $container_id . '/' . $post_action);
curl_setopt($curl, CURLOPT_POST, 1);
if (!empty($post_fields)) {
curl_setopt( $curl, CURLOPT_POSTFIELDS, json_encode($post_fields));
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($curl);
if ($response === false) {
$err = curl_error($curl);
curl_close($curl);
return $err;
}
else {
curl_close($curl);
if (empty($response)) {
return true;
}
else {
return $response;
}
}
}
}
break;
}
}
if ($_GET['ACTION'] == "start") {
$retry = 0;
- while (docker('sogo-mailcow', 'info')[0]['State'] != "running" && $retry <= 3) {
+ while (docker('sogo-mailcow', 'info')['State']['Running'] != 1 && $retry <= 3) {
$response = docker('sogo-mailcow', 'post', 'start');
$last_response = (trim($response) == "\"OK\"") ? '<b><span class="pull-right text-success">OK</span></b>' : '<b><span class="pull-right text-danger">Error: ' . $response . '</span></b>';
if (trim($response) == "\"OK\"") {
break;
}
usleep(1500000);
$retry++;
}
- echo (!isset($last_response)) ? '<b><span class="pull-right text-warning">Not running</span></b>' : $last_response;
+ echo (!isset($last_response)) ? '<b><span class="pull-right text-warning">Already running</span></b>' : $last_response;
}
if ($_GET['ACTION'] == "stop") {
$retry = 0;
- while (docker('sogo-mailcow', 'info')[0]['State'] == "running" && $retry <= 3) {
+ while (docker('sogo-mailcow', 'info')['State']['Running'] == 1 && $retry <= 3) {
$response = docker('sogo-mailcow', 'post', 'stop');
$last_response = (trim($response) == "\"OK\"") ? '<b><span class="pull-right text-success">OK</span></b>' : '<b><span class="pull-right text-danger">Error: ' . $response . '</span></b>';
if (trim($response) == "\"OK\"") {
break;
}
usleep(1500000);
$retry++;
}
echo (!isset($last_response)) ? '<b><span class="pull-right text-warning">Not running</span></b>' : $last_response;
}
?>
File Metadata
详情
附加的
Mime Type
text/x-diff
Expires
9月 12 Fri, 2:56 AM (22 h, 43 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5713
默认替代文本
(8 KB)
Attached To
Mode
rMAILCOW mailcow-tracking
附加的
Detach File
Event Timeline
Log In to Comment