Expose nicely
[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):
38e0ee81
AR
63 try:
64 server = CONFIG["servers"][name]
65 except KeyError:
66 die_with_usage("Unknown server, please configure")
67
68 # Associate manager configuration
82ee0c31 69 server["ssh"] = CONFIG["managers"][server["manager"]]
38e0ee81 70
6e59af0c
AR
71 # Meta access
72 server["name"] = name
73
82ee0c31 74 return server
d8ddde1a 75
21ead159
AR
76def gpio_export(server, pin, mode):
77 if mode:
78 open_ssh(server, "echo " + str(pin) + " > /sys/class/gpio/export")
79 open_ssh(server, "echo out > /sys/class/gpio/gpio" + str(pin) + "/direction")
80 else:
81 open_ssh(server, "echo " + str(pin) + " > /sys/class/gpio/unexport")
82
83def gpio_write(server, pin, value):
84 open_ssh(server, "echo " + str(value) + " > /sys/class/gpio/gpio" + str(pin) + "/value")
85
86def power_button(server, pin, state):
87 # Hold down the power to force off (via the EC),
88 # or just flick on to turn on
89
90 gpio_write(server, pin, 1)
91 time.sleep(2 if state == POWER_OFF else 0.5)
92 gpio_write(server, pin, 0)
93
94POWER_OFF = 0
95POWER_ON = 1
96POWER_REBOOT = 2
97
d8ddde1a 98def set_server_power(state, server):
e799dcd1
AR
99 conf = server["power"]
100
21ead159 101 # TODO: Invert
c10fa78c 102 # Export pin, configure, write value, unexport
21ead159
AR
103 pin = conf["pin"]
104
105 gpio_export(server, pin, True)
106
107 # Act like a power button
108
109 if state == POWER_OFF or state == POWER_ON:
110 power_button(server, pin, state)
111 elif state == POWER_REBOOT:
112 # Requires that we already be online.
113 power_button(server, pin, POWER_OFF)
114 power_button(server, pin, POWER_ON)
115
116 gpio_export(server, pin, False)
d8ddde1a 117
6e59af0c
AR
118def open_tty(s):
119 if s["tty"]["uncolor"]:
120 # Broken serial port, workaround TTY garbage with libremanage-serial
121 subprocess.run(["libremanage-serial", s["name"])
122 else:
123 # Use native GNU screen
124 return open_ssh(s, "screen " + s["tty"]["file"] + " " + str(s["tty"]["baud"]), force_tty=True),
125
d8ddde1a 126COMMANDS = {
e14d7650
AR
127 # Power managemment
128
21ead159
AR
129 "shutdown": functools.partial(set_server_power, POWER_OFF),
130 "poweron": functools.partial(set_server_power, POWER_ON),
131 "reboot": functools.partial(set_server_power, POWER_REBOOT),
e14d7650
AR
132
133 # TTY access (or keyboard if wired as such)
134
6e59af0c 135 "tty": open_tty,
bc9c5ec0
AR
136 "tty-baud": lambda s: open_ssh(s, "stty -F "+ s["tty"]["file"] + " " + str(s["tty"]["baud"])),
137 "tty-read": lambda s: open_ssh(s, "cat " + s["tty"]["file"], force_tty=True),
8b1252ae 138 "tty-write": lambda s: open_ssh(s, "stdbuf -o0 cat > " + s["tty"]["file"], force_tty=True),
e14c36b3 139
da2eb513 140 # SSH sanity tests
82ee0c31 141
28c204f9 142 "sanity": lambda s: open_ssh(s, "whoami"),
a69e9151 143 "sanity-sh": lambda s: open_ssh(s, ""),
d8ddde1a
AR
144}
145
d24528e4 146def issue_command(server_name, command):
d8ddde1a 147 server = get_server_handle(server_name)
40a688c0
AR
148
149 try:
da2eb513 150 callback = COMMANDS[command]
40a688c0
AR
151 except KeyError:
152 die_with_usage("Invalid command supplied")
d24528e4 153
82ee0c31 154 callback(server)
e14d7650 155
22f864bb
AR
156# Load configuration, get command, and go!
157
60c3f92a
AR
158try:
159 with open(os.path.expanduser("~/.libremanage.json")) as f:
160 CONFIG = json.load(f)
161except FileNotFoundError:
162 die_with_usage("Configuration file missing in ~/.libremanage.json")
22f864bb
AR
163
164if len(sys.argv) != 3:
165 die_with_usage("Incorrect number of arguments")
166
d24528e4 167issue_command(sys.argv[1], sys.argv[2])