2016-03-14 17:30:22 +01:00
|
|
|
import argparse
|
|
|
|
import logging
|
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
|
|
|
|
import sys
|
2016-04-14 06:29:40 +02:00
|
|
|
import socket
|
2016-04-21 04:02:52 +02:00
|
|
|
import platform
|
2016-04-14 06:29:40 +02:00
|
|
|
|
2016-04-02 04:06:06 +02:00
|
|
|
from appdirs import user_data_dir
|
2016-04-21 04:02:52 +02:00
|
|
|
from twisted.web import server
|
2016-03-14 17:30:22 +01:00
|
|
|
from twisted.internet import reactor, defer
|
|
|
|
from jsonrpc.proxy import JSONRPCProxy
|
|
|
|
|
2016-04-21 04:02:52 +02:00
|
|
|
from lbrynet.lbrynet_daemon.LBRYDaemonServer import LBRYDaemonServer
|
2016-05-30 21:49:25 +02:00
|
|
|
from lbrynet.conf import API_CONNECTION_STRING, API_INTERFACE, API_ADDRESS, API_PORT, \
|
|
|
|
DEFAULT_WALLET, UI_ADDRESS, DEFAULT_UI_BRANCH
|
2016-03-14 17:30:22 +01:00
|
|
|
|
2016-04-21 04:02:52 +02:00
|
|
|
|
2016-04-07 09:12:09 +02:00
|
|
|
if sys.platform != "darwin":
|
|
|
|
log_dir = os.path.join(os.path.expanduser("~"), ".lbrynet")
|
|
|
|
else:
|
|
|
|
log_dir = user_data_dir("LBRY")
|
|
|
|
|
2016-04-09 21:43:10 +02:00
|
|
|
if not os.path.isdir(log_dir):
|
|
|
|
os.mkdir(log_dir)
|
|
|
|
|
2016-04-07 09:12:09 +02:00
|
|
|
LOG_FILENAME = os.path.join(log_dir, 'lbrynet-daemon.log')
|
2016-03-14 17:30:22 +01:00
|
|
|
log = logging.getLogger(__name__)
|
2016-04-20 08:56:29 +02:00
|
|
|
handler = logging.handlers.RotatingFileHandler(LOG_FILENAME, maxBytes=2097152, backupCount=5)
|
2016-04-09 21:43:10 +02:00
|
|
|
log.addHandler(handler)
|
2016-04-16 01:37:27 +02:00
|
|
|
log.setLevel(logging.INFO)
|
2016-03-14 17:30:22 +01:00
|
|
|
|
2016-04-21 04:02:52 +02:00
|
|
|
|
2016-04-14 06:29:40 +02:00
|
|
|
REMOTE_SERVER = "www.google.com"
|
|
|
|
|
2016-04-16 01:37:27 +02:00
|
|
|
|
2016-04-14 06:29:40 +02:00
|
|
|
def test_internet_connection():
|
|
|
|
try:
|
|
|
|
host = socket.gethostbyname(REMOTE_SERVER)
|
|
|
|
s = socket.create_connection((host, 80), 2)
|
|
|
|
return True
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
|
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)
|
|
|
|
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='')
|
2016-03-26 02:03:58 +01:00
|
|
|
parser.add_argument("--ui",
|
2016-04-02 04:06:06 +02:00
|
|
|
help="path to custom UI folder",
|
2016-04-21 04:02:52 +02:00
|
|
|
default=None)
|
2016-04-11 22:15:12 +02:00
|
|
|
parser.add_argument("--branch",
|
2016-05-30 21:49:25 +02:00
|
|
|
help="Branch of lbry-web-ui repo to use, defaults on master")
|
2016-04-16 01:37:27 +02:00
|
|
|
parser.add_argument('--no-launch', dest='launchui', action="store_false")
|
2016-04-18 05:23:20 +02:00
|
|
|
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)
|
2016-04-18 05:23:20 +02:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
if args.logtoconsole:
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
2016-03-14 17:30:22 +01:00
|
|
|
|
2016-04-18 02:16:35 +02:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2016-03-29 22:42:47 +02:00
|
|
|
try:
|
|
|
|
JSONRPCProxy.from_url(API_CONNECTION_STRING).is_running()
|
|
|
|
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-04-18 02:16:35 +02:00
|
|
|
if args.launchui:
|
|
|
|
webbrowser.open(UI_ADDRESS)
|
2016-03-29 22:42:47 +02:00
|
|
|
return
|
|
|
|
except:
|
|
|
|
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"
|
|
|
|
print "To view activity, view the log file here: " + LOG_FILENAME
|
2016-04-21 04:02:52 +02:00
|
|
|
print "Web UI is available at http://%s:%i" % (API_INTERFACE, API_PORT)
|
2016-04-18 05:23:20 +02:00
|
|
|
print "JSONRPC API is available at " + API_CONNECTION_STRING
|
|
|
|
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():
|
|
|
|
lbry = LBRYDaemonServer()
|
2016-04-02 04:06:06 +02:00
|
|
|
|
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)
|
2016-04-22 08:45:05 +02:00
|
|
|
if args.launchui:
|
|
|
|
d.addCallback(lambda _: webbrowser.open(UI_ADDRESS))
|
2016-04-02 04:06:06 +02:00
|
|
|
|
2016-04-21 04:02:52 +02:00
|
|
|
reactor.listenTCP(API_PORT, server.Site(lbry.root), interface=API_INTERFACE)
|
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
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
start()
|