2016-03-14 17:30:22 +01:00
|
|
|
import argparse
|
2016-04-07 09:12:09 +02:00
|
|
|
import logging.handlers
|
2016-03-22 15:37:13 +01:00
|
|
|
import os
|
2016-04-02 04:06:06 +02:00
|
|
|
import webbrowser
|
2016-10-27 20:28:56 +02:00
|
|
|
import sys
|
2016-04-14 06:29:40 +02:00
|
|
|
|
2016-09-20 22:58:30 +02:00
|
|
|
from twisted.web import server, guard
|
2016-10-26 01:36:40 +02:00
|
|
|
from twisted.internet import defer, reactor, error
|
2016-09-20 22:58:30 +02:00
|
|
|
from twisted.cred import portal
|
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-09-20 22:58:30 +02:00
|
|
|
from lbrynet.lbrynet_daemon.auth.auth import PasswordChecker, HttpPasswordRealm
|
2016-09-21 09:49:52 +02:00
|
|
|
from lbrynet.lbrynet_daemon.auth.util import initialize_api_key_file
|
2016-10-23 07:17:24 +02:00
|
|
|
from lbrynet import conf
|
2016-07-25 20:04:30 +02:00
|
|
|
from lbrynet.core import log_support
|
2016-10-18 03:00:24 +02:00
|
|
|
from lbrynet.core import utils
|
2016-10-07 20:01:59 +02:00
|
|
|
from lbrynet.lbrynet_daemon.DaemonServer import DaemonServer
|
|
|
|
from lbrynet.lbrynet_daemon.DaemonRequest import DaemonRequest
|
2016-10-27 21:18:25 +02:00
|
|
|
from lbrynet.conf import settings
|
2016-10-19 06:12:44 +02:00
|
|
|
|
2016-04-21 04:02:52 +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():
|
|
|
|
def _disp_shutdown():
|
2016-04-21 04:02:52 +02:00
|
|
|
print "Shutting down lbrynet-daemon from command line"
|
2016-03-14 17:30:22 +01:00
|
|
|
log.info("Shutting down lbrynet-daemon from command line")
|
|
|
|
|
|
|
|
def _disp_not_running():
|
2016-04-21 04:02:52 +02:00
|
|
|
print "Attempt to shut down lbrynet-daemon from command line when daemon isn't running"
|
2016-03-14 17:30:22 +01:00
|
|
|
log.info("Attempt to shut down lbrynet-daemon from command line when daemon isn't running")
|
|
|
|
|
|
|
|
d = defer.Deferred(None)
|
2016-10-19 06:12:44 +02:00
|
|
|
d.addCallback(lambda _: JSONRPCProxy.from_url(settings.API_CONNECTION_STRING).stop())
|
2016-03-14 17:30:22 +01:00
|
|
|
d.addCallbacks(lambda _: _disp_shutdown(), lambda _: _disp_not_running())
|
|
|
|
d.callback(None)
|
|
|
|
|
|
|
|
|
|
|
|
def start():
|
|
|
|
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,
|
2016-11-10 20:26:21 +01:00
|
|
|
default=conf.LBRYUM_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",
|
|
|
|
help='Branch of lbry-web-ui repo to use, defaults to {}'.format(settings.ui_branch),
|
|
|
|
default=settings.ui_branch)
|
|
|
|
parser.add_argument('--no-launch', dest='launchui', action="store_false")
|
|
|
|
parser.add_argument("--http-auth", dest="useauth", action="store_true")
|
|
|
|
parser.add_argument(
|
|
|
|
'--log-to-console', dest='logtoconsole', action='store_true',
|
|
|
|
help=('Set to enable console logging. Set the --verbose flag '
|
|
|
|
' to enable more detailed console logging'))
|
|
|
|
parser.add_argument(
|
|
|
|
'--quiet', dest='quiet', action="store_true",
|
|
|
|
help=('If log-to-console is not set, setting this disables all console output. '
|
|
|
|
'If log-to-console is set, this argument is ignored'))
|
|
|
|
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
|
|
|
args = parser.parse_args()
|
|
|
|
|
2016-10-22 00:26:36 +02:00
|
|
|
utils.setup_certs_for_windows()
|
2016-04-18 02:16:35 +02:00
|
|
|
|
2016-10-31 22:19:19 +01:00
|
|
|
conf.update_settings_from_file()
|
|
|
|
update_settings_from_args(args)
|
|
|
|
|
|
|
|
lbrynet_log = settings.get_log_filename()
|
|
|
|
log_support.configure_logging(lbrynet_log, args.logtoconsole, args.verbose)
|
2016-11-16 20:38:38 +01:00
|
|
|
log.debug('Final Settings: %s', settings.get_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')
|
2016-10-19 06:12:44 +02:00
|
|
|
JSONRPCProxy.from_url(settings.API_CONNECTION_STRING).is_running()
|
2016-03-29 22:42:47 +02:00
|
|
|
log.info("lbrynet-daemon is already running")
|
2016-04-21 04:02:52 +02:00
|
|
|
if not args.logtoconsole:
|
|
|
|
print "lbrynet-daemon is already running"
|
2016-03-29 22:42:47 +02:00
|
|
|
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
|
|
|
pass
|
|
|
|
|
2016-03-14 17:30:22 +01:00
|
|
|
log.info("Starting lbrynet-daemon from command line")
|
|
|
|
|
2016-04-18 05:23:20 +02:00
|
|
|
if not args.logtoconsole and not args.quiet:
|
|
|
|
print "Starting lbrynet-daemon from command line"
|
2016-06-07 10:30:22 +02:00
|
|
|
print "To view activity, view the log file here: " + lbrynet_log
|
2016-10-26 09:16:33 +02:00
|
|
|
print "Web UI is available at http://%s:%i" % (settings.API_INTERFACE, settings.api_port)
|
2016-10-19 06:12:44 +02:00
|
|
|
print "JSONRPC API is available at " + settings.API_CONNECTION_STRING
|
2016-04-18 05:23:20 +02:00
|
|
|
print "To quit press ctrl-c or call 'stop' via the API"
|
2016-04-02 04:06:06 +02:00
|
|
|
|
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()
|
|
|
|
analytics_manager.send_server_startup()
|
|
|
|
start_server_and_listen(args.launchui, args.useauth, analytics_manager)
|
2016-04-14 06:29:40 +02:00
|
|
|
reactor.run()
|
2016-04-21 04:02:52 +02:00
|
|
|
|
2016-04-18 05:23:20 +02:00
|
|
|
if not args.logtoconsole and not args.quiet:
|
|
|
|
print "\nClosing lbrynet-daemon"
|
2016-04-14 06:29:40 +02:00
|
|
|
else:
|
|
|
|
log.info("Not connected to internet, unable to start")
|
2016-04-21 04:02:52 +02:00
|
|
|
if not args.logtoconsole:
|
|
|
|
print "Not connected to internet, unable to start"
|
2016-04-14 06:29:40 +02:00
|
|
|
return
|
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):
|
|
|
|
to_pass = {}
|
|
|
|
if args.ui:
|
|
|
|
to_pass['local_ui_path'] = args.ui
|
|
|
|
if args.branch:
|
|
|
|
to_pass['ui_branch'] = args.branch
|
|
|
|
to_pass['use_auth_http'] = args.useauth
|
2016-11-05 19:10:26 +01:00
|
|
|
to_pass['wallet'] = args.wallet
|
2016-10-31 22:19:19 +01:00
|
|
|
settings.update(to_pass)
|
|
|
|
|
|
|
|
|
2016-11-10 21:49:51 +01:00
|
|
|
def log_and_kill(failure, analytics_manager):
|
|
|
|
analytics_manager.send_server_startup_error(failure.getErrorMessage() + " " + str(failure))
|
2016-10-25 23:48:15 +02:00
|
|
|
log_support.failure(failure, log, 'Failed to startup: %s')
|
2016-11-02 13:23:37 +01:00
|
|
|
reactor.callFromThread(reactor.stop)
|
2016-10-25 23:48:15 +02:00
|
|
|
|
|
|
|
|
2016-11-10 21:49:51 +01:00
|
|
|
def start_server_and_listen(launchui, use_auth, analytics_manager):
|
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
|
|
|
kwargs: passed along to `DaemonServer().start()`
|
|
|
|
"""
|
2016-11-10 21:49:51 +01:00
|
|
|
daemon_server = DaemonServer(analytics_manager)
|
|
|
|
d = daemon_server.start()
|
|
|
|
d.addCallback(lambda _: listen(daemon_server, use_auth))
|
2016-10-26 01:36:40 +02:00
|
|
|
if launchui:
|
|
|
|
d.addCallback(lambda _: webbrowser.open(settings.UI_ADDRESS))
|
2016-11-10 21:49:51 +01:00
|
|
|
d.addCallback(lambda _: analytics_manager.send_server_startup_success())
|
|
|
|
d.addErrback(log_and_kill, analytics_manager)
|
2016-10-26 01:36:40 +02:00
|
|
|
|
2016-11-02 13:23:37 +01:00
|
|
|
|
2016-11-10 21:49:51 +01:00
|
|
|
def listen(daemon_server, use_auth):
|
|
|
|
site_base = get_site_base(use_auth, daemon_server.root)
|
2016-10-26 01:36:40 +02:00
|
|
|
lbrynet_server = server.Site(site_base)
|
|
|
|
lbrynet_server.requestFactory = DaemonRequest
|
|
|
|
try:
|
2016-10-28 23:57:25 +02:00
|
|
|
reactor.listenTCP(settings.api_port, lbrynet_server, interface=settings.API_INTERFACE)
|
2016-10-26 01:36:40 +02:00
|
|
|
except error.CannotListenError:
|
|
|
|
log.info('Daemon already running, exiting app')
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
def get_site_base(use_auth, root):
|
|
|
|
if use_auth:
|
|
|
|
log.info("Using authenticated API")
|
|
|
|
return create_auth_session(root)
|
|
|
|
else:
|
|
|
|
log.info("Using non-authenticated API")
|
|
|
|
return server.Site(root)
|
|
|
|
|
|
|
|
|
|
|
|
def create_auth_session(root):
|
|
|
|
pw_path = os.path.join(settings.data_dir, ".api_keys")
|
|
|
|
initialize_api_key_file(pw_path)
|
|
|
|
checker = PasswordChecker.load_file(pw_path)
|
|
|
|
realm = HttpPasswordRealm(root)
|
|
|
|
portal_to_realm = portal.Portal(realm, [checker, ])
|
|
|
|
factory = guard.BasicCredentialFactory('Login to lbrynet api')
|
|
|
|
_lbrynet_server = guard.HTTPAuthSessionWrapper(portal_to_realm, [factory, ])
|
|
|
|
return _lbrynet_server
|
|
|
|
|
|
|
|
|
2016-05-02 10:10:50 +02:00
|
|
|
if __name__ == "__main__":
|
2016-07-16 08:15:58 +02:00
|
|
|
start()
|