Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,50 +1,153 @@
|
|
1 |
import gradio as gr
|
2 |
import docker
|
3 |
import os
|
4 |
-
from docker.errors import
|
5 |
|
6 |
-
#
|
7 |
client = docker.from_env()
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
return "Invalid API Key"
|
12 |
-
|
13 |
try:
|
14 |
-
|
15 |
-
|
16 |
|
17 |
-
|
18 |
-
image, logs = client.images.build(path=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
return f"
|
|
|
|
|
|
|
|
|
24 |
except Exception as e:
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import docker
|
3 |
import os
|
4 |
+
from docker.errors import DockerException, BuildError, APIError
|
5 |
|
6 |
+
# Docker client setup
|
7 |
client = docker.from_env()
|
8 |
|
9 |
+
# Function to build Docker image with status update and error handling
|
10 |
+
def build_docker_image(dockerfile, context_directory, api_key, status_callback):
|
|
|
|
|
11 |
try:
|
12 |
+
if api_key != os.getenv("DOCKER_API_KEY"):
|
13 |
+
return "Invalid API Key"
|
14 |
|
15 |
+
status_callback("Building image...")
|
16 |
+
image, logs = client.images.build(path=context_directory, dockerfile=dockerfile, rm=True)
|
17 |
+
status_callback(f"Image built successfully: {image.tags[0]}")
|
18 |
+
return f"Image built successfully: {image.tags[0]}"
|
19 |
+
|
20 |
+
except BuildError as e:
|
21 |
+
status_callback(f"Build error: {e}")
|
22 |
+
return f"Build error: {e}"
|
23 |
+
except APIError as e:
|
24 |
+
status_callback(f"Docker API error: {e}")
|
25 |
+
return f"Docker API error: {e}"
|
26 |
+
except Exception as e:
|
27 |
+
status_callback(f"Unexpected error: {e}")
|
28 |
+
return f"Unexpected error: {e}"
|
29 |
+
|
30 |
+
# Function to pull an image from Docker Hub with error handling
|
31 |
+
def pull_image_from_dockerhub(image_name, api_key, status_callback):
|
32 |
+
try:
|
33 |
+
if api_key != os.getenv("DOCKER_API_KEY"):
|
34 |
+
return "Invalid API Key"
|
35 |
|
36 |
+
status_callback(f"Pulling image: {image_name}...")
|
37 |
+
image = client.images.pull(image_name)
|
38 |
+
status_callback(f"Image {image_name} pulled successfully: {image.id}")
|
39 |
+
return f"Image {image_name} pulled successfully: {image.id}"
|
40 |
+
|
41 |
+
except APIError as e:
|
42 |
+
status_callback(f"Docker API error: {e}")
|
43 |
+
return f"Docker API error: {e}"
|
44 |
except Exception as e:
|
45 |
+
status_callback(f"Unexpected error: {e}")
|
46 |
+
return f"Unexpected error: {e}"
|
47 |
+
|
48 |
+
# Function to start a container with error handling
|
49 |
+
def start_container(image_name, status_callback):
|
50 |
+
try:
|
51 |
+
status_callback(f"Starting container with image: {image_name}...")
|
52 |
+
container = client.containers.run(image_name, detach=True)
|
53 |
+
status_callback(f"Container {container.id} started successfully")
|
54 |
+
return f"Container {container.id} started successfully"
|
55 |
+
except DockerException as e:
|
56 |
+
status_callback(f"Docker error: {e}")
|
57 |
+
return f"Docker error: {e}"
|
58 |
+
except Exception as e:
|
59 |
+
status_callback(f"Unexpected error: {e}")
|
60 |
+
return f"Unexpected error: {e}"
|
61 |
+
|
62 |
+
# Function to stop a container with error handling
|
63 |
+
def stop_container(container_id, status_callback):
|
64 |
+
try:
|
65 |
+
status_callback(f"Stopping container {container_id}...")
|
66 |
+
container = client.containers.get(container_id)
|
67 |
+
container.stop()
|
68 |
+
status_callback(f"Container {container.id} stopped successfully")
|
69 |
+
return f"Container {container.id} stopped successfully"
|
70 |
+
except docker.errors.NotFound as e:
|
71 |
+
status_callback(f"Error: Container {container_id} not found")
|
72 |
+
return f"Error: Container {container_id} not found"
|
73 |
+
except DockerException as e:
|
74 |
+
status_callback(f"Docker error: {e}")
|
75 |
+
return f"Docker error: {e}"
|
76 |
+
except Exception as e:
|
77 |
+
status_callback(f"Unexpected error: {e}")
|
78 |
+
return f"Unexpected error: {e}"
|
79 |
+
|
80 |
+
# Tab 1 - Build Docker Image with Status Visualization
|
81 |
+
def docker_build_tab():
|
82 |
+
with gr.Column():
|
83 |
+
dockerfile_input = gr.Textbox(label="Dockerfile Content", lines=10)
|
84 |
+
context_directory_input = gr.Textbox(label="Context Directory", placeholder="Enter directory path", lines=1)
|
85 |
+
api_key_input = gr.Textbox(label="Docker API Key", type="password")
|
86 |
+
build_button = gr.Button("Build Image")
|
87 |
+
build_output = gr.Textbox(label="Build Output", interactive=False)
|
88 |
+
status_output = gr.Textbox(label="Status", interactive=False)
|
89 |
+
|
90 |
+
build_button.click(build_docker_image, inputs=[dockerfile_input, context_directory_input, api_key_input, status_output], outputs=build_output)
|
91 |
+
|
92 |
+
return dockerfile_input, context_directory_input, api_key_input, build_button, build_output, status_output
|
93 |
+
|
94 |
+
# Tab 2 - Pull Docker Image from Docker Hub with Status Visualization
|
95 |
+
def docker_pull_tab():
|
96 |
+
with gr.Column():
|
97 |
+
image_name_input = gr.Textbox(label="Docker Image (e.g., python:3.8)", lines=1)
|
98 |
+
api_key_input = gr.Textbox(label="Docker API Key", type="password")
|
99 |
+
pull_button = gr.Button("Pull Image")
|
100 |
+
pull_output = gr.Textbox(label="Pull Output", interactive=False)
|
101 |
+
status_output = gr.Textbox(label="Status", interactive=False)
|
102 |
+
|
103 |
+
pull_button.click(pull_image_from_dockerhub, inputs=[image_name_input, api_key_input, status_output], outputs=pull_output)
|
104 |
+
|
105 |
+
return image_name_input, api_key_input, pull_button, pull_output, status_output
|
106 |
+
|
107 |
+
# Tab 3 - Container Management with Status Visualization
|
108 |
+
def container_management_tab():
|
109 |
+
with gr.Column():
|
110 |
+
start_image_input = gr.Textbox(label="Image Name", lines=1)
|
111 |
+
start_button = gr.Button("Start Container")
|
112 |
+
start_output = gr.Textbox(label="Start Output", interactive=False)
|
113 |
+
status_output = gr.Textbox(label="Status", interactive=False)
|
114 |
+
|
115 |
+
stop_container_input = gr.Textbox(label="Container ID to Stop", lines=1)
|
116 |
+
stop_button = gr.Button("Stop Container")
|
117 |
+
stop_output = gr.Textbox(label="Stop Output", interactive=False)
|
118 |
+
|
119 |
+
start_button.click(start_container, inputs=[start_image_input, status_output], outputs=start_output)
|
120 |
+
stop_button.click(stop_container, inputs=[stop_container_input, status_output], outputs=stop_output)
|
121 |
+
|
122 |
+
return start_image_input, start_button, start_output, stop_container_input, stop_button, stop_output, status_output
|
123 |
+
|
124 |
+
# Tab 4 - Code Editor
|
125 |
+
def code_editor_tab():
|
126 |
+
with gr.Column():
|
127 |
+
code_input = gr.Textbox(label="Code Editor", lines=15, interactive=True)
|
128 |
+
save_button = gr.Button("Save Code")
|
129 |
+
save_output = gr.Textbox(label="Save Output", interactive=False)
|
130 |
+
|
131 |
+
save_button.click(lambda code: "Code saved successfully" if code else "No code to save", inputs=code_input, outputs=save_output)
|
132 |
+
|
133 |
+
return code_input, save_button, save_output
|
134 |
+
|
135 |
+
# Gradio Interface Setup
|
136 |
+
def main():
|
137 |
+
with gr.Blocks() as demo:
|
138 |
+
gr.Markdown("### Docker Management Gradio App")
|
139 |
+
|
140 |
+
with gr.Tabs():
|
141 |
+
with gr.TabItem("Build Docker Image"):
|
142 |
+
docker_build_tab()
|
143 |
+
with gr.TabItem("Pull Docker Image from Docker Hub"):
|
144 |
+
docker_pull_tab()
|
145 |
+
with gr.TabItem("Container Management"):
|
146 |
+
container_management_tab()
|
147 |
+
with gr.TabItem("Code Editor"):
|
148 |
+
code_editor_tab()
|
149 |
+
|
150 |
+
demo.launch()
|
151 |
+
|
152 |
+
if __name__ == "__main__":
|
153 |
+
main()
|