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 | |
d8ddde1a | 21 | import functools |
93f19e92 | 22 | |
b266c229 | 23 | USAGE = """ |
93f19e92 AR |
24 | Usage: |
25 | ||
26 | $ libremanage [server name] [command] | |
27 | ||
28 | Example: | |
29 | ||
30 | $ libremanage web2 reboot | |
31 | ||
32 | Server names are defined in the accompanying config.py. | |
33 | ||
34 | Valid commands are as follows: | |
35 | ||
36 | - shutdown, reboot, poweron: Power management | |
e14d7650 | 37 | - screen: Open TTY in GNU Screen |
93f19e92 AR |
38 | """ |
39 | ||
40a688c0 AR |
40 | def die_with_usage(message): |
41 | print(message) | |
b266c229 AR |
42 | print(USAGE) |
43 | sys.exit(1) | |
44 | ||
40a688c0 AR |
45 | if len(sys.argv) != 3: |
46 | die_with_usage("Incorrect number of arguments") | |
47 | ||
d8ddde1a AR |
48 | def get_server_handle(name): |
49 | # TODO: resolve based on config, SSH in, give self-contained handle? | |
50 | return name | |
51 | ||
52 | def set_server_power(state, server): | |
53 | print("Setting server " + server + " to power state " + str(state)) | |
54 | ||
55 | COMMANDS = { | |
e14d7650 AR |
56 | # Power managemment |
57 | ||
58 | "shutdown": (False, functools.partial(set_server_power, 0)), | |
59 | "poweron": (False, functools.partial(set_server_power, 1)), | |
60 | "reboot": (False, lambda s: (set_server_power(0, s), set_server_power(1, s))), | |
61 | ||
62 | # TTY access (or keyboard if wired as such) | |
63 | ||
64 | "screen": (True, lambda s: print("Screening on " + s)) | |
d8ddde1a AR |
65 | } |
66 | ||
d24528e4 | 67 | def issue_command(server_name, command): |
d8ddde1a | 68 | server = get_server_handle(server_name) |
d24528e4 | 69 | print(server_name, command) |
40a688c0 AR |
70 | |
71 | try: | |
e14d7650 | 72 | (visible_shell, callback) = COMMANDS[command] |
40a688c0 AR |
73 | except KeyError: |
74 | die_with_usage("Invalid command supplied") | |
d24528e4 | 75 | |
e14d7650 AR |
76 | print("Shell visible? " + str(visible_shell)) |
77 | callback(server_name) | |
78 | ||
d24528e4 | 79 | issue_command(sys.argv[1], sys.argv[2]) |