summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoic Guegan <loic.guegan@mailbox.org>2023-10-27 12:37:05 +0200
committerLoic Guegan <loic.guegan@mailbox.org>2023-10-27 12:37:05 +0200
commitf8d4827e49c5066c6a604a1bdb43406e4388f929 (patch)
tree03e9c4223595b79785cabc2e1d67638215e5d6e2
parentbe63ecd7dcd86480b7d134c0b281d5a3160531eb (diff)
Minor changes
-rw-r--r--clusterman/__main__.py5
-rw-r--r--clusterman/commands/frontend.py17
-rw-r--r--clusterman/commands/node.py27
3 files changed, 28 insertions, 21 deletions
diff --git a/clusterman/__main__.py b/clusterman/__main__.py
index 598ea7a..d22786b 100644
--- a/clusterman/__main__.py
+++ b/clusterman/__main__.py
@@ -18,6 +18,9 @@ def main():
# List
node_cmd_list=node_subparsers.add_parser("list")
node_cmd_list.add_argument("-g", "--group" ,help="Group to list")
+ # Exec
+ node_cmd_list=node_subparsers.add_parser("exec")
+ node_cmd_list.add_argument("cmd",help="Command to run",nargs=argparse.REMAINDER)
##### Frontend commands #####
target_frontend = subparsers.add_parser("frontend")
@@ -54,6 +57,8 @@ def main():
node.ls(args.group)
else:
node.ls()
+ elif args.command == "exec":
+ node.exec(args.cmd)
else:
target_node.print_help(sys.stderr)
sys.exit(1)
diff --git a/clusterman/commands/frontend.py b/clusterman/commands/frontend.py
index f275827..4d7ecc4 100644
--- a/clusterman/commands/frontend.py
+++ b/clusterman/commands/frontend.py
@@ -1,28 +1,24 @@
import os, json, re
+from datetime import datetime
from clusterman.config import CONF
-from clusterman.commands.node import get_node_in_group
+import clusterman.commands.node as node
def info():
- nodes=None
- if os.path.exists(CONF.NODE_FILE):
- with open(CONF.NODE_FILE) as f:
- nodes=json.load(f)
-
+ nodes=node.get_node_list()
cache=None
if os.path.exists(CONF.CACHE_FILE):
with open(CONF.CACHE_FILE) as f:
cache=json.load(f)
# Node
- print("Node count: ",end="")
- print("NA") if nodes==None else print(len(nodes))
+ print("Node count: "+str(len(nodes)))
# Groups
print("Node groups: ",end="")
if len(CONF["cluster"]["groups"]) > 0:
content=list()
for group in CONF["cluster"]["groups"].keys():
- content.append("{}({})".format(group,len(get_node_in_group(group))))
+ content.append("{}({})".format(group,len(node.get_node_in_group(group))))
print(", ".join(content))
else:
print("NA")
@@ -31,7 +27,8 @@ def info():
# Cache
print("Last node scan: ",end="")
if cache!=None and "last_scan" in CONF["cache"]:
- print(CONF["cache"]["last_scan"])
+ dt=datetime.fromtimestamp(int(CONF["cache"]["last_scan"]), tz=None)
+ print(dt)
else:
print("NA")
diff --git a/clusterman/commands/node.py b/clusterman/commands/node.py
index f57f743..ddadc49 100644
--- a/clusterman/commands/node.py
+++ b/clusterman/commands/node.py
@@ -1,16 +1,20 @@
import os, json, time, re, sys, subprocess
from clusterman.config import CONF
-
-def get_node_in_group(group):
+def get_node_list():
nodes_path=CONF.NODE_FILE
- nodes=None
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 nodes is not None and group in CONF["cluster"]["groups"]:
+ 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:
@@ -18,16 +22,11 @@ def get_node_in_group(group):
ingroup.append(node)
break;
return ingroup
-
-
def ls(group=None):
nodes_path=CONF.NODE_FILE
- nodes=None
- if os.path.exists(nodes_path):
- with open(nodes_path) as f:
- nodes=json.load(f)
- else:
+ nodes=get_node_list()
+ if len(nodes)<=0:
print("Please perform a scan before")
exit(0)
# Print nodes
@@ -88,3 +87,9 @@ def check(timeout):
print("Success: All nodes are reachable")
else:
print("Error: Some of your nodes are not reachable")
+
+def exec(command):
+ print(["ssh", "-i", CONF["ssh_key_path"], command])
+ for ip in get_node_list():
+ print("----- Node {} -----".format(ip))
+ subprocess.run(["ssh", "-i", CONF["ssh_key_path"],"root@"+ip, " ".join(command)])