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