Load config
[libremanage.git] / libremanage.py
CommitLineData
3f7ab73a
AR
1"""
2libremanage - Lightweight, free software for remote side-chanel server management
3
4Copyright (C) 2018 Alyssa Rosenzweig
5
6This program is free software: you can redistribute it and/or modify
7it under the terms of the GNU Affero General Public License as published by
8the Free Software Foundation, either version 3 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU Affero General Public License for more details.
15
16You should have received a copy of the GNU Affero General Public License
17along with this program. If not, see <https://www.gnu.org/licenses/>.
18"""
93f19e92
AR
19
20import sys
61110684 21import json
d8ddde1a 22import functools
93f19e92 23
b266c229 24USAGE = """
93f19e92
AR
25Usage:
26
27 $ libremanage [server name] [command]
28
29Example:
30
31 $ libremanage web2 reboot
32
33Server names are defined in the accompanying config.py.
34
35Valid commands are as follows:
36
37 - shutdown, reboot, poweron: Power management
9ed4bf16 38 - tty: Open TTY in GNU Screen
93f19e92
AR
39"""
40
61110684
AR
41with open("config.json") as f:
42 CONFIG = json.load(f)
43print(CONFIG)
44
40a688c0
AR
45def die_with_usage(message):
46 print(message)
b266c229
AR
47 print(USAGE)
48 sys.exit(1)
49
40a688c0
AR
50if len(sys.argv) != 3:
51 die_with_usage("Incorrect number of arguments")
52
d8ddde1a
AR
53def get_server_handle(name):
54 # TODO: resolve based on config, SSH in, give self-contained handle?
55 return name
56
57def set_server_power(state, server):
58 print("Setting server " + server + " to power state " + str(state))
59
60COMMANDS = {
e14d7650
AR
61 # Power managemment
62
63 "shutdown": (False, functools.partial(set_server_power, 0)),
64 "poweron": (False, functools.partial(set_server_power, 1)),
65 "reboot": (False, lambda s: (set_server_power(0, s), set_server_power(1, s))),
66
67 # TTY access (or keyboard if wired as such)
68
9ed4bf16 69 "tty": (True, lambda s: print("Screening on " + s))
d8ddde1a
AR
70}
71
d24528e4 72def issue_command(server_name, command):
d8ddde1a 73 server = get_server_handle(server_name)
d24528e4 74 print(server_name, command)
40a688c0
AR
75
76 try:
e14d7650 77 (visible_shell, callback) = COMMANDS[command]
40a688c0
AR
78 except KeyError:
79 die_with_usage("Invalid command supplied")
d24528e4 80
e14d7650
AR
81 print("Shell visible? " + str(visible_shell))
82 callback(server_name)
83
d24528e4 84issue_command(sys.argv[1], sys.argv[2])