From b1876d586efb061750681e8bfebe6b3a2f13edfa Mon Sep 17 00:00:00 2001 From: Ryan Kraus Date: Sun, 30 Aug 2015 20:01:02 -0400 Subject: [PATCH 1/7] Fixed importing of dependency for map tracker. --- .../components/device_tracker/nmap_tracker.py | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/device_tracker/nmap_tracker.py b/homeassistant/components/device_tracker/nmap_tracker.py index ee1650594ee..f2da939a79e 100644 --- a/homeassistant/components/device_tracker/nmap_tracker.py +++ b/homeassistant/components/device_tracker/nmap_tracker.py @@ -26,13 +26,6 @@ from collections import namedtuple import subprocess import re -try: - from libnmap.process import NmapProcess - from libnmap.parser import NmapParser, NmapParserException - LIB_LOADED = True -except ImportError: - LIB_LOADED = False - import homeassistant.util.dt as dt_util from homeassistant.const import CONF_HOSTS from homeassistant.helpers import validate_config @@ -52,13 +45,16 @@ REQUIREMENTS = ['python-libnmap==0.6.1'] def get_scanner(hass, config): """ Validates config and returns a Nmap scanner. """ + # pylint: disable=unused-variable if not validate_config(config, {DOMAIN: [CONF_HOSTS]}, _LOGGER): return None - if not LIB_LOADED: + try: + import libnmap # noqa + except ImportError: _LOGGER.error("Error while importing dependency python-libnmap.") - return False + return None scanner = NmapDeviceScanner(config[DOMAIN]) @@ -83,6 +79,13 @@ class NmapDeviceScanner(object): """ This class scans for devices using nmap """ def __init__(self, config): + from libnmap.process import NmapProcess + from libnmap.parser import NmapParser, NmapParserException + + self.nmap_process = NmapProcess + self.nmap_parser = NmapParser + self.nmap_parser_exception = NmapParserException + self.last_results = [] self.hosts = config[CONF_HOSTS] @@ -116,7 +119,7 @@ class NmapDeviceScanner(object): """ Parses results from an nmap scan. Returns True if successful, False otherwise. """ try: - results = NmapParser.parse(stdout) + results = self.nmap_parser.parse(stdout) now = dt_util.now() self.last_results = [] for host in results.hosts: @@ -134,7 +137,7 @@ class NmapDeviceScanner(object): self.last_results.append(device) _LOGGER.info("nmap scan successful") return True - except NmapParserException as parse_exc: + except self.nmap_parser_exception as parse_exc: _LOGGER.error("failed to parse nmap results: %s", parse_exc.msg) self.last_results = [] return False @@ -159,7 +162,7 @@ class NmapDeviceScanner(object): target_list = [t.ip for t in exclude_targets] options += " --exclude {}".format(",".join(target_list)) - nmap = NmapProcess(targets=self.hosts, options=options) + nmap = self.nmap_process(targets=self.hosts, options=options) nmap.run() From e6ac22514027c1e697504d7cc8276c666b047dad Mon Sep 17 00:00:00 2001 From: Ryan Kraus Date: Sun, 30 Aug 2015 20:22:02 -0400 Subject: [PATCH 2/7] Fixed device tracker to install scanner requirements. --- homeassistant/components/device_tracker/__init__.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/device_tracker/__init__.py b/homeassistant/components/device_tracker/__init__.py index 099c23973f0..234863c571c 100644 --- a/homeassistant/components/device_tracker/__init__.py +++ b/homeassistant/components/device_tracker/__init__.py @@ -15,6 +15,7 @@ from homeassistant.helpers import validate_config from homeassistant.helpers.entity import _OVERWRITE import homeassistant.util as util import homeassistant.util.dt as dt_util +from homeassistant.bootstrap import prepare_setup_platform from homeassistant.helpers.event import track_utc_time_change from homeassistant.const import ( @@ -62,9 +63,9 @@ def setup(hass, config): return False tracker_type = config[DOMAIN].get(CONF_PLATFORM) - - tracker_implementation = get_component( - 'device_tracker.{}'.format(tracker_type)) + + tracker_implementation = \ + prepare_setup_platform(hass, config, DOMAIN, tracker_type) if tracker_implementation is None: _LOGGER.error("Unknown device_tracker type specified: %s.", From ff470c8ffeedad5741f18e67f0c254135b102c02 Mon Sep 17 00:00:00 2001 From: Ryan Kraus Date: Tue, 1 Sep 2015 02:12:00 -0400 Subject: [PATCH 3/7] Added core daemon function as flags. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Created three additional flags for the hass command: -v - Toggle verbose log file output —pid-file - Specify PID file path —daemon - Launch as daemon (nix only) The core now binds to SIGQUIT on nix systems to trigger a clean shutdown. Modified HTTP server to write logging messages through the logging module. --- homeassistant/__main__.py | 77 ++++++++++++++++++++++++++++++-- homeassistant/bootstrap.py | 58 +++++++++++++----------- homeassistant/components/http.py | 9 ++++ homeassistant/core.py | 6 ++- 4 files changed, 119 insertions(+), 31 deletions(-) diff --git a/homeassistant/__main__.py b/homeassistant/__main__.py index d7e81eee13c..e0502da705d 100644 --- a/homeassistant/__main__.py +++ b/homeassistant/__main__.py @@ -77,8 +77,72 @@ def get_arguments(): '--open-ui', action='store_true', help='Open the webinterface in a browser') + parser.add_argument( + '-v', '--verbose', + action='store_true', + help="Enable verbose logging to file.") + parser.add_argument( + '--pid-file', + metavar='path_to_pid_file', + default=None, + help='Path to PID file useful for running as daemon') + if os.name != "nt": + parser.add_argument( + '--daemon', + action='store_true', + help='Run Home Assistant as daemon') - return parser.parse_args() + arguments = parser.parse_args() + if os.name == "nt": + arguments.daemon = False + return arguments + + +def daemonize(): + """ Move current process to daemon process """ + # create first fork + pid = os.fork() + if pid > 0: + sys.exit(0) + + # decouple fork + os.setsid() + os.umask(0) + + # create second fork + pid = os.fork() + if pid > 0: + sys.exit(0) + + +def check_pid(pid_file): + """ Check that HA is not already running """ + # check pid file + if pid_file: + try: + pid = int(open(pid_file, 'r').readline()) + except IOError: + pass + else: + try: + os.kill(pid, 0) + except OSError: + pass + else: + print('Fatal Error: HomeAssistant is already running.') + sys.exit(1) + + +def write_pid(pid_file): + """ Create PID File """ + # store pid + if pid_file: + # write pid file + pid = os.getpid() + try: + open(pid_file, 'w').write(str(pid)) + except IOError: + pass def main(): @@ -90,15 +154,22 @@ def main(): config_dir = os.path.join(os.getcwd(), args.config) ensure_config_path(config_dir) + # daemon functions + check_pid(args.pid_file) + if args.daemon: + daemonize() + write_pid(args.pid_file) + if args.demo_mode: hass = bootstrap.from_config_dict({ 'frontend': {}, 'demo': {} - }, config_dir=config_dir) + }, config_dir=config_dir, daemon=args.daemon, verbose=args.verbose) else: config_file = ensure_config_file(config_dir) print('Config directory:', config_dir) - hass = bootstrap.from_config_file(config_file) + hass = bootstrap.from_config_file( + config_file, daemon=args.daemon, verbose=args.verbose) if args.open_ui: def open_browser(event): diff --git a/homeassistant/bootstrap.py b/homeassistant/bootstrap.py index 98c7ae63a8b..74b831d58c1 100644 --- a/homeassistant/bootstrap.py +++ b/homeassistant/bootstrap.py @@ -150,7 +150,8 @@ def mount_local_lib_path(config_dir): # pylint: disable=too-many-branches, too-many-statements -def from_config_dict(config, hass=None, config_dir=None, enable_log=True): +def from_config_dict(config, hass=None, config_dir=None, enable_log=True, + verbose=False, daemon=False): """ Tries to configure Home Assistant from a config dict. @@ -166,7 +167,7 @@ def from_config_dict(config, hass=None, config_dir=None, enable_log=True): process_ha_core_config(hass, config.get(core.DOMAIN, {})) if enable_log: - enable_logging(hass) + enable_logging(hass, verbose, daemon) _ensure_loader_prepared(hass) @@ -195,7 +196,7 @@ def from_config_dict(config, hass=None, config_dir=None, enable_log=True): return hass -def from_config_file(config_path, hass=None): +def from_config_file(config_path, hass=None, verbose=False, daemon=False): """ Reads the configuration file and tries to start all the required functionality. Will add functionality to 'hass' parameter if given, @@ -209,35 +210,36 @@ def from_config_file(config_path, hass=None): hass.config.config_dir = config_dir mount_local_lib_path(config_dir) - enable_logging(hass) + enable_logging(hass, verbose, daemon) config_dict = config_util.load_config_file(config_path) return from_config_dict(config_dict, hass, enable_log=False) -def enable_logging(hass): +def enable_logging(hass, verbose=False, daemon=False): """ Setup the logging for home assistant. """ - logging.basicConfig(level=logging.INFO) - fmt = ("%(log_color)s%(asctime)s %(levelname)s (%(threadName)s) " - "[%(name)s] %(message)s%(reset)s") - try: - from colorlog import ColoredFormatter - logging.getLogger().handlers[0].setFormatter(ColoredFormatter( - fmt, - datefmt='%y-%m-%d %H:%M:%S', - reset=True, - log_colors={ - 'DEBUG': 'cyan', - 'INFO': 'green', - 'WARNING': 'yellow', - 'ERROR': 'red', - 'CRITICAL': 'red', - } - )) - except ImportError: - _LOGGER.warning( - "Colorlog package not found, console coloring disabled") + if not daemon: + logging.basicConfig(level=logging.INFO) + fmt = ("%(log_color)s%(asctime)s %(levelname)s (%(threadName)s) " + "[%(name)s] %(message)s%(reset)s") + try: + from colorlog import ColoredFormatter + logging.getLogger().handlers[0].setFormatter(ColoredFormatter( + fmt, + datefmt='%y-%m-%d %H:%M:%S', + reset=True, + log_colors={ + 'DEBUG': 'cyan', + 'INFO': 'green', + 'WARNING': 'yellow', + 'ERROR': 'red', + 'CRITICAL': 'red', + } + )) + except ImportError: + _LOGGER.warning( + "Colorlog package not found, console coloring disabled") # Log errors to a file if we have write access to file or config dir err_log_path = hass.config.path('home-assistant.log') @@ -251,11 +253,13 @@ def enable_logging(hass): err_handler = logging.FileHandler( err_log_path, mode='w', delay=True) - err_handler.setLevel(logging.WARNING) + err_handler.setLevel(logging.INFO if verbose else logging.WARNING) err_handler.setFormatter( logging.Formatter('%(asctime)s %(name)s: %(message)s', datefmt='%y-%m-%d %H:%M:%S')) - logging.getLogger('').addHandler(err_handler) + logger = logging.getLogger('') + logger.addHandler(err_handler) + logger.setLevel(logging.INFO) # this sets the minimum log level else: _LOGGER.error( diff --git a/homeassistant/components/http.py b/homeassistant/components/http.py index a28def8e7ba..0b4f6165bed 100644 --- a/homeassistant/components/http.py +++ b/homeassistant/components/http.py @@ -208,6 +208,11 @@ class HomeAssistantHTTPServer(ThreadingMixIn, HTTPServer): """ Registers a path wit the server. """ self.paths.append((method, url, callback, require_auth)) + def log_message(self, fmt, *args): + """ Redirect built-in log to HA logging """ + # pylint: disable=no-self-use + _LOGGER.info(fmt, *args) + # pylint: disable=too-many-public-methods,too-many-locals class RequestHandler(SimpleHTTPRequestHandler): @@ -225,6 +230,10 @@ class RequestHandler(SimpleHTTPRequestHandler): self._session = None SimpleHTTPRequestHandler.__init__(self, req, client_addr, server) + def log_message(self, fmt, *arguments): + """ Redirect built-in log to HA logging """ + _LOGGER.info(fmt, *arguments) + def _handle_request(self, method): # pylint: disable=too-many-branches """ Does some common checks and calls appropriate method. """ url = urlparse(self.path) diff --git a/homeassistant/core.py b/homeassistant/core.py index c04e9a9ab63..1e48fce8029 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -9,6 +9,7 @@ of entities and react to changes. import os import time import logging +import signal import threading import enum import re @@ -73,13 +74,16 @@ class HomeAssistant(object): will block until called. """ request_shutdown = threading.Event() - def stop_homeassistant(service): + def stop_homeassistant(*args): """ Stops Home Assistant. """ request_shutdown.set() self.services.register( DOMAIN, SERVICE_HOMEASSISTANT_STOP, stop_homeassistant) + if os.name != "nt": + signal.signal(signal.SIGQUIT, stop_homeassistant) + while not request_shutdown.isSet(): try: time.sleep(1) From 5b06e8d25e8b4a9a5150d57da311fa07be9562b5 Mon Sep 17 00:00:00 2001 From: Ryan Kraus Date: Tue, 1 Sep 2015 02:37:52 -0400 Subject: [PATCH 4/7] Daemon handling cleanup. More cleanly handling whether a PID file should be checked. Fatal Error on PID file IOError. Pylint fix to bootstrap. --- homeassistant/__main__.py | 25 +++++++++++++------------ homeassistant/bootstrap.py | 2 +- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/homeassistant/__main__.py b/homeassistant/__main__.py index e0502da705d..f855b8f5496 100644 --- a/homeassistant/__main__.py +++ b/homeassistant/__main__.py @@ -118,19 +118,18 @@ def daemonize(): def check_pid(pid_file): """ Check that HA is not already running """ # check pid file - if pid_file: + try: + pid = int(open(pid_file, 'r').readline()) + except IOError: + pass + else: try: - pid = int(open(pid_file, 'r').readline()) - except IOError: + os.kill(pid, 0) + except OSError: pass else: - try: - os.kill(pid, 0) - except OSError: - pass - else: - print('Fatal Error: HomeAssistant is already running.') - sys.exit(1) + print('Fatal Error: HomeAssistant is already running.') + sys.exit(1) def write_pid(pid_file): @@ -142,7 +141,8 @@ def write_pid(pid_file): try: open(pid_file, 'w').write(str(pid)) except IOError: - pass + print('Fatal Error: Unable to write pid file {}'.format(pid_file)) + sys.exit(1) def main(): @@ -155,7 +155,8 @@ def main(): ensure_config_path(config_dir) # daemon functions - check_pid(args.pid_file) + if args.pid_file: + check_pid(args.pid_file) if args.daemon: daemonize() write_pid(args.pid_file) diff --git a/homeassistant/bootstrap.py b/homeassistant/bootstrap.py index 74b831d58c1..4b98765e34d 100644 --- a/homeassistant/bootstrap.py +++ b/homeassistant/bootstrap.py @@ -149,7 +149,7 @@ def mount_local_lib_path(config_dir): sys.path.insert(0, os.path.join(config_dir, 'lib')) -# pylint: disable=too-many-branches, too-many-statements +# pylint: disable=too-many-branches, too-many-statements, too-many-arguments def from_config_dict(config, hass=None, config_dir=None, enable_log=True, verbose=False, daemon=False): """ From 4ca8f184e61432ff3cca6cfefc3346270c56ff5b Mon Sep 17 00:00:00 2001 From: Ryan Kraus Date: Tue, 1 Sep 2015 03:22:43 -0400 Subject: [PATCH 5/7] Updated Debian daemon script. --- homeassistant/__main__.py | 3 ++ scripts/hass-daemon | 97 ++++++++++++++++++++++++++++++++++++ scripts/homeassistant-pi.sh | 78 ----------------------------- scripts/homeassistant.daemon | 88 -------------------------------- 4 files changed, 100 insertions(+), 166 deletions(-) create mode 100644 scripts/hass-daemon delete mode 100755 scripts/homeassistant-pi.sh delete mode 100755 scripts/homeassistant.daemon diff --git a/homeassistant/__main__.py b/homeassistant/__main__.py index f855b8f5496..e219b973f37 100644 --- a/homeassistant/__main__.py +++ b/homeassistant/__main__.py @@ -121,13 +121,16 @@ def check_pid(pid_file): try: pid = int(open(pid_file, 'r').readline()) except IOError: + # PID File does not exist pass else: try: os.kill(pid, 0) except OSError: + # PID does not exist pass else: + # PID already exists print('Fatal Error: HomeAssistant is already running.') sys.exit(1) diff --git a/scripts/hass-daemon b/scripts/hass-daemon new file mode 100644 index 00000000000..5206a446725 --- /dev/null +++ b/scripts/hass-daemon @@ -0,0 +1,97 @@ +#!/bin/sh +### BEGIN INIT INFO +# Provides: hass +# 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: Home\ Assistant +### END INIT INFO + +# /etc/init.d Service Script for Home Assistant +# Created with: https://gist.github.com/naholyr/4275302#file-new-service-sh +# +# Installation: +# 1) If any commands need to run before executing hass (like loading a +# virutal environment), put them in PRE_EXEC. +# 2) Set RUN_AS to the username that should be used to execute hass. +# 3) Copy this script to /etc/init.d/ +# 4) Install this script +# sudo hass-daemon install +# 5) Restart Machine +# +# After installation, HA should start automatically. If HA does not start, +# check the log file output for errors. +# /var/opt/homeassistant/home-assistant.log + +PRE_EXEC="" +RUN_AS="USER" +PID_FILE="/var/run/hass.pid" +CONFIG_DIR="/var/opt/homeassistant" +FLAGS="-v --config $CONFIG_DIR --pid-file $PID_FILE --daemon" + +start() { + if [ -f $PID_FILE ] && kill -0 $(cat $PID_FILE); then + echo 'Service already running' >&2 + return 1 + fi + echo 'Starting service…' >&2 + local CMD="$PRE_EXEC; hass $FLAGS;" + su -c "$CMD" $RUN_AS + echo 'Service started' >&2 +} + +stop() { + if [ ! -f "$PID_FILE" ] || ! kill -0 $(cat "$PID_FILE"); then + echo 'Service not running' >&2 + return 1 + fi + echo 'Stopping service…' >&2 + kill -3 $(cat "$PID_FILE") + echo 'Service stopped' >&2 +} + +install() { + echo "Installing Home Assistant Daemon (hass-daemon)" + touch $PID_FILE + chwon $RUN_AS $PID_FILE + mkdir -p $CONFIG_DIR + chown $RUN_AS $CONFIG_DIR + update-rc.d homeassistant defaults +} + +uninstall() { + echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] " + local SURE + read SURE + if [ "$SURE" = "yes" ]; then + stop + rm -fv "$PID_FILE" + echo "Notice: The config directory has not been removed" + echo $CONFIG_DIR + update-rc.d -f hass-daemon remove + rm -fv "$0" + echo "Home Assistant Daemon has been removed. Home Assistant is still installed." + fi +} + +case "$1" in + start) + start + ;; + stop) + stop + ;; + install) + install + ;; + uninstall) + uninstall + ;; + restart) + stop + start + ;; + *) + echo "Usage: $0 {start|stop|restart|install|uninstall}" +esac diff --git a/scripts/homeassistant-pi.sh b/scripts/homeassistant-pi.sh deleted file mode 100755 index 1d5537f0191..00000000000 --- a/scripts/homeassistant-pi.sh +++ /dev/null @@ -1,78 +0,0 @@ -#!/bin/sh - -# To script is for running Home Assistant as a service and automatically starting it on boot. -# Assuming you have cloned the HA repo into /home/pi/Apps/home-assistant adjust this path if necessary -# This also assumes you installed HA on your raspberry pi using the instructions here: -# https://home-assistant.io/getting-started/ -# -# To install to the following: -# sudo cp /home/pi/Apps/home-assistant/scripts/homeassistant-pi.sh /etc/init.d/homeassistant.sh -# sudo chmod +x /etc/init.d/homeassistant.sh -# sudo chown root:root /etc/init.d/homeassistant.sh -# -# If you want HA to start on boot also run the following: -# sudo update-rc.d homeassistant.sh defaults -# sudo update-rc.d homeassistant.sh enable -# -# You should now be able to start HA by running -# sudo /etc/init.d/homeassistant.sh start - -### BEGIN INIT INFO -# Provides: myservice -# Required-Start: $remote_fs $syslog -# Required-Stop: $remote_fs $syslog -# Default-Start: 2 3 4 5 -# Default-Stop: 0 1 6 -# Short-Description: Put a short description of the service here -# Description: Put a long description of the service here -### END INIT INFO - -# Change the next 3 lines to suit where you install your script and what you want to call it -DIR=/home/pi/Apps/home-assistant -DAEMON="/home/pi/.pyenv/shims/python3 -m homeassistant" -DAEMON_NAME=homeassistant - -# Add any command line options for your daemon here -DAEMON_OPTS="" - -# This next line determines what user the script runs as. -# Root generally not recommended but necessary if you are using the Raspberry Pi GPIO from Python. -DAEMON_USER=pi - -# The process ID of the script when it runs is stored here: -PIDFILE=/var/run/$DAEMON_NAME.pid - -. /lib/lsb/init-functions - -do_start () { - log_daemon_msg "Starting system $DAEMON_NAME daemon" - start-stop-daemon --start --background --chdir $DIR --pidfile $PIDFILE --make-pidfile --user $DAEMON_USER --chuid $DAEMON_USER --startas $DAEMON -- $DAEMON_OPTS - log_end_msg $? -} -do_stop () { - log_daemon_msg "Stopping system $DAEMON_NAME daemon" - start-stop-daemon --stop --pidfile $PIDFILE --retry 10 - log_end_msg $? -} - -case "$1" in - - start|stop) - do_${1} - ;; - - restart|reload|force-reload) - do_stop - do_start - ;; - - status) - status_of_proc "$DAEMON_NAME" "$DAEMON" && exit 0 || exit $? - ;; - *) - echo "Usage: /etc/init.d/$DAEMON_NAME {start|stop|restart|status}" - exit 1 - ;; - -esac -exit 0 diff --git a/scripts/homeassistant.daemon b/scripts/homeassistant.daemon deleted file mode 100755 index 4dd6b37a9c5..00000000000 --- a/scripts/homeassistant.daemon +++ /dev/null @@ -1,88 +0,0 @@ -#!/bin/sh -### BEGIN INIT INFO -# Provides: homeassistant -# 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: Home\ Assistant -### END INIT INFO - -# /etc/init.d Service Script for Home Assistant -# Created with: https://gist.github.com/naholyr/4275302#file-new-service-sh -# -# Installation: -# 1) Populate RUNAS and RUNDIR variables -# 2) Create Log -- sudo touch /var/log/homeassistant.log -# 3) Set Log Ownership -- sudo chown USER:GROUP /var/log/homeassistant.log -# 4) Create PID File -- sudo touch /var/run/homeassistant.pid -# 5) Set PID File Ownership -- sudo chown USER:GROUP /var/run/homeassistant.pid -# 6) Install init.d script -- cp homeassistant.daemon /etc/init.d/homeassistant -# 7) Setup HA for Auto Start -- sudo update-rc.d homeassistant defaults -# 8) Run HA -- sudo service homeassistant start -# -# After installation, HA should start automatically. If HA does not start, -# check the log file output for errors. (/var/log/homeassistant.log) -# -# For this script, it is assumed that you are using a local Virtual Environment -# per the install directions as http://home-assistant.io -# If you are not, the SCRIPT variable must be modified to point to the correct -# Python environment. - -SCRIPT="source bin/activate; ./bin/python -m homeassistant" -RUNAS= -RUNDIR= -PIDFILE=/var/run/homeassistant.pid -LOGFILE=/var/log/homeassistant.log - -start() { - if [ -f /var/run/$PIDNAME ] && kill -0 $(cat /var/run/$PIDNAME); then - echo 'Service already running' >&2 - return 1 - fi - echo 'Starting service…' >&2 - local CMD="cd $RUNDIR; $SCRIPT &> \"$LOGFILE\" & echo \$!" - su -c "$CMD" $RUNAS > "$PIDFILE" - echo 'Service started' >&2 -} - -stop() { - if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then - echo 'Service not running' >&2 - return 1 - fi - echo 'Stopping service…' >&2 - kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE" - echo 'Service stopped' >&2 -} - -uninstall() { - echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] " - local SURE - read SURE - if [ "$SURE" = "yes" ]; then - stop - rm -f "$PIDFILE" - echo "Notice: log file is not be removed: '$LOGFILE'" >&2 - update-rc.d -f homeassistant remove - rm -fv "$0" - fi -} - -case "$1" in - start) - start - ;; - stop) - stop - ;; - uninstall) - uninstall - ;; - restart) - stop - start - ;; - *) - echo "Usage: $0 {start|stop|restart|uninstall}" -esac From 7992882fa3adf1adcfa37eef7d083c3d16979ed8 Mon Sep 17 00:00:00 2001 From: Ryan Kraus Date: Tue, 1 Sep 2015 03:29:07 -0400 Subject: [PATCH 6/7] Cleanup PID checking logic and write PID logic. --- homeassistant/__main__.py | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/homeassistant/__main__.py b/homeassistant/__main__.py index e219b973f37..6e12afa46c0 100644 --- a/homeassistant/__main__.py +++ b/homeassistant/__main__.py @@ -122,30 +122,25 @@ def check_pid(pid_file): pid = int(open(pid_file, 'r').readline()) except IOError: # PID File does not exist - pass - else: - try: - os.kill(pid, 0) - except OSError: - # PID does not exist - pass - else: - # PID already exists - print('Fatal Error: HomeAssistant is already running.') - sys.exit(1) + return + + try: + os.kill(pid, 0) + except OSError: + # PID does not exist + return + print('Fatal Error: HomeAssistant is already running.') + sys.exit(1) def write_pid(pid_file): """ Create PID File """ - # store pid - if pid_file: - # write pid file - pid = os.getpid() - try: - open(pid_file, 'w').write(str(pid)) - except IOError: - print('Fatal Error: Unable to write pid file {}'.format(pid_file)) - sys.exit(1) + pid = os.getpid() + try: + open(pid_file, 'w').write(str(pid)) + except IOError: + print('Fatal Error: Unable to write pid file {}'.format(pid_file)) + sys.exit(1) def main(): @@ -162,7 +157,8 @@ def main(): check_pid(args.pid_file) if args.daemon: daemonize() - write_pid(args.pid_file) + if args.pid_file: + write_pid(args.pid_file) if args.demo_mode: hass = bootstrap.from_config_dict({ From 18569104faa9f266a3bd88ea52588be64e716505 Mon Sep 17 00:00:00 2001 From: Ryan Kraus Date: Tue, 1 Sep 2015 03:54:59 -0400 Subject: [PATCH 7/7] Fixes to new Debian daemon script. --- scripts/hass-daemon | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/scripts/hass-daemon b/scripts/hass-daemon index 5206a446725..d11c2669e87 100644 --- a/scripts/hass-daemon +++ b/scripts/hass-daemon @@ -13,12 +13,17 @@ # # Installation: # 1) If any commands need to run before executing hass (like loading a -# virutal environment), put them in PRE_EXEC. +# virutal environment), put them in PRE_EXEC. This command must end with +# a semicolon. # 2) Set RUN_AS to the username that should be used to execute hass. # 3) Copy this script to /etc/init.d/ -# 4) Install this script -# sudo hass-daemon install -# 5) Restart Machine +# sudo cp hass-daemon /etc/init.d/hass-daemon +# sudo chmod +x /etc/init.d/hass-daemon +# 4) Register the daemon with Linux +# sudo update-rc.d hass-daemon defaults +# 5) Install this service +# sudo service hass-daemon install +# 6) Restart Machine # # After installation, HA should start automatically. If HA does not start, # check the log file output for errors. @@ -36,7 +41,7 @@ start() { return 1 fi echo 'Starting service…' >&2 - local CMD="$PRE_EXEC; hass $FLAGS;" + local CMD="$PRE_EXEC hass $FLAGS;" su -c "$CMD" $RUN_AS echo 'Service started' >&2 } @@ -53,11 +58,10 @@ stop() { install() { echo "Installing Home Assistant Daemon (hass-daemon)" - touch $PID_FILE - chwon $RUN_AS $PID_FILE + echo "999999" > $PID_FILE + chown $RUN_AS $PID_FILE mkdir -p $CONFIG_DIR chown $RUN_AS $CONFIG_DIR - update-rc.d homeassistant defaults } uninstall() {