Minor changes

This commit is contained in:
Loic Guegan 2023-10-29 09:40:33 +01:00
parent e34fc73d55
commit f4da143142
2 changed files with 28 additions and 28 deletions

View file

@ -1,38 +1,17 @@
import os, json, time, re, sys, subprocess import os, json, time, re, sys, subprocess
from clusterman.config import CONF from clusterman.config import CONF
from clusterman.utils import ssh_exec import clusterman.utils as utils
def get_node_list():
nodes_path=CONF.NODE_FILE
if os.path.exists(nodes_path):
with open(nodes_path) as f:
nodes=json.load(f)
return nodes
return list()
def get_node_in_group(group):
nodes=get_node_list()
# Search
ingroup=list()
if len(nodes) > 0 and group in CONF["cluster"]["groups"]:
patterns=[re.compile(pattern) for pattern in CONF["cluster"]["groups"][group]]
for node in nodes:
for pattern in patterns:
if pattern.match(node):
ingroup.append(node)
break;
return ingroup
def ls(group=None): def ls(group=None):
nodes_path=CONF.NODE_FILE nodes_path=CONF.NODE_FILE
nodes=get_node_list() nodes=utils.get_node_list()
if len(nodes)<=0: if len(nodes)<=0:
print("Please perform a scan before") print("Please perform a scan before")
exit(0) exit(0)
# Print nodes # Print nodes
if group is not None: if group is not None:
for node in get_node_in_group(group): for node in utils.get_node_in_group(group):
print(node) print(node)
else: else:
for node in nodes: for node in nodes:
@ -65,7 +44,7 @@ def scan(timeout):
CONF.save() CONF.save()
def check(timeout): def check(timeout):
nodes=get_node_list() nodes=utils.get_node_list()
if len(nodes) <= 0: if len(nodes) <= 0:
print("Please perform a scan before") print("Please perform a scan before")
exit(0) exit(0)
@ -85,8 +64,8 @@ def check(timeout):
print("Error: Some of your nodes are not reachable") print("Error: Some of your nodes are not reachable")
def exec(command, group=None): def exec(command, group=None):
nodes=get_node_list() if group is None else get_node_in_group(group) nodes=utils.get_node_list() if group is None else get_node_in_group(group)
for ip in nodes: for ip in nodes:
print("----- Node {} -----".format(ip)) print("----- Node {} -----".format(ip))
print(ssh_exec(ip," ".join(command))) print(utils.ssh_exec(ip," ".join(command)))

View file

@ -1,4 +1,4 @@
import subprocess import subprocess, os, json
from clusterman.config import CONF from clusterman.config import CONF
def ssh_exec(host,command,use_key=True): def ssh_exec(host,command,use_key=True):
@ -11,3 +11,24 @@ def ssh_exec(host,command,use_key=True):
output=subprocess.check_output(["ssh","-o", "StrictHostKeyChecking=no", "{}@{}".format(user,ip), command]) output=subprocess.check_output(["ssh","-o", "StrictHostKeyChecking=no", "{}@{}".format(user,ip), command])
return output.decode("utf-8") return output.decode("utf-8")
def get_node_list():
nodes_path=CONF.NODE_FILE
if os.path.exists(nodes_path):
with open(nodes_path) as f:
nodes=json.load(f)
return nodes
return list()
def get_node_in_group(group):
nodes=get_node_list()
# Search
ingroup=list()
if len(nodes) > 0 and group in CONF["cluster"]["groups"]:
patterns=[re.compile(pattern) for pattern in CONF["cluster"]["groups"][group]]
for node in nodes:
for pattern in patterns:
if pattern.match(node):
ingroup.append(node)
break;
return ingroup