Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.
Python awsaws-emulatoraws-localaws-sdkdevtoolsdockerdynamodbec2emulatorlambdalocalstacklocalstack-alternativeministackmock-awsopen-sourcepythons3sqsterraform
feat: support .py init scripts and bundle AWS CLI (#272)
Akhil adigaf9c3bda1589c
diff --git a/Dockerfile b/Dockerfileindex 34ba3722..59d86486 100644--- a/Dockerfile+++ b/Dockerfile@@ -1,22 +1,38 @@+FROM python:3.12-alpine AS builder++RUN pip install --no-cache-dir --no-compile \+ uvicorn==0.30.6 \+ "cbor2>=5.4.0" \+ "defusedxml>=0.7" \+ "docker>=7.0.0" \+ "pyyaml>=6.0" \+ "cryptography>=41.0" \+ "awscli"++# Strip awscli help examples (~25 MB) and Python cache files (~15 MB).+RUN rm -rf /usr/local/lib/python3.12/site-packages/awscli/examples \+ && find /usr/local/lib/python3.12/site-packages -type d -name '__pycache__' -exec rm -rf {} + 2>/dev/null \+ && rm -rf /usr/local/lib/python3.12/site-packages/pip*.dist-info \+ && rm -rf /usr/local/lib/python3.12/site-packages/pip*+FROM python:3.12-alpineLABEL maintainer="MiniStack" \description="Local AWS Service Emulator — drop-in LocalStack replacement"# Upgrade base packages to pick up latest security patches.-RUN apk upgrade --no-cache && apk add --no-cache nodejs && rm -f /usr/bin/wget /bin/wget+RUN apk upgrade --no-cache && apk add --no-cache nodejs bash && rm -f /usr/bin/wget /bin/wgetWORKDIR /opt/ministack-# Install all Python dependencies.-RUN pip install --no-cache-dir --upgrade pip && \- pip install --no-cache-dir \- uvicorn==0.30.6 \- "cbor2>=5.4.0" \- "defusedxml>=0.7" \- "docker>=7.0.0" \- "pyyaml>=6.0" \- "cryptography>=41.0"+# Copy cleaned Python packages and CLI entrypoints from builder.+COPY --from=builder /usr/local/lib/python3.12/site-packages /usr/local/lib/python3.12/site-packages+COPY --from=builder /usr/local/bin/aws /usr/local/bin/aws+COPY --from=builder /usr/local/bin/aws_completer /usr/local/bin/aws_completer+COPY --from=builder /usr/local/bin/uvicorn /usr/local/bin/uvicorn++COPY bin/awslocal /usr/local/bin/awslocal+RUN chmod +x /usr/local/bin/awslocalCOPY ministack/ ministack/diff --git a/README.md b/README.mdindex a96b388c..4616df1b 100644--- a/README.md+++ b/README.md@@ -633,8 +633,8 @@ MiniStack supports two types of init scripts, with LocalStack-compatible paths:| Phase | MiniStack path | LocalStack-compatible path ||-------|----------------|---------------------------|-| Pre-start | `/docker-entrypoint-initaws.d/*.sh` | `/etc/localstack/init/boot.d/*.sh` |-| Post-ready | `/docker-entrypoint-initaws.d/ready.d/*.sh` | `/etc/localstack/init/ready.d/*.sh` |+| Pre-start | `/docker-entrypoint-initaws.d/*.{sh,py}` | `/etc/localstack/init/boot.d/*.{sh,py}` |+| Post-ready | `/docker-entrypoint-initaws.d/ready.d/*.{sh,py}` | `/etc/localstack/init/ready.d/*.{sh,py}` |Scripts from both paths are merged, deduplicated by filename, and run in alphabetical order.If the same filename exists in both paths, the MiniStack-native path takes priority.diff --git a/ministack/app.py b/ministack/app.pyindex 17a83c08..65510b4e 100644--- a/ministack/app.py+++ b/ministack/app.py@@ -694,7 +694,7 @@ async def _wait_for_port(port, timeout=30):async def _run_ready_scripts():- """Execute .sh scripts from ready.d directories after the server is ready."""+ """Execute .sh/.py scripts from ready.d directories after the server is ready."""scripts = _collect_scripts('/docker-entrypoint-initaws.d/ready.d', '/etc/localstack/init/ready.d')if not scripts:return@@ -704,8 +704,9 @@ async def _run_ready_scripts():for script_path in scripts:logger.info('Running ready script: %s', script_path)try:+ cmd = [sys.executable, script_path] if script_path.endswith('.py') else ['sh', script_path]proc = await asyncio.create_subprocess_exec(- 'sh', script_path,+ *cmd,stdout=asyncio.subprocess.PIPE,stderr=asyncio.subprocess.PIPE,env=os.environ,@@ -726,19 +727,19 @@ async def _run_ready_scripts():def _collect_scripts(*dirs):- """Collect .sh scripts from multiple directories, deduped by filename."""+ """Collect .sh/.py scripts from multiple directories, deduped by filename."""seen = {}for d in dirs:if not os.path.isdir(d):continuefor f in sorted(os.listdir(d)):- if f.endswith('.sh') and f not in seen:+ if f.endswith(('.sh', '.py')) and f not in seen:seen[f] = os.path.join(d, f)return [seen[f] for f in sorted(seen)]def _run_init_scripts():- """Execute .sh scripts from init directories in alphabetical order."""+ """Execute .sh/.py scripts from init directories in alphabetical order."""scripts = _collect_scripts('/docker-entrypoint-initaws.d', '/etc/localstack/init/boot.d')if not scripts:return@@ -746,8 +747,9 @@ def _run_init_scripts():for script_path in scripts:logger.info("Running init script: %s", script_path)try:+ cmd = [sys.executable, script_path] if script_path.endswith('.py') else ["sh", script_path]result = subprocess.run(- ["sh", script_path], env=os.environ,+ cmd, env=os.environ,capture_output=True, text=True, timeout=300,)if result.stdout:diff --git a/tests/test_init_scripts.py b/tests/test_init_scripts.pyindex 35f0165b..96b6e908 100644--- a/tests/test_init_scripts.py+++ b/tests/test_init_scripts.py@@ -1,6 +1,9 @@-"""Tests for init script collection from multiple directories."""+"""Tests for init script collection and execution from multiple directories (.sh and .py)."""-from ministack.app import _collect_scripts+import os+import sys++from ministack.app import _collect_scripts, _run_init_scriptsdef test_collect_scripts_single_dir(tmp_path):@@ -72,5 +75,69 @@ def test_collect_scripts_alphabetical_order(tmp_path):(tmp_path / "02-middle.sh").write_text("")result = _collect_scripts(str(tmp_path))- names = [r.split("/")[-1] for r in result]+ names = [os.path.basename(r) for r in result]assert names == ["01-first.sh", "02-middle.sh", "03-last.sh"]+++def test_collect_scripts_py_files(tmp_path):+ (tmp_path / "01-seed.sh").write_text("#!/bin/sh\necho seed")+ (tmp_path / "02-migrate.py").write_text("print('migrate')")++ result = _collect_scripts(str(tmp_path))+ assert len(result) == 2+ assert result[0].endswith("01-seed.sh")+ assert result[1].endswith("02-migrate.py")+++def test_collect_scripts_mixed_sort_order(tmp_path):+ (tmp_path / "03-cleanup.sh").write_text("")+ (tmp_path / "01-setup.sh").write_text("")+ (tmp_path / "02-migrate.py").write_text("")++ result = _collect_scripts(str(tmp_path))+ names = [os.path.basename(r) for r in result]+ assert names == ["01-setup.sh", "02-migrate.py", "03-cleanup.sh"]+++def test_collect_scripts_ignores_non_script_files(tmp_path):+ (tmp_path / "01-seed.sh").write_text("")+ (tmp_path / "02-migrate.py").write_text("")+ (tmp_path / "readme.md").write_text("")+ (tmp_path / "config.json").write_text("")+ (tmp_path / "notes.txt").write_text("")++ result = _collect_scripts(str(tmp_path))+ assert len(result) == 2+ names = [os.path.basename(r) for r in result]+ assert names == ["01-seed.sh", "02-migrate.py"]+++def test_init_scripts_uses_correct_interpreter(tmp_path, monkeypatch):+ sh_script = tmp_path / "01-setup.sh"+ py_script = tmp_path / "02-migrate.py"+ sh_script.write_text("#!/bin/sh\necho hi")+ py_script.write_text("print('hi')")++ monkeypatch.setattr(+ 'ministack.app._collect_scripts',+ lambda *a: [str(sh_script), str(py_script)]+ )++ calls = []++ def fake_run(cmd, **kwargs):+ calls.append(cmd)+ class Result:+ returncode = 0+ stdout = ""+ stderr = ""+ return Result()++ monkeypatch.setattr('subprocess.run', fake_run)++ _run_init_scripts()++ assert calls[0][0] == "sh"+ assert calls[0][1] == str(sh_script)+ assert calls[1][0] == sys.executable+ assert calls[1][1] == str(py_script)