Remove debug prints
[libremanage.git] / libremanage
1 #!/usr/bin/python3
2
3 """
4 libremanage - Lightweight, free software for remote side-chanel server management
5
6 Copyright (C) 2018 Alyssa Rosenzweig
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU Affero General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Affero General Public License for more details.
17
18 You should have received a copy of the GNU Affero General Public License
19 along with this program. If not, see <https://www.gnu.org/licenses/>.
20 """
21
22 USAGE = """
23 Usage:
24
25 $ libremanage [server name] [command]
26
27 Example:
28
29 $ libremanage web2 reboot
30
31 Server names are defined in the accompanying config.py.
32
33 Valid commands are as follows:
34
35 - shutdown, reboot, poweron: Power management
36 - tty: Open TTY in GNU Screen
37 """
38
39 import sys
40 import json
41 import functools
42 import subprocess
43
44 def open_ssh(server, command, force_tty=False):
45 config = server["ssh"]
46 subprocess.run(["ssh", "-t" if force_tty else "", config["username"] + "@" + config["host"], "-p", str(config["port"]), command])
47
48 def die_with_usage(message):
49 print(message)
50 print(USAGE)
51 sys.exit(1)
52
53 def get_server_handle(name):
54 try:
55 server = CONFIG["servers"][name]
56 except KeyError:
57 die_with_usage("Unknown server, please configure")
58
59 # Associate manager configuration
60 server["ssh"] = CONFIG["managers"][server["manager"]]
61
62 return server
63
64 def set_server_power(state, server):
65 conf = server["power"]
66
67 # Set invert to write LOW for power on and HIGH for off
68 if conf["invert"]:
69 state = 1 - state
70
71 # Export pin, configure, write value, unexport
72 open_ssh(server, "echo " + str(conf["pin"]) + " > /sys/class/gpio/export")
73 open_ssh(server, "echo out > /sys/class/gpio/gpio" + str(conf["pin"]) + "/direction")
74 open_ssh(server, "echo " + str(state) + " > /sys/class/gpio/gpio" + str(conf["pin"]) + "/value")
75 open_ssh(server, "echo " + str(conf["pin"]) + " > /sys/class/gpio/unexport")
76
77 COMMANDS = {
78 # Power managemment
79
80 "shutdown": functools.partial(set_server_power, 0),
81 "poweron": functools.partial(set_server_power, 1),
82 "reboot": lambda s: (set_server_power(0, s), set_server_power(1, s)),
83
84 # TTY access (or keyboard if wired as such)
85
86 "tty": lambda s: open_ssh(s, "screen " + s["tty"]["file"] + " " + str(s["tty"]["baud"]), force_tty=True),
87
88 # SSH sanity tests
89
90 "sanity": lambda s: open_ssh(s, "whoami"),
91 "console": lambda s: open_ssh(s, ""),
92 }
93
94 def issue_command(server_name, command):
95 server = get_server_handle(server_name)
96
97 try:
98 callback = COMMANDS[command]
99 except KeyError:
100 die_with_usage("Invalid command supplied")
101
102 callback(server)
103
104 # Load configuration, get command, and go!
105
106 with open("config.json") as f:
107 CONFIG = json.load(f)
108
109 if len(sys.argv) != 3:
110 die_with_usage("Incorrect number of arguments")
111
112 issue_command(sys.argv[1], sys.argv[2])