Minor changes

This commit is contained in:
Loïc Guégan 2023-10-27 10:56:14 +02:00
parent 37ac5a14dd
commit ade516e6f8
3 changed files with 29 additions and 26 deletions

View file

@ -1,6 +1,6 @@
import os, json, re import os, json, re
from clusterman.config import CONF from clusterman.config import CONF
from clusterman.commands.node import get_node_in_group
def info(): def info():
nodes=None nodes=None
@ -20,18 +20,10 @@ def info():
# Groups # Groups
print("Node groups: ",end="") print("Node groups: ",end="")
if len(CONF["cluster"]["groups"]) > 0: if len(CONF["cluster"]["groups"]) > 0:
pattern=None content=list()
for group in CONF["cluster"]["groups"].keys(): for group in CONF["cluster"]["groups"].keys():
if not pattern == None: content.append("{}({})".format(group,len(get_node_in_group(group))))
print(", ", end="") print(", ".join(content))
pattern = re.compile(CONF["cluster"]["groups"][group])
count=0
if not nodes == None:
for ip in nodes:
if pattern.match(ip):
count=count+1
print("{}({})".format(group,count),end="")
print()
else: else:
print("NA") print("NA")

View file

@ -2,6 +2,25 @@ import os, json, time, re, sys
from clusterman.config import CONF from clusterman.config import CONF
def get_node_in_group(group):
nodes_path=CONF.NODE_FILE
nodes=None
if os.path.exists(nodes_path):
with open(nodes_path) as f:
nodes=json.load(f)
# Search
ingroup=list()
if nodes is not None 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=None nodes=None
@ -11,20 +30,12 @@ def ls(group=None):
else: else:
print("Please perform a scan before") print("Please perform a scan before")
exit(0) exit(0)
# Setup group pattern
pattern=None
if group is not None:
if group in CONF["cluster"]["groups"]:
pattern = re.compile(CONF["cluster"]["groups"][group])
else:
print("Group {} not found".format(group))
sys.exit(1)
# Print nodes # Print nodes
for node in nodes: if group is not None:
if pattern is not None: for node in get_node_in_group(group):
if pattern.match(node): print(node)
print(node) else:
else: for node in nodes:
print(node) print(node)
def scan(timeout): def scan(timeout):

View file

@ -14,7 +14,7 @@ class Config:
"ip4_to": "10.128.0.140", "ip4_to": "10.128.0.140",
"ip4_ignore": ["10.0.0.5", "10.0.0.1"], "ip4_ignore": ["10.0.0.5", "10.0.0.1"],
"groups": { "groups": {
"all": "*" "all": [".*"]
} }
}, },
"plugins": { "ls": "ls -al" }, "plugins": { "ls": "ls -al" },