diff --git a/admin b/admin index cdee2a3..3cc702d 100755 --- a/admin +++ b/admin @@ -10,14 +10,21 @@ from sys import argv hosts_file = "hosts.csv" workdir = Path("./workdir") + def pull(id): - run(["scp", f"{id}:~/.ssh/authorized_keys" , str(workdir/Path(id))], check=True, capture_output=True) + run(["scp", f"{id}:~/.ssh/authorized_keys", + str(workdir/Path(id))], + check=True, capture_output=True) print(id, "pulled.") + def push(id): - run(["scp", str(workdir/Path(id)), f"{id}:~/.ssh/authorized_keys"], check=True, capture_output=True) + run(["scp", str(workdir/Path(id)), + f"{id}:~/.ssh/authorized_keys"], + check=True, capture_output=True) print(id, "pushed.") + def compose(id): authorized_set = set() with open(workdir/Path(id)) as f: @@ -25,19 +32,24 @@ def compose(id): authorized_set.add(line) print("".join(authorized_set)) -operations = { "push" : push, - "pull" : pull, - "compose" : compose} + +operations = {"push": push, + "pull": pull, + "compose": compose} + def construct_predicate(id_list, group_list): if "all" in (group_list+id_list): return lambda id, groups: True + def predicate(id, groups): return (id in id_list) or set(groups).intersection(group_list) return predicate + def construct_predicate_for_pushing(id_list, group_list, own_keys): - def predicate(id, group): + + def predicate(id, groups): with open(workdir/Path(id)) as keylist: if any(key in own_keys for key in keylist): return ("all" in (group_list+id_list) or @@ -47,6 +59,7 @@ def construct_predicate_for_pushing(id_list, group_list, own_keys): return False return predicate + def execute(operation, predicate): with open(hosts_file) as csvfile: for row in reader(csvfile, delimiter=","): @@ -54,6 +67,7 @@ def execute(operation, predicate): if predicate(id, groups): operation(id) + parser = ArgumentParser() parser.add_argument('-H', '--host', action='append') parser.add_argument('-g', '--group', action='append') @@ -64,9 +78,9 @@ id_list = list() if args.host is None else args.host group_list = list() if args.group is None else args.group operation = operations[args.operation] -if(operation=="push"): +if(operation == "push"): # collect local public keys - own_keys=list() + own_keys = list() for keyfile in Path(environ["HOME"]+"/.ssh").glob("id_*.pub"): with open(keyfile) as f: own_keys.append(next(f)) @@ -74,5 +88,4 @@ if(operation=="push"): else: pred = construct_predicate(id_list, group_list) -execute(operation,pred) - +execute(operation, pred)