From a7a5f88142f0d7c085dc98d88fc3f04c914bcde2 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Fri, 20 Sep 2019 11:25:12 -0500 Subject: [PATCH] added restart command --- onionr.sh | 1 - onionr/etc/cleanup/__init__.py | 14 ++++---- onionr/etc/onionrvalues.py | 9 ++++- onionr/filepaths/__init__.py | 3 ++ onionr/onionrcommands/parser/arguments.py | 2 ++ onionr/onionrcommands/restartonionr.py | 40 +++++++++++++++++++++++ start-daemon.sh | 1 + tests/test_filepaths.py | 2 ++ 8 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 onionr/onionrcommands/restartonionr.py diff --git a/onionr.sh b/onionr.sh index d7114740..783d1f05 100755 --- a/onionr.sh +++ b/onionr.sh @@ -1,6 +1,5 @@ #!/bin/sh ORIG_ONIONR_RUN_DIR=`pwd` -export ORIG_ONIONR_RUN_DIR cd "$(dirname "$0")" cd onionr/ ./__init__.py "$@" diff --git a/onionr/etc/cleanup/__init__.py b/onionr/etc/cleanup/__init__.py index 7f72962a..56b3e402 100644 --- a/onionr/etc/cleanup/__init__.py +++ b/onionr/etc/cleanup/__init__.py @@ -1,10 +1,12 @@ import os, filepaths -def delete_run_files(): + +def _safe_remove(path): try: - os.remove(filepaths.public_API_host_file) + os.remove(path) except FileNotFoundError: pass - try: - os.remove(filepaths.private_API_host_file) - except FileNotFoundError: - pass \ No newline at end of file + +def delete_run_files(): + _safe_remove(filepaths.public_API_host_file) + _safe_remove(filepaths.private_API_host_file) + _safe_remove(filepaths.daemon_mark_file) diff --git a/onionr/etc/onionrvalues.py b/onionr/etc/onionrvalues.py index 642fe4c7..e669cdfb 100755 --- a/onionr/etc/onionrvalues.py +++ b/onionr/etc/onionrvalues.py @@ -18,6 +18,10 @@ along with this program. If not, see . ''' import platform +import os + +import filepaths + DENIABLE_PEER_ADDRESS = "OVPCZLOXD6DC5JHX4EQ3PSOGAZ3T24F75HQLIUZSDSMYPEOXCPFA====" PASSWORD_LENGTH = 25 ONIONR_TAGLINE = 'Private P2P Communication - GPLv3 - https://Onionr.net' @@ -43,4 +47,7 @@ platform = platform.system() if platform == 'Windows': SCRIPT_NAME = 'run-windows.bat' else: - SCRIPT_NAME = 'onionr.sh' + if os.path.exists(filepaths.daemon_mark_file): + SCRIPT_NAME = 'start-daemon.sh' + else: + SCRIPT_NAME = 'onionr.sh' diff --git a/onionr/filepaths/__init__.py b/onionr/filepaths/__init__.py index ec58b4a0..ae35889c 100644 --- a/onionr/filepaths/__init__.py +++ b/onionr/filepaths/__init__.py @@ -1,7 +1,9 @@ from utils import identifyhome +import os home = identifyhome.identify_home() if not home.endswith('/'): home += '/' +app_root = os.path.dirname(os.path.realpath(__file__)) + '/../../' usage_file = home + 'disk-usage.txt' block_data_location = home + 'blocks/' contacts_location = home + 'contacts/' @@ -15,6 +17,7 @@ announce_cache = home + 'announcecache.dat' export_location = home + 'block-export/' upload_list = home + 'upload-list.json' config_file = home + 'config.json' +daemon_mark_file = app_root + '/daemon-true.txt' tor_hs_address_file = home + 'hs/hostname' diff --git a/onionr/onionrcommands/parser/arguments.py b/onionr/onionrcommands/parser/arguments.py index 1fd2484d..bce3f40a 100644 --- a/onionr/onionrcommands/parser/arguments.py +++ b/onionr/onionrcommands/parser/arguments.py @@ -26,6 +26,7 @@ from .. import pubkeymanager # commands to add or change id from .. import resettor # commands to reset the tor data directory or transport keypair from .. import resetplugins # command to reinstall default plugins from .. import softreset # command to delete onionr blocks +from .. import restartonionr # command to restart Onionr import onionrexceptions from onionrutils import importnewblocks # func to import new blocks import onionrevents as events @@ -38,6 +39,7 @@ def get_arguments()->dict: ('version',): version.version, ('start', 'daemon'): daemonlaunch.start, ('stop', 'kill'): daemonlaunch.kill_daemon, + ('restart',): restartonionr.restart, ('add-address', 'addaddress', 'addadder'): keyadders.add_address, ('openhome', 'gui', 'openweb', 'open-home', 'open-web'): openwebinterface.open_home, ('add-site', 'addsite', 'addhtml', 'add-html'): filecommands.add_html, diff --git a/onionr/onionrcommands/restartonionr.py b/onionr/onionrcommands/restartonionr.py new file mode 100644 index 00000000..996ab406 --- /dev/null +++ b/onionr/onionrcommands/restartonionr.py @@ -0,0 +1,40 @@ +""" + Onionr - Private P2P Communication + + Command to restart Onionr +""" +""" + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +""" +import time +import os +import subprocess +import platform + +from etc import onionrvalues +from onionrutils import localcommand +import logger + +from . import daemonlaunch + +SCRIPT_NAME = os.path.dirname(os.path.realpath(__file__)) + f'/../../{onionrvalues.SCRIPT_NAME}' + +def restart(): + logger.info('Restarting Onionr', terminal=True) + daemonlaunch.kill_daemon() + while localcommand.local_command('ping', maxWait=8) == 'pong!': + time.sleep(0.3) + subprocess.Popen([SCRIPT_NAME, 'start']) + +restart.onionr_help = 'Gracefully restart Onionr' diff --git a/start-daemon.sh b/start-daemon.sh index d427d8e8..c5128b7f 100755 --- a/start-daemon.sh +++ b/start-daemon.sh @@ -2,4 +2,5 @@ cd "$(dirname "$0")" echo "starting Onionr daemon..." echo "run onionr.sh stop to stop the daemon" +touch daemon-true.txt exec nohup ./onionr.sh start > /dev/null 2>&1 & disown diff --git a/tests/test_filepaths.py b/tests/test_filepaths.py index 45cbbd51..13890174 100644 --- a/tests/test_filepaths.py +++ b/tests/test_filepaths.py @@ -12,5 +12,7 @@ class TestFilePaths(unittest.TestCase): def test_filepaths_main(self): home = identifyhome.identify_home() self.assertTrue(filepaths.home.startswith(home)) + def test_app_root_path(self): + self.assertTrue(os.path.exists(filepaths.app_root + '/onionr.sh')) unittest.main()