2016-12-21 19:54:09 +01:00
|
|
|
from lbrynet.core import log_support
|
|
|
|
|
2016-03-14 17:30:22 +01:00
|
|
|
import argparse
|
2016-04-07 09:12:09 +02:00
|
|
|
import logging.handlers
|
2016-04-14 06:29:40 +02:00
|
|
|
|
2016-12-06 22:08:15 +01:00
|
|
|
from twisted.internet import defer, reactor
|
2016-03-14 17:30:22 +01:00
|
|
|
from jsonrpc.proxy import JSONRPCProxy
|
|
|
|
|
2016-11-10 21:49:51 +01:00
|
|
|
from lbrynet import analytics
|
2016-10-23 07:17:24 +02:00
|
|
|
from lbrynet import conf
|
2016-10-18 03:00:24 +02:00
|
|
|
from lbrynet.core import utils
|
2017-01-09 20:03:25 +01:00
|
|
|
from lbrynet.lbrynet_daemon.auth.client import LBRYAPIClient
|
2016-10-07 20:01:59 +02:00
|
|
|
from lbrynet.lbrynet_daemon.DaemonServer import DaemonServer
|
2016-10-19 06:12:44 +02:00
|
|
|
|
2016-06-07 10:19:51 +02:00
|
|
|
log = logging.getLogger(__name__)
|
2016-07-25 20:04:30 +02:00
|
|
|
|
2016-04-21 04:02:52 +02:00
|
|
|
|
2016-04-14 06:29:40 +02:00
|
|
|
def test_internet_connection():
|
2016-10-18 03:00:24 +02:00
|
|
|
return utils.check_connection()
|
2016-04-14 06:29:40 +02:00
|
|
|
|
2016-03-14 17:30:22 +01:00
|
|
|
|
|
|
|
def stop():
|
2017-01-02 19:06:57 +01:00
|
|
|
conf.initialize_settings()
|
|
|
|
log_support.configure_console()
|
|
|
|
try:
|
2017-01-09 20:03:25 +01:00
|
|
|
LBRYAPIClient.get_client().call('stop')
|
2017-01-02 19:06:57 +01:00
|
|
|
except Exception:
|
|
|
|
log.exception('Failed to stop deamon')
|
|
|
|
else:
|
2016-03-14 17:30:22 +01:00
|
|
|
log.info("Shutting down lbrynet-daemon from command line")
|
|
|
|
|
|
|
|
|
|
|
|
def start():
|
2016-12-21 20:55:43 +01:00
|
|
|
conf.initialize_settings()
|
2017-01-17 04:23:20 +01:00
|
|
|
|
2016-03-14 17:30:22 +01:00
|
|
|
parser = argparse.ArgumentParser(description="Launch lbrynet-daemon")
|
|
|
|
parser.add_argument("--wallet",
|
2016-12-01 17:59:36 +01:00
|
|
|
help="lbryum or ptc for testing, default lbryum",
|
2016-03-14 17:30:22 +01:00
|
|
|
type=str,
|
2017-01-17 04:23:20 +01:00
|
|
|
default=conf.settings['wallet'])
|
2016-10-22 00:26:36 +02:00
|
|
|
parser.add_argument("--ui", help="path to custom UI folder", default=None)
|
|
|
|
parser.add_argument(
|
|
|
|
"--branch",
|
2017-01-17 04:23:20 +01:00
|
|
|
help='Branch of lbry-web-ui repo to use, defaults to {}'.format(conf.settings['ui_branch']),
|
|
|
|
default=conf.settings['ui_branch'])
|
2017-01-30 21:04:07 +01:00
|
|
|
parser.add_argument('--launch-ui', dest='launchui', action="store_true")
|
2017-01-17 04:23:20 +01:00
|
|
|
parser.add_argument("--http-auth", dest="useauth", action="store_true",
|
|
|
|
default=conf.settings['use_auth_http'])
|
2016-10-22 00:26:36 +02:00
|
|
|
parser.add_argument(
|
|
|
|
'--quiet', dest='quiet', action="store_true",
|
2017-01-30 21:08:32 +01:00
|
|
|
help='Disable all console output.')
|
2016-10-22 00:26:36 +02:00
|
|
|
parser.add_argument(
|
|
|
|
'--verbose', nargs="*",
|
|
|
|
help=('Enable debug output. Optionally specify loggers for which debug output '
|
|
|
|
'should selectively be applied.'))
|
2016-04-18 05:23:20 +02:00
|
|
|
|
2017-01-17 04:23:20 +01:00
|
|
|
args = parser.parse_args()
|
2016-10-31 22:19:19 +01:00
|
|
|
update_settings_from_args(args)
|
|
|
|
|
2016-12-21 20:55:43 +01:00
|
|
|
lbrynet_log = conf.settings.get_log_filename()
|
2017-01-30 21:08:32 +01:00
|
|
|
log_support.configure_logging(lbrynet_log, not args.quiet, args.verbose)
|
2017-01-17 04:23:20 +01:00
|
|
|
log.debug('Final Settings: %s', conf.settings.get_current_settings_dict())
|
2016-10-19 06:12:44 +02:00
|
|
|
|
2016-03-29 22:42:47 +02:00
|
|
|
try:
|
2016-11-11 17:10:00 +01:00
|
|
|
log.debug('Checking for an existing lbrynet daemon instance')
|
2017-01-17 04:23:20 +01:00
|
|
|
JSONRPCProxy.from_url(conf.settings.get_api_connection_string()).is_running()
|
2016-03-29 22:42:47 +02:00
|
|
|
log.info("lbrynet-daemon is already running")
|
|
|
|
return
|
2016-11-11 17:10:00 +01:00
|
|
|
except Exception:
|
|
|
|
log.debug('No lbrynet instance found, continuing to start')
|
2016-03-29 22:42:47 +02:00
|
|
|
|
2016-03-14 17:30:22 +01:00
|
|
|
log.info("Starting lbrynet-daemon from command line")
|
|
|
|
|
2016-04-21 04:02:52 +02:00
|
|
|
if test_internet_connection():
|
2016-11-10 21:49:51 +01:00
|
|
|
analytics_manager = analytics.Manager.new_instance()
|
|
|
|
start_server_and_listen(args.launchui, args.useauth, analytics_manager)
|
2016-04-14 06:29:40 +02:00
|
|
|
reactor.run()
|
|
|
|
else:
|
|
|
|
log.info("Not connected to internet, unable to start")
|
2016-05-02 10:10:50 +02:00
|
|
|
|
2016-10-25 23:48:15 +02:00
|
|
|
|
2016-10-31 22:19:19 +01:00
|
|
|
def update_settings_from_args(args):
|
2017-01-17 04:23:20 +01:00
|
|
|
cli_settings = {}
|
2016-10-31 22:19:19 +01:00
|
|
|
if args.ui:
|
2017-01-17 04:23:20 +01:00
|
|
|
cli_settings['local_ui_path'] = args.ui
|
2016-10-31 22:19:19 +01:00
|
|
|
if args.branch:
|
2017-01-17 04:23:20 +01:00
|
|
|
cli_settings['ui_branch'] = args.branch
|
|
|
|
cli_settings['use_auth_http'] = args.useauth
|
|
|
|
cli_settings['wallet'] = args.wallet
|
2017-01-17 18:29:09 +01:00
|
|
|
conf.settings.update(cli_settings, data_types=(conf.TYPE_CLI,))
|
2016-10-31 22:19:19 +01:00
|
|
|
|
|
|
|
|
2016-12-28 18:20:04 +01:00
|
|
|
@defer.inlineCallbacks
|
2017-01-26 17:01:03 +01:00
|
|
|
def start_server_and_listen(launchui, use_auth, analytics_manager, max_tries=5):
|
2016-10-26 01:36:40 +02:00
|
|
|
"""The primary entry point for launching the daemon.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
launchui: set to true to open a browser window
|
|
|
|
use_auth: set to true to enable http authentication
|
2016-11-10 21:49:51 +01:00
|
|
|
analytics_manager: to send analytics
|
2016-10-26 01:36:40 +02:00
|
|
|
"""
|
2016-12-21 21:59:52 +01:00
|
|
|
analytics_manager.send_server_startup()
|
2016-12-30 21:31:43 +01:00
|
|
|
log_support.configure_analytics_handler(analytics_manager)
|
2017-01-26 17:01:03 +01:00
|
|
|
tries = 1
|
|
|
|
while tries < max_tries:
|
|
|
|
log.info('Making attempt %s / %s to startup', tries, max_tries)
|
|
|
|
try:
|
|
|
|
daemon_server = DaemonServer(analytics_manager)
|
|
|
|
yield daemon_server.start(use_auth, launchui)
|
|
|
|
analytics_manager.send_server_startup_success()
|
|
|
|
break
|
|
|
|
except Exception as e:
|
|
|
|
log.exception('Failed to startup')
|
|
|
|
analytics_manager.send_server_startup_error(str(e))
|
|
|
|
tries += 1
|
|
|
|
else:
|
2016-12-28 18:20:04 +01:00
|
|
|
reactor.callFromThread(reactor.stop)
|
2016-10-26 01:36:40 +02:00
|
|
|
|
2016-11-02 13:23:37 +01:00
|
|
|
|
2016-05-02 10:10:50 +02:00
|
|
|
if __name__ == "__main__":
|
2016-07-16 08:15:58 +02:00
|
|
|
start()
|