lbry-sdk/lbrynet/lbrynet_daemon/LBRYDaemonControl.py

128 lines
4.4 KiB
Python
Raw Normal View History

import argparse
import logging
import logging.handlers
import os
import webbrowser
import sys
import socket
2016-06-07 10:19:51 +02:00
from appdirs import user_data_dir
from twisted.web import server
from twisted.internet import reactor, defer
from jsonrpc.proxy import JSONRPCProxy
from lbrynet.core import log_support
from lbrynet.lbrynet_daemon.LBRYDaemonServer import LBRYDaemonServer, LBRYDaemonRequest
from lbrynet.conf import API_CONNECTION_STRING, API_INTERFACE, API_PORT, \
UI_ADDRESS, DEFAULT_UI_BRANCH, LOG_FILE_NAME
# TODO: stop it!
2016-06-07 10:19:51 +02:00
if sys.platform != "darwin":
log_dir = os.path.join(os.path.expanduser("~"), ".lbrynet")
else:
log_dir = user_data_dir("LBRY")
2016-06-07 10:19:51 +02:00
if not os.path.isdir(log_dir):
os.mkdir(log_dir)
2016-06-07 10:30:22 +02:00
lbrynet_log = os.path.join(log_dir, LOG_FILE_NAME)
2016-06-07 10:19:51 +02:00
log = logging.getLogger(__name__)
REMOTE_SERVER = "www.google.com"
def test_internet_connection():
try:
host = socket.gethostbyname(REMOTE_SERVER)
s = socket.create_connection((host, 80), 2)
return True
except:
return False
def stop():
def _disp_shutdown():
print "Shutting down lbrynet-daemon from command line"
log.info("Shutting down lbrynet-daemon from command line")
def _disp_not_running():
print "Attempt to shut down lbrynet-daemon from command line when daemon isn't running"
log.info("Attempt to shut down lbrynet-daemon from command line when daemon isn't running")
d = defer.Deferred(None)
d.addCallback(lambda _: JSONRPCProxy.from_url(API_CONNECTION_STRING).stop())
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",
help="lbrycrd or lbryum, default lbryum",
type=str,
2016-06-02 02:52:15 +02:00
default='')
parser.add_argument("--ui",
help="path to custom UI folder",
default=None)
parser.add_argument("--branch",
2016-05-30 21:49:25 +02:00
help="Branch of lbry-web-ui repo to use, defaults on master")
parser.add_argument('--no-launch', dest='launchui', action="store_false")
parser.add_argument('--log-to-console', dest='logtoconsole', action="store_true")
parser.add_argument('--quiet', dest='quiet', action="store_true")
2016-05-30 21:49:25 +02:00
parser.set_defaults(branch=False, launchui=True, logtoconsole=False, quiet=False)
args = parser.parse_args()
log_support.configure_file_handler(lbrynet_log)
log_support.configure_loggly_handler()
if args.logtoconsole:
2016-08-11 07:14:21 +02:00
log_support.configure_console(level='DEBUG')
log_support.disable_third_party_loggers()
log_support.disable_noisy_loggers()
try:
JSONRPCProxy.from_url(API_CONNECTION_STRING).is_running()
log.info("lbrynet-daemon is already running")
if not args.logtoconsole:
print "lbrynet-daemon is already running"
if args.launchui:
webbrowser.open(UI_ADDRESS)
return
except:
pass
log.info("Starting lbrynet-daemon from command line")
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
print "Web UI is available at http://%s:%i" % (API_INTERFACE, API_PORT)
print "JSONRPC API is available at " + API_CONNECTION_STRING
print "To quit press ctrl-c or call 'stop' via the API"
if test_internet_connection():
lbry = LBRYDaemonServer()
2016-05-30 21:49:25 +02:00
d = lbry.start(branch=args.branch if args.branch else DEFAULT_UI_BRANCH,
user_specified=args.ui,
wallet=args.wallet,
branch_specified=True if args.branch else False)
if args.launchui:
d.addCallback(lambda _: webbrowser.open(UI_ADDRESS))
lbrynet_server = server.Site(lbry.root)
lbrynet_server.requestFactory = LBRYDaemonRequest
reactor.listenTCP(API_PORT, lbrynet_server, interface=API_INTERFACE)
reactor.run()
if not args.logtoconsole and not args.quiet:
print "\nClosing lbrynet-daemon"
else:
log.info("Not connected to internet, unable to start")
if not args.logtoconsole:
print "Not connected to internet, unable to start"
return
2016-05-02 10:10:50 +02:00
if __name__ == "__main__":
start()