-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathinstallHomeassistant.sh
More file actions
197 lines (167 loc) · 8.14 KB
/
installHomeassistant.sh
File metadata and controls
197 lines (167 loc) · 8.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/bin/bash
# By default, skip checks is set to 0. If you want to skip the OS and Architecture checks, set variable below to 1
SKIP_CHECKS=0 # Set variable to 1 to skip
set -e
FLAG_FILE="/var/tmp/installHomeassistant_incomplete.flag"
CONFIG_PROGRESS="/var/tmp/installHomeassistant_progress"
SCRIPT_PATH="$(realpath "$0")"
BASHRC_FILE="$HOME/.bashrc"
# prompt user function
prompt_user() {
echo "The script has rebooted your system, but hasn't finished. Would you like to continue? (y/n)"
read -r choice
if [[ $choice != "y" ]]; then
echo "Aborting the script. Re-running the script may break the system. Follow manual steps instead."
rm -f "$FLAG_FILE" "$CONFIG_PROGRESS"
exit 0
fi
}
# Check if the script is incomplete
if [[ -f "$FLAG_FILE" ]]; then
prompt_user
STEP=$(cat "$CONFIG_PROGRESS")
else
STEP=0
echo "No flag file found."
echo "Starting new execution..."
touch "$FLAG_FILE"
echo "flag file created"
fi
case $STEP in
0)
echo "I: This script is making kernel configuration changes."
echo "I: Re-running the script may break the system. Follow manual steps instead."
echo "I: The script has to reboot the system multiple times. Save your work before continuing"
echo "I: You'll get prompted to resume the script after every reboot."
echo "I: You'll have to sign back in, after the reboot and change to the root user using 'sudo su - to continue the install automatically."
echo "Do you want to continue (y/n)"
read -r continue
if [[ $continue != "y" ]]; then
echo "Aborting the script. Re-running the script should be safe."
rm -f "$FLAG_FILE" "$CONFIG_PROGRESS"
exit 0
fi
# Add resume logic to .bashrc
if ! grep -Fxq "if [[ -f $FLAG_FILE ]]; then" "$BASHRC_FILE"; then
# Appends the following lines to the .bashrc file
printf "\n# Run script after reboot if it's incomplete\n" >> "$BASHRC_FILE"
echo "if [[ -f $FLAG_FILE ]]; then" >> "$BASHRC_FILE"
echo " $SCRIPT_PATH" >> "$BASHRC_FILE"
echo "fi" >> "$BASHRC_FILE"
echo "I: Added script resume logic to .bashrc."
echo "To continue script after reboot login to the shell with the root user."
echo "I: Don't add anything else to .bashrc while script is running."
else
echo ".bashrc already contains the resume logic."
fi
echo "I: Step 0: Making kernel configuration changes..."
# Step 0
# If SKIP_CHECKS is 1, skip the checks
if [ "$SKIP_CHECKS" -eq 0 ]; then
# Check OS compatibility
os_info=$(hostnamectl status | grep "Operating System:" | cut -d: -f2 | xargs)
if [ "$os_info" != "Debian GNU/Linux 12 (bookworm)" ]; then
echo "E: OS does not match an OS where the script was tested on. Exiting."
echo "I: If you still want to try the automatic installation (which might work), edit the script to ignore these checks."
echo "I: To do this, enter an editor and uncomment the following line at the top of the script: 'SKIP_CHECKS=1'"
echo "I: If your untested OS works, please open an issue at https://github.com/HuckleberryLovesYou/Homeassistant-Supervised-on-Raspberry-Pi-5/issues for this OS to be added."
exit 1
fi
echo "I: OS used is supported"
# Check Architecture
if [ "$(uname -m)" != "aarch64" ]; then
echo "E: Architecture is not 64-bit. Exiting."
echo "I: If you still want to try the automatic installation, edit the script to ignore these checks."
echo "I: To do this, enter an editor and uncomment the following line at the top of the script: 'SKIP_CHECKS=1'"
echo "I: If your untested architecture works, please open an issue at https://github.com/HuckleberryLovesYou/Homeassistant-Supervised-on-Raspberry-Pi-5/issues for this architecture to be added."
exit 1
fi
echo "I: Architecture used is supported"
else
echo "I: The OS and Architecture checks were skipped because SKIP_CHECKS was equal to 1."
fi
echo "I: Home Assistant Supervised will now be installed:"
# Kernel config
echo "I: Adding kernel configurations..."
if ! grep -q "kernel=kernel8.img" /boot/firmware/config.txt; then
sed -i '1s/^/kernel=kernel8.img\n/' /boot/firmware/config.txt # adds string at the top of the file
fi
if ! grep -q "apparmor=1 security=apparmor" /boot/firmware/cmdline.txt; then
sed -z '$ s/\n$//' /boot/firmware/cmdline.txt > /tmp/cmdline.txt && mv /tmp/cmdline.txt /boot/firmware/cmdline.txt && printf " apparmor=1 security=apparmor" >> /boot/firmware/cmdline.txt
fi
echo "I: A reboot is now required."
echo "I: System will reboot in 5 seconds."
sleep 5
echo "1" > "$CONFIG_PROGRESS"
reboot
;;
1)
echo "I: Step 1: Resuming after first reboot..."
# Update system
echo "I: Updating the system..."
apt update -y && apt upgrade -y && apt autoremove -y # added autoremove to ensure clean installation
# Docker-ce and Docker Compose installation
echo "I: Installing Docker and Docker Compose..."
apt install curl -y
curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh
rm get-docker.sh
apt install -y libffi-dev libssl-dev python3-dev python3 python3-pip docker-compose
systemctl enable docker
user=$(whoami)
usermod -aG docker $user
mkdir -p "$HOME/docker-data"
echo "I: A directory was created for your docker data."
echo "I: You can change the location, but it makes sense to store your data in a organized file structure."
echo "I: You need to log out and back in for Docker group changes to take effect."
# Installation of Dependencies
echo "I: Installing dependencies for Homeassistant..."
apt install apparmor jq wget curl udisks2 libglib2.0-bin network-manager dbus systemd-journal-remote cifs-utils lsb-release nfs-common systemd-resolved -y
systemctl restart systemd-resolved.service
# Downloading and installing OS-Agent
echo "I: Downloading OS-Agent..."
wget -O os-agent_linux_aarch64.deb $(curl -s https://api.github.com/repos/home-assistant/os-agent/releases/latest | grep "browser_download_url.*linux_aarch64.deb" | cut -d '"' -f 4)
echo "I: Installing OS-Agent..."
dpkg -i os-agent_linux_aarch64.deb || { echo "E: OS-Agent installation failed." >&2; exit 1; }
echo "I: OS-Agent was succesfully installed."
# Downloading and installing Home Assistant Supervised
echo "I: Downloading Home Assistant Supervised..."
wget -O homeassistant-supervised.deb https://github.com/home-assistant/supervised-installer/releases/latest/download/homeassistant-supervised.deb
echo "I: Installing Home Assistant Supervised..."
echo "In the next step you get to select the maschine type for homeassistant-supervised."
echo "You have to select 'raspberrypi5-64'. Press enter to continue."
read -r continue_maschine_type
apt install ./homeassistant-supervised.deb -y || { echo "E: Home Assistant Supervised installation failed." >&2; exit 1; }
echo "I: Homeassistant Supervised was succesfully installed."
echo "I: A reboot is now required."
echo "I: System will reboot in 5 seconds."
echo "2" > "$CONFIG_PROGRESS"
sleep 5
reboot
;;
2)
echo "I: Starting cleanup..."
# Removing .bashrc content
echo "I: Removing resume logic added to .bashrc..."
sed -i '/# Run script after reboot if it'\''s incomplete/,/fi/d' "$BASHRC_FILE"
# Removing flag file and progress file
echo "I: Removing configuration files..."
rm -f "$FLAG_FILE" "$CONFIG_PROGRESS"
# Removing downlaoded OS-Agent and homeassistant-supervised installer
echo "I: Removing downloaded installers..."
rm -f homeassistant-supervised.deb
rm -f os-agent_linux_aarch64.deb
# Removing unused packages
apt autoremove -y
sleep 10
echo "I: Finished cleanup."
hostname=$(hostname)
echo "I: You are now able to reach Home Assistant at http://$hostname:8123. Keep in mind, it could take some time to start Homeassistant."
sleep 2
echo "I: Script finished successfully!"
;;
*)
echo "E: Unknown state. Aborting."
exit 1
;;
esac