Initial version of the rc script

This commit is contained in:
Brendan 2024-03-23 21:00:29 +11:00
parent f5b3c801b6
commit a663e77efe
2 changed files with 141 additions and 2 deletions

View file

@ -1,2 +1,12 @@
# freebsd-home-assistant-rc-script # An RC script for Home Assistant Core in a Python virtual environment on FreeBSD
RC script for Home Assistant Core in a Python virtual environment on FreeBSD
This rc script assumes that you have installed Home Assistant Core in a Python virtual environment.
See these instructions for an example of how to do this: https://blog.brendans-bits.com/posts/2024/upgradable-home-assistant-in-a-freebsd-jail/
This script was developed to make it easy to change Python virtual environments as Home Assistant Core is upgraded between releases.
For example: to upgrade from HA Core 2024.2 to 2024.3, you would create a new python virtual environment where you install the new version of HA.
To begin using the new version of HA, you change the path in the `homeassistant_environment` variable in `/etc/rc.conf` to point to the new python virtual environment and then restart the `homeassistant` service.
The script also includes a custom `status` method that shows you which versions of Python and of Home Assistant Core are running.
Suggestions and contributions welcomed.

View file

@ -0,0 +1,129 @@
#!/bin/sh
# PROVIDE: homeassistant
# REQUIRE: DAEMON NETWORKING
# KEYWORD: shutdown
# Version 20240323
#
# homeassistant_enable: Enable the Home Assistant service
# Required: Yes
# Default: "NO"
#
# homeassistant_environment: Path to where you have installed Home Assistant Core
# Required: Yes
# Default: ""
# This needs to provide a path to the Python virtual environment
# where you have installed Home Assistant Core
#
# homeassistant_user: The user that will run Home Assistant Core
# Required: No
# Default: "homeassistant"
#
# homeassistant_group: The group that will run Home Assistant Core
# Required: No
# Default: "homeassistant"
#
# homeassistant_log_folder: Where Home Assistant Core will store logs
# Required: Yes
# Default: "/var/log/homeassistant/"
#
# homeassistant_config_folder: Where Home Assistant Core stores config files and database
# Required: Yes
# Default: "/home/homeassistant/.homeassistant"
#
# homeassistant_run_options: Other flags to pass to Home Assistant Core
# Required: No
# Default: ""
# Any flags that can be passed to `hass` go here. Examples include:
# - `--verbose` to enable verbose logging
# Note: `--ignore-os-check`, --log-file`, and `--config` are already passed.
. /etc/rc.subr
name="homeassistant"
rcvar=homeassistant_enable
load_rc_config $name
: ${homeassistant_enable:="NO"}
: ${homeassistant_environment:=""}
: ${homeassistant_user:="homeassistant"}
: ${homeassistant_group:="homeassistant"}
: ${homeassistant_log_folder="/var/log/homeassistant"}
: ${homeassistant_config_folder="/home/homeassistant/.homeassistant"}
: ${homeassistant_run_options=""}
pid_directory=/var/run/homeassistant
pidfile="${pid_directory}/${name}_child.pid"
ppidfile="${pid_directory}/${name}_daemon.pid"
homeassistant_command="${homeassistant_environment}/bin/python ${homeassistant_environment}/bin/hass"
command="/usr/sbin/daemon"
procname="${homeassistant_environment}/bin/python"
procname_args="${homeassistant_environment}/bin/hass --ignore-os-check --config=${homeassistant_config_folder} --log-file=${homeassistant_log_folder}/homeassistant-daemon.log"
command_args="-p ${pidfile} -P ${ppidfile} -f -r ${procname} ${procname_args} ${homeassistant_run_options} ${rc_flags}"
start_precmd="${name}_prestart"
homeassistant_prestart()
{
# Check if PIF directory exists
if [ ! -d ${pid_directory} ]; then
install -d -o ${homeassistant_user} ${pid_directory}
fi
# Check that the log folder exists
if [ ! -e ${homeassistant_log_folder} ]; then
install -d -o ${homeassistant_user} -g ${homeassistant_group} -m 760 ${homeassistant_log_folder}
fi
# Check if the HA env variable has been set
if [ -z ${homeassistant_environment} ]; then
echo "Please set 'homeassistant_environment=' in /etc/rc.conf" && exit 1
fi
# Check if the provided HA environment actually exists
if [ ! -e ${homeassistant_environment}/bin/hass ]; then
echo "Home Assistant script not found - please install Home Assistant" && exit 1
fi
}
# Custom status command adapted from:
# https://github.com/tprelog/iocage-homeassistant/blob/master/overlay/usr/local/etc/rc.d/homeassistant
status_cmd=${name}_status
homeassistant_status() {
local _http_ _ip_
if [ -n "${rc_pid}" ]; then
: "${homeassistant_secure:="NO"}" # This is only a cosmetic variable - used by the status_cmd
_ip_="$(ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p')"
checkyesno homeassistant_secure && _http_="https" || _http_="http"
echo "${name} is running as pid ${rc_pid}."
echo "${_http_}://${_ip_}:${homeassistant_port:-"8123"}" # This is only a cosmetic variable
${homeassistant_environment}/bin/python --version
${homeassistant_environment}/bin/hass --version
else
echo "${name} is not running."
return 1
fi
}
stop_cmd="${name}_stop"
homeassistant_stop()
{
echo "Stopping PID: $(cat ${ppidfile})"
/bin/kill -9 $(cat ${ppidfile})
echo "Stopping PID: $(cat ${pidfile})"
/bin/kill -15 $(cat ${pidfile})
}
run_rc_command "$1"