2017-04-30 07:04:49 +02:00
|
|
|
"""Start Home Assistant."""
|
2021-03-17 17:34:55 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2016-02-19 06:27:50 +01:00
|
|
|
import argparse
|
2021-09-28 09:05:06 +02:00
|
|
|
import faulthandler
|
2016-02-19 06:27:50 +01:00
|
|
|
import os
|
2016-05-20 08:20:59 +02:00
|
|
|
import platform
|
|
|
|
import subprocess
|
2014-11-03 02:27:32 +01:00
|
|
|
import sys
|
2016-01-27 04:39:59 +01:00
|
|
|
import threading
|
2016-07-21 07:38:52 +02:00
|
|
|
|
2019-12-09 16:42:10 +01:00
|
|
|
from homeassistant.const import REQUIRED_PYTHON_VER, RESTART_EXIT_CODE, __version__
|
2015-08-30 02:06:54 +02:00
|
|
|
|
2021-09-28 09:05:06 +02:00
|
|
|
FAULT_LOG_FILENAME = "home-assistant.log.fault"
|
|
|
|
|
2015-05-01 07:44:24 +02:00
|
|
|
|
2016-07-21 07:38:52 +02:00
|
|
|
def validate_python() -> None:
|
2017-06-08 15:53:12 +02:00
|
|
|
"""Validate that the right Python version is running."""
|
2018-02-23 08:22:27 +01:00
|
|
|
if sys.version_info[:3] < REQUIRED_PYTHON_VER:
|
2019-07-31 21:25:30 +02:00
|
|
|
print(
|
2020-04-05 17:48:55 +02:00
|
|
|
"Home Assistant requires at least Python "
|
|
|
|
f"{REQUIRED_PYTHON_VER[0]}.{REQUIRED_PYTHON_VER[1]}.{REQUIRED_PYTHON_VER[2]}"
|
2019-07-31 21:25:30 +02:00
|
|
|
)
|
2015-08-31 17:53:59 +02:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
2016-07-21 07:38:52 +02:00
|
|
|
def ensure_config_path(config_dir: str) -> None:
|
2016-03-08 00:06:04 +01:00
|
|
|
"""Validate the configuration directory."""
|
2020-04-04 17:07:36 +02:00
|
|
|
# pylint: disable=import-outside-toplevel
|
2016-03-27 17:44:15 +02:00
|
|
|
import homeassistant.config as config_util
|
2019-07-31 21:25:30 +02:00
|
|
|
|
|
|
|
lib_dir = os.path.join(config_dir, "deps")
|
2015-08-30 02:06:54 +02:00
|
|
|
|
2015-01-09 03:45:27 +01:00
|
|
|
# Test if configuration directory exists
|
2014-11-23 21:57:29 +01:00
|
|
|
if not os.path.isdir(config_dir):
|
2015-08-30 05:31:33 +02:00
|
|
|
if config_dir != config_util.get_default_config_dir():
|
2019-07-31 21:25:30 +02:00
|
|
|
print(
|
2020-01-03 14:47:06 +01:00
|
|
|
f"Fatal Error: Specified configuration directory {config_dir} "
|
|
|
|
"does not exist"
|
2019-07-31 21:25:30 +02:00
|
|
|
)
|
2015-08-30 05:31:33 +02:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
try:
|
|
|
|
os.mkdir(config_dir)
|
|
|
|
except OSError:
|
2019-07-31 21:25:30 +02:00
|
|
|
print(
|
2020-01-03 14:47:06 +01:00
|
|
|
"Fatal Error: Unable to create default configuration "
|
|
|
|
f"directory {config_dir}"
|
2019-07-31 21:25:30 +02:00
|
|
|
)
|
2015-08-30 05:31:33 +02:00
|
|
|
sys.exit(1)
|
2015-08-30 02:06:54 +02:00
|
|
|
|
|
|
|
# Test if library directory exists
|
|
|
|
if not os.path.isdir(lib_dir):
|
|
|
|
try:
|
|
|
|
os.mkdir(lib_dir)
|
|
|
|
except OSError:
|
2020-01-03 14:47:06 +01:00
|
|
|
print(f"Fatal Error: Unable to create library directory {lib_dir}")
|
2015-08-30 05:31:33 +02:00
|
|
|
sys.exit(1)
|
2015-04-26 19:05:01 +02:00
|
|
|
|
2015-08-30 08:02:07 +02:00
|
|
|
|
2016-07-21 07:38:52 +02:00
|
|
|
def get_arguments() -> argparse.Namespace:
|
2016-03-08 00:06:04 +01:00
|
|
|
"""Get parsed passed in arguments."""
|
2020-04-04 17:07:36 +02:00
|
|
|
# pylint: disable=import-outside-toplevel
|
2016-03-27 17:44:15 +02:00
|
|
|
import homeassistant.config as config_util
|
2019-07-31 21:25:30 +02:00
|
|
|
|
2015-08-30 08:02:07 +02:00
|
|
|
parser = argparse.ArgumentParser(
|
2019-07-31 21:25:30 +02:00
|
|
|
description="Home Assistant: Observe, Control, Automate."
|
|
|
|
)
|
|
|
|
parser.add_argument("--version", action="version", version=__version__)
|
2015-01-09 03:45:27 +01:00
|
|
|
parser.add_argument(
|
2019-07-31 21:25:30 +02:00
|
|
|
"-c",
|
|
|
|
"--config",
|
|
|
|
metavar="path_to_config_dir",
|
2015-08-30 03:11:24 +02:00
|
|
|
default=config_util.get_default_config_dir(),
|
2019-07-31 21:25:30 +02:00
|
|
|
help="Directory that contains the Home Assistant configuration",
|
|
|
|
)
|
2015-01-18 07:23:07 +01:00
|
|
|
parser.add_argument(
|
2020-01-14 22:03:02 +01:00
|
|
|
"--safe-mode", action="store_true", help="Start Home Assistant in safe mode"
|
2019-07-31 21:25:30 +02:00
|
|
|
)
|
2016-02-06 15:48:36 +01:00
|
|
|
parser.add_argument(
|
2019-07-31 21:25:30 +02:00
|
|
|
"--debug", action="store_true", help="Start Home Assistant in debug mode"
|
|
|
|
)
|
2015-01-18 06:13:02 +01:00
|
|
|
parser.add_argument(
|
2019-07-31 21:25:30 +02:00
|
|
|
"--open-ui", action="store_true", help="Open the webinterface in a browser"
|
|
|
|
)
|
2015-09-04 23:50:57 +02:00
|
|
|
parser.add_argument(
|
2019-07-31 21:25:30 +02:00
|
|
|
"--skip-pip",
|
|
|
|
action="store_true",
|
|
|
|
help="Skips pip install of required packages on startup",
|
|
|
|
)
|
2015-09-01 08:12:00 +02:00
|
|
|
parser.add_argument(
|
2019-07-31 21:25:30 +02:00
|
|
|
"-v", "--verbose", action="store_true", help="Enable verbose logging to file."
|
|
|
|
)
|
2015-09-01 08:12:00 +02:00
|
|
|
parser.add_argument(
|
2019-07-31 21:25:30 +02:00
|
|
|
"--pid-file",
|
|
|
|
metavar="path_to_pid_file",
|
2015-09-01 08:12:00 +02:00
|
|
|
default=None,
|
2019-07-31 21:25:30 +02:00
|
|
|
help="Path to PID file useful for running as daemon",
|
|
|
|
)
|
2015-09-05 00:22:42 +02:00
|
|
|
parser.add_argument(
|
2019-07-31 21:25:30 +02:00
|
|
|
"--log-rotate-days",
|
2015-09-05 00:22:42 +02:00
|
|
|
type=int,
|
|
|
|
default=None,
|
2019-07-31 21:25:30 +02:00
|
|
|
help="Enables daily log rotation and keeps up to the specified days",
|
|
|
|
)
|
2017-09-14 06:22:42 +02:00
|
|
|
parser.add_argument(
|
2019-07-31 21:25:30 +02:00
|
|
|
"--log-file",
|
2017-09-14 06:22:42 +02:00
|
|
|
type=str,
|
|
|
|
default=None,
|
2020-01-02 20:17:10 +01:00
|
|
|
help="Log file to write to. If not set, CONFIG/home-assistant.log is used",
|
2019-07-31 21:25:30 +02:00
|
|
|
)
|
2018-04-18 16:18:44 +02:00
|
|
|
parser.add_argument(
|
2019-07-31 21:25:30 +02:00
|
|
|
"--log-no-color", action="store_true", help="Disable color logs"
|
|
|
|
)
|
2016-05-20 08:20:59 +02:00
|
|
|
parser.add_argument(
|
2019-07-31 21:25:30 +02:00
|
|
|
"--runner",
|
|
|
|
action="store_true",
|
2019-08-23 18:53:33 +02:00
|
|
|
help=f"On restart exit with code {RESTART_EXIT_CODE}",
|
2019-07-31 21:25:30 +02:00
|
|
|
)
|
2016-07-03 20:38:14 +02:00
|
|
|
parser.add_argument(
|
2019-07-31 21:25:30 +02:00
|
|
|
"--script", nargs=argparse.REMAINDER, help="Run one of the embedded scripts"
|
|
|
|
)
|
2016-05-20 08:20:59 +02:00
|
|
|
if os.name == "posix":
|
2015-09-01 08:12:00 +02:00
|
|
|
parser.add_argument(
|
2019-07-31 21:25:30 +02:00
|
|
|
"--daemon", action="store_true", help="Run Home Assistant as daemon"
|
|
|
|
)
|
2015-09-01 08:12:00 +02:00
|
|
|
|
|
|
|
arguments = parser.parse_args()
|
2016-05-20 08:20:59 +02:00
|
|
|
if os.name != "posix" or arguments.debug or arguments.runner:
|
2019-07-31 21:25:30 +02:00
|
|
|
setattr(arguments, "daemon", False)
|
2016-05-20 08:20:59 +02:00
|
|
|
|
2015-09-01 08:12:00 +02:00
|
|
|
return arguments
|
|
|
|
|
|
|
|
|
2016-07-21 07:38:52 +02:00
|
|
|
def daemonize() -> None:
|
2016-03-08 00:06:04 +01:00
|
|
|
"""Move current process to daemon process."""
|
|
|
|
# Create first fork
|
2021-09-19 01:31:35 +02:00
|
|
|
if os.fork() > 0:
|
2015-09-01 08:12:00 +02:00
|
|
|
sys.exit(0)
|
|
|
|
|
2016-03-08 00:06:04 +01:00
|
|
|
# Decouple fork
|
2015-09-01 08:12:00 +02:00
|
|
|
os.setsid()
|
|
|
|
|
2016-03-08 00:06:04 +01:00
|
|
|
# Create second fork
|
2021-09-19 01:31:35 +02:00
|
|
|
if os.fork() > 0:
|
2015-09-01 08:12:00 +02:00
|
|
|
sys.exit(0)
|
|
|
|
|
2016-05-20 08:20:07 +02:00
|
|
|
# redirect standard file descriptors to devnull
|
2021-04-25 02:39:24 +02:00
|
|
|
# pylint: disable=consider-using-with
|
2021-07-28 09:41:45 +02:00
|
|
|
infd = open(os.devnull, encoding="utf8")
|
|
|
|
outfd = open(os.devnull, "a+", encoding="utf8")
|
2016-05-20 08:20:07 +02:00
|
|
|
sys.stdout.flush()
|
|
|
|
sys.stderr.flush()
|
2016-05-20 16:03:08 +02:00
|
|
|
os.dup2(infd.fileno(), sys.stdin.fileno())
|
|
|
|
os.dup2(outfd.fileno(), sys.stdout.fileno())
|
|
|
|
os.dup2(outfd.fileno(), sys.stderr.fileno())
|
2016-05-20 08:20:07 +02:00
|
|
|
|
2015-09-01 08:12:00 +02:00
|
|
|
|
2016-07-21 07:38:52 +02:00
|
|
|
def check_pid(pid_file: str) -> None:
|
2017-06-08 15:53:12 +02:00
|
|
|
"""Check that Home Assistant is not already running."""
|
2016-03-08 00:06:04 +01:00
|
|
|
# Check pid file
|
2015-09-01 08:37:52 +02:00
|
|
|
try:
|
2021-07-28 09:41:45 +02:00
|
|
|
with open(pid_file, encoding="utf8") as file:
|
2018-01-31 11:30:48 +01:00
|
|
|
pid = int(file.readline())
|
2019-09-04 19:09:24 +02:00
|
|
|
except OSError:
|
2015-09-01 09:22:43 +02:00
|
|
|
# PID File does not exist
|
2015-09-01 09:29:07 +02:00
|
|
|
return
|
|
|
|
|
2016-05-20 20:45:16 +02:00
|
|
|
# If we just restarted, we just found our own pidfile.
|
|
|
|
if pid == os.getpid():
|
|
|
|
return
|
|
|
|
|
2015-09-01 09:29:07 +02:00
|
|
|
try:
|
|
|
|
os.kill(pid, 0)
|
|
|
|
except OSError:
|
|
|
|
# PID does not exist
|
|
|
|
return
|
2020-01-05 13:09:17 +01:00
|
|
|
print("Fatal Error: Home Assistant is already running.")
|
2015-09-01 09:29:07 +02:00
|
|
|
sys.exit(1)
|
2015-09-01 08:12:00 +02:00
|
|
|
|
|
|
|
|
2016-07-21 07:38:52 +02:00
|
|
|
def write_pid(pid_file: str) -> None:
|
2016-03-08 00:06:04 +01:00
|
|
|
"""Create a PID File."""
|
2015-09-01 09:29:07 +02:00
|
|
|
pid = os.getpid()
|
|
|
|
try:
|
2021-07-28 09:41:45 +02:00
|
|
|
with open(pid_file, "w", encoding="utf8") as file:
|
2018-01-31 11:30:48 +01:00
|
|
|
file.write(str(pid))
|
2019-09-04 19:09:24 +02:00
|
|
|
except OSError:
|
2019-08-23 18:53:33 +02:00
|
|
|
print(f"Fatal Error: Unable to write pid file {pid_file}")
|
2015-09-01 09:29:07 +02:00
|
|
|
sys.exit(1)
|
2015-01-09 03:45:27 +01:00
|
|
|
|
2015-09-15 08:17:01 +02:00
|
|
|
|
2016-07-21 07:38:52 +02:00
|
|
|
def closefds_osx(min_fd: int, max_fd: int) -> None:
|
2016-05-20 08:20:59 +02:00
|
|
|
"""Make sure file descriptors get closed when we restart.
|
2016-03-08 00:06:04 +01:00
|
|
|
|
2016-05-20 08:20:59 +02:00
|
|
|
We cannot call close on guarded fds, and we cannot easily test which fds
|
|
|
|
are guarded. But we can set the close-on-exec flag on everything we want to
|
|
|
|
get rid of.
|
2016-02-06 15:48:36 +01:00
|
|
|
"""
|
2020-04-04 17:07:36 +02:00
|
|
|
# pylint: disable=import-outside-toplevel
|
2020-08-29 08:23:55 +02:00
|
|
|
from fcntl import F_GETFD, F_SETFD, FD_CLOEXEC, fcntl
|
2016-05-20 08:20:59 +02:00
|
|
|
|
|
|
|
for _fd in range(min_fd, max_fd):
|
|
|
|
try:
|
|
|
|
val = fcntl(_fd, F_GETFD)
|
|
|
|
if not val & FD_CLOEXEC:
|
|
|
|
fcntl(_fd, F_SETFD, val | FD_CLOEXEC)
|
2019-09-04 19:09:24 +02:00
|
|
|
except OSError:
|
2016-05-20 08:20:59 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2021-03-17 17:34:55 +01:00
|
|
|
def cmdline() -> list[str]:
|
2016-05-20 08:20:59 +02:00
|
|
|
"""Collect path and arguments to re-execute the current hass instance."""
|
2019-07-31 21:25:30 +02:00
|
|
|
if os.path.basename(sys.argv[0]) == "__main__.py":
|
2016-05-20 20:45:16 +02:00
|
|
|
modulepath = os.path.dirname(sys.argv[0])
|
2019-07-31 21:25:30 +02:00
|
|
|
os.environ["PYTHONPATH"] = os.path.dirname(modulepath)
|
|
|
|
return [sys.executable] + [arg for arg in sys.argv if arg != "--daemon"]
|
2017-07-06 05:02:16 +02:00
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
return [arg for arg in sys.argv if arg != "--daemon"]
|
2016-05-20 08:20:59 +02:00
|
|
|
|
|
|
|
|
2016-07-21 07:38:52 +02:00
|
|
|
def try_to_restart() -> None:
|
2017-06-08 15:53:12 +02:00
|
|
|
"""Attempt to clean up state and start a new Home Assistant instance."""
|
2016-05-20 08:20:59 +02:00
|
|
|
# Things should be mostly shut down already at this point, now just try
|
|
|
|
# to clean up things that may have been left behind.
|
2019-07-31 21:25:30 +02:00
|
|
|
sys.stderr.write("Home Assistant attempting to restart.\n")
|
2016-05-20 08:20:59 +02:00
|
|
|
|
|
|
|
# Count remaining threads, ideally there should only be one non-daemonized
|
|
|
|
# thread left (which is us). Nothing we really do with it, but it might be
|
|
|
|
# useful when debugging shutdown/restart issues.
|
2016-05-22 06:35:33 +02:00
|
|
|
try:
|
2019-07-31 21:25:30 +02:00
|
|
|
nthreads = sum(
|
|
|
|
thread.is_alive() and not thread.daemon for thread in threading.enumerate()
|
|
|
|
)
|
2016-05-22 06:35:33 +02:00
|
|
|
if nthreads > 1:
|
2019-08-23 18:53:33 +02:00
|
|
|
sys.stderr.write(f"Found {nthreads} non-daemonic threads.\n")
|
2016-05-22 06:35:33 +02:00
|
|
|
|
|
|
|
# Somehow we sometimes seem to trigger an assertion in the python threading
|
|
|
|
# module. It seems we find threads that have no associated OS level thread
|
|
|
|
# which are not marked as stopped at the python level.
|
|
|
|
except AssertionError:
|
|
|
|
sys.stderr.write("Failed to count non-daemonic threads.\n")
|
2016-05-20 08:20:59 +02:00
|
|
|
|
|
|
|
# Try to not leave behind open filedescriptors with the emphasis on try.
|
2016-01-27 04:39:59 +01:00
|
|
|
try:
|
2016-05-20 08:20:59 +02:00
|
|
|
max_fd = os.sysconf("SC_OPEN_MAX")
|
2016-01-27 04:39:59 +01:00
|
|
|
except ValueError:
|
2016-05-20 08:20:59 +02:00
|
|
|
max_fd = 256
|
2016-01-27 04:39:59 +01:00
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
if platform.system() == "Darwin":
|
2016-05-20 08:20:59 +02:00
|
|
|
closefds_osx(3, max_fd)
|
|
|
|
else:
|
|
|
|
os.closerange(3, max_fd)
|
2016-02-06 15:48:36 +01:00
|
|
|
|
2017-06-08 15:53:12 +02:00
|
|
|
# Now launch into a new instance of Home Assistant. If this fails we
|
2016-05-20 08:20:59 +02:00
|
|
|
# fall through and exit with error 100 (RESTART_EXIT_CODE) in which case
|
|
|
|
# systemd will restart us when RestartForceExitStatus=100 is set in the
|
|
|
|
# systemd.service file.
|
2017-06-08 15:53:12 +02:00
|
|
|
sys.stderr.write("Restarting Home Assistant\n")
|
2016-05-20 08:20:59 +02:00
|
|
|
args = cmdline()
|
|
|
|
os.execv(args[0], args)
|
2016-01-27 04:39:59 +01:00
|
|
|
|
|
|
|
|
2016-07-21 07:38:52 +02:00
|
|
|
def main() -> int:
|
2016-03-08 00:06:04 +01:00
|
|
|
"""Start Home Assistant."""
|
2017-04-06 08:23:02 +02:00
|
|
|
validate_python()
|
|
|
|
|
2018-09-22 09:54:37 +02:00
|
|
|
# Run a simple daemon runner process on Windows to handle restarts
|
2019-07-31 21:25:30 +02:00
|
|
|
if os.name == "nt" and "--runner" not in sys.argv:
|
|
|
|
nt_args = cmdline() + ["--runner"]
|
2018-09-22 09:54:37 +02:00
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
subprocess.check_call(nt_args)
|
|
|
|
sys.exit(0)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
sys.exit(0)
|
|
|
|
except subprocess.CalledProcessError as exc:
|
|
|
|
if exc.returncode != RESTART_EXIT_CODE:
|
|
|
|
sys.exit(exc.returncode)
|
2017-04-06 08:23:02 +02:00
|
|
|
|
2015-01-21 07:14:10 +01:00
|
|
|
args = get_arguments()
|
|
|
|
|
2016-07-03 20:38:14 +02:00
|
|
|
if args.script is not None:
|
2020-04-04 17:07:36 +02:00
|
|
|
# pylint: disable=import-outside-toplevel
|
2016-07-03 20:38:14 +02:00
|
|
|
from homeassistant import scripts
|
2019-07-31 21:25:30 +02:00
|
|
|
|
2016-07-03 20:38:14 +02:00
|
|
|
return scripts.run(args.script)
|
|
|
|
|
2020-01-14 22:03:02 +01:00
|
|
|
config_dir = os.path.abspath(os.path.join(os.getcwd(), args.config))
|
2015-08-30 08:02:07 +02:00
|
|
|
ensure_config_path(config_dir)
|
2015-01-09 03:45:27 +01:00
|
|
|
|
2016-03-08 00:06:04 +01:00
|
|
|
# Daemon functions
|
2015-09-01 08:37:52 +02:00
|
|
|
if args.pid_file:
|
|
|
|
check_pid(args.pid_file)
|
2015-09-01 08:12:00 +02:00
|
|
|
if args.daemon:
|
|
|
|
daemonize()
|
2015-09-01 09:29:07 +02:00
|
|
|
if args.pid_file:
|
|
|
|
write_pid(args.pid_file)
|
2015-09-01 08:12:00 +02:00
|
|
|
|
2020-07-07 00:58:53 +02:00
|
|
|
# pylint: disable=import-outside-toplevel
|
|
|
|
from homeassistant import runner
|
|
|
|
|
|
|
|
runtime_conf = runner.RuntimeConfig(
|
|
|
|
config_dir=config_dir,
|
|
|
|
verbose=args.verbose,
|
|
|
|
log_rotate_days=args.log_rotate_days,
|
|
|
|
log_file=args.log_file,
|
|
|
|
log_no_color=args.log_no_color,
|
|
|
|
skip_pip=args.skip_pip,
|
|
|
|
safe_mode=args.safe_mode,
|
|
|
|
debug=args.debug,
|
|
|
|
open_ui=args.open_ui,
|
|
|
|
)
|
|
|
|
|
2021-09-28 09:05:06 +02:00
|
|
|
fault_file_name = os.path.join(config_dir, FAULT_LOG_FILENAME)
|
|
|
|
with open(fault_file_name, mode="a", encoding="utf8") as fault_file:
|
|
|
|
faulthandler.enable(fault_file)
|
|
|
|
exit_code = runner.run(runtime_conf)
|
|
|
|
faulthandler.disable()
|
|
|
|
|
|
|
|
if os.path.getsize(fault_file_name) == 0:
|
|
|
|
os.remove(fault_file_name)
|
|
|
|
|
2016-05-20 08:20:59 +02:00
|
|
|
if exit_code == RESTART_EXIT_CODE and not args.runner:
|
|
|
|
try_to_restart()
|
|
|
|
|
2019-12-16 07:29:19 +01:00
|
|
|
return exit_code
|
2015-08-30 05:31:33 +02:00
|
|
|
|
2014-11-03 02:27:32 +01:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2016-02-06 15:48:36 +01:00
|
|
|
sys.exit(main())
|