Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

finish example file_transfer #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
target
target
example/*/run
8 changes: 8 additions & 0 deletions example/file_transfer/convert_param.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import json
Copy link

Choose a reason for hiding this comment

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

python -c ?

import base64

with open("param.json",'r') as f:
param = json.load(f)
file_name = base64.b64decode(param["param"])
Copy link

Choose a reason for hiding this comment

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

?

with open("file_name",'w') as f:
f.write(file_name.decode())
1 change: 1 addition & 0 deletions example/file_transfer/example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello, world!
64 changes: 64 additions & 0 deletions example/file_transfer/playbook.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
[file_transfer]
workdir = "$PLAYBOOK_TRANSFER/run"
name = "file_transfer"

[file_transfer.roles]
[file_transfer.roles.sender]
max_num = 1
[file_transfer.roles.sender.playbook]
workdir = "$PLAYBOOK_TRANSFER/run/{{user_id[..8]}}/{{task_id[..8]}}"

[[file_transfer.roles.sender.playbook.steps]]
step_name = "convert_param"
process = "python3 $PLAYBOOK_TRANSFER/convert_param.py"
process_wait = "convert_param"
check_exit_code = 0

[[file_transfer.roles.sender.playbook.steps]]
step_name = "load_file"
Copy link

Choose a reason for hiding this comment

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

reorganize? ln?

process = "cp $PLAYBOOK_TRANSFER/$(cat file_name) send_file"
process_wait = "load_file"
check_exit_code = 0

[[file_transfer.roles.sender.playbook.steps]]
send_variable = "file_content"
file = "send_file"
to_role = "receiver"

[[file_transfer.roles.sender.playbook.steps]]
step_name = "remove_tmp"
process = "rm send_file file_name"
process_wait = "remove_tmp"
check_exit_code = 0

[file_transfer.roles.receiver]
[file_transfer.roles.receiver.playbook]
workdir = "$PLAYBOOK_TRANSFER/run/{{user_id[..8]}}/{{task_id[..8]}}"

[[file_transfer.roles.receiver.playbook.steps]]
step_name = "convert_param"
process = "python3 $PLAYBOOK_TRANSFER/convert_param.py"
process_wait = "convert_param"
check_exit_code = 0

[[file_transfer.roles.receiver.playbook.steps]]
recv_variable = "file_content"
from_role = "sender"
file = "receive_file"
index = 0

[[file_transfer.roles.receiver.playbook.steps]]
step_name = "store_file"
process = "cp receive_file $(cat file_name)"
Copy link

Choose a reason for hiding this comment

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

mv

process_wait = "store_file"
check_exit_code = 0

[[file_transfer.roles.receiver.playbook.steps]]
create_entry = "example:file_transfer:receive_file_content"
file = "receive_file"

[[file_transfer.roles.receiver.playbook.steps]]
step_name = "remove_tmp"
process = "rm receive_file file_name"
process_wait = "remove_tmp"
check_exit_code = 0
30 changes: 30 additions & 0 deletions example/file_transfer/run_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import colink as CL
import subprocess
from typing import List
import os

if __name__ == "__main__":
colink_home = os.environ["COLINK_HOME"]
assert colink_home != None, "Please set COLINK_HOME environment variable"
ir = CL.InstantRegistry()
cls:List[CL.CoLink] = []
roles = ["sender", "receiver"]
participants = []
threads = []
for i in range(2):
Copy link

Choose a reason for hiding this comment

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

?

cl:CL.CoLink = CL.InstantServer().get_colink().switch_to_generated_user()
threads.append(subprocess.Popen(
[f"{colink_home}/colink-playbook","--addr",cl.core_addr,"--jwt",cl.jwt],
env={
"PLAYBOOK_TRANSFER": os.path.abspath("./example/file_transfer"),
"COLINK_PLAYBOOK_CONFIG": os.path.abspath("./example/file_transfer/playbook.toml")},
))
participants.append(CL.Participant(user_id=cl.get_user_id(), role=roles[i]))
cls.append(cl)
task_id = cls[0].run_task("file_transfer", "example.txt", participants, True)
cls[1].wait_task(task_id)
recv_file = cls[1].read_entry("example:file_transfer:receive_file_content")
print("Received file: ", recv_file)
Copy link

Choose a reason for hiding this comment

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

review naming?

for t in threads:
t:subprocess.Popen
t.terminate()