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 print("Setting to power state " + str(state
))
66 conf
= server
["power"]
68 # Set invert to write LOW for power on and HIGH for off
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")
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
)),
87 # TTY access (or keyboard if wired as such)
89 "tty": lambda s
: open_ssh(s
, "screen " + s
["tty"]["file"] + " " + str(s
["tty"]["baud"]), force_tty
=True),
93 "sanity": lambda s
: open_ssh(s
, "whoami"),
94 "console": lambda s
: open_ssh(s
, ""),
97 def issue_command(server_name
, command
):
98 server
= get_server_handle(server_name
)
99 print(server_name
, command
)
102 callback
= COMMANDS
[command
]
104 die_with_usage("Invalid command supplied")
108 # Load configuration, get command, and go!
110 with
open("config.json") as f
:
111 CONFIG
= json
.load(f
)
113 if len(sys
.argv
) != 3:
114 die_with_usage("Incorrect number of arguments")
116 issue_command(sys
.argv
[1], sys
.argv
[2])