Fixing poc.

This commit is contained in:
Roberto Abdelkader Martínez Pérez
2019-04-26 08:38:32 +02:00
parent 79a039fb89
commit 7f120a87e7
4 changed files with 20 additions and 37 deletions
+3 -1
View File
@@ -149,7 +149,9 @@ disable=print-statement,
unused-argument, unused-argument,
no-value-for-parameter, no-value-for-parameter,
missing-docstring, missing-docstring,
fixme fixme,
broad-except,
redefined-builtin
# Enable the message, report, category or checker with the given id(s). You can # Enable the message, report, category or checker with the given id(s). You can
+8 -4
View File
@@ -14,17 +14,21 @@ def kroute():
@click.option("-X", "--method", default="GET") @click.option("-X", "--method", default="GET")
@click.option("--url", envvar='KAPOW_URL') @click.option("--url", envvar='KAPOW_URL')
@click.argument("url_pattern", nargs=1) @click.argument("url_pattern", nargs=1)
@click.argument("command_file") @click.argument("command_file", required=False)
def add(url_pattern, entrypoint, command, method, url, command_file): def add(url_pattern, entrypoint, command, method, url, command_file):
if command: if command:
# Command is given inline
source = command source = command
elif command_file is None:
# No command
source = ""
elif command_file == '-': elif command_file == '-':
# Read commands from stdin
source = sys.stdin.read() source = sys.stdin.read()
elif command_file is not None: else:
# Read commands from a file
with open(command_file, 'r', encoding='utf-8') as handler: with open(command_file, 'r', encoding='utf-8') as handler:
source = handler.read() source = handler.read()
else:
source = ""
response = requests.post(f"{url}/routes", response = requests.post(f"{url}/routes",
json={"method": method, json={"method": method,
+4 -20
View File
@@ -1,30 +1,14 @@
#!/bin/bash #!/bin/bash
touch input kroute add -X POST --entrypoint ./topdf '/convert/{from}/pdf'
touch output
tail -F input | tr '[:lower:]' '[:upper:]' > output &
kroute add -X POST --entrypoint /home/nil/Project/kapow/kapow2/examples/topdf '/convert/{from}/pdf' kroute add -X POST --entrypoint '/bin/zsh -c' '/convert/{from}/{to}' - <<-'EOF'
kroute add -X POST --entrypoint /bin/zsh '/convert/{from}/{to}' - <<-'EOF'
pandoc --from=$(request /match/from) --to=$(request /match/to) --output=>(response /body) =(request /body) pandoc --from=$(request /match/from) --to=$(request /match/to) --output=>(response /body) =(request /body)
EOF EOF
kroute add -X GET '/formats/input' - <<-EOF kroute add -X GET '/formats/input' - <<-'EOF'
pandoc --list-input-formats | response /body pandoc --list-input-formats | response /body
EOF EOF
kroute add '/formats/output' - <<-EOF kroute add -X GET '/formats/output' -c 'pandoc --list-output-formats | grep -v pdf | response /body'
pandoc --list-output-formats | grep -v pdf | response /body
EOF
kroute add '/tail' - <<-EOF
tail -f /tmp/mispelotas | response /stream
EOF
kroute add -X POST '/tr' --command '
request /body >> input
tail -n1 output | response /body
'
+5 -12
View File
@@ -161,18 +161,18 @@ def handle_route(entrypoint, command):
connection = CONNECTIONS[id] = ConnectionHandler(request) connection = CONNECTIONS[id] = ConnectionHandler(request)
executable, *params = shlex.split(entrypoint) executable, *params = shlex.split(entrypoint)
args = ' '.join([shlex.quote(token) for token in params] + [shlex.quote(command)]) args = ' '.join([executable] + [shlex.quote(token) for token in params] + [shlex.quote(command)])
# Run the source # Run the source
shell_task = await asyncio.create_subprocess_exec( shell_task = await asyncio.create_subprocess_shell(
args, args,
executable=executable,
env={**os.environ, env={**os.environ,
"KAPOW_URL": "http://localhost:8080/kapow", "KAPOW_URL": "http://localhost:8080/kapow",
"KAPOW_CONNECTION": id, "KAPOW_CONNECTION": id,
"PATH": ":".join([os.path.join(os.path.dirname(os.path.realpath(__file__)), "bin"), "PATH": ":".join([os.path.join(os.path.dirname(os.path.realpath(__file__)), "bin"),
os.environ["PATH"]]), os.environ["PATH"]]),
}, },
shell=False) stdin=asyncio.subprocess.DEVNULL)
await shell_task.wait() await shell_task.wait()
del CONNECTIONS[id] del CONNECTIONS[id]
@@ -193,7 +193,6 @@ async def get_routes(request):
async def create_route(request): async def create_route(request):
request.app.router._frozen = False request.app.router._frozen = False
content = await request.json() content = await request.json()
print(f'Defined new route {content["method"]} {content["url_pattern"]}')
name = "ROUTE_" + str(uuid4()).replace('-', '_') name = "ROUTE_" + str(uuid4()).replace('-', '_')
request.app.router.add_route(content["method"], request.app.router.add_route(content["method"],
content["url_pattern"], content["url_pattern"],
@@ -215,13 +214,7 @@ async def delete_route(request):
######################################################################## ########################################################################
async def run_init_script(): async def run_init_script():
if len(sys.argv) > 1: if len(sys.argv) < 2:
# Script given
# with open(sys.argv[1], 'r') as scriptfile:
# script = scriptfile.read()
pass
else:
# script = sys.stdin.read()
raise RuntimeError("Script file is mandatory.") raise RuntimeError("Script file is mandatory.")
shell_task = await asyncio.create_subprocess_shell( shell_task = await asyncio.create_subprocess_shell(