ansible: Add info command to `manage_jenkins_node.py`
authorKienan Stewart <kstewart@efficios.com>
Mon, 7 Oct 2024 14:19:23 +0000 (10:19 -0400)
committerKienan Stewart <kstewart@efficios.com>
Mon, 7 Oct 2024 14:19:23 +0000 (10:19 -0400)
Change-Id: I0ce0177ae637d56e865594ce253b9ab52619f546
Signed-off-by: Kienan Stewart <kstewart@efficios.com>
automation/ansible/scripts/manage_jenkins_node.py

index 66bb29364df9ec90ecbd253317ce2776e768fce4..50e25bc9aac5c10fb7ef087ed959d9e4e6faeeae 100755 (executable)
@@ -2,8 +2,11 @@
 
 import argparse
 import configparser
+import enum
+import json
 import logging
 import pathlib
+import pprint
 import requests
 import re
 import sys
@@ -13,6 +16,15 @@ import xml.etree.ElementTree
 import jenkins
 
 
+class OutputFormat(enum.Enum):
+    pprint = "pprint"
+    json = "json"
+    pjson = "pjson"  # Pretty json
+
+    def __str__(self):
+        return self.value
+
+
 def get_hypervisor(server, nodes, args):
     found = 0
     for node in nodes:
@@ -31,6 +43,21 @@ def get_hypervisor(server, nodes, args):
         sys.exit(2)
 
 
+def get_info(server, nodes, args):
+    data = []
+    for node in nodes:
+        data.append(server.get_node_info(node["name"]))
+
+    if args.format == OutputFormat.pprint:
+        pprint.PrettyPrinter().pprint(data)
+    elif args.format == OutputFormat.json:
+        print(json.dumps(data))
+    elif args.format == OutputFormat.pjson:
+        print(json.dumps(data, sort_keys=True, indent=4))
+    else:
+        raise Exception("Unknown output format")
+
+
 def toggle_nodes(server, nodes, args, want_offline=True):
     changed = []
     for node in nodes:
@@ -213,6 +240,20 @@ def get_argument_parser():
         "node", default="", help="A python regex to filter nodes by", nargs="?"
     )
 
+    info_parser = subparsers.add_parser("info", help="Get node info")
+    info_parser.set_defaults(callback=get_info)
+    info_parser.add_argument(
+        "node", default="", help="A python regex to filter nodes by", nargs="?"
+    )
+    info_parser.add_argument(
+        "-f",
+        "--format",
+        default="pprint",
+        help="The output format",
+        type=OutputFormat,
+        choices=list(OutputFormat),
+    )
+
     return parser
 
 
This page took 0.024148 seconds and 4 git commands to generate.