2021-12-08 14:43:59 +00:00
|
|
|
#!/usr/bin/env python3
|
2022-09-23 15:55:45 +00:00
|
|
|
import json, os, sys, time, pathlib, socket, shutil, urllib3
|
|
|
|
import minio
|
|
|
|
from os import environ as env
|
|
|
|
from pathlib import Path
|
|
|
|
from . import shared, flavor
|
2021-12-08 14:43:59 +00:00
|
|
|
|
2022-09-23 15:55:45 +00:00
|
|
|
storage_path = "./i/am/not/defined"
|
|
|
|
version = flavor.minio["minio-20220917"]
|
|
|
|
unix_sock = str(Path(shared.storage_path) / "minio.sock")
|
|
|
|
access_key = "minioadmin"
|
|
|
|
secret_key = "minioadmin"
|
|
|
|
client = minio.Minio(
|
|
|
|
f"[{env['IP']}]:9000",
|
|
|
|
access_key="minioadmin",
|
|
|
|
secret_key="minioadmin",
|
|
|
|
secure=False,
|
|
|
|
http_client=urllib3.PoolManager(
|
2022-09-23 16:09:54 +00:00
|
|
|
timeout=2,
|
|
|
|
retries=False,
|
2022-09-23 15:55:45 +00:00
|
|
|
)
|
|
|
|
)
|
2021-12-08 14:43:59 +00:00
|
|
|
|
2022-09-23 15:55:45 +00:00
|
|
|
if 'HOST' in env:
|
|
|
|
storage_path = str(Path(shared.storage_path) / "minio" / env['HOST'])
|
|
|
|
if 'ZONE' in env and env['ZONE'] != "":
|
|
|
|
storage_path = str(Path(shared.storage_path) / "minio" / env['ZONE'] / env['HOST'])
|
2021-12-08 14:43:59 +00:00
|
|
|
|
2022-09-23 15:55:45 +00:00
|
|
|
stdout = Path(storage_path) / "minio.stdout"
|
|
|
|
stderr = Path(storage_path) / "minio.stderr"
|
|
|
|
pid = Path(storage_path) / "daemon.pid"
|
|
|
|
|
|
|
|
def destroy():
|
|
|
|
if os.path.exists(pid):
|
|
|
|
try:
|
|
|
|
shared.exec(f"kill -9 `cat {pid}`")
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
if len(str(storage_path)) > 8:
|
|
|
|
shutil.rmtree(storage_path, ignore_errors=True)
|
|
|
|
|
|
|
|
def deploy_coord():
|
|
|
|
destroy()
|
|
|
|
if os.path.exists(unix_sock):
|
|
|
|
os.unlink(unix_sock)
|
|
|
|
|
|
|
|
os.makedirs(storage_path)
|
2021-12-08 14:43:59 +00:00
|
|
|
|
|
|
|
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
2022-09-23 15:55:45 +00:00
|
|
|
sock.bind(unix_sock)
|
2021-12-08 14:43:59 +00:00
|
|
|
sock.listen()
|
|
|
|
|
2022-09-23 15:55:45 +00:00
|
|
|
# Create sockets
|
|
|
|
fl = [ co for co, addr in [ sock.accept() for i in range(1, shared.count()) ]]
|
|
|
|
|
|
|
|
# Receive configurations, centralize them
|
|
|
|
me = [ { "ip": os.environ['IP'], "path": storage_path } ]
|
|
|
|
others = [ json.loads(co.makefile().readline()) for co in fl ]
|
|
|
|
identities = others + me
|
|
|
|
shared.log(f"ident: {identities}")
|
2021-12-08 14:43:59 +00:00
|
|
|
|
2022-09-23 15:55:45 +00:00
|
|
|
# Dispatch them
|
2021-12-08 14:43:59 +00:00
|
|
|
msg = f"{json.dumps(identities)}\n".encode()
|
|
|
|
[ co.send(msg) for co in fl ]
|
|
|
|
|
|
|
|
run_minio(identities)
|
|
|
|
|
2022-09-23 15:55:45 +00:00
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
if client.bucket_exists("sync"): break
|
|
|
|
client.make_bucket("sync")
|
|
|
|
break
|
|
|
|
except Exception as e:
|
|
|
|
shared.log("waiting for bootstrap...", e)
|
|
|
|
time.sleep(1)
|
|
|
|
shared.log("ready")
|
|
|
|
|
|
|
|
def deploy_follow():
|
|
|
|
destroy()
|
|
|
|
os.makedirs(storage_path)
|
|
|
|
|
2021-12-08 14:43:59 +00:00
|
|
|
co = None
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
2022-09-23 15:55:45 +00:00
|
|
|
sock.connect(unix_sock)
|
2021-12-08 14:43:59 +00:00
|
|
|
co = sock.makefile()
|
|
|
|
break
|
|
|
|
except Exception as err:
|
2022-09-23 15:55:45 +00:00
|
|
|
shared.log('conn failed, wait 1 sec, err is', err)
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
# send my identity
|
|
|
|
my_identity = json.dumps({ "ip": os.environ['IP'], "path": storage_path })
|
2021-12-08 14:43:59 +00:00
|
|
|
sock.send(f"{my_identity}\n".encode())
|
2022-09-23 15:55:45 +00:00
|
|
|
|
|
|
|
# get all
|
2021-12-08 14:43:59 +00:00
|
|
|
identities = json.loads(co.readline())
|
|
|
|
|
|
|
|
run_minio(identities)
|
2022-09-23 15:55:45 +00:00
|
|
|
sync_on_bucket_up()
|
|
|
|
shared.log("ready")
|
|
|
|
|
|
|
|
def sync_on_bucket_up():
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
if client.bucket_exists("sync"): break
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
shared.log("waiting for bucket 'sync'...")
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
def sync_on_bucket_down():
|
|
|
|
while True:
|
2022-09-23 16:47:13 +00:00
|
|
|
try:
|
|
|
|
if not client.bucket_exists("sync"): break
|
|
|
|
time.sleep(1)
|
|
|
|
except Exception as e:
|
|
|
|
shared.log("the cluster is probably already half shutdown, so errors are expected ->", e)
|
|
|
|
break
|
2021-12-08 14:43:59 +00:00
|
|
|
|
2022-09-23 15:55:45 +00:00
|
|
|
def delete_sync_bucket():
|
|
|
|
client.remove_bucket("sync")
|
2021-12-08 14:43:59 +00:00
|
|
|
|
|
|
|
def run_minio(identities):
|
2022-09-23 15:55:45 +00:00
|
|
|
|
|
|
|
# Required to prevent Minio error: "/tmp/mknet-store/minio/node1` is part of root drive, will not be used"
|
|
|
|
# https://github.com/minio/minio/issues/15720
|
|
|
|
env['CI'] = "true"
|
|
|
|
env['MINIO_CI_CD'] = "true"
|
|
|
|
|
|
|
|
cmd = f"{version['path']} server --console-address ':9001' --address ':9000'"
|
2021-12-08 14:43:59 +00:00
|
|
|
for ident in identities:
|
|
|
|
cmd += f" http://[{ident['ip']}]:9000{ident['path']}"
|
2022-09-23 15:55:45 +00:00
|
|
|
cmd += f" > {stdout} 2> {stderr}"
|
|
|
|
cmd += f" & echo $! > {pid}"
|
2021-12-08 14:43:59 +00:00
|
|
|
|
2022-09-23 15:55:45 +00:00
|
|
|
shared.log("launch: ", cmd)
|
|
|
|
os.system(cmd)
|