Skip to content

Commit

Permalink
added rpc with timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Hofmann committed Jun 14, 2019
1 parent dd3bd53 commit 65ce03c
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
13 changes: 13 additions & 0 deletions rpc/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
ARG BUILD_FROM
FROM $BUILD_FROM

# Add env
ENV LANG C.UTF-8

# Setup base
RUN apk add --no-cache samba-common-tools samba-common

# Copy data
COPY run.sh /

CMD [ "/run.sh" ]
32 changes: 32 additions & 0 deletions rpc/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "RPC Shutdown with timeout",
"version": "1.1",
"slug": "rpc_shutdown",
"description": "Simple way for remote windows shutdowns",
"url": "https://github.com/m2hofi94/my-hassio-addons/tree/master/rpc",
"arch": ["armhf", "armv7", "aarch64", "amd64", "i386"],
"startup": "services",
"boot": "auto",
"stdin": true,
"host_network": true,
"options": {
"computers": [
{
"alias": "test-pc",
"address": "192.168.0.1",
"credentials": "user%password",
"timeout": 30
}
]
},
"schema": {
"computers": [
{
"alias": "match(^[\\w-]*$)",
"address": "str",
"credentials": "str",
"timeout": "int"
}
]
}
}
5 changes: 5 additions & 0 deletions rpc/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# RPC Shutdown with Timeout

Copied from https://github.com/home-assistant/hassio-addons/tree/master/rpc_shutdown, but added a configurable timeout. For a detailed description look here: https://home-assistant.io/addons/rpc_shutdown/

Also removed regex from the credentials field, because it did not allow a password with a `%` in it. If your password contains a percentage sign, simply provide it as is: `user%pass%word`.
31 changes: 31 additions & 0 deletions rpc/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash
set -e

CONFIG_PATH=/data/options.json

COMPUTERS=$(jq --raw-output '.computers | length' $CONFIG_PATH)

# Read from STDIN aliases to send shutdown
while read -r input; do
# remove json stuff
input="$(echo "$input" | jq --raw-output '.')"
echo "[Info] Read alias: $input"

# Find aliases -> computer
for (( i=0; i < "$COMPUTERS"; i++ )); do
ALIAS=$(jq --raw-output ".computers[$i].alias" $CONFIG_PATH)
ADDRESS=$(jq --raw-output ".computers[$i].address" $CONFIG_PATH)
CREDENTIALS=$(jq --raw-output ".computers[$i].credentials" $CONFIG_PATH)
TIMEOUT=$(jq --raw-output ".computers[$i].timeout" $CONFIG_PATH)

# Not the correct alias
if [ "$ALIAS" != "$input" ]; then
continue
fi

echo "[Info] Shutdown $input -> $ADDRESS"
if ! msg="$(net rpc shutdown -I "$ADDRESS" -t "$TIMEOUT" -U "$CREDENTIALS" -C "TIME: $TIMEOUT")"; then
echo "[Error] Shutdown fails -> $msg"
fi
done
done

0 comments on commit 65ce03c

Please sign in to comment.