""" 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 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 """ 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 = { "shutdown": functools.partial(set_server_power, 0), "poweron": functools.partial(set_server_power, 1), "reboot": lambda s: (set_server_power(0, s), set_server_power(1, s)) } def issue_command(server_name, command): server = get_server_handle(server_name) print(server_name, command) try: COMMANDS[command](server_name) except KeyError: die_with_usage("Invalid command supplied") issue_command(sys.argv[1], sys.argv[2])