Azeez98 commited on
Commit
3b509f1
·
verified ·
1 Parent(s): f67d201

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +277 -277
app.py CHANGED
@@ -1,277 +1,277 @@
1
- import os
2
- import subprocess
3
- import tarfile
4
- import asyncio
5
- from fastapi import FastAPI, HTTPException, Form, UploadFile, File, Depends, Query, Response
6
- from fastapi.responses import HTMLResponse, FileResponse, StreamingResponse
7
- from pydantic import BaseModel
8
- from tempfile import NamedTemporaryFile
9
- from typing import List
10
- from pydantic import BaseModel
11
-
12
- app = FastAPI()
13
-
14
- REQUIREMENTS_FILE = "requirements1.txt"
15
-
16
- class DockerImageParams(BaseModel):
17
- image_name: str
18
- tag: str = 'latest'
19
-
20
- app = FastAPI()
21
-
22
- async def stream_log(file_path):
23
- with open(file_path, 'r') as file:
24
- while True:
25
- line = file.readline()
26
- if line:
27
- yield line
28
- else:
29
- await asyncio.sleep(0.1)
30
-
31
- @app.post("/download-dependencies")
32
- async def download_dependencies(requirements_file: UploadFile = File(...)):
33
- try:
34
- with NamedTemporaryFile(delete=False) as tmp:
35
- tmp.write(requirements_file.file.read())
36
- tmp_path = tmp.name
37
-
38
- # Ensure the directories exist
39
- os.makedirs("/tmp/dependencies", exist_ok=True)
40
-
41
- log_file_path = "/tmp/dependencies/download.log"
42
- with open(log_file_path, "w") as log_file:
43
- # Download dependencies
44
- result = subprocess.run(
45
- ["pip", "download", "-r", tmp_path, "-d", "/tmp/dependencies"],
46
- stdout=log_file,
47
- stderr=log_file
48
- )
49
-
50
- if result.returncode != 0:
51
- raise HTTPException(status_code=500, detail="Error downloading dependencies. See log file for details.")
52
-
53
- # Create a tar file
54
- tar_path = "/tmp/dependencies.tar.gz"
55
- with tarfile.open(tar_path, "w:gz") as tar:
56
- for root, _, files in os.walk("/tmp/dependencies"):
57
- for file in files:
58
- file_path = os.path.join(root, file)
59
- tar.add(file_path, arcname=file)
60
- tar.add(log_file_path, arcname="download.log")
61
-
62
- return StreamingResponse(stream_log(log_file_path), media_type="text/plain")
63
-
64
- except subprocess.CalledProcessError as e:
65
- raise HTTPException(status_code=500, detail=f"Error downloading dependencies: {str(e)}")
66
- except Exception as e:
67
- raise HTTPException(status_code=500, detail=str(e))
68
- finally:
69
- os.remove(tmp_path)
70
-
71
- class DockerImageParams(BaseModel):
72
- image_name: str
73
- tag: str = 'latest'
74
-
75
- def download_docker_image(image_name, tag='latest', destination='/tmp/docker-images'):
76
- try:
77
- os.makedirs(destination, exist_ok=True)
78
-
79
- tar_path = os.path.join(destination, f'{image_name.replace("/", "_")}.tar')
80
-
81
- # Remove the existing tar file if it exists
82
- if os.path.exists(tar_path):
83
- os.remove(tar_path)
84
-
85
- # Skopeo command to copy the image as a tar file
86
- command = [
87
- 'skopeo', 'copy',
88
- f'docker://{image_name}:{tag}',
89
- f'docker-archive:{tar_path}'
90
- ]
91
- subprocess.run(command, check=True)
92
- print(f"Image '{image_name}:{tag}' downloaded successfully to {destination}.")
93
- return tar_path
94
- except subprocess.CalledProcessError as e:
95
- print(f"Error downloading image: {str(e)}")
96
- raise HTTPException(status_code=500, detail=f"Error downloading Docker image: {str(e)}")
97
-
98
- @app.get("/", response_class=HTMLResponse)
99
- async def read_root():
100
- html_content = """
101
- <html>
102
- <head>
103
- <title>Azeez's Help Desk</title>
104
- <link href src"/css/index.css" rel="stylesheet">
105
- <script src="js/index.js></script>
106
- </head>
107
- <body>
108
- <h1>Welcome to Azeez's Help Desk :)</h1>
109
-
110
- <div class="tab">
111
- <button class="tablink" onclick="openTab(event, 'Docker')" id="defaultOpen">Docker Image Download</button>
112
- <button class="tablink" onclick="openTab(event, 'Pip')">Pip Dependencies Download</button>
113
- <button class="tablink" onclick="openTab(event, 'Deb')">Debian Packages Download</button>
114
- </div>
115
-
116
- <div id="Docker" class="tabcontent">
117
- <h2>Docker Image Download</h2>
118
- <form onsubmit="handleDockerFormSubmit(event)">
119
- <label for="image_name">Docker Image Name:</label>
120
- <input type="text" id="image_name" name="image_name" required><br><br>
121
-
122
- <label for="tag">Docker Image Tag:</label>
123
- <input type="text" id="tag" name="tag" value="latest" required><br><br>
124
-
125
- <input type="submit" id="docker-submit-button" value="Download Docker Image">
126
- </form>
127
- <div id="docker-message" style="display: none; margin-top: 10px;"></div>
128
- </div>
129
-
130
- <div id="Pip" class="tabcontent">
131
- <h2>Pip Dependencies Download</h2>
132
- <form onsubmit="handlePipFormSubmit(event)">
133
- <label for="requirements_file">Requirements File:</label>
134
- <input type="file" id="requirements_file" name="requirements_file" accept=".txt" required><br><br>
135
-
136
- <input type="submit" id="pip-submit-button" value="Download Dependencies">
137
- </form>
138
- <div id="pip-message" style="display: none; margin-top: 10px;"></div>
139
- </div>
140
-
141
- <div id="Deb" class="tabcontent">
142
- <h2>Debian Packages Download</h2>
143
- <form onsubmit="handleDebFormSubmit(event)">
144
- <label for="deb_packages">Debian Package Names (comma-separated):</label>
145
- <input type="text" id="deb_packages" name="deb_packages" required><br><br>
146
-
147
- <input type="submit" id="deb-submit-button" value="Download Debian Packages">
148
- </form>
149
- <div id="deb-message" style="display: none; margin-top: 10px;"></div>
150
- </div>
151
- <div id="progress-container">
152
- <div id="progress-bar">0%</div>
153
- </div>
154
- </body>
155
- </html>
156
- """
157
- return HTMLResponse(content=html_content)
158
-
159
- @app.get("/download-docker-image")
160
- async def download_docker_image_endpoint(image_name: str = Query(...), tag: str = Query('latest')):
161
- tar_path = download_docker_image(image_name, tag)
162
- file_size = os.path.getsize(tar_path)
163
-
164
- def iterfile():
165
- with open(tar_path, 'rb') as file:
166
- while chunk := file.read(1024 * 1024): # Read in 1 MB chunks
167
- yield chunk
168
-
169
- headers = {
170
- "Content-Disposition": f'attachment; filename="{image_name.replace("/", "_")}.tar"',
171
- "Content-Length": str(file_size)
172
- }
173
-
174
- return StreamingResponse(iterfile(), media_type='application/x-tar', headers=headers)
175
-
176
- def create_tar_file(files_to_package: List[str], tar_filename: str, destination_dir: str):
177
- """
178
- Create a tar file containing specified files.
179
- Args:
180
- - files_to_package (list): List of paths to files to include in the tar file.
181
- - tar_filename (str): Name of the tar file to create.
182
- - destination_dir (str): Directory to save the tar file.
183
- """
184
- try:
185
- tar_path = os.path.join(destination_dir, tar_filename)
186
- with tarfile.open(tar_path, "w:gz") as tar:
187
- for file_path in files_to_package:
188
- tar.add(file_path, arcname=os.path.basename(file_path))
189
- print(f"Created tar file '{tar_filename}' successfully in '{destination_dir}'.")
190
- return tar_path
191
- except Exception as e:
192
- print(f"Error creating tar file: {e}")
193
- raise
194
-
195
- def download_deb_packages(package_names: List[str], destination_dir: str) -> List[str]:
196
- """
197
- Download Debian packages (`.deb`) and their dependencies using `apt-get download`.
198
- Args:
199
- - package_names (list): List of package names to download.
200
- - destination_dir (str): Directory to save downloaded packages.
201
- Returns:
202
- - List of paths to downloaded `.deb` packages.
203
- """
204
- try:
205
- # Create the destination directory if it doesn't exist
206
- os.makedirs(destination_dir, exist_ok=True)
207
-
208
- downloaded_packages = []
209
-
210
- # Download each package and its dependencies
211
- for package_name in package_names:
212
- # Run apt-get update to refresh package index
213
- # subprocess.run(['apt-get', 'update'], check=True)
214
- # Download the package to the destination directory
215
- subprocess.run(['apt-get', 'download', package_name, '-d', destination_dir], check=True)
216
- # Build the full path to the downloaded package
217
- deb_filename = f"{package_name}.deb"
218
- downloaded_packages.append(os.path.join(destination_dir, deb_filename))
219
-
220
- return downloaded_packages
221
-
222
- except subprocess.CalledProcessError as e:
223
- print(f"Error downloading packages: {e}")
224
- raise
225
-
226
- import subprocess
227
-
228
- def download_make_package(destination_dir):
229
- try:
230
- # Ensure the destination directory exists
231
- subprocess.run(['mkdir', '-p', destination_dir])
232
-
233
- # Download the make package
234
- result = subprocess.run(['apt-get', 'download', 'make', '-d', destination_dir], check=True)
235
-
236
- if result.returncode == 0:
237
- print(f"Downloaded make package and dependencies to {destination_dir} successfully.")
238
- else:
239
- print("Failed to download make package.")
240
-
241
- except subprocess.CalledProcessError as e:
242
- print(f"Error downloading packages: {e}")
243
-
244
- @app.post("/download-deb-packages")
245
- async def download_deb_packages_handler(deb_packages: str = Form(...)):
246
- try:
247
- destination_dir = '/tmp/downloaded_packages'
248
- subprocess.run(['mkdir', '-p', destination_dir]) # Ensure destination directory exists
249
-
250
- package_name='make'
251
-
252
- # Download the package using apt-get
253
- result = subprocess.run(['apt-get', 'install', package_name], check=True)
254
-
255
- # If download is successful, return the downloaded package
256
- if result.returncode == 0:
257
- tar_filename = f'{package_name}.tar.gz'
258
- tar_path = f'{destination_dir}/{tar_filename}'
259
- return FileResponse(tar_path, filename=tar_filename)
260
-
261
- # If download fails, raise HTTPException
262
- else:
263
- raise HTTPException(status_code=500, detail=f"Failed to download {package_name} package.")
264
-
265
- except subprocess.CalledProcessError as e:
266
- error_message = str(e.stderr)
267
- print(f"Error downloading packages: {error_message}")
268
- raise HTTPException(status_code=500, detail=f"Error downloading {package_name} package: {error_message}")
269
-
270
- except Exception as e:
271
- print(f"Error: {str(e)}")
272
- raise HTTPException(status_code=500, detail=f"Failed to download {package_name} package: {str(e)}")
273
-
274
-
275
- if __name__ == "__main__":
276
- import uvicorn
277
- uvicorn.run(app, host="0.0.0.0", port=5000)
 
1
+ import os
2
+ import subprocess
3
+ import tarfile
4
+ import asyncio
5
+ from fastapi import FastAPI, HTTPException, Form, UploadFile, File, Depends, Query, Response
6
+ from fastapi.responses import HTMLResponse, FileResponse, StreamingResponse
7
+ from pydantic import BaseModel
8
+ from tempfile import NamedTemporaryFile
9
+ from typing import List
10
+ from pydantic import BaseModel
11
+
12
+ app = FastAPI()
13
+
14
+ REQUIREMENTS_FILE = "requirements1.txt"
15
+
16
+ class DockerImageParams(BaseModel):
17
+ image_name: str
18
+ tag: str = 'latest'
19
+
20
+ app = FastAPI()
21
+
22
+ async def stream_log(file_path):
23
+ with open(file_path, 'r') as file:
24
+ while True:
25
+ line = file.readline()
26
+ if line:
27
+ yield line
28
+ else:
29
+ await asyncio.sleep(0.1)
30
+
31
+ @app.post("/download-dependencies")
32
+ async def download_dependencies(requirements_file: UploadFile = File(...)):
33
+ try:
34
+ with NamedTemporaryFile(delete=False) as tmp:
35
+ tmp.write(requirements_file.file.read())
36
+ tmp_path = tmp.name
37
+
38
+ # Ensure the directories exist
39
+ os.makedirs("/tmp/dependencies", exist_ok=True)
40
+
41
+ log_file_path = "/tmp/dependencies/download.log"
42
+ with open(log_file_path, "w") as log_file:
43
+ # Download dependencies
44
+ result = subprocess.run(
45
+ ["pip", "download", "-r", tmp_path, "-d", "/tmp/dependencies"],
46
+ stdout=log_file,
47
+ stderr=log_file
48
+ )
49
+
50
+ if result.returncode != 0:
51
+ raise HTTPException(status_code=500, detail="Error downloading dependencies. See log file for details.")
52
+
53
+ # Create a tar file
54
+ tar_path = "/tmp/dependencies.tar.gz"
55
+ with tarfile.open(tar_path, "w:gz") as tar:
56
+ for root, _, files in os.walk("/tmp/dependencies"):
57
+ for file in files:
58
+ file_path = os.path.join(root, file)
59
+ tar.add(file_path, arcname=file)
60
+ tar.add(log_file_path, arcname="download.log")
61
+
62
+ return StreamingResponse(stream_log(log_file_path), media_type="text/plain")
63
+
64
+ except subprocess.CalledProcessError as e:
65
+ raise HTTPException(status_code=500, detail=f"Error downloading dependencies: {str(e)}")
66
+ except Exception as e:
67
+ raise HTTPException(status_code=500, detail=str(e))
68
+ finally:
69
+ os.remove(tmp_path)
70
+
71
+ class DockerImageParams(BaseModel):
72
+ image_name: str
73
+ tag: str = 'latest'
74
+
75
+ def download_docker_image(image_name, tag='latest', destination='/tmp/docker-images'):
76
+ try:
77
+ os.makedirs(destination, exist_ok=True)
78
+
79
+ tar_path = os.path.join(destination, f'{image_name.replace("/", "_")}.tar')
80
+
81
+ # Remove the existing tar file if it exists
82
+ if os.path.exists(tar_path):
83
+ os.remove(tar_path)
84
+
85
+ # Skopeo command to copy the image as a tar file
86
+ command = [
87
+ 'skopeo', 'copy',
88
+ f'docker://{image_name}:{tag}',
89
+ f'docker-archive:{tar_path}'
90
+ ]
91
+ subprocess.run(command, check=True)
92
+ print(f"Image '{image_name}:{tag}' downloaded successfully to {destination}.")
93
+ return tar_path
94
+ except subprocess.CalledProcessError as e:
95
+ print(f"Error downloading image: {str(e)}")
96
+ raise HTTPException(status_code=500, detail=f"Error downloading Docker image: {str(e)}")
97
+
98
+ @app.get("/", response_class=HTMLResponse)
99
+ async def read_root():
100
+ html_content = """
101
+ <html>
102
+ <head>
103
+ <title>Azeez's Help Desk</title>
104
+ <link href src"./css/index.css" rel="stylesheet">
105
+ <script src="./js/index.js></script>
106
+ </head>
107
+ <body>
108
+ <h1>Welcome to Azeez's Help Desk :)</h1>
109
+
110
+ <div class="tab">
111
+ <button class="tablink" onclick="openTab(event, 'Docker')" id="defaultOpen">Docker Image Download</button>
112
+ <button class="tablink" onclick="openTab(event, 'Pip')">Pip Dependencies Download</button>
113
+ <button class="tablink" onclick="openTab(event, 'Deb')">Debian Packages Download</button>
114
+ </div>
115
+
116
+ <div id="Docker" class="tabcontent">
117
+ <h2>Docker Image Download</h2>
118
+ <form onsubmit="handleDockerFormSubmit(event)">
119
+ <label for="image_name">Docker Image Name:</label>
120
+ <input type="text" id="image_name" name="image_name" required><br><br>
121
+
122
+ <label for="tag">Docker Image Tag:</label>
123
+ <input type="text" id="tag" name="tag" value="latest" required><br><br>
124
+
125
+ <input type="submit" id="docker-submit-button" value="Download Docker Image">
126
+ </form>
127
+ <div id="docker-message" style="display: none; margin-top: 10px;"></div>
128
+ </div>
129
+
130
+ <div id="Pip" class="tabcontent">
131
+ <h2>Pip Dependencies Download</h2>
132
+ <form onsubmit="handlePipFormSubmit(event)">
133
+ <label for="requirements_file">Requirements File:</label>
134
+ <input type="file" id="requirements_file" name="requirements_file" accept=".txt" required><br><br>
135
+
136
+ <input type="submit" id="pip-submit-button" value="Download Dependencies">
137
+ </form>
138
+ <div id="pip-message" style="display: none; margin-top: 10px;"></div>
139
+ </div>
140
+
141
+ <div id="Deb" class="tabcontent">
142
+ <h2>Debian Packages Download</h2>
143
+ <form onsubmit="handleDebFormSubmit(event)">
144
+ <label for="deb_packages">Debian Package Names (comma-separated):</label>
145
+ <input type="text" id="deb_packages" name="deb_packages" required><br><br>
146
+
147
+ <input type="submit" id="deb-submit-button" value="Download Debian Packages">
148
+ </form>
149
+ <div id="deb-message" style="display: none; margin-top: 10px;"></div>
150
+ </div>
151
+ <div id="progress-container">
152
+ <div id="progress-bar">0%</div>
153
+ </div>
154
+ </body>
155
+ </html>
156
+ """
157
+ return HTMLResponse(content=html_content)
158
+
159
+ @app.get("/download-docker-image")
160
+ async def download_docker_image_endpoint(image_name: str = Query(...), tag: str = Query('latest')):
161
+ tar_path = download_docker_image(image_name, tag)
162
+ file_size = os.path.getsize(tar_path)
163
+
164
+ def iterfile():
165
+ with open(tar_path, 'rb') as file:
166
+ while chunk := file.read(1024 * 1024): # Read in 1 MB chunks
167
+ yield chunk
168
+
169
+ headers = {
170
+ "Content-Disposition": f'attachment; filename="{image_name.replace("/", "_")}.tar"',
171
+ "Content-Length": str(file_size)
172
+ }
173
+
174
+ return StreamingResponse(iterfile(), media_type='application/x-tar', headers=headers)
175
+
176
+ def create_tar_file(files_to_package: List[str], tar_filename: str, destination_dir: str):
177
+ """
178
+ Create a tar file containing specified files.
179
+ Args:
180
+ - files_to_package (list): List of paths to files to include in the tar file.
181
+ - tar_filename (str): Name of the tar file to create.
182
+ - destination_dir (str): Directory to save the tar file.
183
+ """
184
+ try:
185
+ tar_path = os.path.join(destination_dir, tar_filename)
186
+ with tarfile.open(tar_path, "w:gz") as tar:
187
+ for file_path in files_to_package:
188
+ tar.add(file_path, arcname=os.path.basename(file_path))
189
+ print(f"Created tar file '{tar_filename}' successfully in '{destination_dir}'.")
190
+ return tar_path
191
+ except Exception as e:
192
+ print(f"Error creating tar file: {e}")
193
+ raise
194
+
195
+ def download_deb_packages(package_names: List[str], destination_dir: str) -> List[str]:
196
+ """
197
+ Download Debian packages (`.deb`) and their dependencies using `apt-get download`.
198
+ Args:
199
+ - package_names (list): List of package names to download.
200
+ - destination_dir (str): Directory to save downloaded packages.
201
+ Returns:
202
+ - List of paths to downloaded `.deb` packages.
203
+ """
204
+ try:
205
+ # Create the destination directory if it doesn't exist
206
+ os.makedirs(destination_dir, exist_ok=True)
207
+
208
+ downloaded_packages = []
209
+
210
+ # Download each package and its dependencies
211
+ for package_name in package_names:
212
+ # Run apt-get update to refresh package index
213
+ # subprocess.run(['apt-get', 'update'], check=True)
214
+ # Download the package to the destination directory
215
+ subprocess.run(['apt-get', 'download', package_name, '-d', destination_dir], check=True)
216
+ # Build the full path to the downloaded package
217
+ deb_filename = f"{package_name}.deb"
218
+ downloaded_packages.append(os.path.join(destination_dir, deb_filename))
219
+
220
+ return downloaded_packages
221
+
222
+ except subprocess.CalledProcessError as e:
223
+ print(f"Error downloading packages: {e}")
224
+ raise
225
+
226
+ import subprocess
227
+
228
+ def download_make_package(destination_dir):
229
+ try:
230
+ # Ensure the destination directory exists
231
+ subprocess.run(['mkdir', '-p', destination_dir])
232
+
233
+ # Download the make package
234
+ result = subprocess.run(['apt-get', 'download', 'make', '-d', destination_dir], check=True)
235
+
236
+ if result.returncode == 0:
237
+ print(f"Downloaded make package and dependencies to {destination_dir} successfully.")
238
+ else:
239
+ print("Failed to download make package.")
240
+
241
+ except subprocess.CalledProcessError as e:
242
+ print(f"Error downloading packages: {e}")
243
+
244
+ @app.post("/download-deb-packages")
245
+ async def download_deb_packages_handler(deb_packages: str = Form(...)):
246
+ try:
247
+ destination_dir = '/tmp/downloaded_packages'
248
+ subprocess.run(['mkdir', '-p', destination_dir]) # Ensure destination directory exists
249
+
250
+ package_name='make'
251
+
252
+ # Download the package using apt-get
253
+ result = subprocess.run(['apt-get', 'install', package_name], check=True)
254
+
255
+ # If download is successful, return the downloaded package
256
+ if result.returncode == 0:
257
+ tar_filename = f'{package_name}.tar.gz'
258
+ tar_path = f'{destination_dir}/{tar_filename}'
259
+ return FileResponse(tar_path, filename=tar_filename)
260
+
261
+ # If download fails, raise HTTPException
262
+ else:
263
+ raise HTTPException(status_code=500, detail=f"Failed to download {package_name} package.")
264
+
265
+ except subprocess.CalledProcessError as e:
266
+ error_message = str(e.stderr)
267
+ print(f"Error downloading packages: {error_message}")
268
+ raise HTTPException(status_code=500, detail=f"Error downloading {package_name} package: {error_message}")
269
+
270
+ except Exception as e:
271
+ print(f"Error: {str(e)}")
272
+ raise HTTPException(status_code=500, detail=f"Failed to download {package_name} package: {str(e)}")
273
+
274
+
275
+ if __name__ == "__main__":
276
+ import uvicorn
277
+ uvicorn.run(app, host="0.0.0.0", port=5000)