""" libremanage - Lightweight, free software for remote side-chanel server management Copyright (C) 2018 Alyssa Rosenzweig This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . """ import sys import json import functools USAGE = """ Usage: $ libremanage [server name] [command] Example: $ libremanage web2 reboot 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 """ with open("config.json") as f: CONFIG = json.load(f) print(CONFIG) def die_with_usage(message): print(message) print(USAGE) sys.exit(1) 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)) } 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])