Skip to content

Commit

Permalink
Merge pull request #611 from oduwsdl/issue-610
Browse files Browse the repository at this point in the history
Fix semantics of host/port as @ibnesayeed recommended in #609
  • Loading branch information
machawk1 authored Jun 22, 2019
2 parents 2e8061a + bdecf23 commit 63433f3
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 21 deletions.
13 changes: 7 additions & 6 deletions ipwb/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
# ipwb modules
from . import replay
from . import indexer
from . import util
from . import util as ipwbUtil

from .util import IPFSAPI_HOST, IPFSAPI_PORT, IPWBREPLAY_HOST, IPWBREPLAY_PORT
from .util import IPWBREPLAY_HOST, IPWBREPLAY_PORT


def main():
util.checkForUpdate()
ipwbUtil.checkForUpdate()
args = checkArgs(sys.argv)


def checkArgs_index(args):
if not util.isDaemonAlive():
if not ipwbUtil.isDaemonAlive():
sys.exit()
encKey = None
compressionLevel = None
Expand Down Expand Up @@ -137,8 +137,9 @@ def checkArgs(argsIn):

parser.add_argument(
'-d', '--daemon',
help='Location of ipfs daemon (default 127.0.0.1:5001)',
default='{0}:{1}'.format(IPFSAPI_HOST, IPFSAPI_PORT),
help=("Multi-address of IPFS daemon "
"(default /dns/localhost/tcp/5001/http)"),
default=ipwbUtil.IPFSAPI_MUTLIADDRESS,
dest='daemon_address')
parser.add_argument(
'-v', '--version', help='Report the version of ipwb', action='version',
Expand Down
7 changes: 5 additions & 2 deletions ipwb/indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
from six import PY2
from six import PY3

from .util import IPFSAPI_HOST, IPFSAPI_PORT
from .util import iso8601ToDigits14
from . import util as ipwbUtils

# from warcio.archiveiterator import ArchiveIterator

Expand All @@ -52,7 +52,10 @@

DEBUG = False

IPFS_API = ipfsapi.Client(f"/dns/{IPFSAPI_HOST}/tcp/{IPFSAPI_PORT}/http")
IPFS_API = ipwbUtils.createIPFSClient()
if IPFS_API is None:
print("Error initializing IPFS API client")
sys.exit()


def s2b(s): # Convert str to bytes, cross-py
Expand Down
14 changes: 8 additions & 6 deletions ipwb/replay.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

from . import util as ipwbUtils
from .util import unsurt
from .util import IPFSAPI_HOST, IPFSAPI_PORT, IPWBREPLAY_HOST, IPWBREPLAY_PORT
from .util import IPWBREPLAY_HOST, IPWBREPLAY_PORT
from .util import INDEX_FILE

from . import indexer
Expand All @@ -68,7 +68,11 @@
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.debug = False

IPFS_API = ipfsapi.Client(f"/dns/{IPFSAPI_HOST}/tcp/{IPFSAPI_PORT}/http")

IPFS_API = ipwbUtils.createIPFSClient()
if IPFS_API is None:
print("Error initializing IPFS API client")
sys.exit()


@app.context_processor
Expand Down Expand Up @@ -552,9 +556,8 @@ def all_exception_handler(error):

@app.route('/ipwbadmin', strict_slashes=False)
def showAdmin():
ipfsEndpoint = '{0}:{1}'.format(IPFSAPI_HOST, IPFSAPI_PORT)
status = {'ipwbVersion': ipwbVersion,
'ipfsEndpoint': ipfsEndpoint}
'ipfsEndpoint': ipwbUtils.IPFSAPI_MUTLIADDRESS}
iFile = ipwbUtils.getIPWBReplayIndexPath()

mementoInfo = calculateMementoInfoInIndex(iFile)
Expand Down Expand Up @@ -605,8 +608,7 @@ def showLandingPage():
def show_uri(path, datetime=None):
global IPFS_API

daemonAddress = '{0}:{1}'.format(IPFSAPI_HOST, IPFSAPI_PORT)
if not ipwbUtils.isDaemonAlive(daemonAddress):
if not ipwbUtils.isDaemonAlive(ipwbUtils.IPFSAPI_MUTLIADDRESS):
errStr = ('IPFS daemon not running. '
'Start it using $ ipfs daemon on the command-line '
' or from the <a href="/">'
Expand Down
30 changes: 23 additions & 7 deletions ipwb/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,18 @@

# from requests.exceptions import ConnectionError
from ipfshttpclient.exceptions import ConnectionError
from ipfshttpclient.exceptions import AddressError
from multiaddr.exceptions import StringParseError

IPFSAPI_MUTLIADDRESS = '/dns/localhost/tcp/5001/http'
# or '/dns/{host}/tcp/{port}/http'
# or '/ip4/{ipaddress}/tcp/{port}/http'
# or '/ip6/{ipaddress}/tcp/{port}/http

IPFSAPI_HOST = 'localhost'
IPFSAPI_PORT = 5001
IPWBREPLAY_HOST = 'localhost'
IPWBREPLAY_PORT = 5000
IPWBREPLAY_ADDRESS = 'localhost:5000'

(IPWBREPLAY_HOST, IPWBREPLAY_PORT) = IPWBREPLAY_ADDRESS.split(':')
IPWBREPLAY_PORT = int(IPWBREPLAY_PORT)

INDEX_FILE = 'samples/indexes/salam-home.cdxj'

Expand All @@ -39,16 +45,26 @@
dtPattern = re.compile(r"^(\d{4})(\d{2})?(\d{2})?(\d{2})?(\d{2})?(\d{2})?$")


def isDaemonAlive(hostAndPort="{0}:{1}".format(IPFSAPI_HOST, IPFSAPI_PORT)):
def createIPFSClient(daemonMultiaddr=IPFSAPI_MUTLIADDRESS):
try:
return ipfsapi.Client(daemonMultiaddr)
except (StringParseError, AddressError):
return None # Malformed multiaddress for the daemon


def isDaemonAlive(daemonMultiaddr=IPFSAPI_MUTLIADDRESS):
"""Ensure that the IPFS daemon is running via HTTP before proceeding"""
client = ipfsapi.Client(f"/dns/{IPFSAPI_HOST}/tcp/{IPFSAPI_PORT}/http")
client = createIPFSClient()
if client is None:
print("Error initializing IPFS API client")
return False

try:
# ConnectionError/AttributeError if IPFS daemon not running
client.id()
return True
except (ConnectionError): # exceptions.AttributeError):
logError("Daemon is not running at http://" + hostAndPort)
logError("Daemon is not running at " + daemonMultiaddr)
return False
except OSError:
logError("IPFS is likely not installed. "
Expand Down

0 comments on commit 63433f3

Please sign in to comment.