From: Ian Kelling Date: Mon, 4 Mar 2019 17:11:06 +0000 (-0500) Subject: finish deprecation of old libremanage command X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=4e2acbd13e0b431ee4698d5bfe33df245b821640;p=libremanage.git finish deprecation of old libremanage command --- diff --git a/README b/README index eb988e1..0aa3c39 100644 --- a/README +++ b/README @@ -2,26 +2,17 @@ First, run hidusb-relay-cmd-setup on the device its connected to. Ian Kelling also has a package for openwrt, not yet published. Ask him if you are interested. -relay-cmd is a simple script config to use the relay from the device its -connected to. - -libremanage is an overly complicated command that is deprecated. Some -problems with it: - -* IMPORTANT: libremanage right now does ssh relay command 1; sleep; ssh - relay command 2. That whole thing needs to be made into a local script - or else if the second ssh command fails, the relay stays in the state - that's supposed to be temporary, making the server impossible to turn - on without unplugging the relay from it's power and usb ports. -* in the config, most options should have defaults. for managers, - host/port/username aren't really needed, ssh_config can take care of - that. -* .libremanage.json should be toml or yaml so it can have comments -* there should be a state command which runs hidusb-relay-cmd state -* The script's function arguments should be made more clear / documented Background: GNUtoo has been working on a similar setup. see https://wiki.parabola.nu/Standalone_BMC. fsf sysadmins, see rt 1346340 -for further development ideas. \ No newline at end of file +for further development ideas. + + +libremanage-color and libremanage-serial were for dealing with console +issues in coreboot. However, most or all of the problems they solved +were due to alyssa's mistake of trying to make it work over an "ssh +command" which was not connected to a terminal. Simply sshing, then +doing screen /dev/ttyUSB0 115200, etc might fix all that. Once I test +that out, I will remove these files. diff --git a/config.json b/config.json deleted file mode 100644 index 4508088..0000000 --- a/config.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "servers": { - "web2": { - "manager": "myboard", - "tty": { - "uncolor": false, - "file": "/dev/ttyUSB0", - "baud": 115200 - }, - "power": { - "type": "hidusb-relay-cmd", - "relay": "HURTM", - "channel": 2 - } - } - }, - "managers": { - "myboard": { - "host": "192.168.0.250", - "port": 22, - "username": "root" - } - } -} diff --git a/hidusb-relay-cmd-setup b/hidusb-relay-cmd-setup index 73ac1fc..275832d 100755 --- a/hidusb-relay-cmd-setup +++ b/hidusb-relay-cmd-setup @@ -19,7 +19,7 @@ cd commandline/makemake/ apt update apt-get install libusb-dev libusb-1.0-0-dev make -cp hidusb-relay-cmd /usr/local/bin +cp relay-cmd hidusb-relay-cmd /usr/local/bin cat <<'EOF' the output should be similar to this: diff --git a/libremanage b/libremanage index 724c14d..1653d2e 100755 --- a/libremanage +++ b/libremanage @@ -1,160 +1,136 @@ -#!/usr/bin/python3 +#!/bin/bash -""" -libremanage - Lightweight, free software for remote side-chanel server management +trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR +set -eE -o pipefail -Copyright (C) 2018 Alyssa Rosenzweig - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU Affero General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU Affero General Public License for more details. - -You should have received a copy of the GNU Affero General Public License -along with this program. If not, see . -""" - -USAGE = """ -Usage: - - $ libremanage [server name] [command] - -Example: - - $ libremanage web2 reboot - -Server names are defined in the accompanying config.py. - -Valid commands are as follows: - - - shutdown, reboot, poweron: Power management - - tty: Open TTY in GNU Screen - - sanity, sanity-sh: SSH sanity tests, which will whoami or just run sh. - -Define a configuration file in ~/.libremanage.json. See the included -config.json for an example. Servers correspond to managed servers; managers -correspond to single-board computers connecting the servers. libremanage SSHs -into the manager to access the server through the side-channel. -""" - -import sys -import json -import functools -import subprocess -import time -import os.path - -def open_ssh(server, command, force_tty=False): - print(command) - config = server["ssh"] - args = ["ssh"] + (["-t"] if force_tty else []) + [config["username"] + "@" + config["host"], "-p", str(config["port"]), command] - subprocess.run(args) - -def die_with_usage(message): - print(message) - print(USAGE) - sys.exit(1) - -def get_server_handle(name): - if name == "localhost": - return - - try: - server = CONFIG["servers"][name] - except KeyError: - die_with_usage("Unknown server, please configure") - - # Associate manager configuration - server["ssh"] = CONFIG["managers"][server["manager"]] - - # Meta access - server["name"] = name - - return server - -def power_write(server, conf, state): - if conf["type"] == "hidusb-relay-cmd": - verb = "on" if state == 1 else "off" - open_ssh(server, "hidusb-relay-cmd ID=" + conf["relay"] + " " + verb + " " + str(conf["channel"])) - else: - print("Unknown power type " + conf["type"]) - -def power_button(server, conf, state): - # Hold down the power to force off (via the EC), - # or just flick on to turn on - - power_write(server, conf, 1) - time.sleep(6 if state == POWER_OFF else 1) - power_write(server, conf, 0) +# verbose command +v() { + printf "$0 running: %s\n" "$*" + "$@" +} -POWER_OFF = 0 -POWER_ON = 1 -POWER_REBOOT = 2 -def set_server_power(state, server): - conf = server["power"] +usage() { + cat < " + s["tty"]["file"], force_tty=True), - - # SSH sanity tests - - "sanity": lambda s: open_ssh(s, "whoami"), - "sanity-sh": lambda s: open_ssh(s, ""), +EOF + if [[ -e /etc/libremanage.conf ]]; then + echo "Current config:" + v cat /etc/libremanage.conf + else + echo "Note: /etc/libremanage.conf does not exist on the current machine" + fi + exit 0 } -def issue_command(server_name, command): - server = get_server_handle(server_name) - - try: - callback = COMMANDS[command] - except KeyError: - die_with_usage("Invalid command supplied") - - callback(server) - -# Load configuration, get command, and go! - -try: - with open(os.path.expanduser("~/.libremanage.json")) as f: - CONFIG = json.load(f) -except FileNotFoundError: - die_with_usage("Configuration file missing in ~/.libremanage.json") - -if len(sys.argv) != 3: - die_with_usage("Incorrect number of arguments") -issue_command(sys.argv[1], sys.argv[2]) +if (( $# < 2 )); then + usage +fi + +read action chan board_id <<<"$@" + +if [[ -e /etc/libremanage.conf ]]; then + source /etc/libremanage.conf +fi + +# we know HOSTNAME is given if it doesn't start with a number. +if [[ $chan != [0-9]* ]]; then + if [[ ! $board_id ]]; then + board_id_var=${chan}_board_id + board_id=${!board_id_var} + fi + chan_var=${chan}_channel + chan=${!chan_var} + + if [[ ! $chan ]]; then + echo "error: channel not found in /etc/libremanage.conf" >&2 + exit 1 + fi +fi + + +if hidusb-relay-cmd state | grep ON; then + printf "%s\n" "WARNING: output from hidusb-relay-cmd state shows an ON relay. this could mean another command instance is running, or it got stuck on due to an uncompleted command." >&2 + # TODO: exit in this case and print out the command to turn it off. +fi + + +case $(hidusb-relay-cmd state|wc -l) in + 0) + echo "error: got 0 lines from running hidusb-relay-cmd state" >&2 + exit 1 + ;; + 1) : ;; + *) + if [[ ! $board_id ]]; then + echo "error: more than 1 relay device, so passing its id is required" >&2 + exit 1 + fi + ;; +esac + + +if [[ $board_id ]]; then + # leave this as an empty var if its not passed + board_id_arg="id=$board_id" +fi + +# ignore errors and continue on if a command fails from here +set +eE +o pipefail +# ignore hup so we complete even if there is a connection problem, and +# force anyone to kill -9 so we might complete in more cases, for +# example if a reboot is happening +trap '' HUP INT QUIT TERM +echo "$0: doing $action. shell commands will be printed to the terminal." +case $action in + poweroff) + v hidusb-relay-cmd $board_id_arg on $chan + v sleep 6 + v hidusb-relay-cmd $board_id_arg off $chan + ;; + poweron) + v hidusb-relay-cmd $board_id_arg on $chan + v sleep 1 + v hidusb-relay-cmd $board_id_arg off $chan + ;; + reboot) + v hidusb-relay-cmd $board_id_arg on $chan + v sleep 6 + v hidusb-relay-cmd $board_id_arg off $chan + v sleep 1 + v hidusb-relay-cmd $board_id_arg on $chan + v sleep 1 + v hidusb-relay-cmd $board_id_arg off $chan + ;; + *) + echo "error: action arg not supported" >&2 + exit 1 +esac +echo "$0: script ended. exiting" diff --git a/libremanage-serial b/libremanage-serial index 4ebf0dd..f612725 100755 --- a/libremanage-serial +++ b/libremanage-serial @@ -1,4 +1,5 @@ #!/bin/sh +# DO NOT USE. See README libremanage $1 tty-baud tmux new-session -d "bash -c 'libremanage $1 tty-read | libremanage-uncolor'" tmux split-window -h "libremanage $1 tty-write" diff --git a/libremanage-uncolor b/libremanage-uncolor index 96081bc..a2ee1f7 100755 --- a/libremanage-uncolor +++ b/libremanage-uncolor @@ -1,4 +1,5 @@ #!/usr/bin/env perl +# DO NOT USE. See README ## uncolor ? remove terminal escape sequences such as color changes ## Source: https://unix.stackexchange.com/questions/14684/removing-control-chars-including-console-codes-colours-from-script-output by Gilles while (<>) { diff --git a/relay-cmd b/relay-cmd deleted file mode 100755 index 46c6924..0000000 --- a/relay-cmd +++ /dev/null @@ -1,112 +0,0 @@ -#!/bin/bash - -trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR - -usage() { - cat <&2 - exit 1 - fi -fi - - -if hidusb-relay-cmd state | grep ON; then - printf "%s\n" "WARNING: output from hidusb-relay-cmd state shows an ON relay. this could mean another command instance is running, or it got stuck on due to an uncompleted command." >&2 - # TODO: exit in this case and print out the command to turn it off. -fi - - -case $(hidusb-relay-cmd state|wc -l) in - 0) - echo "error: got 0 lines from running hidusb-relay-cmd state" >&2 - exit 1 - ;; - 1) : ;; - *) - if [[ ! $relay_id ]]; then - echo "error: more than 1 relay device, so passing its id is required" >&2 - exit 1 - fi - ;; -esac - - -if [[ $relay_id ]]; then - # leave this as an empty var if its not passed - relay_id_arg="id=$relay_id" -fi - -# verbose command -v() { - printf "$0 running: %s\n" "$*" - "$@" -} - - -# ignore hup so we complete even if there is a connection problem. -trap '' HUP -echo "$0: doing $action. shell commands will be printed to the terminal." -case $action in - poweroff) - v hidusb-relay-cmd $relay_id_arg on $chan - v sleep 6 - v hidusb-relay-cmd $relay_id_arg off $chan - ;; - poweron) - v hidusb-relay-cmd $relay_id_arg on $chan - v sleep 1 - v hidusb-relay-cmd $relay_id_arg off $chan - ;; - reboot) - v hidusb-relay-cmd $relay_id_arg on $chan - v sleep 6 - v hidusb-relay-cmd $relay_id_arg off $chan - v sleep 1 - v hidusb-relay-cmd $relay_id_arg on $chan - v sleep 1 - v hidusb-relay-cmd $relay_id_arg off $chan - ;; - *) - echo "error: action arg not supported" >&2 - exit 1 -esac -echo "$0: script ended. exiting"