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