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)
return server
-def gpio_export(server, pin, mode):
- if mode:
- open_ssh(server, "echo " + str(pin) + " > /sys/class/gpio/export")
- open_ssh(server, "echo out > /sys/class/gpio/gpio" + str(pin) + "/direction")
+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:
- open_ssh(server, "echo " + str(pin) + " > /sys/class/gpio/unexport")
+ print("Unknown power type " + conf["type"])
-def gpio_write(server, pin, value):
- open_ssh(server, "echo " + str(value) + " > /sys/class/gpio/gpio" + str(pin) + "/value")
-
-def power_button(server, pin, state):
+def power_button(server, conf, state):
# Hold down the power to force off (via the EC),
# or just flick on to turn on
- gpio_write(server, pin, 1)
- time.sleep(2 if state == POWER_OFF else 0.5)
- gpio_write(server, pin, 0)
+ power_write(server, conf, 1)
+ time.sleep(6 if state == POWER_OFF else 1)
+ power_write(server, conf, 0)
POWER_OFF = 0
POWER_ON = 1
def set_server_power(state, server):
conf = server["power"]
- # TODO: Invert
- # Export pin, configure, write value, unexport
- pin = conf["pin"]
-
- gpio_export(server, pin, True)
-
# Act like a power button
if state == POWER_OFF or state == POWER_ON:
- power_button(server, pin, state)
+ power_button(server, conf, state)
elif state == POWER_REBOOT:
# Requires that we already be online.
- power_button(server, pin, POWER_OFF)
- power_button(server, pin, POWER_ON)
-
- gpio_export(server, pin, False)
+ power_button(server, conf, POWER_OFF)
+ power_button(server, conf, POWER_ON)
def open_tty(s):
if s["tty"]["uncolor"]: