#!/usr/bin/env python3 from os import symlink from os.path import exists from argparse import ArgumentParser from sys import exit, stderr from argparse import ArgumentParser from getpass import getuser from email.mime.text import MIMEText from subprocess import Popen, PIPE from socket import gethostname def eprint(*args, **kwargs): print(*args, file=stderr, **kwargs) def mail(sender, user, subject, body): domain = gethostname() message = MIMEText(body) message["From"] = "{}@{}".format(sender, domain) message["To"] = "{}@{}".format(user, domain) message["Subject"] = subject command = shell_split("/usr/sbin/sendmail -t -oi") proc = Popen(command, stdin=PIPE, universal_newlines=True) proc.communicate(message.as_string()) def remove_script_from_queue(queue_file, splitter, scripts, index): script_name = sorted(list(scripts.keys()))[index] del scripts[script_name] lines = list() for name, attrs in scripts: path = attrs.get("path", "") desc = attrs.get("desc", "") user = attrs.get("user", "") lines.append(splitter.join([name, path, desc, user])) with open(queue_file, "w") as qfile: qfile.write("\n".join(lines)) description = "Administrative tool for handling shared user script submissions" parser = ArgumentParser(prog="uscript", description=description) parser.add_argument("-l", "--list", dest="list", action="store_true", help="list all submitted scripts") parser.add_argument("-a", "--approve", dest="approve", nargs="?", help="approval of script index based on position from --list") parser.add_argument("-R", "--reject", dest="reject", nargs="?", help="rejection of script index based on position from --list") arguments = parser.parse_args() config_file = "/center/etc/shared" queue_file = "{}.queue".format("/center/etc/shared") splitter = "%%" queued = None if not exists(queue_file): print("No scripts are queued") exit(0) with open(queue_file, "r") as qfile: queued = qfile.read().split("\n") if len(queued) == 1 and len(queued[0]) == 0: print("No scripts are queued") exit(0) scripts = dict() names = list() for queue in queued: name, path, desc, user = queue.split(splitter) scripts[name] = { "path": path, "desc": desc, "user": user } names.append(name) names = sorted(names) if arguments.list: header = "Index\tScript\tAuthor".expandtabs() print(header) print("-" * len(header)) for n, name in enumerate(names): author = scripts[name]["user"] print("{}\t{}\t{}".format(n, name, author).expandtabs()) approved = arguments.approve rejected = arguments.reject if approved is not None: approved = int(approved) if 0 > approved >= len(names): eprint("ERROR: That index does not exist, see --list") exit(1) script_name = names[approved] script = scripts[script_name] script_path = script["path"] user = script["user"] sender = getuser() user_bin = "/center/bin" symlink(script_path, "{}/{}".format(user_bin, script_name)) remove_script_from_queue(queue_file, splitter, scripts, approved) subject = "{} has been approved".format(script_name) body = "".join([ "Congratulations! Your script '{}' has been ".format(script_name), "approved by {} and has been added to the server's ".format(sender), "user script repository found at /center/bin." ]) mail(sender, user, subject, body) print("'{}' has been approved, email has been sent".format(script_name)) if rejected is not None: rejected = int(rejected) if 0 > rejected >= len(names): eprint("ERROR: That index does not exist, see --list") exit(1) script_name = names[rejected] script = scripts[script_name] user = script["user"] sender = getuser() remove_script_from_queue(queue_file, splitter, scripts, rejected) print("'{}' has been rejected".format(script_name)) reason = input("Provide reason for user ({}): ".format(author)) subject = "{} has been rejected".format(script_name) body = "".join([ "Unfortunately, your script '{}' has been ".format(script_name), "rejected by {} for the following reason(s):\n{}".format(sender) ]) mail(sender, user, subject, body) print("'{}' has been rejected, email has been sent".format(script_name))