Make scripts available via CLI (#2426)
* Rename sqlalchemy migrate script * Add script support to CLI
This commit is contained in:
parent
a2e45b8fdd
commit
3447fdc76f
4 changed files with 105 additions and 79 deletions
|
@ -7,7 +7,6 @@ import platform
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
import threading
|
import threading
|
||||||
import time
|
|
||||||
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
__version__,
|
__version__,
|
||||||
|
@ -110,22 +109,14 @@ def get_arguments():
|
||||||
type=int,
|
type=int,
|
||||||
default=None,
|
default=None,
|
||||||
help='Enables daily log rotation and keeps up to the specified days')
|
help='Enables daily log rotation and keeps up to the specified days')
|
||||||
parser.add_argument(
|
|
||||||
'--install-osx',
|
|
||||||
action='store_true',
|
|
||||||
help='Installs as a service on OS X and loads on boot.')
|
|
||||||
parser.add_argument(
|
|
||||||
'--uninstall-osx',
|
|
||||||
action='store_true',
|
|
||||||
help='Uninstalls from OS X.')
|
|
||||||
parser.add_argument(
|
|
||||||
'--restart-osx',
|
|
||||||
action='store_true',
|
|
||||||
help='Restarts on OS X.')
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--runner',
|
'--runner',
|
||||||
action='store_true',
|
action='store_true',
|
||||||
help='On restart exit with code {}'.format(RESTART_EXIT_CODE))
|
help='On restart exit with code {}'.format(RESTART_EXIT_CODE))
|
||||||
|
parser.add_argument(
|
||||||
|
'--script',
|
||||||
|
nargs=argparse.REMAINDER,
|
||||||
|
help='Run one of the embedded scripts')
|
||||||
if os.name == "posix":
|
if os.name == "posix":
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--daemon',
|
'--daemon',
|
||||||
|
@ -196,46 +187,6 @@ def write_pid(pid_file):
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
def install_osx():
|
|
||||||
"""Setup to run via launchd on OS X."""
|
|
||||||
with os.popen('which hass') as inp:
|
|
||||||
hass_path = inp.read().strip()
|
|
||||||
|
|
||||||
with os.popen('whoami') as inp:
|
|
||||||
user = inp.read().strip()
|
|
||||||
|
|
||||||
cwd = os.path.dirname(__file__)
|
|
||||||
template_path = os.path.join(cwd, 'startup', 'launchd.plist')
|
|
||||||
|
|
||||||
with open(template_path, 'r', encoding='utf-8') as inp:
|
|
||||||
plist = inp.read()
|
|
||||||
|
|
||||||
plist = plist.replace("$HASS_PATH$", hass_path)
|
|
||||||
plist = plist.replace("$USER$", user)
|
|
||||||
|
|
||||||
path = os.path.expanduser("~/Library/LaunchAgents/org.homeassistant.plist")
|
|
||||||
|
|
||||||
try:
|
|
||||||
with open(path, 'w', encoding='utf-8') as outp:
|
|
||||||
outp.write(plist)
|
|
||||||
except IOError as err:
|
|
||||||
print('Unable to write to ' + path, err)
|
|
||||||
return
|
|
||||||
|
|
||||||
os.popen('launchctl load -w -F ' + path)
|
|
||||||
|
|
||||||
print("Home Assistant has been installed. \
|
|
||||||
Open it here: http://localhost:8123")
|
|
||||||
|
|
||||||
|
|
||||||
def uninstall_osx():
|
|
||||||
"""Unload from launchd on OS X."""
|
|
||||||
path = os.path.expanduser("~/Library/LaunchAgents/org.homeassistant.plist")
|
|
||||||
os.popen('launchctl unload ' + path)
|
|
||||||
|
|
||||||
print("Home Assistant has been uninstalled.")
|
|
||||||
|
|
||||||
|
|
||||||
def closefds_osx(min_fd, max_fd):
|
def closefds_osx(min_fd, max_fd):
|
||||||
"""Make sure file descriptors get closed when we restart.
|
"""Make sure file descriptors get closed when we restart.
|
||||||
|
|
||||||
|
@ -358,23 +309,13 @@ def main():
|
||||||
|
|
||||||
args = get_arguments()
|
args = get_arguments()
|
||||||
|
|
||||||
|
if args.script is not None:
|
||||||
|
from homeassistant import scripts
|
||||||
|
return scripts.run(args.script)
|
||||||
|
|
||||||
config_dir = os.path.join(os.getcwd(), args.config)
|
config_dir = os.path.join(os.getcwd(), args.config)
|
||||||
ensure_config_path(config_dir)
|
ensure_config_path(config_dir)
|
||||||
|
|
||||||
# OS X launchd functions
|
|
||||||
if args.install_osx:
|
|
||||||
install_osx()
|
|
||||||
return 0
|
|
||||||
if args.uninstall_osx:
|
|
||||||
uninstall_osx()
|
|
||||||
return 0
|
|
||||||
if args.restart_osx:
|
|
||||||
uninstall_osx()
|
|
||||||
# A small delay is needed on some systems to let the unload finish.
|
|
||||||
time.sleep(0.5)
|
|
||||||
install_osx()
|
|
||||||
return 0
|
|
||||||
|
|
||||||
# Daemon functions
|
# Daemon functions
|
||||||
if args.pid_file:
|
if args.pid_file:
|
||||||
check_pid(args.pid_file)
|
check_pid(args.pid_file)
|
||||||
|
|
22
homeassistant/scripts/__init__.py
Normal file
22
homeassistant/scripts/__init__.py
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
"""Home Assistant command line scripts."""
|
||||||
|
import importlib
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
def run(args):
|
||||||
|
"""Run a script."""
|
||||||
|
scripts = [fil[:-3] for fil in os.listdir(os.path.dirname(__file__))
|
||||||
|
if fil.endswith('.py') and fil != '__init__.py']
|
||||||
|
|
||||||
|
if not args:
|
||||||
|
print('Please specify a script to run.')
|
||||||
|
print('Available scripts:', ', '.join(scripts))
|
||||||
|
return 1
|
||||||
|
|
||||||
|
if args[0] not in scripts:
|
||||||
|
print('Invalid script specified.')
|
||||||
|
print('Available scripts:', ', '.join(scripts))
|
||||||
|
return 1
|
||||||
|
|
||||||
|
script = importlib.import_module('homeassistant.scripts.' + args[0])
|
||||||
|
return script.run(args[1:])
|
23
homeassistant/scripts/convert_db_to_sqlalchemy.py → homeassistant/scripts/db_migrator.py
Executable file → Normal file
23
homeassistant/scripts/convert_db_to_sqlalchemy.py → homeassistant/scripts/db_migrator.py
Executable file → Normal file
|
@ -1,5 +1,3 @@
|
||||||
#!/usr/bin/env python
|
|
||||||
|
|
||||||
"""Script to convert an old-format home-assistant.db to a new format one."""
|
"""Script to convert an old-format home-assistant.db to a new format one."""
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
|
@ -46,11 +44,11 @@ def print_progress(iteration, total, prefix='', suffix='', decimals=2,
|
||||||
print("\n")
|
print("\n")
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def run(args):
|
||||||
"""The actual script body."""
|
"""The actual script body."""
|
||||||
# pylint: disable=too-many-locals,invalid-name,too-many-statements
|
# pylint: disable=too-many-locals,invalid-name,too-many-statements
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
description="Home Assistant: Observe, Control, Automate.")
|
description="Migrate legacy DB to SQLAlchemy format.")
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'-c', '--config',
|
'-c', '--config',
|
||||||
metavar='path_to_config_dir',
|
metavar='path_to_config_dir',
|
||||||
|
@ -66,6 +64,9 @@ def main():
|
||||||
type=str,
|
type=str,
|
||||||
help="Connect to URI and import (implies --append)"
|
help="Connect to URI and import (implies --append)"
|
||||||
"eg: mysql://localhost/homeassistant")
|
"eg: mysql://localhost/homeassistant")
|
||||||
|
parser.add_argument(
|
||||||
|
'--script',
|
||||||
|
choices=['db_migrator'])
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
@ -76,7 +77,7 @@ def main():
|
||||||
if config_dir != config_util.get_default_config_dir():
|
if config_dir != config_util.get_default_config_dir():
|
||||||
print(('Fatal Error: Specified configuration directory does '
|
print(('Fatal Error: Specified configuration directory does '
|
||||||
'not exist {} ').format(config_dir))
|
'not exist {} ').format(config_dir))
|
||||||
sys.exit(1)
|
return 1
|
||||||
else:
|
else:
|
||||||
config_dir = config_util.get_default_config_dir()
|
config_dir = config_util.get_default_config_dir()
|
||||||
|
|
||||||
|
@ -86,13 +87,13 @@ def main():
|
||||||
if not os.path.exists(src_db):
|
if not os.path.exists(src_db):
|
||||||
print("Fatal Error: Old format database '{}' does not exist".format(
|
print("Fatal Error: Old format database '{}' does not exist".format(
|
||||||
src_db))
|
src_db))
|
||||||
sys.exit(1)
|
return 1
|
||||||
if not args.uri and (os.path.exists(dst_db) and not args.append):
|
if not args.uri and (os.path.exists(dst_db) and not args.append):
|
||||||
print("Fatal Error: New format database '{}' exists already - "
|
print("Fatal Error: New format database '{}' exists already - "
|
||||||
"Remove it or use --append".format(dst_db))
|
"Remove it or use --append".format(dst_db))
|
||||||
print("Note: --append must maintain an ID mapping and is much slower")
|
print("Note: --append must maintain an ID mapping and is much slower"
|
||||||
print("and requires sufficient memory to track all event IDs")
|
"and requires sufficient memory to track all event IDs")
|
||||||
sys.exit(1)
|
return 1
|
||||||
|
|
||||||
conn = sqlite3.connect(src_db)
|
conn = sqlite3.connect(src_db)
|
||||||
uri = args.uri or "sqlite:///{}".format(dst_db)
|
uri = args.uri or "sqlite:///{}".format(dst_db)
|
||||||
|
@ -182,6 +183,4 @@ def main():
|
||||||
print_progress(n, num_rows)
|
print_progress(n, num_rows)
|
||||||
session.commit()
|
session.commit()
|
||||||
c.close()
|
c.close()
|
||||||
|
return 0
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
64
homeassistant/scripts/macos.py
Normal file
64
homeassistant/scripts/macos.py
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
"""Script to install/uninstall HA into OS X."""
|
||||||
|
import os
|
||||||
|
import time
|
||||||
|
|
||||||
|
|
||||||
|
def install_osx():
|
||||||
|
"""Setup to run via launchd on OS X."""
|
||||||
|
with os.popen('which hass') as inp:
|
||||||
|
hass_path = inp.read().strip()
|
||||||
|
|
||||||
|
with os.popen('whoami') as inp:
|
||||||
|
user = inp.read().strip()
|
||||||
|
|
||||||
|
cwd = os.path.dirname(__file__)
|
||||||
|
template_path = os.path.join(cwd, 'startup', 'launchd.plist')
|
||||||
|
|
||||||
|
with open(template_path, 'r', encoding='utf-8') as inp:
|
||||||
|
plist = inp.read()
|
||||||
|
|
||||||
|
plist = plist.replace("$HASS_PATH$", hass_path)
|
||||||
|
plist = plist.replace("$USER$", user)
|
||||||
|
|
||||||
|
path = os.path.expanduser("~/Library/LaunchAgents/org.homeassistant.plist")
|
||||||
|
|
||||||
|
try:
|
||||||
|
with open(path, 'w', encoding='utf-8') as outp:
|
||||||
|
outp.write(plist)
|
||||||
|
except IOError as err:
|
||||||
|
print('Unable to write to ' + path, err)
|
||||||
|
return
|
||||||
|
|
||||||
|
os.popen('launchctl load -w -F ' + path)
|
||||||
|
|
||||||
|
print("Home Assistant has been installed. \
|
||||||
|
Open it here: http://localhost:8123")
|
||||||
|
|
||||||
|
|
||||||
|
def uninstall_osx():
|
||||||
|
"""Unload from launchd on OS X."""
|
||||||
|
path = os.path.expanduser("~/Library/LaunchAgents/org.homeassistant.plist")
|
||||||
|
os.popen('launchctl unload ' + path)
|
||||||
|
|
||||||
|
print("Home Assistant has been uninstalled.")
|
||||||
|
|
||||||
|
|
||||||
|
def run(args):
|
||||||
|
"""Handle OSX commandline script."""
|
||||||
|
commands = 'install', 'uninstall', 'restart'
|
||||||
|
if not args or args[0] not in commands:
|
||||||
|
print('Invalid command. Available commands:', ', '.join(commands))
|
||||||
|
return 1
|
||||||
|
|
||||||
|
if args[0] == 'install':
|
||||||
|
install_osx()
|
||||||
|
return 0
|
||||||
|
elif args[0] == 'uninstall':
|
||||||
|
uninstall_osx()
|
||||||
|
return 0
|
||||||
|
elif args[0] == 'restart':
|
||||||
|
uninstall_osx()
|
||||||
|
# A small delay is needed on some systems to let the unload finish.
|
||||||
|
time.sleep(0.5)
|
||||||
|
install_osx()
|
||||||
|
return 0
|
Loading…
Add table
Add a link
Reference in a new issue