Skip to content
Snippets Groups Projects
Commit 6c659580 authored by Omar Yassin's avatar Omar Yassin
Browse files

add simulator using docker

parent 6a00dcfe
Branches
No related tags found
No related merge requests found
......@@ -4,11 +4,6 @@ ARG TARGETOS TARGETARCH
ENV SRC_DIR /kubo
# Download packages first so they can be cached.
COPY go.mod go.sum $SRC_DIR/
RUN cd $SRC_DIR \
&& go mod download
COPY . $SRC_DIR
# Preload an in-tree but disabled-by-default plugin by adding it to the IPFS_PLUGINS variable
......
volumes
\ No newline at end of file
version: '3.8'
services:
ipfs#T:
container_name: kubo#T
build: .
restart: unless-stopped
volumes:
- ./compose/n#T/ipfs:/data/ipfs
- ./compose/n#T/fuse:/ipfs
- ./compose/n#T/ipns:/ipns
environment:
- IPFS_PATH=/data/ipfs
import subprocess
import logging
import json
import time
import os
nodes = []
"""A global list of tuples containing the container names and their IPFS peer IDs."""
def bts(b: bytes) -> str:
"""Convert a byte string to a regular string."""
return b.decode("utf-8").replace("\n", " ").strip()
def initialize_containers(kubo_type: str, number: int) -> list:
"""Initialize a number of kubo containers of a given type (original or strapless)."""
# Initialize kubo containers and collect their names.
containers = []
for i in range(number):
container_name = f"kubo-{kubo_type}-n{i}"
while True:
initialization_output = subprocess.run([f"{os.path.dirname(__file__)}/start-kubo.sh", kubo_type, str(i)], capture_output=True)
if initialization_output.returncode == 0:
containers.append(container_name)
break
# The container may already exist, so we need to stop it first.
logging.warning("Failed to start %s, retrying again.\nstderr=%s", container_name, bts(initialization_output.stderr))
subprocess.run(["docker", "stop", container_name], capture_output=True)
time.sleep(0.1)
# Create a map/list of these container numbers to their names peer IDs.
global nodes
nodes = []
for i, container_name in enumerate(containers):
while True:
ipfs_id_output = subprocess.run(["docker", "exec", container_name, "ipfs", "id"], capture_output=True)
if ipfs_id_output.returncode == 0:
ipfs_id = json.loads(ipfs_id_output.stdout)
nodes.append((container_name, ipfs_id["Addresses"][4]))
break
# The container may not be ready yet, so we need to wait.
logging.warning("Failed to get IPFS peer ID for %s, retrying again.\nstderr=%s", container_name, bts(ipfs_id_output.stderr))
time.sleep(0.1)
logging.info("Initialized %d %s kubo containers.", number, kubo_type)
return nodes
def connect(i: int, j: int) -> bool:
"""Try to connect two kubo containers."""
connect_output = subprocess.run(["docker", "exec", nodes[i][0], "ipfs", "swarm", "connect", nodes[j][1]], capture_output=True)
if connect_output.returncode == 0:
return True
logging.error("Failed to connect %s to %s.\nstderr=%s", nodes[i][0], nodes[j][0], bts(connect_output.stderr))
return False
print(initialize_containers("strapless", 4))
print(connect(0, 1))
print(connect(1, 2))
print(connect(2, 3))
\ No newline at end of file
#!/bin/bash
DIR=$(dirname "$(realpath $0)")
docker_run() {
docker run \
-v $DIR/volumes/$1/n$2/ipfs:/data/ipfs \
-v $DIR/volumes/$1/n$2/fuse:/fuse \
-v $DIR/volumes/$1/n$2/ipns:/ipns \
-e IPFS_PATH=/data/ipfs \
--rm --detach --name kubo-$1-n$2 \
kubo-$1 >/dev/null \
&& echo "kubo-$1-n$2"
}
if [ "$1" = "strapless" ] || [ "$1" = "original" ]
then
docker_run $1 $2
else
echo "Usage: start-kubo.sh [strapless|original] [number]"
exit 1
fi
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment