-
-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Protect against aborted connections (#42)
Co-authored-by: Joakim Sørensen <[email protected]>
- Loading branch information
Showing
1 changed file
with
112 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,138 +1,143 @@ | ||
#!/bin/bash | ||
# wget -O - https://get.hacs.xyz | bash - | ||
set -e | ||
|
||
RED_COLOR='\033[0;31m' | ||
GREEN_COLOR='\033[0;32m' | ||
GREEN_YELLOW='\033[1;33m' | ||
NO_COLOR='\033[0m' | ||
|
||
declare haPath | ||
declare -a paths=( | ||
"$PWD" | ||
"$PWD/config" | ||
"/config" | ||
"/homeassistant" | ||
"$HOME/.homeassistant" | ||
"/usr/share/hassio/homeassistant" | ||
) | ||
declare currentVersion | ||
declare currentYear | ||
declare currentMonth | ||
declare currentPatch | ||
declare targetVersion | ||
declare targetYear | ||
declare targetMonth | ||
declare targetPatch | ||
|
||
function info () { echo -e "${GREEN_COLOR}INFO: $1${NO_COLOR}";} | ||
function warn () { echo -e "${GREEN_YELLOW}WARN: $1${NO_COLOR}";} | ||
function error () { echo -e "${RED_COLOR}ERROR: $1${NO_COLOR}"; if [ "$2" != "false" ]; then exit 1;fi; } | ||
|
||
function checkRequirement () { | ||
if [ -z "$(command -v "$1")" ]; then | ||
error "'$1' is not installed" | ||
fi | ||
} | ||
|
||
checkRequirement "wget" | ||
checkRequirement "unzip" | ||
|
||
info "Trying to find the correct directory..." | ||
for path in "${paths[@]}"; do | ||
if [ -n "$haPath" ]; then | ||
break | ||
fi | ||
function run() { | ||
set -e | ||
|
||
RED_COLOR='\033[0;31m' | ||
GREEN_COLOR='\033[0;32m' | ||
GREEN_YELLOW='\033[1;33m' | ||
NO_COLOR='\033[0m' | ||
|
||
declare haPath | ||
declare -a paths=( | ||
"$PWD" | ||
"$PWD/config" | ||
"/config" | ||
"/homeassistant" | ||
"$HOME/.homeassistant" | ||
"/usr/share/hassio/homeassistant" | ||
) | ||
declare currentVersion | ||
declare currentYear | ||
declare currentMonth | ||
declare currentPatch | ||
declare targetVersion | ||
declare targetYear | ||
declare targetMonth | ||
declare targetPatch | ||
|
||
function info () { echo -e "${GREEN_COLOR}INFO: $1${NO_COLOR}";} | ||
function warn () { echo -e "${GREEN_YELLOW}WARN: $1${NO_COLOR}";} | ||
function error () { echo -e "${RED_COLOR}ERROR: $1${NO_COLOR}"; if [ "$2" != "false" ]; then exit 1;fi; } | ||
|
||
function checkRequirement () { | ||
if [ -z "$(command -v "$1")" ]; then | ||
error "'$1' is not installed" | ||
fi | ||
} | ||
|
||
if [ -f "$path/.HA_VERSION" ]; then | ||
haPath="$path" | ||
fi | ||
done | ||
|
||
if [ -n "$haPath" ]; then | ||
info "Found Home Assistant configuration directory at '$haPath'" | ||
cd "$haPath" || error "Could not change path to $haPath" | ||
if [ ! -d "$haPath/custom_components" ]; then | ||
info "Creating custom_components directory..." | ||
mkdir "$haPath/custom_components" | ||
fi | ||
checkRequirement "wget" | ||
checkRequirement "unzip" | ||
|
||
info "Changing to the custom_components directory..." | ||
cd "$haPath/custom_components" || error "Could not change path to $haPath/custom_components" | ||
info "Trying to find the correct directory..." | ||
for path in "${paths[@]}"; do | ||
if [ -n "$haPath" ]; then | ||
break | ||
fi | ||
|
||
info "Downloading HACS" | ||
rm -f "$haPath/custom_components/hacs.zip" | ||
wget "https://github.com/hacs/integration/releases/latest/download/hacs.zip" | ||
if [ -f "$path/.HA_VERSION" ]; then | ||
haPath="$path" | ||
fi | ||
done | ||
|
||
if [ -d "$haPath/custom_components/hacs" ]; then | ||
warn "HACS directory already exist, cleaning up..." | ||
rm -R "$haPath/custom_components/hacs" | ||
fi | ||
if [ -n "$haPath" ]; then | ||
info "Found Home Assistant configuration directory at '$haPath'" | ||
cd "$haPath" || error "Could not change path to $haPath" | ||
if [ ! -d "$haPath/custom_components" ]; then | ||
info "Creating custom_components directory..." | ||
mkdir "$haPath/custom_components" | ||
fi | ||
|
||
info "Creating HACS directory..." | ||
mkdir "$haPath/custom_components/hacs" | ||
info "Changing to the custom_components directory..." | ||
cd "$haPath/custom_components" || error "Could not change path to $haPath/custom_components" | ||
|
||
info "Unpacking HACS..." | ||
unzip "$haPath/custom_components/hacs.zip" -d "$haPath/custom_components/hacs" >/dev/null 2>&1 | ||
info "Downloading HACS" | ||
rm -f "$haPath/custom_components/hacs.zip" | ||
wget "https://github.com/hacs/integration/releases/latest/download/hacs.zip" | ||
|
||
if [ -d "$haPath/custom_components/hacs" ]; then | ||
warn "HACS directory already exist, cleaning up..." | ||
rm -R "$haPath/custom_components/hacs" | ||
fi | ||
|
||
echo | ||
info "Verifying versions" | ||
targetVersion=$(sed -n -e '/^MINIMUM_HA_VERSION/p' "$haPath/custom_components/hacs/const.py" | cut -d '"' -f 2) | ||
currentVersion=$(cat "$haPath/.HA_VERSION") | ||
info "Creating HACS directory..." | ||
mkdir "$haPath/custom_components/hacs" | ||
|
||
info "Current version is ${currentVersion}, minimum version is ${targetVersion}" | ||
info "Unpacking HACS..." | ||
unzip "$haPath/custom_components/hacs.zip" -d "$haPath/custom_components/hacs" >/dev/null 2>&1 | ||
|
||
targetYear=$(echo "${targetVersion}" | cut -d "." -f 1) | ||
currentYear=$(echo "${currentVersion}" | cut -d "." -f 1) | ||
|
||
if [ "${currentVersion}" == "2023.12.0" ]; then | ||
rm -R "$haPath/custom_components/hacs" | ||
rm -f "$haPath/custom_components/hacs.zip" | ||
error "HACS will not work on version 2023.12.0 of Home Assistant, upgrade to 2023.12.1 (or newer) before re-running this script." | ||
fi | ||
echo | ||
info "Verifying versions" | ||
targetVersion=$(sed -n -e '/^MINIMUM_HA_VERSION/p' "$haPath/custom_components/hacs/const.py" | cut -d '"' -f 2) | ||
currentVersion=$(cat "$haPath/.HA_VERSION") | ||
|
||
if [ "${currentYear}" -lt "${targetYear}" ]; then | ||
rm -R "$haPath/custom_components/hacs" | ||
rm -f "$haPath/custom_components/hacs.zip" | ||
error "Version ${currentVersion} is not new enough, needs at least ${targetVersion}" | ||
fi | ||
info "Current version is ${currentVersion}, minimum version is ${targetVersion}" | ||
|
||
targetYear=$(echo "${targetVersion}" | cut -d "." -f 1) | ||
currentYear=$(echo "${currentVersion}" | cut -d "." -f 1) | ||
|
||
if [ "${currentYear}" == "${targetYear}" ]; then | ||
targetMonth=$(echo "${targetVersion}" | cut -d "." -f 2) | ||
currentMonth=$(echo "${currentVersion}" | cut -d "." -f 2) | ||
if [ "${currentVersion}" == "2023.12.0" ]; then | ||
rm -R "$haPath/custom_components/hacs" | ||
rm -f "$haPath/custom_components/hacs.zip" | ||
error "HACS will not work on version 2023.12.0 of Home Assistant, upgrade to 2023.12.1 (or newer) before re-running this script." | ||
fi | ||
|
||
if [ "${currentMonth}" -lt "${targetMonth}" ]; then | ||
if [ "${currentYear}" -lt "${targetYear}" ]; then | ||
rm -R "$haPath/custom_components/hacs" | ||
rm -f "$haPath/custom_components/hacs.zip" | ||
error "Version ${currentVersion} is not new enough, needs at least ${targetVersion}" | ||
fi | ||
|
||
if [ "${currentMonth}" == "${targetMonth}" ]; then | ||
targetPatch=$(echo "${targetVersion}" | cut -d "." -f 3) | ||
currentPatch=$(echo "${currentVersion}" | cut -d "." -f 3) | ||
|
||
if [ "${currentPatch}" -lt "${targetPatch}" ]; then | ||
if [ "${currentYear}" == "${targetYear}" ]; then | ||
targetMonth=$(echo "${targetVersion}" | cut -d "." -f 2) | ||
currentMonth=$(echo "${currentVersion}" | cut -d "." -f 2) | ||
|
||
if [ "${currentMonth}" -lt "${targetMonth}" ]; then | ||
rm -R "$haPath/custom_components/hacs" | ||
rm -f "$haPath/custom_components/hacs.zip" | ||
error "Version ${currentVersion} is not new enough, needs at least ${targetVersion}" | ||
fi | ||
|
||
if [ "${currentMonth}" == "${targetMonth}" ]; then | ||
targetPatch=$(echo "${targetVersion}" | cut -d "." -f 3) | ||
currentPatch=$(echo "${currentVersion}" | cut -d "." -f 3) | ||
|
||
if [ "${currentPatch}" -lt "${targetPatch}" ]; then | ||
rm -R "$haPath/custom_components/hacs" | ||
rm -f "$haPath/custom_components/hacs.zip" | ||
error "Version ${currentVersion} is not new enough, needs at least ${targetVersion}" | ||
fi | ||
fi | ||
fi | ||
|
||
echo | ||
info "Removing HACS zip file..." | ||
rm -f "$haPath/custom_components/hacs.zip" | ||
info "Installation complete." | ||
echo | ||
info "Remember to restart Home Assistant before you configure it" | ||
|
||
else | ||
echo | ||
error "Could not find the directory for Home Assistant" false | ||
echo "Manually change the directory to the root of your Home Assistant configuration" | ||
echo "With the user that is running Home Assistant" | ||
echo "and run the script again" | ||
exit 1 | ||
fi | ||
} | ||
|
||
echo | ||
info "Removing HACS zip file..." | ||
rm -f "$haPath/custom_components/hacs.zip" | ||
info "Installation complete." | ||
echo | ||
info "Remember to restart Home Assistant before you configure it" | ||
|
||
else | ||
echo | ||
error "Could not find the directory for Home Assistant" false | ||
echo "Manually change the directory to the root of your Home Assistant configuration" | ||
echo "With the user that is running Home Assistant" | ||
echo "and run the script again" | ||
exit 1 | ||
fi | ||
run |