Better sanity test
[libremanage.git] / libremanage.py
index 7a59af92da90526b7ebd63cad6765c50c0bd53c6..37e1b59b1aa3af1240f6f00e3dc074d816e6c44d 100644 (file)
@@ -18,6 +18,9 @@ along with this program.  If not, see <https://www.gnu.org/licenses/>.
 """
 
 import sys
+import json
+import functools
+import subprocess
 
 USAGE = """
 Usage:
@@ -33,12 +36,58 @@ Server names are defined in the accompanying config.py.
 Valid commands are as follows:
 
     - shutdown, reboot, poweron: Power management
+    - tty: Open TTY in GNU Screen
 """
 
-#sys.argv[1]
-if len(sys.argv) != 3:
-    print("Incorrect number of arguments")
+with open("config.json") as f:
+    CONFIG = json.load(f)
+
+def open_ssh(config):
+    subprocess.run(["ssh", config["username"] + "@" + config["host"], "-p", str(config["port"])])
+
+open_ssh(CONFIG["managers"]["myboard"])
+print(CONFIG)
+
+def die_with_usage(message):
+    print(message)
     print(USAGE)
     sys.exit(1)
 
-print(len(sys.argv))
+if len(sys.argv) != 3:
+    die_with_usage("Incorrect number of arguments")
+
+def get_server_handle(name):
+    # TODO: resolve based on config, SSH in, give self-contained handle?
+    return name
+
+def set_server_power(state, server):
+    print("Setting server " + server + " to power state " + str(state))
+
+COMMANDS = {
+        # Power managemment
+
+        "shutdown": (False, functools.partial(set_server_power, 0)),
+        "poweron": (False, functools.partial(set_server_power, 1)),
+        "reboot": (False, lambda s: (set_server_power(0, s), set_server_power(1, s))),
+
+        # TTY access (or keyboard if wired as such)
+
+        "tty": (True, lambda s: print("Screening on " + s)),
+
+        # SSH sanity test
+        "console": (True, lambda s: open_ssh(CONFIG["managers"]["myboard"])),
+}
+
+def issue_command(server_name, command):
+    server = get_server_handle(server_name)
+    print(server_name, command)
+
+    try:
+        (visible_shell, callback) = COMMANDS[command]
+    except KeyError:
+        die_with_usage("Invalid command supplied")
+
+    print("Shell visible? " + str(visible_shell))
+    callback(server_name)
+
+issue_command(sys.argv[1], sys.argv[2])