Skip to content

Commit

Permalink
Merge pull request #1699 from colossatr0n/dev
Browse files Browse the repository at this point in the history
Update INSTALL.rst to include init.d instructions. Correct DEV.rst on…
  • Loading branch information
acockburn committed Mar 14, 2023
2 parents cc76da2 + 70604b9 commit 7c8aeee
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,11 @@ docs/_build/
target/
.idea/

# Python
venv

# Mac stuff
.DS_Store

# Vim
*~
2 changes: 1 addition & 1 deletion docs/DEV.rst
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ The following command downloads and install the optional dependencies, as define
$ pip install -r doc-requirements.txt
Then `cd` to the `docs` subdirectory, where all the `rst` files are found, and run the following command:
From the project's root directory, run the following command:

.. code:: console
Expand Down
181 changes: 179 additions & 2 deletions docs/INSTALL.rst
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,13 @@ A brief description of them follows:
``-s``, ``-i``, ``-t``, ``-e`` time travel options
Useful only for testing. Described in more detail in the API documentation.


Starting At Reboot (Systemd)
----------------------------

To run ``AppDaemon`` at reboot, you can set it up to run as a ``systemd
service``. To run it with ``init.d`` instead, see the next section.

Automatically starting on boot
------------------------------

Expand Down Expand Up @@ -265,8 +272,178 @@ Activate the service
Now AppDaemon should be up and running and good to go.

Upgrading
=========
Starting At Reboot (Init.d)
----------------------------

To run ``AppDaemon`` at reboot, you can set it up to run as an ``init.d
service``. To run it with ``systemd`` instead, see the previous section.

Add Init.d Service
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

First, create a new file using vi:

.. code:: bash
$ sudo vi /etc/init.d/appdaemon-daemon
Copy and paste the following script into the new file, making sure that the following variables
are set according to your setup.

- APPDAEMON_INSTALL_DIR
- Location of appdaemon installation.
- PRE_EXEC
- Command for starting the python venv for appdaemon.
- APPDAEMON_BIN
- Location of appdaemon binary.
- RUN_AS
- Usually the same user you are using to run Home Assistant.
- CONFIG_DIR
- Location of Home Assistant config.

::

#!/bin/sh
### BEGIN INIT INFO
# Provides: appdaemon
# Required-Start: $local_fs $network $named $time $syslog
# Required-Stop: $local_fs $network $named $time $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: AppDaemon
### END INIT INFO

# /etc/init.d Service Script for AppDaemon
APPDAEMON_INSTALL_DIR="/srv/appdaemon"
PRE_EXEC="cd $APPDAEMON_INSTALL_DIR; python3.9 -m venv .; source bin/activate;"
APPDAEMON_BIN="/srv/appdaemon/bin/appdaemon"
RUN_AS="homeassistant"
PID_DIR="/var/run/appdaemon"
PID_FILE="$PID_DIR/appdaemon.pid"
CONFIG_DIR="/home/$RUN_AS/.homeassistant"
LOG_DIR="/var/log/appdaemon"
LOG_FILE="$LOG_DIR/appdaemon.log"
FLAGS="-c $CONFIG_DIR"
DAEMONIZE="daemonize -c $APPDAEMON_INSTALL_DIR -e $LOG_FILE.stderr -o $LOG_FILE.stdout -p $PID_FILE -l $PID_FILE -v"

start() {
create_piddir
if [ -f $PID_FILE ] && kill -0 $(cat $PID_FILE) 2> /dev/null; then
echo 'Service already running' >&2
return 1
fi
echo -n 'Starting service' >&2
local CMD="$PRE_EXEC $DAEMONIZE $APPDAEMON_BIN $FLAGS"
su -s /bin/bash -c "$CMD" $RUN_AS
if [ $? -ne 0 ]; then
echo "Failed" >&2
else
echo 'Done' >&2
fi
}

stop() {
if [ ! -f "$PID_FILE" ] || ! kill -0 $(cat "$PID_FILE") 2> /dev/null; then
echo 'Service not running' >&2
return 1
fi
echo -n 'Stopping service' >&2
kill $(cat "$PID_FILE")
while ps -p $(cat "$PID_FILE") > /dev/null 2>&1; do sleep 1;done;
rm -f $PID_FILE
echo 'Done' >&2
}

install() {
echo "Installing AppDaemon Daemon (appdaemon-daemon)"
update-rc.d appdaemon-daemon defaults
create_piddir
mkdir -p $CONFIG_DIR
chown $RUN_AS $CONFIG_DIR
mkdir -p $LOG_DIR
chown $RUN_AS $LOG_DIR
}

uninstall() {
echo "Are you really sure you want to uninstall this service? The INIT script will"
echo -n "also be deleted! That cannot be undone. [yes|No] "
local SURE
read SURE
if [ "$SURE" = "yes" ]; then
stop
remove_piddir
echo "Notice: The config directory has not been removed"
echo $CONFIG_DIR
echo "Notice: The log directory has not been removed"
echo $LOG_DIR
update-rc.d -f appdaemon-daemon remove
rm -fv "$0"
echo "AppDaemon Daemon has been removed. AppDaemon is still installed."
fi
}

create_piddir() {
if [ ! -d "$PID_DIR" ]; then
mkdir -p $PID_DIR
chown $RUN_AS "$PID_DIR"
fi
}

remove_piddir() {
if [ -d "$PID_DIR" ]; then
if [ -e "$PID_FILE" ]; then
rm -fv "$PID_FILE"
fi
rmdir -v "$PID_DIR"
fi
}

case "$1" in
start)
start
;;
stop)
stop
;;
install)
install
;;
uninstall)
uninstall
;;
restart)
stop
start
;;
*)
echo "Usage: $0 {start|stop|restart|install|uninstall}"
esac

Save the file and then make it executable:

.. code:: bash
$ sudo chmod +x /etc/init.d/appdaemon-daemon
Activate Init.d Service
~~~~~~~~~~~~~~~~~~~~~~~~

.. code:: bash
$ sudo service appdaemon-daemon install
That's it. After a restart, AppDaemon will start automatically.

If ``AppDaemon`` doesn't start, check the log file output for errors at ``/var/log/appdaemon/appdaemon.log``.

If you want to start/stop ``AppDaemon`` manually, use:

.. code:: bash
$ sudo service appdaemon-daemon <start|stop>
Updating AppDaemon
------------------

To update AppDaemon after a new release has been published, run the
following command to update your local installation:
Expand Down

0 comments on commit 7c8aeee

Please sign in to comment.