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