summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore6
-rw-r--r--Makefile62
-rw-r--r--index.php.in89
-rw-r--r--lights-out-init.service.in10
-rw-r--r--lights-out.conf8
-rw-r--r--lights-out.in135
-rw-r--r--lights-out@.service.in8
7 files changed, 316 insertions, 2 deletions
diff --git a/.gitignore b/.gitignore
index 0191024..b9bf5ae 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,4 @@
-.ssh
-password
+index.php
+lights-out-init.service
+lights-out
+lights-out@.service
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..d1a9a3c
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,62 @@
+#
+# lights-out - simple skripts for lights-out like management from a raspi
+#
+# Copyright (c) 2021 Erich Eckner <opensource at eckner dot net>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 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 General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+# USA.
+#
+
+SHELL = /bin/bash
+DESTDIR =
+ETCDIR = /etc
+BINDIR = /usr/bin
+LIBDIR = /usr/lib
+HTTPDIR = /srv/http/lights-out
+
+VERSION = 0.2
+
+all: index.php lights-out lights-out-init.service lights-out@.service
+
+%: %.in
+ sed " \
+ s/#VERSION#/$(VERSION)/; \
+ s@#BINDIR#@$(BINDIR)@; \
+ s@#ETCDIR#@$(ETCDIR)@; \
+ s@#HELPTEXT#\(\s\+\)#@ --help \1display this help and exit\n --version\1display version and exit@; \
+ " $< > $@ && \
+ ( [[ "$@" = *.* ]] || chmod +x "$@" )
+
+.PHONY: install dist clean
+
+install: all
+ install -D -m0755 -t $(DESTDIR)$(BINDIR) lights-out
+ install -D -m0644 -t $(DESTDIR)$(LIBDIR)/systemd/system lights-out@.service lights-out-init.service
+ install -D -m0644 -t $(DESTDIR)$(ETCDIR) lights-out.conf
+ install -D -m0644 -t $(DESTDIR)$(HTTPDIR)/httpdocs index.php
+ install -D -m0600 -o http -g http /dev/null $(DESTDIR)$(HTTPDIR)/password
+
+clean:
+ git clean -x -d -f
+
+dist: clean
+ git status --porcelain 2> /dev/null | grep -q "\S" && (git add .; git commit -m"neue Version: $(VERSION)") || true
+ ! git tag -d v$(VERSION) 2> /dev/null
+ git tag v$(VERSION)
+ knock-knock || true
+ git push
+ git push --tags
+
+# End of file
diff --git a/index.php.in b/index.php.in
new file mode 100644
index 0000000..efa6b85
--- /dev/null
+++ b/index.php.in
@@ -0,0 +1,89 @@
+<?php
+
+$f = popen('#BINDIR#/lights-out get power', 'r');
+if ($f === false) {
+ print 'Cannot getpower status' . "\n";
+ die();
+}
+$power = trim(fgets($f));
+pclose($f);
+
+if ($power == '') {
+ print 'Cannot read gpio - is it installed?' . "\n";
+ die();
+}
+
+$f = popen('#BINDIR#/lights-out name', 'r');
+if ($f === false) {
+ print 'Error reading server name' . "\n";
+ die();
+}
+$server = trim(fgets($f));
+pclose($f);
+
+if ($server == '') {
+ print 'Empty server name in config' . "\n";
+ die();
+}
+
+if (array_key_exists('HTTPS', $_SERVER)
+&& ($_SERVER['HTTPS'] == 'on'))
+ $https = true;
+else
+ $https = false;
+
+function push_it() {
+ if (!array_key_exists('password', $_POST))
+ return;
+ $f = fopen('../password', 'r');
+ if ($f === false) {
+ print '<font color="ff0000">Cannot read password file</font><br>' . "\n";
+ return;
+ }
+ $saved_hash = trim(fgets($f));
+ fclose($f);
+ $entered_hash = $_POST['password'];
+ for ($i=0; $i<1000; $i++)
+ $entered_hash = hash('sha512', $entered_hash . $i . $_POST['password']);
+ if ($saved_hash != $entered_hash) {
+ print '<font color="ff0000">Wrong password: ' . $entered_hash . '</font><br>' . "\n";
+ return;
+ }
+ foreach ($_POST as $key => $dummy)
+ if (preg_match('/^push_([0-9]+)$/', $key, $duration))
+ break;
+ if (count($duration) != 2)
+ return;
+ $duration = $duration[1];
+ if (($duration > 0)
+ && ($duration < 300)) {
+ print 'pushing power button for ' . ($duration * 0.1) . ' seconds ... <br>' . "\n";
+ flush();
+ shell_exec('#BINDIR#/lights-out push power ' . ($duration * 0.1));
+ print '... done<br>' . "\n";
+ }
+}
+
+print '<html>' . "\n";
+print '<head>' . "\n";
+print '<title>fenster lights-out</title>' . "\n";
+if (array_key_exists('password', $_POST))
+ print '<meta http-equiv="refresh" content="1;/">' . "\n";
+print '</head>' . "\n";
+print '<body>' . "\n";
+print 'Fenster is currently ';
+if ($power == '0')
+ print '<font color="ff0000">off</font>';
+else
+ print '<font color="00ff00">on</font>';
+print '<br>' . "\n";
+if ($https) {
+ push_it();
+ print '<form action="" method="post">' . "\n";
+ print 'Passwort: <input name="password" type="password"><br>' . "\n";
+ print '<input type="submit" name="push_1" value="short power button push"><br>' . "\n";
+ print '<input type="submit" name="push_150" value="long power button push"><br>' . "\n";
+ print '</form>' . "\n";
+}
+print '</body>' . "\n";
+print '</html>' . "\n";
diff --git a/lights-out-init.service.in b/lights-out-init.service.in
new file mode 100644
index 0000000..a7e0f55
--- /dev/null
+++ b/lights-out-init.service.in
@@ -0,0 +1,10 @@
+[Unit]
+Description=configure the GPIO pins for lights out
+
+[Service]
+Type=oneshot
+RemainAfterExit=yes
+ExecStart=#BINDIR#/lights-out init
+
+[Install]
+WantedBy=multi-user.target
diff --git a/lights-out.conf b/lights-out.conf
new file mode 100644
index 0000000..7f6dc9e
--- /dev/null
+++ b/lights-out.conf
@@ -0,0 +1,8 @@
+#!/hint/bash
+
+server='fenster'
+
+inputs['power']=7
+inputs['hdd']=15
+outputs['power']=9
+outputs['reset']=8
diff --git a/lights-out.in b/lights-out.in
new file mode 100644
index 0000000..8d90c02
--- /dev/null
+++ b/lights-out.in
@@ -0,0 +1,135 @@
+#!/bin/bash
+
+display_help() {
+ >&2 printf 'lights-out version #VERSION#\n'
+ >&2 printf '\n'
+ >&2 printf 'usage:\n'
+ >&2 printf ' lights-out get $input: display inverted pin state of $input\n'
+ >&2 printf ' lights-out help: display this help and exit\n'
+ >&2 printf ' lights-out init: init gpio\n'
+ >&2 printf ' lights-out name: print the name of the server\n'
+ >&2 printf ' lights-out push $output $time: simulate push on $output for $time seconds\n'
+ >&2 printf ' lights-out turn on: turn on machine if not yet running\n'
+}
+
+check_argument_count() {
+ local count="$1"
+ shift
+ if [ $# -ne "${count}" ]; then
+ >&2 printf 'wrong number of arguments: "%s" expects %s argument' "$1" "${count}"
+ if [ "${count}" -ne 1 ]; then
+ printf 's'
+ fi
+ printf ', but %s were given\n\n' "$#"
+ display_help
+ exit 1
+ fi
+}
+
+if [ $# -lt 1 ]; then
+ display_help
+ exit 1
+fi
+
+if [ ! -r '#ETCDIR#/lights-out.conf' ]; then
+ >&2 printf 'cannot read config file "#ETCDIR#/lights-out.conf"\n'
+ exit 1
+fi
+
+if ! command -v gpio >/dev/null 2>&1; then
+ >&2 printf 'cannot find command "gpio" - is it installed?\n'
+ exit
+fi
+
+unset inputs
+unset outputs
+declare -A inputs
+declare -A outputs
+
+. '#ETCDIR#/lights-out.conf'
+
+if [ -z "${inputs['power']}" ]; then
+ >&2 printf 'invalid config "#ETCDIR#/lights-out.conf": no power input defined.\n'
+ exit 1
+fi
+
+if [ -z "${outputs['power']}" ]; then
+ >&2 printf 'invalid config "#ETCDIR#/lights-out.conf": no power output defined.\n'
+ exit 1
+fi
+
+if [ -z "${server}" ]; then
+ >&2 printf 'invalid config "#ETCDIR#/lights-out.conf": no server name defined.\n'
+ exit 1
+fi
+
+case "$1" in
+ 'get')
+ check_argument_count 2 "$@"
+ if [ -z "${inputs["$2"]}" ]; then
+ >&2 printf 'unknown input "%s"\n\n' "$2"
+ display_help
+ exit 1
+ fi
+ value=$(
+ gpio read "${inputs["$2"]}"
+ ) || exit $?
+ echo $((1-value))
+ ;;
+ 'help')
+ display_help
+ ;;
+ 'init')
+ check_argument_count 1 "$@"
+ for input in "${inputs[@]}"; do
+ gpio mode "${input}" in || exit $?
+ gpio mode "${input}" up || exit $?
+ done
+ for output in "${outputs[@]}"; do
+ gpio write "${output}" 0 || exit $?
+ gpio mode "${output}" out || exit $?
+ done
+ ;;
+ 'name')
+ printf '%s\n' "${server}"
+ ;;
+ 'push')
+ check_argument_count 3 "$@"
+ if [ -z "${outputs["$2"]}" ]; then
+ >&2 printf 'unknown output "%s"\n\n' "$2"
+ display_help
+ exit 1
+ fi
+ gpio write "${outputs["$2"]}" 1 || exit $?
+ sleep "$3"
+ gpio write "${outputs["$2"]}" 0 || exit $?
+ ;;
+ 'turn')
+ if [ $# -ne 2 ] \
+ || [ "x$2" != 'xon' ]; then
+ >&2 printf 'unknown command "%s"\n\n' "$*"
+ display_help
+ exit 1
+ fi
+ value=$(
+ "$0" get power
+ ) || exit $?
+ if [ "${value}" -eq 0 ]; then
+ "$0" push power 0.1 || exit $?
+ fi
+ ;;
+ 'turn on')
+ check_argument_count 1 "$@"
+ value=$(
+ "$0" get power
+ ) || exit $?
+ if [ "${value}" -eq 0 ]; then
+ "$0" push power 0.1 || exit $?
+ fi
+ ;;
+ *)
+ >&2 printf 'unknown command "%s"\n\n' "$1"
+ display_help
+ exit 1
+ ;;
+esac
diff --git a/lights-out@.service.in b/lights-out@.service.in
new file mode 100644
index 0000000..2fe6759
--- /dev/null
+++ b/lights-out@.service.in
@@ -0,0 +1,8 @@
+[Unit]
+Description=lights out %I
+Needs=lights-out-init.service
+After=lights-out-init.service
+
+[Service]
+Type=oneshot
+ExecStart=#BINDIR#/lights-out %I