Skip to content

Commit

Permalink
consistently pass API and web settings to cousteau
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisamin committed May 23, 2022
1 parent 8cc6087 commit 53c4d40
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 20 deletions.
4 changes: 2 additions & 2 deletions ripe/atlas/tools/commands/measure/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ def run(self):
self._handle_api_error(response) # Raises an exception

pk = response["measurements"][0]
url = "{0}/measurements/{1}/".format(conf["ripe-ncc"]["endpoint"], pk)
url = "{0}/measurements/{1}/".format(conf["website-url"], pk)

self.ok(
f"Looking good! Measurement {pk} was created and details about "
Expand Down Expand Up @@ -330,7 +330,7 @@ def create(self):
creation_class = self.CREATION_CLASSES[self._type]

return AtlasCreateRequest(
server=conf["ripe-ncc"]["endpoint"].replace("https://", ""),
server=conf["api-server"],
key=self.arguments.auth,
user_agent=self.user_agent,
measurements=[creation_class(**self._get_measurement_kwargs())],
Expand Down
16 changes: 13 additions & 3 deletions ripe/atlas/tools/commands/measurement_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from ..helpers.colours import colourise
from ..helpers.sanitisers import sanitise
from ..helpers.validators import ArgumentType
from ..settings import conf


class Command(MetaDataMixin, BaseCommand):
Expand All @@ -36,7 +37,11 @@ def add_arguments(self):
def run(self):

try:
measurement = Measurement(id=self.arguments.id, user_agent=self.user_agent)
measurement = Measurement(
server=conf["api-server"],
id=self.arguments.id,
user_agent=self.user_agent,
)
except APIResponseError:
raise RipeAtlasToolsException("That measurement does not appear to exist")

Expand All @@ -45,12 +50,17 @@ def run(self):

@classmethod
def render_basic(cls, measurement):
url_template = "https://atlas.ripe.net/measurements/{}/"
cls._render(
measurement,
(
("id", "ID"),
("id", "URL", lambda _: colourise(url_template.format(_), "cyan")),
(
"id",
"URL",
lambda id: colourise(
f"{conf['website-url']}/measurements/id/", "cyan"
),
),
("type", "Type", cls._prettify_type),
("status", "Status"),
("description", "Description", sanitise),
Expand Down
2 changes: 2 additions & 0 deletions ripe/atlas/tools/commands/measurement_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from ..helpers.colours import colourise
from ..helpers.sanitisers import sanitise
from ..helpers.validators import ArgumentType
from ..settings import conf


class Command(TabularFieldsMixin, BaseCommand):
Expand Down Expand Up @@ -129,6 +130,7 @@ def run(self):

filters = self._get_filters()
measurements = MeasurementRequest(
server=conf["api-server"],
return_objects=True, user_agent=self.user_agent, **filters
)
truncated_measurements = itertools.islice(measurements, self.arguments.limit)
Expand Down
14 changes: 11 additions & 3 deletions ripe/atlas/tools/commands/probe_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from ..helpers.colours import colourise
from ..helpers.sanitisers import sanitise
from ..helpers.validators import ArgumentType
from ..settings import conf


class Command(MetaDataMixin, BaseCommand):
Expand All @@ -36,14 +37,21 @@ def add_arguments(self):
def run(self):

try:
probe = Probe(id=self.arguments.id, user_agent=self.user_agent)
probe = Probe(
server=conf["api-server"],
id=self.arguments.id,
user_agent=self.user_agent,
)
except APIResponseError:
raise RipeAtlasToolsException("That probe does not appear to exist")

url_template = "https://atlas.ripe.net/probes/{}/"
keys = (
("id", "ID"),
("id", "URL", lambda _: colourise(url_template.format(_), "cyan")),
(
"id",
"URL",
lambda id: colourise(f"{conf['website-url']}/probes/{id}/", "cyan"),
),
("is_public", "Public?", self._prettify_boolean),
("is_anchor", "Anchor?", self._prettify_boolean),
("country_code", "Country"),
Expand Down
1 change: 1 addition & 0 deletions ripe/atlas/tools/commands/probe_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ def run(self):

self.set_aggregators()
probes = ProbeRequest(
server=conf["api-server"],
return_objects=True, user_agent=self.user_agent, **filters
)
truncated_probes = itertools.islice(probes, self.arguments.limit)
Expand Down
6 changes: 5 additions & 1 deletion ripe/atlas/tools/commands/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ def _get_request_auth(self):
def _get_request(self):

kwargs = {
"server": conf["api-server"],
"msm_id": self.arguments.measurement_id,
"user_agent": self.user_agent,
}
Expand Down Expand Up @@ -187,7 +188,10 @@ def run(self):
arguments=self.arguments
)

results = SaganSet(iterable=results, probes=self.arguments.probes)
results = SaganSet(
iterable=results,
probes=self.arguments.probes,
)

if self.arguments.probe_asns:
asn_filters = set([])
Expand Down
23 changes: 13 additions & 10 deletions ripe/atlas/tools/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from .exceptions import RipeAtlasToolsException
from .cache import cache
from .settings import conf


class FilterFactory(object):
Expand Down Expand Up @@ -51,8 +52,7 @@ def filter(self, result):
attr_value = getattr(result.probe, self.key)
except AttributeError:
log = (
"Cousteau's Probe class does not have an attribute "
"called: <{}>"
"Cousteau's Probe class does not have an attribute " "called: <{}>"
).format(self.key)
raise RipeAtlasToolsException(log)
if attr_value == self.value:
Expand Down Expand Up @@ -137,10 +137,14 @@ def __next__(self):
def next(self):
return self.__next__()

@staticmethod
def _attach_probes(sagans):
def _attach_probes(self, sagans):
probes = dict(
[(p.id, p) for p in Probe.get_many(s.probe_id for s in sagans)]
[
(p.id, p)
for p in Probe.get_many(
(s.probe_id for s in sagans)
)
]
)
for sagan in sagans:
sagan.probe = probes[sagan.probe_id]
Expand All @@ -164,7 +168,8 @@ def get(cls, pk):
"""
r = cache.get("probe:{}".format(pk))
if not r:
probe = CProbe(id=pk)
kwargs = {"id": pk, "server": conf["api-server"]}
probe = CProbe(**kwargs)
cache.set("probe:{}".format(probe.id), probe, cls.EXPIRE_TIME)
return probe

Expand All @@ -187,10 +192,8 @@ def get_many(cls, ids):
fetch_ids.append(str(pk))

if fetch_ids:
kwargs = {"id__in": fetch_ids}
for probe in [
p for p in ProbeRequest(return_objects=True, **kwargs)
]:
kwargs = {"id__in": fetch_ids, "server": conf["api-server"]}
for probe in [p for p in ProbeRequest(return_objects=True, **kwargs)]:
cache.set("probe:{}".format(probe.id), probe, cls.EXPIRE_TIME)
r.append(probe)

Expand Down
8 changes: 8 additions & 0 deletions ripe/atlas/tools/settings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ class Configuration(UserSettingsParser):
},
"ripe-ncc": {
"endpoint": "https://atlas.ripe.net",
"stream-base-url": "https://atlas-stream.ripe.net",
"version": 0,
},
}
Expand Down Expand Up @@ -202,6 +203,13 @@ def write(config):
with open(Configuration.USER_RC, "w") as rc:
rc.write(payload)

def get(self):
d = super().get()
d["website-url"] = d["ripe-ncc"]["endpoint"]
d["api-server"] = d["ripe-ncc"]["endpoint"].replace("https://", "")
d["stream-base-url"] = d["ripe-ncc"]["stream-base-url"]
return d


class AliasesDB(UserSettingsParser):
"""
Expand Down
3 changes: 2 additions & 1 deletion ripe/atlas/tools/streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from ripe.atlas.cousteau import AtlasStream
from ripe.atlas.sagan import Result
from .settings import conf


class CaptureLimitExceeded(Exception):
Expand Down Expand Up @@ -48,7 +49,7 @@ def on_result_response(result, *args):
)
results.append(parsed)

stream = AtlasStream()
stream = AtlasStream(base_url=conf["stream-base-url"])
stream.connect()

start = time.time()
Expand Down

0 comments on commit 53c4d40

Please sign in to comment.