fix flake8 problems
This commit is contained in:
parent
c0a92541d6
commit
e66b1bd3b2
33
admin
33
admin
|
@ -10,14 +10,21 @@ from sys import argv
|
||||||
hosts_file = "hosts.csv"
|
hosts_file = "hosts.csv"
|
||||||
workdir = Path("./workdir")
|
workdir = Path("./workdir")
|
||||||
|
|
||||||
|
|
||||||
def pull(id):
|
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.")
|
print(id, "pulled.")
|
||||||
|
|
||||||
|
|
||||||
def push(id):
|
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.")
|
print(id, "pushed.")
|
||||||
|
|
||||||
|
|
||||||
def compose(id):
|
def compose(id):
|
||||||
authorized_set = set()
|
authorized_set = set()
|
||||||
with open(workdir/Path(id)) as f:
|
with open(workdir/Path(id)) as f:
|
||||||
|
@ -25,19 +32,24 @@ def compose(id):
|
||||||
authorized_set.add(line)
|
authorized_set.add(line)
|
||||||
print("".join(authorized_set))
|
print("".join(authorized_set))
|
||||||
|
|
||||||
operations = { "push" : push,
|
|
||||||
"pull" : pull,
|
operations = {"push": push,
|
||||||
"compose" : compose}
|
"pull": pull,
|
||||||
|
"compose": compose}
|
||||||
|
|
||||||
|
|
||||||
def construct_predicate(id_list, group_list):
|
def construct_predicate(id_list, group_list):
|
||||||
if "all" in (group_list+id_list):
|
if "all" in (group_list+id_list):
|
||||||
return lambda id, groups: True
|
return lambda id, groups: True
|
||||||
|
|
||||||
def predicate(id, groups):
|
def predicate(id, groups):
|
||||||
return (id in id_list) or set(groups).intersection(group_list)
|
return (id in id_list) or set(groups).intersection(group_list)
|
||||||
return predicate
|
return predicate
|
||||||
|
|
||||||
|
|
||||||
def construct_predicate_for_pushing(id_list, group_list, own_keys):
|
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:
|
with open(workdir/Path(id)) as keylist:
|
||||||
if any(key in own_keys for key in keylist):
|
if any(key in own_keys for key in keylist):
|
||||||
return ("all" in (group_list+id_list) or
|
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 False
|
||||||
return predicate
|
return predicate
|
||||||
|
|
||||||
|
|
||||||
def execute(operation, predicate):
|
def execute(operation, predicate):
|
||||||
with open(hosts_file) as csvfile:
|
with open(hosts_file) as csvfile:
|
||||||
for row in reader(csvfile, delimiter=","):
|
for row in reader(csvfile, delimiter=","):
|
||||||
|
@ -54,6 +67,7 @@ def execute(operation, predicate):
|
||||||
if predicate(id, groups):
|
if predicate(id, groups):
|
||||||
operation(id)
|
operation(id)
|
||||||
|
|
||||||
|
|
||||||
parser = ArgumentParser()
|
parser = ArgumentParser()
|
||||||
parser.add_argument('-H', '--host', action='append')
|
parser.add_argument('-H', '--host', action='append')
|
||||||
parser.add_argument('-g', '--group', 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
|
group_list = list() if args.group is None else args.group
|
||||||
operation = operations[args.operation]
|
operation = operations[args.operation]
|
||||||
|
|
||||||
if(operation=="push"):
|
if(operation == "push"):
|
||||||
# collect local public keys
|
# collect local public keys
|
||||||
own_keys=list()
|
own_keys = list()
|
||||||
for keyfile in Path(environ["HOME"]+"/.ssh").glob("id_*.pub"):
|
for keyfile in Path(environ["HOME"]+"/.ssh").glob("id_*.pub"):
|
||||||
with open(keyfile) as f:
|
with open(keyfile) as f:
|
||||||
own_keys.append(next(f))
|
own_keys.append(next(f))
|
||||||
|
@ -74,5 +88,4 @@ if(operation=="push"):
|
||||||
else:
|
else:
|
||||||
pred = construct_predicate(id_list, group_list)
|
pred = construct_predicate(id_list, group_list)
|
||||||
|
|
||||||
execute(operation,pred)
|
execute(operation, pred)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue