Skip to content
This repository has been archived by the owner on Oct 30, 2019. It is now read-only.

Preparations for Python 3.5 D-Day. #255

Merged
merged 36 commits into from
Apr 22, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
d04086f
Initial steps
ludeeus Apr 15, 2019
7f72682
Fix return values
ludeeus Apr 15, 2019
6301d5a
Adds instructions
ludeeus Apr 15, 2019
6e6483c
Print message to the user before forcing a migration
ludeeus Apr 15, 2019
5536e7f
Use correct index
ludeeus Apr 15, 2019
9f1964a
returnvalue is not a string.
ludeeus Apr 15, 2019
bac6d9a
skip python install if allready installed
ludeeus Apr 15, 2019
ffa810d
skip only if correct verison
ludeeus Apr 15, 2019
0f0af48
more changes
ludeeus Apr 15, 2019
d286373
shellcheck
ludeeus Apr 15, 2019
afc09db
more shellchecks
ludeeus Apr 15, 2019
ff9256f
Don't block the install script
ludeeus Apr 15, 2019
bcff38a
Fix symlinks
ludeeus Apr 15, 2019
aad4e57
move migration check
ludeeus Apr 15, 2019
db28dfe
remove symlinks :(
ludeeus Apr 15, 2019
75a0083
quotes?
ludeeus Apr 15, 2019
23321d3
# shellcheck disable=SC1091
ludeeus Apr 15, 2019
d1f7088
more quotes
ludeeus Apr 15, 2019
1ed36b9
Find newest installed python version
ludeeus Apr 15, 2019
c24bb19
Use newest version when creating venv
ludeeus Apr 15, 2019
a3731e6
only run if we can run
ludeeus Apr 15, 2019
b511dfe
Remove py3.8
ludeeus Apr 15, 2019
e6cca05
only create file if we have data
ludeeus Apr 15, 2019
d79985e
more checks
ludeeus Apr 15, 2019
a249cbe
Wrong operator
ludeeus Apr 15, 2019
a8214d1
Removed leftover echo from test
ludeeus Apr 15, 2019
779fd87
Simplity migration script
ludeeus Apr 15, 2019
3874058
"styling"
ludeeus Apr 15, 2019
432085f
Adds systeminfo
ludeeus Apr 19, 2019
e66d0dd
use correct var
ludeeus Apr 19, 2019
a503714
remove useless check
ludeeus Apr 19, 2019
9c87dc1
Working state
ludeeus Apr 19, 2019
8f6e9de
Merge branch 'dev' into dday-prep
ludeeus Apr 20, 2019
28ac73e
Adds systeminfo to "tab completion" and help
ludeeus Apr 21, 2019
4869fe0
Merge branch 'dday-prep' of https://github.com/ludeeus/hassbian-scrip…
ludeeus Apr 21, 2019
c73cc86
Version match cleanup
ludeeus Apr 21, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions docs/python.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
# Pyhton

It is not recomended running this script.
**It is recomended to download and install the newest
[Hassbian image][hassbian-image] instead of running this.**

This script will upgrade Python to the latest stable version.\
This script will upgrade Python and Home Assistant to the latest stable version.\
It will also create a new virtual environment to be used for Home Assistant.\
_This upgrade takes long time to finish, on an Raspberry Pi 3
this took about 1 hour._
this takes about 1 hour._

## Upgrade

Expand All @@ -17,8 +13,8 @@ sudo hassbian-config upgrade python

## Additional info

If you have installed additional components directly to `/srv/homeassistant/`
like `libcec` thoese will need to be reinstalled.
If you have installed additional packages directly to `/srv/homeassistant/`
like `libcec` those will need to be reinstalled.

***

Expand All @@ -27,5 +23,4 @@ With inspiration from [blog.ceard.tech][blog].

<!--- Links --->
[blog]: https://blog.ceard.tech/2017/12/upgrading-python-virtual-environment.html
[hassbian-image]: https://github.com/home-assistant/pi-gen/releases/latest
[ludeeus]: https://github.com/ludeeus
94 changes: 85 additions & 9 deletions package/opt/hassbian/suites/homeassistant.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,98 @@ function homeassistant-show-copyright-info {
echo "Copyright(c) 2017 Fredrik Lindqvist <https://github.com/Landrash>."
}

function python-migration {
# Implemented to cope with the announced D-Day of Python 3.5
# To track if this allready have been run, we create a file to hold that "state"
# /srv/homeassistant/hassbian/pythonmigration with HAVENV=pythonversion as the content.

readonly pythonmigrationfile='/srv/homeassistant/hassbian/pythonmigration'
readonly targetpythonversion='3.7'
readonly haversionwithrequirement='0.98.0'

# Get the current python version HA is running under.
currenthapyversion

# Check if the file exist
if [ ! -f "$pythonmigrationfile" ]; then
# file does not exist, let's create it.
echo "HAVENV=$CURRENTHAPYVERSION" > "$pythonmigrationfile"
fi

# Checks to see if migration is needed.
pyversion=$(grep "HAVENV" $pythonmigrationfile | awk -F'=' '{print $2}')
if [[ "${pyversion:0:2}" == "$targetpythonversion" ]]; then
# Migration not needed.
return 0
fi

if [[ "${CURRENTHAPYVERSION:0:2}" == "$targetpythonversion" ]]; then
# Migration not needed.
echo "HAVENV=$CURRENTHAPYVERSION" > "$pythonmigrationfile"
return 0
fi

# Check if migration is needed for the newest HA version.
newversion=$(curl -s https://api.github.com/repos/home-assistant/home-assistant/releases/latest | grep tag_name | awk -F'"' '{print $4}')
if [[ "$newversion" < "$haversionwithrequirement" ]]; then
# Migration not yet needed, store the current version.
echo "HAVENV=$CURRENTHAPYVERSION" > "$pythonmigrationfile"
echo
echo "A migration of your python virtual enviorment for Home Assistant will be needed."
echo "This will take about 1 hour on a raspberry pi 3."
echo "When version $haversionwithrequirement is live you wil not have a choise."
echo
echo -n "Do you want to start this migration now? [N/y] : "
read -r RESPONSE
if [ "$RESPONSE" == "y" ] || [ "$RESPONSE" == "Y" ]; then
source /opt/hassbian/suites/python.sh
python-upgrade-package

# Quit when execution is done.
exit 0
fi
else
echo "
#
# MIGRATION IN PROGRESS
# THIS WILL TAKE A LONG TIME, IT IS IMPORTANT THAT YOU DO NOT INTERRUPT THIS
#
#
#
# AFTER THIS MIGRATION YOUR HOME ASSISTANT INSTANCE WILL BE RUNNING UNDER PYTHON $targetpythonversion
#"
sleep 20
source /opt/hassbian/suites/python.sh
python-upgrade-package

# Quit when execution is done.
exit 0
fi

}

function homeassistant-install-package {
echo "Setting correct premissions"
chown homeassistant:homeassistant -R /srv/homeassistant

# Check if migration is needed.
python-migration

echo "Changing to the homeassistant user"
sudo -u homeassistant -H /bin/bash << EOF

echo "Creating Home Assistant venv"
python3 -m venv /srv/homeassistant
echo "Creating Home Assistant venv"
python3 -m venv /srv/homeassistant

echo "Changing to Home Assistant venv"
source /srv/homeassistant/bin/activate
echo "Changing to Home Assistant venv"
source /srv/homeassistant/bin/activate

echo "Installing latest version of Home Assistant"
python3 -m pip install setuptools wheel
python3 -m pip install homeassistant
echo "Installing latest version of Home Assistant"
python3 -m pip install setuptools wheel
python3 -m pip install homeassistant

echo "Deactivating virtualenv"
deactivate
echo "Deactivating virtualenv"
deactivate
EOF

echo "Enabling Home Assistant service"
Expand Down Expand Up @@ -64,6 +137,9 @@ return 0
}

function homeassistant-upgrade-package {
# Check if migration is needed.
python-migration

if [ "$DEV" == "true" ]; then
echo "This script downloads Home Assistant directly from the dev branch on Github."
echo "you can use this to be on the 'bleeding edge of the development of Home Assistant.'"
Expand Down
36 changes: 8 additions & 28 deletions package/opt/hassbian/suites/python.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,18 @@ function python-show-copyright-info {
}

function python-upgrade-package {
if [ "$FORCE" == "" ]; then
printf "\\n\\n"
echo "This script will change a lot on your pi."
echo "DO NOT run this if it's not absolutely necessary"
echo "You can force run the upgrade script like this:"
echo "sudo hassbian-config upgrade python --force"
return 0
fi

printf "\\n\\n"
echo "It is NOT recomended to run this."
echo -n "Are you absolutely sure you want to run this script? [N/y]: "
read -r RESPONSE
if [ "$RESPONSE" == "y" ] || [ "$RESPONSE" == "Y" ]; then
RESPONSE="Y"
else
return 0
fi

PYTHONVERSION=$(curl -s https://www.python.org/downloads/source/ | grep "Latest Python 3 Release" | cut -d "<" -f 3 | awk -F ' ' '{print $NF}')

echo "Checking current version..."
currentpython=$(sudo -u homeassistant -H /bin/bash << EOF | awk -F ' ' '{print $NF}'
source /srv/homeassistant/bin/activate
python -V
EOF
)
hapyversion$(currenthapyversion)

if [ "$currentpython" == "$PYTHONVERSION" ]; then
if [ "$hapyversion" == "$PYTHONVERSION" ]; then
echo "Python is already the highest stable version.."
return 0
fi


echo "Upgrading to Python $PYTHONVERSION"
apt-get -y update
apt-get install -y build-essential tk-dev libncurses5-dev libncursesw5-dev libreadline6-dev libdb5.3-dev libgdbm-dev libsqlite3-dev libssl-dev libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev
Expand All @@ -65,11 +45,11 @@ echo "Stopping Home Assistant."
systemctl stop [email protected]

echo "Backing up previous virutal enviorment."
mv /srv/homeassistant /srv/homeassistant_"$currentpython"
mv /srv/homeassistant /srv/homeassistant_"$hapyversion"

echo "Creating new virutal environment using Python $PYTHONVERSION"
python"${PYTHONVERSION:: -2}" -m venv /srv/homeassistant
mv /srv/homeassistant_"$currentpython"/hassbian /srv/homeassistant/hassbian
mv /srv/homeassistant_"$hapyversion"/hassbian /srv/homeassistant/hassbian
chown homeassistant:homeassistant -R /srv/homeassistant
apt install python3-pip python3-dev
pip"${PYTHONVERSION:: -2}" install --upgrade virtualenv
Expand All @@ -79,7 +59,7 @@ pip3 install --upgrade setuptools wheel
pip3 install --upgrade homeassistant
deactivate
EOF
mv /home/homeassistant/.homeassistant/deps /home/homeassistant/.homeassistant/deps_"$currentpython"
mv /home/homeassistant/.homeassistant/deps /home/homeassistant/.homeassistant/deps_"$hapyversion"

echo "Starting Home Assistant."
systemctl start [email protected]
Expand All @@ -102,7 +82,7 @@ else
echo -e "\\e[31mReverting..."
systemctl stop [email protected]
rm -R /srv/homeassistant
mv /srv/homeassistant_"$currentpython" /srv/homeassistant
mv /srv/homeassistant_"$hapyversion" /srv/homeassistant
systemctl start [email protected]
echo
return 1
Expand Down
19 changes: 19 additions & 0 deletions package/usr/local/bin/hassbian-config
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,25 @@ function get-all-suite-installers {
return 0
}

function currenthapyversion {
# After you call this function use "$CURRENTHAPYVERSION" to get the value
CURRENTHAPYVERSION=$(sudo -u homeassistant -H /bin/bash << EOF | awk -F ' ' '{print $NF}'
source /srv/homeassistant/bin/activate
python -V
EOF
)
echo "$version"
}

function currenthaversion {
# After you call this function use "$CURRENTHAVERSION" to get the value
CURRENTHAVERSION=$(sudo -u homeassistant -H /bin/bash << EOF | awk -F ' ' '{print $NF}'
source /srv/homeassistant/bin/activate
hass --version
EOF
)
}

function show-suites {
printf "This is a list over all suites in hassbian-config\\n"
printf "If a \\e[1msuite\\e[0m has \\e[32mgreen color\\e[0m in the name, you have already installed it.\\n"
Expand Down