90 lines
2.8 KiB
Bash
Executable File
90 lines
2.8 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
APP_DIR="/opt/shd/plugins/alice"
|
|
SRC_DIR="$(cd "$(dirname "$0")" && pwd)"
|
|
IN_PLACE="${1:-}"
|
|
|
|
sync_project() {
|
|
mkdir -p /opt/shd/plugins
|
|
if command -v rsync >/dev/null 2>&1; then
|
|
rsync -a --delete --exclude '.venv/' --exclude '__pycache__/' --exclude '*.pyc' --exclude '*.zip' "$SRC_DIR/" "$APP_DIR/"
|
|
else
|
|
mkdir -p "$APP_DIR"
|
|
cp -a "$SRC_DIR/." "$APP_DIR/"
|
|
rm -rf "$APP_DIR/.venv" "$APP_DIR/__pycache__"
|
|
find "$APP_DIR" -type d -name '__pycache__' -prune -exec rm -rf {} +
|
|
find "$APP_DIR" -type f \( -name '*.pyc' -o -name '*.zip' \) -delete
|
|
fi
|
|
}
|
|
|
|
if [[ "$SRC_DIR" != "$APP_DIR" && "$IN_PLACE" != "--in-place" ]]; then
|
|
echo "Sync project to $APP_DIR"
|
|
if [[ -w /opt/shd/plugins || -w /opt/shd || -w /opt ]]; then
|
|
sync_project
|
|
else
|
|
if ! command -v sudo >/dev/null 2>&1; then
|
|
echo "Need write access to /opt/shd/plugins or sudo"
|
|
exit 1
|
|
fi
|
|
sudo mkdir -p /opt/shd/plugins
|
|
sudo rm -rf "$APP_DIR"
|
|
sudo mkdir -p "$APP_DIR"
|
|
if command -v rsync >/dev/null 2>&1; then
|
|
sudo rsync -a --delete --exclude '.venv/' --exclude '__pycache__/' --exclude '*.pyc' --exclude '*.zip' "$SRC_DIR/" "$APP_DIR/"
|
|
else
|
|
sudo cp -a "$SRC_DIR/." "$APP_DIR/"
|
|
sudo rm -rf "$APP_DIR/.venv" "$APP_DIR/__pycache__"
|
|
sudo find "$APP_DIR" -type d -name '__pycache__' -prune -exec rm -rf {} +
|
|
sudo find "$APP_DIR" -type f \( -name '*.pyc' -o -name '*.zip' \) -delete
|
|
fi
|
|
if [[ -n "${SUDO_USER:-}" ]]; then
|
|
sudo chown -R "$SUDO_USER":"$SUDO_USER" "$APP_DIR"
|
|
fi
|
|
fi
|
|
exec "$APP_DIR/install.sh" --in-place
|
|
fi
|
|
|
|
cd "$APP_DIR"
|
|
chmod +x cli.sh install.sh start.sh start_plugin_web.sh
|
|
|
|
if [[ ! -d .venv ]]; then
|
|
python3 -m venv .venv
|
|
fi
|
|
|
|
# shellcheck disable=SC1091
|
|
source .venv/bin/activate
|
|
python3 -m pip install --upgrade pip
|
|
python3 -m pip install -r requirements.txt
|
|
python3 -c "import aiohttp, zeroconf" >/dev/null
|
|
|
|
mkdir -p data configs/loxone configs/wb-rules
|
|
: > data/.gitkeep
|
|
|
|
install_service() {
|
|
local src="$1"
|
|
local dst="/etc/systemd/system/$(basename "$src")"
|
|
if [[ -w /etc/systemd/system ]]; then
|
|
cp -f "$src" "$dst"
|
|
else
|
|
sudo cp -f "$src" "$dst"
|
|
fi
|
|
}
|
|
|
|
if command -v systemctl >/dev/null 2>&1; then
|
|
install_service "$APP_DIR/systemd/shd-alice.service"
|
|
install_service "$APP_DIR/systemd/shd-alice-plugin.service"
|
|
if [[ -w /etc/systemd/system ]]; then
|
|
systemctl daemon-reload
|
|
systemctl enable shd-alice.service shd-alice-plugin.service || true
|
|
else
|
|
sudo systemctl daemon-reload
|
|
sudo systemctl enable shd-alice.service shd-alice-plugin.service || true
|
|
fi
|
|
fi
|
|
|
|
echo "Install done: $APP_DIR"
|
|
echo "Next:"
|
|
echo " 1) save token: printf %s 'YANDEX_OAUTH_TOKEN' > $APP_DIR/token.txt && chmod 600 $APP_DIR/token.txt"
|
|
echo " 2) update stations: $APP_DIR/cli.sh columns"
|
|
echo " 3) start web UI: $APP_DIR/cli.sh web"
|