-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
42 lines (35 loc) · 1.31 KB
/
utils.py
File metadata and controls
42 lines (35 loc) · 1.31 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
from kubernetes import client, config
from kubernetes.stream import stream
from fastapi import UploadFile
from os import path
import base64
def upload_textfile_to_pod(api: client.CoreV1Api, name: str, file: UploadFile, destination_folder: str, problem_name: str):
# encode as b64
file_content = file.file.read()
file_content = base64.b64encode(file_content)
file_content = file_content.decode("utf-8")
commands = ['/bin/sh']
# upload tar file to pod
resp = stream(api.connect_get_namespaced_pod_exec, name, "acadnet",
command=commands,
stdin=True, stdout=True, stderr=True, tty=True,
_preload_content=False)
commands = [
"pwd",
"mkdir -p " + destination_folder + "/" + problem_name,
"cd " + destination_folder + "/" + problem_name,
"echo " + file_content + " | base64 -d > " + file.filename
]
while resp.is_open():
resp.update(timeout=1)
# if resp.peek_stdout():
# print("STDOUT: %s" % resp.read_stdout())
# if resp.peek_stderr():
# print("STDERR: %s" % resp.read_stderr())
if commands:
c = commands.pop(0)
# print("Running command... %s\n" % c)
resp.write_stdin(c + "\n")
else:
break
resp.close()