-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker.py
More file actions
288 lines (259 loc) · 10.1 KB
/
docker.py
File metadata and controls
288 lines (259 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import asyncio
import json
import httpx
from fastmcp import FastMCP
base_url = 'YOUR_DOCKER_MANAGER_BASE_URL'
token = 'YOUR_DOCKER_MANAGER_API_TOKEN'
# 初始化 FastMCP 服务器
app = FastMCP('docker')
@app.tool()
def pull_image(image_name: str):
"""
从指定的远程地址拉取镜像。
参数:image_name(字符串):镜像名称
返回:镜像详细信息和 status
"""
url = f'{base_url}/api/pull_image'
response = httpx.post(url, json={'image_name': image_name}, headers={'Authorization': token})
return response.json()
@app.tool()
def list_images():
"""
列出所有镜像。
参数:无
返回:镜像详细信息和 status
"""
url = f'{base_url}/api/list_images'
response = httpx.post(url, headers={'Authorization': token})
return response.json()
@app.tool()
def create_container(image_name: str, container_name: str, cmd: str = "", add_caps: list[str] = [], host_name: str = "", ports_map: list[str] = []):
"""
创建容器。
参数:image_name(字符串):镜像名称
container_name(字符串):容器名称
cmd(字符串):容器启动命令
add_caps(列表):添加的capabilities
host_name(字符串):主机名
ports_map(列表):端口映射(例如:["8080:8080"])
返回:容器详细信息和 status
"""
url = f'{base_url}/api/run_container'
response = httpx.post(url, json={'image_name': image_name, 'container_name': container_name, 'cmd': cmd, 'add_caps': add_caps, 'host_name': host_name, 'ports_map': ports_map}, headers={'Authorization': token})
return response.json()
@app.tool()
def run_container_by_compose(compose_file: str, other_files: str = "{}"):
"""
根据compose文件运行容器。
参数:compose_file(字符串):compose文件内容
other_files(json字符串):其他文件内容(包括Dockerfile或启动必需的脚本等),键名为文件名字符串,键值为文件内容字符串,没有则填空字符串
返回:容器详细信息和 status
"""
url = f'{base_url}/api/run_container_by_compose'
other_files_dict = {}
if other_files == "{}" or other_files == "":
other_files_dict = {}
else:
other_files_dict = json.loads(other_files)
assert isinstance(other_files_dict, dict), "other_files must be a json object"
request_data = {'compose_file': compose_file, 'other_files': other_files_dict, 'delete_temp_dir': False}
response = httpx.post(url, json=request_data, headers={'Authorization': token})
result = response.json()
return result
@app.tool()
def fetch_container_logs(container_name: str):
"""
获取容器日志。
参数:container_name(字符串):容器名称
返回:日志和 status
"""
url = f'{base_url}/api/fetch_container_logs'
response = httpx.post(url, json={'container_name': container_name}, headers={'Authorization': token})
return response.json()
@app.tool()
def stop_container(container_name: str):
"""
停止容器。
参数:container_name(字符串):容器名称
返回:状态
"""
url = f'{base_url}/api/stop_container'
response = httpx.post(url, json={'container_name': container_name}, headers={'Authorization': token})
return response.json()
@app.tool()
def start_container(container_name: str):
"""
启动容器。
参数:container_name(字符串):容器名称
返回:状态
"""
url = f'{base_url}/api/start_container'
response = httpx.post(url, json={'container_name': container_name}, headers={'Authorization': token})
return response.json()
@app.tool()
def restart_container(container_name: str):
"""
重启容器。
参数:container_name(字符串):容器名称
返回:状态
"""
url = f'{base_url}/api/restart_container'
response = httpx.post(url, json={'container_name': container_name}, headers={'Authorization': token})
return response.json()
@app.tool()
def remove_container(container_name: str):
"""
删除容器。
参数:container_name(字符串):容器名称
返回:状态
"""
url = f'{base_url}/api/delete_container'
response = httpx.post(url, json={'container_name': container_name}, headers={'Authorization': token})
return response.json()
@app.tool()
def fetch_container_info(container_name: str):
"""
获取容器信息。
参数:container_name(字符串):容器名称
返回:容器详细信息和 status
"""
url = f'{base_url}/api/fetch_container_info'
response = httpx.post(url, json={'container_name': container_name}, headers={'Authorization': token})
return response.json()
@app.tool()
def create_exec_session(container_name: str):
"""
创建一个新的交互式 shell 会话。推荐使用。
参数:container_name(字符串):容器名称
返回:包含 exec_session_id 的响应和 status
"""
url = f'{base_url}/api/create_exec_session'
response = httpx.post(url, json={'container_name': container_name}, headers={'Authorization': token})
return response.json()
@app.tool()
def execute_command_in_session(container_name: str, cmd: str, exec_session_id: str):
"""
在已存在的 shell 会话中执行命令。推荐使用。
参数:container_name(字符串):容器名称
cmd(字符串):要执行的命令
exec_session_id(字符串):会话ID(由 create_exec_session 返回)
返回:命令输出和 status
"""
url = f'{base_url}/api/execute_command_in_session'
response = httpx.post(url, json={'container_name': container_name, 'cmd': cmd, 'exec_session_id': exec_session_id}, headers={'Authorization': token})
return response.json()
@app.tool()
def close_exec_session(container_name: str, exec_session_id: str):
"""
关闭交互式 shell 会话。
参数:container_name(字符串):容器名称
exec_session_id(字符串):会话ID(由 create_exec_session 返回)
返回:状态
"""
url = f'{base_url}/api/close_exec_session'
response = httpx.post(url, json={'container_name': container_name, 'exec_session_id': exec_session_id}, headers={'Authorization': token})
return response.json()
@app.tool()
def get_more_session_output(container_name: str, exec_session_id: str):
"""
获取更多的交互式 shell 会话输出。当你认为仍有输出未被获取时,可以调用此接口。
参数:exec_session_id(字符串):会话ID(由 create_exec_session 返回)
container_name(字符串):容器名称
返回:会话输出
"""
url = f'{base_url}/api/get_more_session_output'
response = httpx.post(url, json={'exec_session_id': exec_session_id, 'container_name': container_name}, headers={'Authorization': token})
return response.json()
@app.tool()
async def wait_for_seconds(seconds: int):
"""
等待指定的秒数。
参数:seconds(整数):等待的秒数
返回:状态
"""
await asyncio.sleep(seconds)
return {"status": "success"}
@app.tool()
def create_network(network_name: str, is_internal: bool, subnet: str, ip_range: str, gateway: str):
"""
创建一个新的 Docker 网络。
参数:
network_name(字符串):网络名称
is_internal(布尔值):是否为内部网络
subnet(字符串):子网,例如 "172.20.0.0/16"
ip_range(字符串):IP 地址范围,例如 "172.20.10.0/24",可选
gateway(字符串):网关地址,例如 "172.20.0.1",可选
返回:网络创建结果和 status
"""
url = f'{base_url}/api/create_network'
response = httpx.post(url, json={
'network_name': network_name,
'is_internal': is_internal,
'subnet': subnet,
'ip_range': ip_range,
'gateway': gateway
}, headers={'Authorization': token})
return response.json()
@app.tool()
def delete_network(network_name: str):
"""
删除指定的 Docker 网络。
参数:network_name(字符串):网络名称
返回:删除结果和 status
"""
url = f'{base_url}/api/delete_network'
response = httpx.post(url, json={'network_name': network_name}, headers={'Authorization': token})
return response.json()
@app.tool()
def list_networks():
"""
列出所有 Docker 网络。
参数:无
返回:所有网络的详细信息和 status
"""
url = f'{base_url}/api/list_networks'
response = httpx.get(url, headers={'Authorization': token})
return response.json()
@app.tool()
def network_info(network_name: str):
"""
获取指定 Docker 网络的信息。
参数:network_name(字符串):网络名称
返回:网络详细信息和 status
"""
url = f'{base_url}/api/network_info'
response = httpx.post(url, json={'network_name': network_name}, headers={'Authorization': token})
return response.json()
@app.tool()
def connect_network(network_name: str, container_name: str, ipv4_address: str = ""):
"""
将容器连接到指定网络。
参数:
network_name(字符串):网络名称
container_name(字符串):容器名称
ipv4_address(字符串):容器的IPv4地址,可选
如果不提供IPv4地址,则使用Docker自动分配的地址。
返回:连接结果和 status
"""
url = f'{base_url}/api/connect_network'
response = httpx.post(url, json={
'network_name': network_name,
'container_name': container_name,
'ipv4_address': ipv4_address
}, headers={'Authorization': token})
return response.json()
@app.tool()
def disconnect_network(network_name: str, container_name: str):
"""
将容器从指定网络断开连接。
参数:
network_name(字符串):网络名称
container_name(字符串):容器名称
返回:断开结果和 status
"""
url = f'{base_url}/api/disconnect_network'
response = httpx.post(url, json={
'network_name': network_name,
'container_name': container_name
}, headers={'Authorization': token})
return response.json()