4 libremanage - Lightweight, free software for remote side-chanel server management
6 Copyright (C) 2018 Alyssa Rosenzweig
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.
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.
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/>.
25 $ libremanage [server name] [command]
29 $ libremanage web2 reboot
31 Server names are defined in the accompanying config.py.
33 Valid commands are as follows:
35 - shutdown, reboot, poweron: Power management
36 - tty: Open TTY in GNU Screen
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
])
48 def die_with_usage(message
):
53 def get_server_handle(name
):
55 server
= CONFIG
["servers"][name
]
57 die_with_usage("Unknown server, please configure")
59 # Associate manager configuration
60 server
["ssh"] = CONFIG
["managers"][server
["manager"]]
64 def set_server_power(state
, server
):
65 conf
= server
["power"]
67 # Set invert to write LOW for power on and HIGH for off
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")
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
)),
84 # TTY access (or keyboard if wired as such)
86 "tty": lambda s
: open_ssh(s
, "screen " + s
["tty"]["file"] + " " + str(s
["tty"]["baud"]), force_tty
=True),
90 "sanity": lambda s
: open_ssh(s
, "whoami"),
91 "console": lambda s
: open_ssh(s
, ""),
94 def issue_command(server_name
, command
):
95 server
= get_server_handle(server_name
)
98 callback
= COMMANDS
[command
]
100 die_with_usage("Invalid command supplied")
104 # Load configuration, get command, and go!
106 with
open("config.json") as f
:
107 CONFIG
= json
.load(f
)
109 if len(sys
.argv
) != 3:
110 die_with_usage("Incorrect number of arguments")
112 issue_command(sys
.argv
[1], sys
.argv
[2])