#!/bin/python3 import yaml, os, sys, time, pathlib, unshare HOST_MARKER = "__AUTO_MKNET_MINIO_DEPLOY_DYYycw9Z5c5PvylU" storage_path = "/tmp/minio-testnet" # Remove any entry we put in /etc/hosts def cleanup(): with open('/etc/hosts', 'r') as fin: with open('/etc/hosts', 'w') as fout: fout.writelines(filter(lambda line: not HOST_MARKER in line, fin)) def wait(p): print("wait", p) d = os.path.join(storage_path, p) while not os.path.exists(d): time.sleep(1) #os.unlink(d) def post(p): print("post", p) pathlib.Path(os.path.join(storage_path, p)).touch() ## Create a working directory me = int(os.environ['ID']) node_storage_path = os.path.join(storage_path, str(me)) os.makedirs(node_storage_path, exist_ok=True) ## Writing down some info for others for v in [ 'ZONE', 'HOST', 'IP' ]: with open(os.path.join(node_storage_path, v+'.txt'), 'w') as f: f.write(os.environ[v]) netsize = int(os.environ['SIZE']) post("init"+str(me)) ## Delete old hosts if me == 1: cleanup() with open('/etc/hosts', 'a') as f: for i in range(1,netsize+1): wait("init"+str(i)) ip = pathlib.Path(os.path.join(storage_path, str(i), 'IP.txt')).read_text() host = pathlib.Path(os.path.join(storage_path, str(i), 'HOST.txt')).read_text() f.write(f"{ip} {host} minio{i} # {HOST_MARKER}\n") post("hosts") wait("hosts") # Adding entries to the host file # Unsharing the filesystem data=os.path.join(storage_path, "data") os.makedirs(data, exist_ok=True) unshare.unshare(unshare.CLONE_NEWNS) os.system("mount --make-rprivate /") # see https://stackoverflow.com/a/41580252 os.system("mount -t tmpfs tmpfs "+data) last = str(netsize) os.system("minio server --console-address ':9001' --address [" + os.environ['IP'] + "]:9000 http://minio{1.."+last+"}"+data+" > "+node_storage_path + "/minio.log 2>&1")