Add hidusb relay instructions
[libremanage.git] / libremanage
CommitLineData
bbc21163
AR
1#!/usr/bin/python3
2
3f7ab73a
AR
3"""
4libremanage - Lightweight, free software for remote side-chanel server management
5
6Copyright (C) 2018 Alyssa Rosenzweig
7
8This program is free software: you can redistribute it and/or modify
9it under the terms of the GNU Affero General Public License as published by
10the Free Software Foundation, either version 3 of the License, or
11(at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU Affero General Public License for more details.
17
18You should have received a copy of the GNU Affero General Public License
19along with this program. If not, see <https://www.gnu.org/licenses/>.
20"""
93f19e92 21
b266c229 22USAGE = """
93f19e92
AR
23Usage:
24
25 $ libremanage [server name] [command]
26
27Example:
28
29 $ libremanage web2 reboot
30
31Server names are defined in the accompanying config.py.
32
33Valid commands are as follows:
34
35 - shutdown, reboot, poweron: Power management
9ed4bf16 36 - tty: Open TTY in GNU Screen
a69e9151 37 - sanity, sanity-sh: SSH sanity tests, ignore
60c3f92a
AR
38
39Define a configuration file in ~/.libremanage.json. See the included
40config.json for an example. Servers correspond to managed servers; managers
41correspond to single-board computers connecting the servers. libremanage SSHs
42into the manager to access the server through the side-channel.
93f19e92
AR
43"""
44
22f864bb
AR
45import sys
46import json
47import functools
48import subprocess
21ead159 49import time
60c3f92a 50import os.path
e14c36b3 51
2b7bbcf2 52def open_ssh(server, command, force_tty=False):
28c204f9 53 config = server["ssh"]
60c3f92a
AR
54 args = ["ssh"] + (["-t"] if force_tty else []) + [config["username"] + "@" + config["host"], "-p", str(config["port"]), command]
55 subprocess.run(args)
e14c36b3 56
40a688c0
AR
57def die_with_usage(message):
58 print(message)
b266c229
AR
59 print(USAGE)
60 sys.exit(1)
61
d8ddde1a 62def get_server_handle(name):
9537b802
AR
63 if name == "localhost":
64 return
65
38e0ee81
AR
66 try:
67 server = CONFIG["servers"][name]
68 except KeyError:
69 die_with_usage("Unknown server, please configure")
70
71 # Associate manager configuration
82ee0c31 72 server["ssh"] = CONFIG["managers"][server["manager"]]
38e0ee81 73
6e59af0c
AR
74 # Meta access
75 server["name"] = name
76
82ee0c31 77 return server
d8ddde1a 78
21ead159
AR
79def gpio_export(server, pin, mode):
80 if mode:
81 open_ssh(server, "echo " + str(pin) + " > /sys/class/gpio/export")
82 open_ssh(server, "echo out > /sys/class/gpio/gpio" + str(pin) + "/direction")
83 else:
84 open_ssh(server, "echo " + str(pin) + " > /sys/class/gpio/unexport")
85
86def gpio_write(server, pin, value):
87 open_ssh(server, "echo " + str(value) + " > /sys/class/gpio/gpio" + str(pin) + "/value")
88
89def power_button(server, pin, state):
90 # Hold down the power to force off (via the EC),
91 # or just flick on to turn on
92
93 gpio_write(server, pin, 1)
94 time.sleep(2 if state == POWER_OFF else 0.5)
95 gpio_write(server, pin, 0)
96
97POWER_OFF = 0
98POWER_ON = 1
99POWER_REBOOT = 2
100
d8ddde1a 101def set_server_power(state, server):
e799dcd1
AR
102 conf = server["power"]
103
21ead159 104 # TODO: Invert
c10fa78c 105 # Export pin, configure, write value, unexport
21ead159
AR
106 pin = conf["pin"]
107
108 gpio_export(server, pin, True)
109
110 # Act like a power button
111
112 if state == POWER_OFF or state == POWER_ON:
113 power_button(server, pin, state)
114 elif state == POWER_REBOOT:
115 # Requires that we already be online.
116 power_button(server, pin, POWER_OFF)
117 power_button(server, pin, POWER_ON)
118
119 gpio_export(server, pin, False)
d8ddde1a 120
6e59af0c
AR
121def open_tty(s):
122 if s["tty"]["uncolor"]:
123 # Broken serial port, workaround TTY garbage with libremanage-serial
85edc121 124 subprocess.run(["libremanage-serial", s["name"]])
6e59af0c
AR
125 else:
126 # Use native GNU screen
127 return open_ssh(s, "screen " + s["tty"]["file"] + " " + str(s["tty"]["baud"]), force_tty=True),
128
d8ddde1a 129COMMANDS = {
e14d7650
AR
130 # Power managemment
131
21ead159
AR
132 "shutdown": functools.partial(set_server_power, POWER_OFF),
133 "poweron": functools.partial(set_server_power, POWER_ON),
134 "reboot": functools.partial(set_server_power, POWER_REBOOT),
e14d7650
AR
135
136 # TTY access (or keyboard if wired as such)
137
6e59af0c 138 "tty": open_tty,
bc9c5ec0
AR
139 "tty-baud": lambda s: open_ssh(s, "stty -F "+ s["tty"]["file"] + " " + str(s["tty"]["baud"])),
140 "tty-read": lambda s: open_ssh(s, "cat " + s["tty"]["file"], force_tty=True),
8b1252ae 141 "tty-write": lambda s: open_ssh(s, "stdbuf -o0 cat > " + s["tty"]["file"], force_tty=True),
e14c36b3 142
da2eb513 143 # SSH sanity tests
82ee0c31 144
28c204f9 145 "sanity": lambda s: open_ssh(s, "whoami"),
a69e9151 146 "sanity-sh": lambda s: open_ssh(s, ""),
d8ddde1a
AR
147}
148
d24528e4 149def issue_command(server_name, command):
d8ddde1a 150 server = get_server_handle(server_name)
40a688c0
AR
151
152 try:
da2eb513 153 callback = COMMANDS[command]
40a688c0
AR
154 except KeyError:
155 die_with_usage("Invalid command supplied")
d24528e4 156
82ee0c31 157 callback(server)
e14d7650 158
22f864bb
AR
159# Load configuration, get command, and go!
160
60c3f92a
AR
161try:
162 with open(os.path.expanduser("~/.libremanage.json")) as f:
163 CONFIG = json.load(f)
164except FileNotFoundError:
165 die_with_usage("Configuration file missing in ~/.libremanage.json")
22f864bb
AR
166
167if len(sys.argv) != 3:
168 die_with_usage("Incorrect number of arguments")
169
d24528e4 170issue_command(sys.argv[1], sys.argv[2])