From 6d71194d1ed1a121482dcc94fb8eda0050c90921 Mon Sep 17 00:00:00 2001 From: n-hutton Date: Thu, 18 Jan 2024 20:07:32 +0000 Subject: [PATCH] the changes without b64 stuff --- docker/Dockerfile | 14 ++++++++++++-- handle_connection.sh | 27 +++++++++++++++++++++++++++ run_scilla_tcp.sh | 21 +++++++++++++++++++++ write_files.py | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 2 deletions(-) create mode 100755 handle_connection.sh create mode 100755 run_scilla_tcp.sh create mode 100755 write_files.py diff --git a/docker/Dockerfile b/docker/Dockerfile index 42c1671b1..e2f166de6 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,7 +1,10 @@ # escape=\ # Common dependencies of the builder and runner stages. -FROM ubuntu:22.04 AS base +FROM --platform=linux/amd64 ubuntu:22.04 AS base + +RUN apt-get update -y + # Format guideline: one package per line and keep them alphabetically sorted RUN apt-get update -y \ && apt-get install -y software-properties-common \ @@ -92,6 +95,9 @@ COPY scilla.opam . COPY shell.nix . COPY .ocamlformat . COPY tests/ tests +COPY handle_connection.sh . +COPY run_scilla_tcp.sh . +COPY write_files.py . # Make sure vcpkg installs brings in the dependencies RUN --mount=type=cache,target=/root/.cache/vcpkg/ ${VCPKG_ROOT}/vcpkg install --triplet=x64-linux-dynamic @@ -115,10 +121,14 @@ RUN mkdir -p ${VCPKG_INSTALL_LIB_DIR} \ && rm -rf vcpkg_installed \ && ln -s ${BUILD_DIR}/vcpkg_installed vcpkg_installed -FROM ubuntu:22.04 +FROM --platform=linux/amd64 ubuntu:22.04 RUN apt-get update -y \ && apt-get install -y build-essential \ + netcat-openbsd \ + vim \ + socat \ + python3 \ libgmp-dev ARG SOURCE_DIR="/scilla/${MAJOR_VERSION}" diff --git a/handle_connection.sh b/handle_connection.sh new file mode 100755 index 000000000..55b5f5e3d --- /dev/null +++ b/handle_connection.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +echo "Starting server." 1>&2 + +# Start scilla-server in the background +rm -rf /tmp/scilla-socket/server.sock +/scilla/0/bin/scilla-server -socket /tmp/scilla-socket/server.sock 1>&2 & + +# Get the PID of scilla-server +SERVER_PID=$! + +echo "forwarding traffic." 1>&2 + +# Block until we see the scilla server has provided the socket +while [ ! -S /tmp/scilla-socket/server.sock ]; do sleep 1; done + +echo "forwarding other traffic." 1>&2 + +# Forward traffic from stdin (provided by socat) to Unix socket. +# Note this means we ONLY use stderr when echoing within this script to avoid it going to the socket +socat - UNIX-CONNECT:/tmp/scilla-socket/server.sock + +echo "connection closed - killing" 1>&2 +echo "" 1>&2 + +# Kill the scilla-server process when done +kill $SERVER_PID diff --git a/run_scilla_tcp.sh b/run_scilla_tcp.sh new file mode 100755 index 000000000..a9eca4632 --- /dev/null +++ b/run_scilla_tcp.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +echo "Starting server." 1>&2 + +rm -rf myfifo 1>&2 +mkfifo myfifo 1>&2 + +# Clear the directories we will be using the files for +mkdir -p /tmp/scilla_init/ +mkdir -p /tmp/scilla_input/ + +rm -rf /tmp/scilla_init/* +rm -rf /tmp/scilla_input/* + +# netcat listen to 12346 and forward it to/from a local socket (this is the state queries/updates) +nc -lkv 12346 myfifo & + + +# socat - every connection on 12345, run the script handle_connection and pipe the input to it (this is the initial run/create/etc commands) +socat TCP-LISTEN:12347,reuseaddr,fork EXEC:"/scilla/0/write_files.py",nofork & +socat TCP-LISTEN:12345,reuseaddr,fork EXEC:"/scilla/0/handle_connection.sh",nofork diff --git a/write_files.py b/write_files.py new file mode 100755 index 000000000..520afd0d8 --- /dev/null +++ b/write_files.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 + +import sys +import os + +def main(): + # Read the input as binary data from stdin + input_data = sys.stdin.buffer.read() + + # Split the input into a path and data + try: + path, data = input_data.split(b' ', 1) + except ValueError: + print("Invalid input format. Please provide a valid absolute path and data.") + sys.exit(1) + + # Ensure the path is absolute + if not os.path.isabs(path.decode()): + print("Please provide an absolute path.") + sys.exit(1) + + # Write binary data to the specified file + try: + with open(path.decode(), 'wb') as file: + file.write(data) + print(f"Data written to {path.decode()}") + except Exception as e: + print(f"Error writing data to {path.decode()}: {str(e)}") + sys.exit(1) + +if __name__ == "__main__": + main()