+x and shebang
[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):
45 config = server["ssh"]
46 subprocess.run(["ssh", "-t", 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 print("Setting to power state " + str(state))
66 conf = server["power"]
67
68 # Set invert to write LOW for power on and HIGH for off
69 if conf["invert"]:
70 state = 1 - state
71
72 print(conf["pin"])
73
74 # Export pin, configure, write value, unexport
75 open_ssh(server, "echo " + str(conf["pin"]) + " > /sys/class/gpio/export")
76 open_ssh(server, "echo out > /sys/class/gpio/gpio" + str(conf["pin"]) + "/direction")
77 open_ssh(server, "echo " + str(state) + " > /sys/class/gpio/gpio" + str(conf["pin"]) + "/value")
78 open_ssh(server, "echo " + str(conf["pin"]) + " > /sys/class/gpio/unexport")
79
80 COMMANDS = {
81 # Power managemment
82
83 "shutdown": functools.partial(set_server_power, 0),
84 "poweron": functools.partial(set_server_power, 1),
85 "reboot": lambda s: (set_server_power(0, s), set_server_power(1, s)),
86
87 # TTY access (or keyboard if wired as such)
88
89 "tty": lambda s: open_ssh(s, "screen " + s["tty"]["file"] + " " + str(s["tty"]["baud"])),
90
91 # SSH sanity tests
92
93 "sanity": lambda s: open_ssh(s, "whoami"),
94 "console": lambda s: open_ssh(s, ""),
95 }
96
97 def issue_command(server_name, command):
98 server = get_server_handle(server_name)
99 print(server_name, command)
100
101 try:
102 callback = COMMANDS[command]
103 except KeyError:
104 die_with_usage("Invalid command supplied")
105
106 callback(server)
107
108 # Load configuration, get command, and go!
109
110 with open("config.json") as f:
111 CONFIG = json.load(f)
112
113 if len(sys.argv) != 3:
114 die_with_usage("Incorrect number of arguments")
115
116 issue_command(sys.argv[1], sys.argv[2])