Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions fri/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

The Control-Core File Receiving Interface (FRI) is built with is Python-3.10. It is the core component that makes the distributed executions a reality in the Control-Core framework.

# Install Dependencies

Install Jupyter lab
````
$ pip install jupyterlab
````

# Building FRI Container

Expand Down
118 changes: 59 additions & 59 deletions fri/server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import json
import subprocess

cur_path = os.path.dirname(os.path.abspath(__file__))
concore_path = os.path.abspath(os.path.join(cur_path, '../../'))


app = Flask(__name__)
app.secret_key = "secret key"
Expand All @@ -26,13 +29,16 @@ def upload(dir):
errors = {}
success = False

if not os.path.exists(secure_filename(dirname)):
os.makedirs(secure_filename(dirname))
directory_name = os.path.abspath(os.path.join(concore_path, secure_filename(dirname)))

if not os.path.isdir(directory_name):
os.mkdir(directory_name)


for file in files:
if file:
filename = secure_filename(file.filename)
file.save(secure_filename(dirname)+"/"+filename)
file.save(directory_name+"/"+filename)
success = True

if success and errors:
Expand All @@ -49,56 +55,15 @@ def upload(dir):
resp.status_code = 500
return resp

# To execute any python file. For example, /execute/test?apikey=xyz
@app.route('/execute/<dir>', methods=['POST'])
def execute(dir):
apikey = request.args.get('apikey')
dirname = dir + "_" + apikey

if 'file' not in request.files:
resp = jsonify({'message': 'No file in the request'})
resp.status_code = 400
return resp

file = request.files['file']

if file.filename == '':
resp = jsonify({'message': 'No file selected for Executing'})
resp.status_code = 400
return resp

errors = {}
success = False

if not os.path.exists(secure_filename(dirname)):
os.makedirs(secure_filename(dirname))

if file:
filename = secure_filename(file.filename)
file.save(secure_filename(dirname)+"/"+filename)
output_filename = filename + ".out"
file_path = secure_filename(dirname) + "/"+filename
outputfile_path = secure_filename(dirname)+"/"+output_filename
f = open(outputfile_path, "w")
call(["nohup", "python3", file_path], stdout=f)
success = True

if success:
resp = jsonify({'message': 'Files successfully executed'})
resp.status_code = 201
return resp
else:
resp = jsonify(errors)
resp.status_code = 500
return resp

# to download /build/<dir>?fetch=<graphml>. For example, /build/test?fetch=sample1
# to download /build/<dir>?fetch=<graphml>. For example, /build/test?fetch=sample1&apikey=xyz
@app.route('/build/<dir>', methods=['POST'])
def build(dir):
graphml_file = request.args.get('fetch')
makestudy_dir = dir+ "/" + graphml_file #for makestudy
cur_path = os.getcwd()
concore_path = os.path.abspath(os.path.join(cur_path, '../../'))
graphml_file = request.args.get('fetch')
apikey = request.args.get('apikey')
dirname = dir + "_" + apikey
makestudy_dir = dirname + "/" + graphml_file #for makestudy
dir_path = os.path.abspath(os.path.join(concore_path, graphml_file)) #path for ./build
if not os.path.exists(secure_filename(dir_path)):
proc = call(["./makestudy", makestudy_dir], cwd=concore_path)
Expand All @@ -114,33 +79,74 @@ def build(dir):

@app.route('/debug/<dir>', methods=['POST'])
def debug(dir):
cur_path = os.getcwd()
concore_path = os.path.abspath(os.path.join(cur_path, '../../'))
dir_path = os.path.abspath(os.path.join(concore_path, dir))
proc = call(["./debug"], cwd=dir_path)
if(proc == 0):
resp = jsonify({'message': 'Close the pop window after obtaing result'})
resp = jsonify({'message': 'Close the pop window after obtaining result'})
resp.status_code = 201
return resp
else:
resp = jsonify({'message': 'There is an Error'})
resp.status_code = 500
return resp


@app.route('/run/<dir>', methods=['POST'])
def run(dir):
dir_path = os.path.abspath(os.path.join(concore_path, dir))
proc = call(["./run"], cwd=dir_path)
if(proc == 0):
resp = jsonify({'message': 'result prepared'})
resp.status_code = 201
return resp
else:
resp = jsonify({'message': 'There is an Error'})
resp.status_code = 500
return resp

@app.route('/stop/<dir>', methods=['POST'])
def stop(dir):
dir_path = os.path.abspath(os.path.join(concore_path, dir))
proc = call(["./stop"], cwd=dir_path)
if(proc == 0):
resp = jsonify({'message': 'resources cleaned'})
resp.status_code = 201
return resp
else:
resp = jsonify({'message': 'There is an Error'})
resp.status_code = 500
return resp


@app.route('/clear/<dir>', methods=['POST'])
def clear(dir):
dir_path = os.path.abspath(os.path.join(concore_path, dir))
proc = call(["./clear"], cwd=dir_path)
if(proc == 0):
resp = jsonify({'message': 'result deleted'})
resp.status_code = 201
return resp
else:
resp = jsonify({'message': 'There is an Error'})
resp.status_code = 500
return resp

# to download /download/<dir>?fetch=<downloadfile>. For example, /download/test?fetch=example.py.out&apikey=xyz
@app.route('/download/<dir>', methods=['POST', 'GET'])
def download(dir):
download_file = request.args.get('fetch')
apikey = request.args.get('apikey')
Comment thread
pradeeban marked this conversation as resolved.
dirname = dir + "_" + apikey
directory_name = os.path.abspath(os.path.join(concore_path, secure_filename(dirname)))


if not os.path.exists(secure_filename(dirname)):
resp = jsonify({'message': 'Directory not found'})
resp.status_code = 400
return resp

try:
return send_from_directory(secure_filename(dirname), download_file, as_attachment=True)
return send_from_directory(directory_name, download_file, as_attachment=True)
except:
resp = jsonify({'message': 'file not found'})
resp.status_code = 400
Expand All @@ -149,8 +155,6 @@ def download(dir):

@app.route('/destroy/<dir>', methods=['DELETE'])
def destroy(dir):
cur_path = os.getcwd()
concore_path = os.path.abspath(os.path.join(cur_path, '../../'))
proc = call(["./destroy", dir], cwd=concore_path)
if(proc == 0):
resp = jsonify({'message': 'Successfuly deleted Dirctory'})
Expand All @@ -163,8 +167,6 @@ def destroy(dir):

@app.route('/getFilesList/<dir>', methods=['POST'])
def getFilesList(dir):
cur_path = os.getcwd()
concore_path = os.path.abspath(os.path.join(cur_path, '../../'))
dir_path = os.path.abspath(os.path.join(concore_path, dir))
res = []
res = os.listdir(dir_path)
Expand All @@ -174,8 +176,6 @@ def getFilesList(dir):

@app.route('/openJupyter/', methods=['POST'])
def openJupyter():
cur_path = os.getcwd()
concore_path = os.path.abspath(os.path.join(cur_path, '../../'))
proc = subprocess.Popen(['jupyter', 'lab'], shell=False, stdout=subprocess.PIPE, cwd=concore_path)
if proc.poll() is None:
resp = jsonify({'message': 'Successfuly opened Jupyter'})
Expand Down
94 changes: 54 additions & 40 deletions fri/test.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
from cgi import test
import requests
import os
import urllib.request
import time

# function to test upload() method.
def upload():
url = "http://127.0.0.1:5000/upload/test?apikey=xyz"

path = os.path.abspath("example.py")
def upload(files):
url = "http://127.0.0.1:5000/upload/test?apikey=xyz"

payload={}
files=[
('files[]',('example.py',open(path,'rb'),'application/octet-stream'))
]
# files=[

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these commented out?

# ('files[]',('example.py',open(path,'rb'),'application/octet-stream'))
# ]
headers = {}

response = requests.request("POST", url, headers=headers, data=payload, files=files)
Expand All @@ -21,50 +22,46 @@ def upload():

# # *******

# function to test execute() method.
def execute():
url = "http://127.0.0.1:5000/execute/test?apikey=xyz"

path = os.path.abspath("example.py")

payload={}
files=[
('file',('example.py',open(path,'rb'),'application/octet-stream'))
]
headers = {}

response = requests.request("POST", url, headers=headers, data=payload, files=files)

# function to check build
def build(dir, graphml, apikey):
url = "http://127.0.0.1:5000/build/"+dir+"?"+"fetch="+graphml+"&"+"apikey="+apikey
response = requests.request("POST", url)
print(response.text)

# function to test download() method.
def download():
url = "http://127.0.0.1:5000/download/test?fetch=f1.txt&apikey=xyz"
urllib.request.urlretrieve(url, "f1.txt")
# function to debug
def debug(graphml):
url = "http://127.0.0.1:5000/debug/"+graphml
response = requests.request("POST", url)
print(response.text)

# function to test run() method.
def run(graphml):
url = "http://127.0.0.1:5000/run/"+graphml
response = requests.request("POST", url)
print(response.text)

# function to check build
def build():
url = "http://127.0.0.1:5000/build/test?fetch=sample1"
def clear(graphml):
url = "http://127.0.0.1:5000/clear/"+graphml
response = requests.request("POST", url)
print(response.text)

# function to debug
def debug():
url = "http://127.0.0.1:5000/debug/sample1"
def stop(graphml):
url = "http://127.0.0.1:5000/stop/"+graphml
response = requests.request("POST", url)
print(response.text)
print(response.text)


#function to destroy dir.
def destroy():
url = "http://127.0.0.1:5000/destroy/sample1"
def destroy(dir):
url = "http://127.0.0.1:5000/destroy/" + dir
response = requests.request("DELETE", url)

print(response.text)

def getFilesList():
url = "http://127.0.0.1:5000/getFilesList/test"
def getFilesList(dir):
url = "http://127.0.0.1:5000/getFilesList/" + dir
response = requests.request("POST", url)
print(response.text)

Expand All @@ -73,13 +70,30 @@ def openJupyter():
response = requests.request("POST", url)
print(response.text)

# function to test download() method.
def download():
url = "http://127.0.0.1:5000/download/test?fetch=f1.txt&apikey=xyz"
urllib.request.urlretrieve(url, "f1.txt")

# upload()
# execute()
# download()
# build()
# debug()
# destroy()
getFilesList()
# file list to be uploaded
files=[
#('files[]',(file_name,open(file_path,'rb'),'application/octet-stream'))
('files[]',('controller.py',open('/home/amit/Desktop/test_xyz/controller.py','rb'),'application/octet-stream')),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pls do not hard-code the values such as "/home/amit" as these will not work outside your computer. Use relative paths instead.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace '/home/amit/Desktop/test_xyz/controller.py' with '../demo/controller.py'

('files[]',('pm.py',open('/home/amit/Desktop/test_xyz/pm.py','rb'),'application/octet-stream')),
('files[]',('sample1.graphml',open('/home/amit/Desktop/test_xyz/sample1.graphml','rb'),'application/octet-stream')),
# ('files[]',('example.py',open('/home/amit/Desktop/fri/example.py','rb'),'application/octet-stream'))
]


# upload(files)
# build("test", "sample1", "xyz")
# time.sleep(6)
# debug("sample1")
# run("sample1")
# clear("sample1")
# stop("sample1")
# getFilesList("sample1")
# destroy("sample1")
# openJupyter()
# download()