#!/usr/bin/env python3 import sys, os, pwd, subprocess, shutil, getpass, psutil, signal; def chown_r(fd: str | os.PathLike, uid: int, gid: int): # Set owner for the top-level folder os.chown(fd, uid, gid) for root, dirs, files in os.walk(fd): # Set owner on sub-directories for momo in dirs: os.chown(os.path.join(root, momo), uid, gid) # Set owner on files for momo in files: os.chown(os.path.join(root, momo), uid, gid) def chmod_r(fd: str | os.PathLike, mode: int): # Set perms for the top-level folder os.chmod(fd, mode) for root, dirs, files in os.walk(fd): # Set perms on sub-directories for momo in dirs: os.chmod(os.path.join(root, momo), mode) # Set perms on files for momo in files: os.chmod(os.path.join(root, momo), mode) def get_file_owner(filename): return pwd.getpwuid(os.stat(filename).st_uid).pw_name def kill_proc(): for proc in psutil.process_iter(): if proc.name() == "xmpp-http-upload": proc.send_signal(signal.SIGTERM) while proc.is_running(): pass USER = "xmpp-external" SERVICE = "xmpp-http-upload" SERVICE_FILE = f"/etc/systemd/system/{SERVICE}.service" CONFIG_PATH = "/etc/" + SERVICE BIN_PATH = "/usr/bin/" + SERVICE LOCAL_FILES_PATH = "/var/local/" + SERVICE COMPILE_TARGET = "" ERROR_LOG = "build.err" def compile(): env = os.environ.copy() devnull = os.open("/dev/null", os.O_WRONLY) errfile = os.open(ERROR_LOG, os.O_WRONLY | os.O_CREAT) cmdbuild = "cargo build --release" if COMPILE_TARGET: cmdbuild = cmdbuild + " --target=" + COMPILE_TARGET print(f"Compiling {SERVICE} binary...") if os.geteuid() == 0: build = subprocess.run(["su", "-c", cmdbuild, get_file_owner(__file__)], stdout=devnull, stderr=errfile).returncode else: build = subprocess.run(cmdbuild.split(), stdout=devnull, stderr=errfile).returncode if build == 0: print("Binary built successfully!") os.remove(ERROR_LOG) else: print("Binary compilation failed!") exit(3) def copy_binary(): if COMPILE_TARGET: shutil.copy(f"target/{COMPILE_TARGET}/release/{SERVICE}", BIN_PATH) else: shutil.copy(f"target/release/{SERVICE}", BIN_PATH) chmod_r(BIN_PATH, 0o755) if len(sys.argv) < 2: print("""Missing argument! Usage: ./install.py