From bb4ac161792335977582a83eca4b06f2eef03914 Mon Sep 17 00:00:00 2001 From: Dan Mick Date: Thu, 17 Apr 2025 19:33:50 -0700 Subject: [PATCH] remove everything to do with amqp messages - the publish*message functions and callers - the Bus controller - tests and fixtures for the above - the pika dependency Signed-off-by: Dan Mick --- pyproject.toml | 1 - requirements.txt | 1 - shaman/controllers/api/__init__.py | 1 - shaman/controllers/api/bus/__init__.py | 26 ------------------------- shaman/controllers/root.py | 1 - shaman/models/__init__.py | 18 ----------------- shaman/models/builds.py | 3 +-- shaman/models/repos.py | 3 +-- shaman/tests/conftest.py | 18 ----------------- shaman/tests/controllers/test_bus.py | 27 -------------------------- shaman/util.py | 26 ------------------------- 11 files changed, 2 insertions(+), 123 deletions(-) delete mode 100644 shaman/controllers/api/bus/__init__.py delete mode 100644 shaman/tests/controllers/test_bus.py diff --git a/pyproject.toml b/pyproject.toml index f02cb82..c199577 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -16,7 +16,6 @@ dependencies = [ "pecan-notario", "requests", "jinja2", - "pika", ] classifiers = [ 'Development Status :: 4 - Beta', diff --git a/requirements.txt b/requirements.txt index 4b8818a..4649d8c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,5 +9,4 @@ alembic==1.13.2 ipython==5.0.0 requests==2.25.1 jinja2==3.1.4 -pika==1.2.0 enum34==1.1.10 diff --git a/shaman/controllers/api/__init__.py b/shaman/controllers/api/__init__.py index b617ce7..3a1ac7b 100644 --- a/shaman/controllers/api/__init__.py +++ b/shaman/controllers/api/__init__.py @@ -1,4 +1,3 @@ import shaman.controllers.api.repos # noqa import shaman.controllers.api.nodes # noqa import shaman.controllers.api.builds # noqa -import shaman.controllers.api.bus # noqa diff --git a/shaman/controllers/api/bus/__init__.py b/shaman/controllers/api/bus/__init__.py deleted file mode 100644 index 9208133..0000000 --- a/shaman/controllers/api/bus/__init__.py +++ /dev/null @@ -1,26 +0,0 @@ -import json - -from pecan import expose, abort, request -from pecan.secure import secure - -from shaman.auth import github_basic_auth -from shaman.util import publish_message - - -class BusController(object): - - @expose(generic=True) - def index(self): - abort(405) - - @index.when(method='GET') - def index_get(self): - abort(405) - - @secure(github_basic_auth) - @index.when(method='POST', template='json') - def index_post(self, project, topic): - routing_key = "{}.{}".format(project, topic) - body = json.dumps(request.json) - publish_message(routing_key, body) - return {} diff --git a/shaman/controllers/root.py b/shaman/controllers/root.py index 68f39ab..c99423f 100644 --- a/shaman/controllers/root.py +++ b/shaman/controllers/root.py @@ -24,7 +24,6 @@ def index(self): nodes = api.nodes.NodesController() search = _search.SearchController() builds = api.builds.ProjectsAPIController() - bus = api.bus.BusController() class RootController(object): diff --git a/shaman/models/__init__.py b/shaman/models/__init__.py index cf9a72c..37443ae 100644 --- a/shaman/models/__init__.py +++ b/shaman/models/__init__.py @@ -1,5 +1,4 @@ import datetime -import json from sqlalchemy import create_engine, MetaData, event from sqlalchemy.orm import scoped_session, sessionmaker, object_session, mapper from sqlalchemy.ext.declarative import declarative_base @@ -70,23 +69,6 @@ def _date_json_converter(item): if isinstance(item, datetime.datetime): return str(item) - -def publish_update_message(mapper, connection, target): - """ - Send a message to RabbitMQ everytime a Repo - is updated - """ - from shaman.util import publish_message - - if isinstance(target, Build): - topic = "builds" - elif isinstance(target, Repo): - topic = "repos" - routing_key = "{}.{}".format(target.project.name, topic) - body = json.dumps(target.__json__(), default=_date_json_converter) - publish_message(routing_key, body) - - # Utilities: def get_or_create(model, **kwargs): diff --git a/shaman/models/builds.py b/shaman/models/builds.py index 00190aa..159ee00 100644 --- a/shaman/models/builds.py +++ b/shaman/models/builds.py @@ -4,7 +4,7 @@ from sqlalchemy.orm import relationship, backref, deferred from sqlalchemy.event import listen from sqlalchemy.orm.exc import DetachedInstanceError -from shaman.models import Base, update_timestamp, publish_update_message +from shaman.models import Base, update_timestamp from shaman.models.types import JSONType @@ -125,4 +125,3 @@ def get_url(self, up_to_part=None): # listen for timestamp modifications listen(Build, 'before_insert', update_timestamp) listen(Build, 'before_update', update_timestamp) -listen(Build, 'after_update', publish_update_message) diff --git a/shaman/models/repos.py b/shaman/models/repos.py index 148b313..72f63b4 100644 --- a/shaman/models/repos.py +++ b/shaman/models/repos.py @@ -3,7 +3,7 @@ from sqlalchemy.orm import relationship, backref, deferred from sqlalchemy.event import listen from sqlalchemy.orm.exc import DetachedInstanceError -from shaman.models import Base, update_timestamp, publish_update_message +from shaman.models import Base, update_timestamp from shaman.models.types import JSONType @@ -101,4 +101,3 @@ def get_url(self): # listen for timestamp modifications listen(Repo, 'before_insert', update_timestamp) listen(Repo, 'before_update', update_timestamp) -listen(Repo, 'after_update', publish_update_message) diff --git a/shaman/tests/conftest.py b/shaman/tests/conftest.py index 208b0cf..f600693 100644 --- a/shaman/tests/conftest.py +++ b/shaman/tests/conftest.py @@ -43,24 +43,6 @@ def factory(): return Factory -@pytest.fixture(autouse=True) -def no_pika_requests(monkeypatch, factory): - """ - If you don't do anything to patch pika, this fxiture will automatically - patchn it and prevent outbound requests. - """ - fake_connection = factory( - queue_bind=lambda: True, - close=lambda: True, - channel=lambda: factory( - exchange_declare=lambda *a, **kw: True, - queue_bind=lambda *a: True, - basic_publish=lambda *a, **kw: True, - queue_declare=lambda *a, **kw: True,), - ) - monkeypatch.setattr("pika.BlockingConnection", lambda *a: fake_connection) - - def config_file(): here = os.path.abspath(os.path.dirname(__file__)) return os.path.join(here, 'config.py') diff --git a/shaman/tests/controllers/test_bus.py b/shaman/tests/controllers/test_bus.py deleted file mode 100644 index a187ada..0000000 --- a/shaman/tests/controllers/test_bus.py +++ /dev/null @@ -1,27 +0,0 @@ -import hmac -from hashlib import sha1 - - -class TestBusApiController(object): - - def test_post_index_basic_auth(self, app): - result = app.post_json('/api/bus/ceph/test', params={}) - assert result.status_int == 200 - - def test_post_index_github_auth_fails(self, app): - result = app.post_json( - '/api/bus/ceph/test', - headers={'X-Hub-Signature': 'fail'}, - params={}, - expect_errors=True - ) - assert result.status_int == 401 - - def test_post_index_github_auth_succeeds(self, app): - signature = "sha1={}".format(hmac.new(b'secret', b'{}', sha1).hexdigest()) - result = app.post_json( - '/api/bus/ceph/test', - headers={'X-Hub-Signature': signature}, - params={}, - ) - assert result.status_int == 200 diff --git a/shaman/util.py b/shaman/util.py index 612dfbb..a1bffa7 100644 --- a/shaman/util.py +++ b/shaman/util.py @@ -1,6 +1,5 @@ import os import requests -import pika from requests.exceptions import BaseHTTPError, RequestException import datetime import logging @@ -190,28 +189,3 @@ def get_repo_url(query, arch, path=None, repo_file=True): # yum or apt repo file repo_url = os.path.join(repo.chacra_url, 'repo') return repo_url - - -def publish_message(routing_key, body): - """ - Publishes a message to RabbitMQ - """ - credentials = pika.PlainCredentials(conf.RABBIT_USER, conf.RABBIT_PW) - connection = pika.BlockingConnection(pika.ConnectionParameters( - host=conf.RABBIT_HOST, - credentials=credentials - )) - channel = connection.channel() - channel.exchange_declare( - exchange="shaman", - exchange_type="topic", - ) - - properties = pika.BasicProperties(content_type='application/json') - channel.basic_publish( - exchange="shaman", - routing_key=routing_key, - body=body, - properties=properties, - ) - connection.close()