diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..2f8a0fd --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,32 @@ +name: Release to PyPI + +on: + release: + types: [published] + workflow_dispatch: + +jobs: + release: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "3.11" + + - name: Install build dependencies + run: | + python -m pip install --upgrade pip + pip install build twine + + - name: Build package + run: python -m build + + - name: Publish to PyPI + env: + TWINE_USERNAME: __token__ + TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }} + run: twine upload dist/* diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..bb6e669 --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,36 @@ +name: Tests + +on: + push: + branches: [main] + pull_request: + branches: [main] + +jobs: + test: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.8", "3.9", "3.10", "3.11"] + + steps: + - uses: actions/checkout@v4 + + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -e ".[dev]" + + - name: Lint with ruff + run: ruff check . + + - name: Check formatting with black + run: black --check . + + - name: Run tests + run: pytest tests/ -v diff --git a/.gitignore b/.gitignore index 8cd0668..c956e4b 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ __pycache__ localstack_utils.egg-info .ruff_cache dist +*.env diff --git a/README.md b/README.md index 3b4a6c5..e8a9d85 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,9 @@ class TestKinesis(unittest.TestCase): ``` ## Change Log +* 1.0.5: Add support for configurable container name and auth token forwarding +* 1.0.4: Fixes to LocalStack and Container modules +* 1.0.3: Add auto_remove config option * 1.0.2: LocalStack Pro image set as default * 1.0.1: Repository URL fixed * 1.0.0: Initial version diff --git a/localstack_utils/__init__.py b/localstack_utils/__init__.py index 5c4105c..68cdeee 100644 --- a/localstack_utils/__init__.py +++ b/localstack_utils/__init__.py @@ -1 +1 @@ -__version__ = "1.0.1" +__version__ = "1.0.5" diff --git a/localstack_utils/container.py b/localstack_utils/container.py index d0faa9d..8d96bb7 100644 --- a/localstack_utils/container.py +++ b/localstack_utils/container.py @@ -1,4 +1,7 @@ +from __future__ import annotations + import logging +import os import re import docker from time import sleep @@ -27,13 +30,17 @@ def create_localstack_container( pull_new_image: bool = False, image_name: str = LOCALSTACK_IMAGE_NAME, image_tag: str = LATEST_TAG, + container_name: str = DEFAULT_CONTAINER_ID, gateway_listen: str = "0.0.0.0:4566", + auto_remove: bool = False, environment_variables: dict | None = None, bind_ports: dict | None = None, **kwargs, ): environment_variables = environment_variables or {} environment_variables["GATEWAY_LISTEN"] = gateway_listen + if "LOCALSTACK_AUTH_TOKEN" not in environment_variables and os.environ.get("LOCALSTACK_AUTH_TOKEN"): + environment_variables["LOCALSTACK_AUTH_TOKEN"] = os.environ["LOCALSTACK_AUTH_TOKEN"] image_exists = ( True if len(DOCKER_CLIENT.images.list(name=image_name)) else False @@ -49,8 +56,10 @@ def create_localstack_container( return DOCKER_CLIENT.containers.run( image_name, + name=container_name, ports=bind_ports, environment=environment_variables, + auto_remove=auto_remove, detach=True, ) @@ -67,4 +76,4 @@ def wait_for_ready(container, pattern): attempts += 1 if attempts >= MAX_LOG_COLLECTION_ATTEMPTS: - raise "Could not find token: " + pattern.toString() + "in logs" + raise RuntimeError(f"Could not find token: {pattern.pattern} in logs") diff --git a/localstack_utils/localstack.py b/localstack_utils/localstack.py index 311a57a..353aae4 100644 --- a/localstack_utils/localstack.py +++ b/localstack_utils/localstack.py @@ -1,5 +1,4 @@ import re -import sys import docker import logging from localstack_utils.container import Container @@ -41,13 +40,14 @@ def startup(self, docker_configuration): try: self.localstack_container = Container.create_localstack_container( - docker_configuration.pull_new_image, - docker_configuration.image_name, - docker_configuration.image_tag, - docker_configuration.gateway_listen, - docker_configuration.environment_variables, - docker_configuration.port_mappings, - docker_configuration.pro, + pull_new_image=docker_configuration.pull_new_image, + image_name=docker_configuration.image_name, + image_tag=docker_configuration.image_tag, + container_name=docker_configuration.container_name, + gateway_listen=docker_configuration.gateway_listen, + auto_remove=docker_configuration.auto_remove_container, + environment_variables=docker_configuration.environment_variables, + bind_ports=docker_configuration.port_mappings, ) self.setup_logger() @@ -56,10 +56,10 @@ def startup(self, docker_configuration): except docker.errors.APIError: if not docker_configuration.ignore_docker_runerrors: - raise "Unable to start docker" + raise RuntimeError("Unable to start docker") except Exception: - raise sys.exc_info() + raise def stop(self): self.localstack_container.stop() @@ -72,11 +72,13 @@ def setup_logger(self): def startup_localstack( image_name="", tag="", + container_name="", pro=False, ports=None, env_variables=None, gateway_listen="", ignore_docker_errors=False, + auto_remove_container=False, ): global localstack_instance localstack_instance = Localstack.INSTANCE() @@ -89,6 +91,9 @@ def startup_localstack( if tag: config.image_tag = tag + if container_name: + config.container_name = container_name + if ports: config.port_mappings = ports @@ -101,6 +106,9 @@ def startup_localstack( if gateway_listen: config.gateway_listen = gateway_listen + if auto_remove_container: + config.auto_remove_container = auto_remove_container + localstack_instance.startup(config) diff --git a/localstack_utils/localstack_docker_configuration.py b/localstack_utils/localstack_docker_configuration.py index 0c0a0c3..d1e332f 100644 --- a/localstack_utils/localstack_docker_configuration.py +++ b/localstack_utils/localstack_docker_configuration.py @@ -3,6 +3,7 @@ class LocalstackDockerConfiguration: randomize_ports = False image_name = None image_tag = None + container_name = None platform = None gateway_listen = "0.0.0.0:4566" @@ -14,3 +15,4 @@ class LocalstackDockerConfiguration: use_dingle_docker_container = False ignore_docker_runerrors = False pro = False + auto_remove_container = False diff --git a/tests/test_kinesis.py b/tests/test_kinesis.py index 06f2382..89eaaef 100644 --- a/tests/test_kinesis.py +++ b/tests/test_kinesis.py @@ -6,7 +6,7 @@ class TestKinesis(unittest.TestCase): def setUp(self): - startup_localstack() + startup_localstack(image_name="localstack/localstack") def tearDown(self): stop_localstack() @@ -16,6 +16,7 @@ def test_create_stream(self): kinesis = boto3.client( service_name="kinesis", aws_access_key_id="test", + region_name="us-east-1", aws_secret_access_key="test", endpoint_url="http://localhost:4566", )