Commit be979ee0 authored by Marcello Golfieri's avatar Marcello Golfieri
Browse files

parameterized certificates via CLI args, some cleanup

parent 6f08322c
Loading
Loading
Loading
Loading

wgcli.py

100644 → 100755
+56 −32
Original line number Diff line number Diff line
@@ -3,18 +3,18 @@
# Demo script for workgroup.py
# Aug 12, 2015
# Written by S. Thiell <sthiell@stanford.edu>
# Contributors:
#   Marcello Golfieri <golfieri@stanford.edu>

import atexit
import os
import sys

from srcc.utils.workgroup import SUWorkgroupManager, SUWorkgroupAPIError
import argparse

if sys.version_info[0] < 3:
    raise Exception("Must be using Python 3")

WG_CERT = 'certs/xstream.stanford.edu.cert'
WG_CERT_KEY = 'certs/xstream.key'
from srcc.utils.workgroup import SUWorkgroupManager, SUWorkgroupAPIError

HISTFILE = os.path.join(os.path.expanduser("~"), ".wgcli_history")

@@ -22,6 +22,7 @@ HISTFILE = os.path.join(os.path.expanduser("~"), ".wgcli_history")
def readline_setup():
    """Configure readline to automatically load and save a history file."""
    import readline

    readline.parse_and_bind("tab: complete")
    readline.set_completer_delims("")
    try:
@@ -37,7 +38,7 @@ def print_workgroup(workgroupt, workgroup, maxdepth=1000, depth=1):
    if depth > maxdepth:
        return

    print("%sMEMBER: %s" % ("\t" * depth, ', '.join(workgroup.members)))
    print("%sMEMBER: %s" % ("\t" * depth, ", ".join(workgroup.members)))
    for app in workgroup.applications:
        print("%sAPP: %s" % ("\t" * depth, app))
    for admin in workgroup.applications:
@@ -49,16 +50,19 @@ def print_workgroup(workgroupt, workgroup, maxdepth=1000, depth=1):
        return

    for child in list(workgroup.workgroups.values()):
        print_workgroup('WORKGROUP', child, maxdepth, depth+1)
        print_workgroup("WORKGROUP", child, maxdepth, depth + 1)

    for child in list(workgroup.admin_workgroups.values()):
        print_workgroup('ADMIN_WORKGROUP', child, maxdepth, depth+1)
        print_workgroup("ADMIN_WORKGROUP", child, maxdepth, depth + 1)


def cmd_tree(manager, workgroup):
    print_workgroup('WORKGROUP', manager.workgroup(workgroup))
    print_workgroup("WORKGROUP", manager.workgroup(workgroup))


def cmd_ls(manager, workgroup):
    print_workgroup('WORKGROUP', manager.workgroup(workgroup), maxdepth=1)
    print_workgroup("WORKGROUP", manager.workgroup(workgroup), maxdepth=1)


def main():
    """wgcli script main entry point"""
@@ -67,25 +71,45 @@ def main():
    except ImportError:
        sys.stderr.write("Warning: failed to initialize readline")

    manager = SUWorkgroupManager(cert=WG_CERT, cert_key=WG_CERT_KEY)

    print("######################################")
    print("Welcome to Stanford Workgroup CLI tool")
    print("######################################")
    print()
    print("Commands are:")
    print("  workgroup <workgroup>")
    print("     to select a working workgroup")
    print("  ls")
    print("     to list members")
    print("  tree")
    print("     to list members recursively")
    print()
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--wg_cert",
        action="store",
        dest="wg_cert",
        help="Workgroup Manager API Certificate",
        default="certs/xstream.stanford.edu.cert",
    )
    parser.add_argument(
        "--wg_cert_key",
        action="store",
        dest="wg_cert_key",
        help="Workgroup Manager API Key",
        default="certs/xstream.key",
    )
    args = parser.parse_args()

    manager = SUWorkgroupManager(cert=args.wg_cert, cert_key=args.wg_cert_key)

    print(
        """######################################
Welcome to Stanford Workgroup CLI tool
######################################

Commands are:
  workgroup <workgroup>
     to select a working workgroup
  ls
     to list members
  tree
     to list members recursively

"""
    )

    workgroup = None

    cmd = ''
    while cmd.lower() != 'quit':
    cmd = ""
    while cmd.lower() != "quit":
        if workgroup:
            prompt = "%s> " % workgroup.name
        else:
@@ -97,22 +121,22 @@ def main():
            continue
        cmd = args[0]
        try:
            if cmd == 'workgroup':
            if cmd == "workgroup":
                workgroup = manager.workgroup(args[1])
            elif cmd == 'add':
            elif cmd == "add":
                workgroup.add_member(args[1])
            elif cmd == 'remove':
            elif cmd == "remove":
                workgroup.remove_member(args[1])
            elif cmd == 'reload':
            elif cmd == "reload":
                manager.clear()
                workgroup = manager.workgroup(workgroup.name)
            elif cmd == 'ls':
            elif cmd == "ls":
                cmd_ls(manager, workgroup.name)
            elif cmd == 'tree':
            elif cmd == "tree":
                cmd_tree(manager, workgroup.name)
        except SUWorkgroupAPIError as exc:
            print(exc)


if __name__ == '__main__':
if __name__ == "__main__":
    main()