python-ship 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- python_ship/__init__.py +3 -0
- python_ship/__main__.py +3 -0
- python_ship/adapters.py +270 -0
- python_ship/cli.py +588 -0
- python_ship/compose.py +439 -0
- python_ship/config.py +368 -0
- python_ship/contracts.py +234 -0
- python_ship/init.py +369 -0
- python_ship/mutagen.py +89 -0
- python_ship/plugins.py +149 -0
- python_ship/production.py +69 -0
- python_ship/py.typed +0 -0
- python_ship/release.py +175 -0
- python_ship/runtime.py +83 -0
- python_ship/security.py +69 -0
- python_ship/servers.py +72 -0
- python_ship/services.py +353 -0
- python_ship/storage.py +151 -0
- python_ship/stubs.py +149 -0
- python_ship/templates/Dockerfile +52 -0
- python_ship/templates/check-python-security-floor.py +49 -0
- python_ship/templates/garage/Dockerfile +7 -0
- python_ship/templates/garage/garage.toml +15 -0
- python_ship/uv.py +76 -0
- python_ship-0.1.0.dist-info/METADATA +936 -0
- python_ship-0.1.0.dist-info/RECORD +30 -0
- python_ship-0.1.0.dist-info/WHEEL +5 -0
- python_ship-0.1.0.dist-info/entry_points.txt +2 -0
- python_ship-0.1.0.dist-info/licenses/LICENSE.md +21 -0
- python_ship-0.1.0.dist-info/top_level.txt +1 -0
python_ship/services.py
ADDED
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
"""Built-in infrastructure services."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
import sys
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
from .contracts import ServiceContext, ServiceDefinition
|
|
11
|
+
from .mutagen import enabled as mutagen_enabled
|
|
12
|
+
from .production import read_env
|
|
13
|
+
from .storage import BUILTIN_STORAGE
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _healthcheck(test: list[str], **timing: Any) -> dict[str, Any]:
|
|
17
|
+
return {"test": test, "interval": "5s", "timeout": "3s", "retries": 10, **timing}
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _host_value(root: Path, key: str) -> str | None:
|
|
21
|
+
"""A value Compose would interpolate: the host environment first, then the project's .env."""
|
|
22
|
+
if key in os.environ:
|
|
23
|
+
return os.environ[key]
|
|
24
|
+
return read_env(root / ".env").get(key)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class PostgresService:
|
|
28
|
+
key = "postgres"
|
|
29
|
+
group = "database"
|
|
30
|
+
label = "PostgreSQL"
|
|
31
|
+
|
|
32
|
+
def compose_fragment(self, context: ServiceContext) -> dict[str, dict[str, Any]]:
|
|
33
|
+
return {context.service_name: {
|
|
34
|
+
"image": "postgres:17.12-alpine3.24",
|
|
35
|
+
"environment": {
|
|
36
|
+
"POSTGRES_DB": context.setting("DB_NAME", "app"),
|
|
37
|
+
"POSTGRES_USER": context.setting("DB_USER", "app"),
|
|
38
|
+
"POSTGRES_PASSWORD": context.credential("DB_PASSWORD", "ship"),
|
|
39
|
+
},
|
|
40
|
+
"volumes": [f"{context.resource_name}_data:/var/lib/postgresql/data"],
|
|
41
|
+
"healthcheck": _healthcheck(["CMD-SHELL", 'pg_isready -U "$$POSTGRES_USER" -d "$$POSTGRES_DB"']),
|
|
42
|
+
}}
|
|
43
|
+
|
|
44
|
+
def app_environment(self, context: ServiceContext) -> dict[str, str]:
|
|
45
|
+
prefix = context.env_prefix
|
|
46
|
+
return {
|
|
47
|
+
f"{prefix}DB_HOST": context.service_name,
|
|
48
|
+
f"{prefix}DB_PORT": "5432",
|
|
49
|
+
f"{prefix}DB_NAME": context.setting("DB_NAME", "app"),
|
|
50
|
+
f"{prefix}DB_USER": context.setting("DB_USER", "app"),
|
|
51
|
+
f"{prefix}DB_PASSWORD": context.credential("DB_PASSWORD", "ship"),
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
def database_shell(self, context: ServiceContext) -> list[str]:
|
|
55
|
+
return ["sh", "-c", 'exec psql -U "$POSTGRES_USER" -d "$POSTGRES_DB"']
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class MySqlService:
|
|
59
|
+
key = "mysql"
|
|
60
|
+
group = "database"
|
|
61
|
+
label = "MySQL"
|
|
62
|
+
|
|
63
|
+
def validate(self, context: ServiceContext) -> list[str]:
|
|
64
|
+
variable = context.env_prefix + "DB_USER"
|
|
65
|
+
if (_host_value(context.project_root, variable) or "app").lower() == "root":
|
|
66
|
+
return [
|
|
67
|
+
f"{variable}=root is invalid: the MySQL image refuses to start with it. Use a separate "
|
|
68
|
+
f"application user; the root account uses {context.env_prefix}DB_ROOT_PASSWORD."
|
|
69
|
+
]
|
|
70
|
+
return []
|
|
71
|
+
|
|
72
|
+
def compose_fragment(self, context: ServiceContext) -> dict[str, dict[str, Any]]:
|
|
73
|
+
return {context.service_name: {
|
|
74
|
+
"image": "mysql:9.7.4",
|
|
75
|
+
"environment": {
|
|
76
|
+
"MYSQL_DATABASE": context.setting("DB_NAME", "app"),
|
|
77
|
+
"MYSQL_USER": context.setting("DB_USER", "app"),
|
|
78
|
+
"MYSQL_PASSWORD": context.credential("DB_PASSWORD", "ship"),
|
|
79
|
+
"MYSQL_ROOT_PASSWORD": context.credential("DB_ROOT_PASSWORD", "rootship"),
|
|
80
|
+
},
|
|
81
|
+
"volumes": [f"{context.resource_name}_data:/var/lib/mysql"],
|
|
82
|
+
# A socket ping succeeds against the image's temporary init-phase server, before TCP
|
|
83
|
+
# connections are accepted, so ping over 127.0.0.1.
|
|
84
|
+
"healthcheck": _healthcheck(
|
|
85
|
+
["CMD-SHELL", 'mysqladmin ping -h 127.0.0.1 -u"$$MYSQL_USER" -p"$$MYSQL_PASSWORD" --silent'],
|
|
86
|
+
timeout="5s", retries=20, start_period="15s",
|
|
87
|
+
),
|
|
88
|
+
}}
|
|
89
|
+
|
|
90
|
+
def app_environment(self, context: ServiceContext) -> dict[str, str]:
|
|
91
|
+
prefix = context.env_prefix
|
|
92
|
+
return {
|
|
93
|
+
f"{prefix}DB_HOST": context.service_name,
|
|
94
|
+
f"{prefix}DB_PORT": "3306",
|
|
95
|
+
f"{prefix}DB_NAME": context.setting("DB_NAME", "app"),
|
|
96
|
+
f"{prefix}DB_USER": context.setting("DB_USER", "app"),
|
|
97
|
+
f"{prefix}DB_PASSWORD": context.credential("DB_PASSWORD", "ship"),
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
def database_shell(self, context: ServiceContext) -> list[str]:
|
|
101
|
+
return ["sh", "-c", 'exec mysql -u"$MYSQL_USER" -p"$MYSQL_PASSWORD" "$MYSQL_DATABASE"']
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
class RedisService:
|
|
105
|
+
key = "redis"
|
|
106
|
+
group = "cache"
|
|
107
|
+
label = "Redis"
|
|
108
|
+
|
|
109
|
+
def compose_fragment(self, context: ServiceContext) -> dict[str, dict[str, Any]]:
|
|
110
|
+
# RDB snapshots plus an append-only log: losing sessions on a redeploy logs out every user.
|
|
111
|
+
return {context.service_name: {
|
|
112
|
+
"image": "redis:8.10.3-alpine3.23",
|
|
113
|
+
"command": ["redis-server", "--save", "60", "1", "--appendonly", "yes"],
|
|
114
|
+
"volumes": [f"{context.resource_name}_data:/data"],
|
|
115
|
+
"healthcheck": _healthcheck(["CMD", "redis-cli", "ping"]),
|
|
116
|
+
}}
|
|
117
|
+
|
|
118
|
+
def app_environment(self, context: ServiceContext) -> dict[str, str]:
|
|
119
|
+
return {f"{context.env_prefix}REDIS_URL": f"redis://{context.service_name}:6379/0"}
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
class MailpitService:
|
|
123
|
+
key = "mailpit"
|
|
124
|
+
group = "mail"
|
|
125
|
+
label = "Mailpit"
|
|
126
|
+
|
|
127
|
+
def compose_fragment(self, context: ServiceContext) -> dict[str, dict[str, Any]]:
|
|
128
|
+
if context.production:
|
|
129
|
+
return {}
|
|
130
|
+
model: dict[str, Any] = {"image": "axllent/mailpit:v1.31.1"}
|
|
131
|
+
# Only the default instance publishes its web UI, so named instances never collide on a host port.
|
|
132
|
+
if context.instance_name is None:
|
|
133
|
+
model["ports"] = ["127.0.0.1:${MAILPIT_WEB_PORT:-8025}:8025"]
|
|
134
|
+
return {context.service_name: model}
|
|
135
|
+
|
|
136
|
+
def app_environment(self, context: ServiceContext) -> dict[str, str]:
|
|
137
|
+
prefix = context.env_prefix
|
|
138
|
+
return {f"{prefix}MAIL_HOST": context.service_name, f"{prefix}MAIL_PORT": "1025"}
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
class MeilisearchService:
|
|
142
|
+
key = "meilisearch"
|
|
143
|
+
group = "search"
|
|
144
|
+
label = "Meilisearch"
|
|
145
|
+
|
|
146
|
+
def compose_fragment(self, context: ServiceContext) -> dict[str, dict[str, Any]]:
|
|
147
|
+
return {context.service_name: {
|
|
148
|
+
"image": "getmeili/meilisearch:v1.56.2",
|
|
149
|
+
"environment": {
|
|
150
|
+
"MEILI_MASTER_KEY": context.credential("MEILISEARCH_KEY", "shipsearchkey"),
|
|
151
|
+
"MEILI_NO_ANALYTICS": "true",
|
|
152
|
+
"MEILI_ENV": "production" if context.production else "development",
|
|
153
|
+
},
|
|
154
|
+
"volumes": [f"{context.resource_name}_data:/meili_data"],
|
|
155
|
+
"healthcheck": _healthcheck(
|
|
156
|
+
["CMD-SHELL", "curl -fsS http://127.0.0.1:7700/health >/dev/null"], start_period="5s",
|
|
157
|
+
),
|
|
158
|
+
}}
|
|
159
|
+
|
|
160
|
+
def app_environment(self, context: ServiceContext) -> dict[str, str]:
|
|
161
|
+
prefix = context.env_prefix
|
|
162
|
+
return {
|
|
163
|
+
f"{prefix}MEILISEARCH_URL": f"http://{context.service_name}:7700",
|
|
164
|
+
f"{prefix}MEILISEARCH_KEY": context.credential("MEILISEARCH_KEY", "shipsearchkey"),
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
class RabbitMQService:
|
|
169
|
+
key = "rabbitmq"
|
|
170
|
+
group = "broker"
|
|
171
|
+
label = "RabbitMQ"
|
|
172
|
+
|
|
173
|
+
def compose_fragment(self, context: ServiceContext) -> dict[str, dict[str, Any]]:
|
|
174
|
+
model: dict[str, Any] = {
|
|
175
|
+
"image": "rabbitmq:4.3.7-management-alpine",
|
|
176
|
+
"environment": {
|
|
177
|
+
"RABBITMQ_DEFAULT_USER": context.setting("RABBITMQ_USER", "app"),
|
|
178
|
+
"RABBITMQ_DEFAULT_PASS": context.credential("RABBITMQ_PASSWORD", "ship"),
|
|
179
|
+
},
|
|
180
|
+
"volumes": [f"{context.resource_name}_data:/var/lib/rabbitmq"],
|
|
181
|
+
"healthcheck": _healthcheck(["CMD", "rabbitmq-diagnostics", "-q", "ping"],
|
|
182
|
+
interval="10s", timeout="10s", retries=12, start_period="20s"),
|
|
183
|
+
}
|
|
184
|
+
if context.environment.is_development and context.instance_name is None:
|
|
185
|
+
model["ports"] = ["127.0.0.1:${RABBITMQ_MANAGEMENT_PORT:-15672}:15672"]
|
|
186
|
+
return {context.service_name: model}
|
|
187
|
+
|
|
188
|
+
def app_environment(self, context: ServiceContext) -> dict[str, str]:
|
|
189
|
+
user = context.setting("RABBITMQ_USER", "app")
|
|
190
|
+
password = context.credential("RABBITMQ_PASSWORD", "ship")
|
|
191
|
+
return {f"{context.env_prefix}AMQP_URL": f"amqp://{user}:{password}@{context.service_name}:5672//"}
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
class CeleryService:
|
|
195
|
+
"""Celery workers from the app's own image, with a broker and a result backend wired in.
|
|
196
|
+
|
|
197
|
+
The broker is RabbitMQ when selected, otherwise Redis. Results go to Redis when it is
|
|
198
|
+
selected (chords and other Canvas primitives need a real result backend), else to RPC.
|
|
199
|
+
"""
|
|
200
|
+
|
|
201
|
+
key = "celery"
|
|
202
|
+
group = "tasks"
|
|
203
|
+
label = "Celery (worker and beat)"
|
|
204
|
+
|
|
205
|
+
def _redis(self, context: ServiceContext) -> str | None:
|
|
206
|
+
if context.config.services.get("cache") == "redis":
|
|
207
|
+
return context.config.service_names.get("redis", "redis")
|
|
208
|
+
return None
|
|
209
|
+
|
|
210
|
+
def _rabbitmq(self, context: ServiceContext) -> str | None:
|
|
211
|
+
if context.config.services.get("broker") == "rabbitmq":
|
|
212
|
+
return context.config.service_names.get("rabbitmq", "rabbitmq")
|
|
213
|
+
return None
|
|
214
|
+
|
|
215
|
+
def validate(self, context: ServiceContext) -> list[str]:
|
|
216
|
+
if self._redis(context) is None and self._rabbitmq(context) is None:
|
|
217
|
+
return ["Celery needs a broker: select RabbitMQ (`ship init --broker rabbitmq`) or Redis (`ship init --cache redis`)."]
|
|
218
|
+
return []
|
|
219
|
+
|
|
220
|
+
def _celery_app(self, context: ServiceContext) -> str:
|
|
221
|
+
module = context.config.app.partition(":")[0]
|
|
222
|
+
if context.config.framework == "django":
|
|
223
|
+
# project.asgi -> project, where Django projects define their Celery app.
|
|
224
|
+
return module.rsplit(".", 1)[0] if "." in module else module
|
|
225
|
+
return module
|
|
226
|
+
|
|
227
|
+
def compose_fragment(self, context: ServiceContext) -> dict[str, dict[str, Any]]:
|
|
228
|
+
# $$ reaches the container as $, so the shell expands the variables at start.
|
|
229
|
+
arguments = "-A $${CELERY_APP} worker --loglevel=$${CELERY_LOG_LEVEL:-info}"
|
|
230
|
+
worker = f"exec celery {arguments}"
|
|
231
|
+
if context.environment.is_development:
|
|
232
|
+
# Restart the worker on code changes when watchfiles is installed. It runs Celery's own
|
|
233
|
+
# entry point, because watchfiles doesn't expand variables in a command string.
|
|
234
|
+
worker = (f"if python -c 'import watchfiles' 2>/dev/null; then exec watchfiles --filter python "
|
|
235
|
+
f"--args \"{arguments}\" celery.__main__.main /app; else {worker}; fi")
|
|
236
|
+
beat = "exec celery -A $${CELERY_APP} beat --loglevel=$${CELERY_LOG_LEVEL:-info} --schedule /tmp/celerybeat-schedule"
|
|
237
|
+
return {
|
|
238
|
+
context.service_name: context.app_process(["sh", "-c", worker]),
|
|
239
|
+
f"{context.service_name}-beat": context.app_process(["sh", "-c", beat]),
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
def app_environment(self, context: ServiceContext) -> dict[str, str]:
|
|
243
|
+
redis = self._redis(context)
|
|
244
|
+
rabbitmq = self._rabbitmq(context)
|
|
245
|
+
if rabbitmq is not None:
|
|
246
|
+
user = context.setting("RABBITMQ_USER", "app")
|
|
247
|
+
password = context.credential("RABBITMQ_PASSWORD", "ship")
|
|
248
|
+
broker = f"amqp://{user}:{password}@{rabbitmq}:5672//"
|
|
249
|
+
else:
|
|
250
|
+
broker = f"redis://{redis}:6379/1"
|
|
251
|
+
return {
|
|
252
|
+
"CELERY_APP": context.setting("CELERY_APP", self._celery_app(context)),
|
|
253
|
+
"CELERY_BROKER_URL": broker,
|
|
254
|
+
"CELERY_RESULT_BACKEND": f"redis://{redis}:6379/2" if redis else "rpc://",
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
# Browsers force a single-label host such as "app" to HTTPS, so the app also gets this
|
|
259
|
+
# network alias in development whenever a browser-testing service is selected.
|
|
260
|
+
BROWSER_APP_HOST = "ship-app.test"
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
class SeleniumService:
|
|
264
|
+
key = "selenium"
|
|
265
|
+
group = "testing"
|
|
266
|
+
label = "Selenium (Chrome)"
|
|
267
|
+
|
|
268
|
+
def compose_fragment(self, context: ServiceContext) -> dict[str, dict[str, Any]]:
|
|
269
|
+
if context.production:
|
|
270
|
+
return {}
|
|
271
|
+
return {context.service_name: {
|
|
272
|
+
"image": "selenium/standalone-chrome:4.52.0-20261101",
|
|
273
|
+
"shm_size": "2gb",
|
|
274
|
+
"healthcheck": _healthcheck(
|
|
275
|
+
["CMD-SHELL", "/opt/bin/check-grid.sh --host 127.0.0.1 --port 4444"],
|
|
276
|
+
interval="10s", timeout="10s", retries=12, start_period="20s",
|
|
277
|
+
),
|
|
278
|
+
}}
|
|
279
|
+
|
|
280
|
+
def app_environment(self, context: ServiceContext) -> dict[str, str]:
|
|
281
|
+
return {
|
|
282
|
+
"SELENIUM_REMOTE_URL": f"http://{context.service_name}:4444/wd/hub",
|
|
283
|
+
"APP_URL": f"http://{BROWSER_APP_HOST}:8000",
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
class PlaywrightService:
|
|
288
|
+
key = "playwright"
|
|
289
|
+
group = "testing"
|
|
290
|
+
label = "Playwright (browser server)"
|
|
291
|
+
version = "1.64.1"
|
|
292
|
+
|
|
293
|
+
def compose_fragment(self, context: ServiceContext) -> dict[str, dict[str, Any]]:
|
|
294
|
+
if context.production:
|
|
295
|
+
return {}
|
|
296
|
+
probe = (
|
|
297
|
+
"const s=require('net').connect(3000,'127.0.0.1');"
|
|
298
|
+
"s.on('connect',()=>{s.end();process.exit(0)});s.on('error',()=>process.exit(1))"
|
|
299
|
+
)
|
|
300
|
+
return {context.service_name: {
|
|
301
|
+
"image": f"mcr.microsoft.com/playwright:v{self.version}-noble",
|
|
302
|
+
"init": True,
|
|
303
|
+
"user": "pwuser",
|
|
304
|
+
"working_dir": "/home/pwuser",
|
|
305
|
+
"shm_size": "2gb",
|
|
306
|
+
"command": ["/bin/sh", "-c", f"npx -y playwright@{self.version} run-server --port 3000 --host 0.0.0.0"],
|
|
307
|
+
"healthcheck": _healthcheck(
|
|
308
|
+
["CMD", "node", "-e", probe], interval="10s", timeout="5s", retries=12, start_period="30s",
|
|
309
|
+
),
|
|
310
|
+
}}
|
|
311
|
+
|
|
312
|
+
def app_environment(self, context: ServiceContext) -> dict[str, str]:
|
|
313
|
+
return {
|
|
314
|
+
"PLAYWRIGHT_WS_URL": f"ws://{context.service_name}:3000/",
|
|
315
|
+
"APP_URL": f"http://{BROWSER_APP_HOST}:8000",
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
class NodeService:
|
|
320
|
+
key = "node"
|
|
321
|
+
group = "frontend"
|
|
322
|
+
label = "Node.js (npm, Vite HMR)"
|
|
323
|
+
|
|
324
|
+
def compose_fragment(self, context: ServiceContext) -> dict[str, dict[str, Any]]:
|
|
325
|
+
if context.production:
|
|
326
|
+
return {}
|
|
327
|
+
environment = {"VITE_PORT": "${VITE_PORT:-5173}"}
|
|
328
|
+
if sys.platform == "win32" and not mutagen_enabled():
|
|
329
|
+
# Docker Desktop doesn't forward file events from Windows folders into containers,
|
|
330
|
+
# so Vite's watcher (chokidar) polls; under Mutagen the files are local and events work.
|
|
331
|
+
environment["CHOKIDAR_USEPOLLING"] = "true"
|
|
332
|
+
return {context.service_name: {
|
|
333
|
+
"image": "node:24.18.0-alpine3.24",
|
|
334
|
+
"init": True,
|
|
335
|
+
"working_dir": "/app",
|
|
336
|
+
"volumes": [".:/app", "node_modules:/app/node_modules"],
|
|
337
|
+
"command": ["sleep", "infinity"],
|
|
338
|
+
"ports": ["127.0.0.1:${VITE_PORT:-5173}:${VITE_PORT:-5173}"],
|
|
339
|
+
"environment": environment,
|
|
340
|
+
}}
|
|
341
|
+
|
|
342
|
+
def app_environment(self, context: ServiceContext) -> dict[str, str]:
|
|
343
|
+
return {}
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
BUILTIN_SERVICES: dict[str, ServiceDefinition] = {
|
|
347
|
+
service.key: service
|
|
348
|
+
for service in (
|
|
349
|
+
PostgresService(), MySqlService(), RedisService(), RabbitMQService(), CeleryService(), MailpitService(),
|
|
350
|
+
MeilisearchService(),
|
|
351
|
+
SeleniumService(), PlaywrightService(), NodeService(), *BUILTIN_STORAGE,
|
|
352
|
+
)
|
|
353
|
+
}
|
python_ship/storage.py
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
"""Built-in S3-compatible storage services."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from .contracts import ServiceContext
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def _s3_environment(context: ServiceContext, port: int, region: str, access_default: str, secret_default: str) -> dict[str, str]:
|
|
11
|
+
prefix = context.env_prefix
|
|
12
|
+
return {
|
|
13
|
+
f"{prefix}AWS_ENDPOINT": f"http://{context.service_name}:{port}",
|
|
14
|
+
f"{prefix}AWS_USE_PATH_STYLE_ENDPOINT": "true",
|
|
15
|
+
f"{prefix}AWS_DEFAULT_REGION": region,
|
|
16
|
+
f"{prefix}AWS_ACCESS_KEY_ID": context.setting("AWS_ACCESS_KEY_ID", access_default),
|
|
17
|
+
f"{prefix}AWS_SECRET_ACCESS_KEY": context.credential("AWS_SECRET_ACCESS_KEY", secret_default),
|
|
18
|
+
f"{prefix}AWS_BUCKET": context.setting("AWS_BUCKET", "local"),
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _healthcheck(test: list[str]) -> dict[str, Any]:
|
|
23
|
+
return {"test": test, "interval": "5s", "timeout": "5s", "retries": 12, "start_period": "10s"}
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class GarageService:
|
|
27
|
+
"""Provisions its default key and bucket on boot.
|
|
28
|
+
|
|
29
|
+
The final image has no shell, so the per-instance RPC address is substituted
|
|
30
|
+
into ``ship/garage/garage.toml`` at image build time instead.
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
key = "garage"
|
|
34
|
+
group = "storage"
|
|
35
|
+
label = "Garage"
|
|
36
|
+
|
|
37
|
+
def compose_fragment(self, context: ServiceContext) -> dict[str, dict[str, Any]]:
|
|
38
|
+
return {context.service_name: {
|
|
39
|
+
"build": {
|
|
40
|
+
"context": "./ship/garage",
|
|
41
|
+
"dockerfile": "Dockerfile",
|
|
42
|
+
"args": {"GARAGE_RPC_PUBLIC_ADDR": f"{context.service_name}:3901"},
|
|
43
|
+
},
|
|
44
|
+
"command": ["/garage", "server", "--single-node", "--default-access-key", "--default-bucket"],
|
|
45
|
+
"environment": {
|
|
46
|
+
"GARAGE_DEFAULT_ACCESS_KEY": context.setting("AWS_ACCESS_KEY_ID", "shipaccesskey"),
|
|
47
|
+
"GARAGE_DEFAULT_SECRET_KEY": context.credential("AWS_SECRET_ACCESS_KEY", "shipsecretplaceholder"),
|
|
48
|
+
"GARAGE_DEFAULT_BUCKET": context.setting("AWS_BUCKET", "local"),
|
|
49
|
+
"GARAGE_RPC_SECRET": context.credential("GARAGE_RPC_SECRET", "0" * 64),
|
|
50
|
+
"GARAGE_ADMIN_TOKEN": context.credential("GARAGE_ADMIN_TOKEN", "shipsecret"),
|
|
51
|
+
},
|
|
52
|
+
"volumes": [f"{context.resource_name}_data:/data", f"{context.resource_name}_meta:/meta"],
|
|
53
|
+
"healthcheck": _healthcheck(["CMD", "/garage", "status"]),
|
|
54
|
+
}}
|
|
55
|
+
|
|
56
|
+
def app_environment(self, context: ServiceContext) -> dict[str, str]:
|
|
57
|
+
return _s3_environment(context, 3900, "garage", "shipaccesskey", "shipsecretplaceholder")
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
# SeaweedFS's S3 gateway is unauthenticated without an identity file, so one is generated from the
|
|
61
|
+
# injected credentials at boot: JSON-escaped, and $$-escaped so Compose doesn't interpolate it.
|
|
62
|
+
_SEAWEED_IDENTITY = (
|
|
63
|
+
"mkdir -p /etc/seaweedfs"
|
|
64
|
+
" && ACCESS_KEY_ESC=$$(printf '%s' \"$$AWS_ACCESS_KEY_ID\" | sed 's/\\\\/\\\\\\\\/g; s/\"/\\\\\"/g')"
|
|
65
|
+
" && SECRET_KEY_ESC=$$(printf '%s' \"$$AWS_SECRET_ACCESS_KEY\" | sed 's/\\\\/\\\\\\\\/g; s/\"/\\\\\"/g')"
|
|
66
|
+
" && printf '{\"identities\":[{\"name\":\"ship\",\"credentials\":[{\"accessKey\":\"%s\",\"secretKey\":\"%s\"}],"
|
|
67
|
+
"\"actions\":[\"Admin\",\"Read\",\"Write\",\"List\",\"Tagging\"]}]}' \"$$ACCESS_KEY_ESC\" \"$$SECRET_KEY_ESC\""
|
|
68
|
+
" > /etc/seaweedfs/s3_identity.json"
|
|
69
|
+
" && exec weed server -s3 -s3.port=8333 -s3.config=/etc/seaweedfs/s3_identity.json -dir=/data"
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
class SeaweedFSService:
|
|
74
|
+
key = "seaweedfs"
|
|
75
|
+
group = "storage"
|
|
76
|
+
label = "SeaweedFS"
|
|
77
|
+
|
|
78
|
+
def compose_fragment(self, context: ServiceContext) -> dict[str, dict[str, Any]]:
|
|
79
|
+
return {context.service_name: {
|
|
80
|
+
"image": "chrislusf/seaweedfs:4.46",
|
|
81
|
+
"entrypoint": ["/bin/sh", "-c"],
|
|
82
|
+
"command": [_SEAWEED_IDENTITY],
|
|
83
|
+
"environment": {
|
|
84
|
+
"AWS_ACCESS_KEY_ID": context.setting("AWS_ACCESS_KEY_ID", "ship"),
|
|
85
|
+
"AWS_SECRET_ACCESS_KEY": context.credential("AWS_SECRET_ACCESS_KEY", "shipsecret"),
|
|
86
|
+
},
|
|
87
|
+
"volumes": [f"{context.resource_name}_data:/data"],
|
|
88
|
+
"healthcheck": {
|
|
89
|
+
**_healthcheck(["CMD", "wget", "-q", "-O", "-", "http://127.0.0.1:9333/cluster/status"]),
|
|
90
|
+
"interval": "10s", "retries": 10,
|
|
91
|
+
},
|
|
92
|
+
}}
|
|
93
|
+
|
|
94
|
+
def app_environment(self, context: ServiceContext) -> dict[str, str]:
|
|
95
|
+
return _s3_environment(context, 8333, "us-east-1", "ship", "shipsecret")
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
class RustFSService:
|
|
99
|
+
"""RustFS publishes no console port: its console currently returns AccessDenied (rustfs/rustfs#8013)."""
|
|
100
|
+
|
|
101
|
+
key = "rustfs"
|
|
102
|
+
group = "storage"
|
|
103
|
+
label = "RustFS"
|
|
104
|
+
|
|
105
|
+
def compose_fragment(self, context: ServiceContext) -> dict[str, dict[str, Any]]:
|
|
106
|
+
return {context.service_name: {
|
|
107
|
+
"image": "rustfs/rustfs:1.0.0",
|
|
108
|
+
"environment": {
|
|
109
|
+
"RUSTFS_ACCESS_KEY": context.setting("AWS_ACCESS_KEY_ID", "ship"),
|
|
110
|
+
"RUSTFS_SECRET_KEY": context.credential("AWS_SECRET_ACCESS_KEY", "shipsecret"),
|
|
111
|
+
},
|
|
112
|
+
"volumes": [f"{context.resource_name}_data:/data"],
|
|
113
|
+
"healthcheck": _healthcheck(["CMD", "curl", "-f", "http://127.0.0.1:9000/health"]),
|
|
114
|
+
}}
|
|
115
|
+
|
|
116
|
+
def app_environment(self, context: ServiceContext) -> dict[str, str]:
|
|
117
|
+
return _s3_environment(context, 9000, "us-east-1", "ship", "shipsecret")
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
class SiloService:
|
|
121
|
+
"""``siloImage`` picks the standard image (RHEL 9 base, needs an x86-64-v2 CPU) or the distroless one."""
|
|
122
|
+
|
|
123
|
+
key = "silo"
|
|
124
|
+
group = "storage"
|
|
125
|
+
label = "Silo"
|
|
126
|
+
release = "RELEASE.2026-09-16T00-00-00Z"
|
|
127
|
+
|
|
128
|
+
def compose_fragment(self, context: ServiceContext) -> dict[str, dict[str, Any]]:
|
|
129
|
+
suffix = "-distroless" if context.config.silo_image == "distroless" else ""
|
|
130
|
+
model: dict[str, Any] = {
|
|
131
|
+
"image": f"pgsty/silo:{self.release}{suffix}",
|
|
132
|
+
"command": ["server", "/data", "--console-address", ":9001"],
|
|
133
|
+
"environment": {
|
|
134
|
+
"MINIO_ROOT_USER": context.setting("AWS_ACCESS_KEY_ID", "ship"),
|
|
135
|
+
"MINIO_ROOT_PASSWORD": context.credential("AWS_SECRET_ACCESS_KEY", "shipsecret"),
|
|
136
|
+
},
|
|
137
|
+
"volumes": [f"{context.resource_name}_data:/data"],
|
|
138
|
+
# The distroless image has no curl; Silo's own probe works in both.
|
|
139
|
+
"healthcheck": _healthcheck(["CMD", "/usr/bin/silo", "healthcheck", "live"]),
|
|
140
|
+
}
|
|
141
|
+
if context.environment.is_development:
|
|
142
|
+
variable = f"{context.env_prefix}SILO_CONSOLE_PORT"
|
|
143
|
+
default = "9001" if context.instance_name is None else "0"
|
|
144
|
+
model["ports"] = [f"127.0.0.1:${{{variable}:-{default}}}:9001"]
|
|
145
|
+
return {context.service_name: model}
|
|
146
|
+
|
|
147
|
+
def app_environment(self, context: ServiceContext) -> dict[str, str]:
|
|
148
|
+
return _s3_environment(context, 9000, "us-east-1", "ship", "shipsecret")
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
BUILTIN_STORAGE = (GarageService(), SeaweedFSService(), RustFSService(), SiloService())
|
python_ship/stubs.py
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
"""The ship/ directory `ship init` publishes, and checks that it is current."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from importlib.resources import files
|
|
6
|
+
import json
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
from . import __version__
|
|
10
|
+
from .config import ShipConfig
|
|
11
|
+
from .uv import UV_VERSION
|
|
12
|
+
|
|
13
|
+
FLOOR_SCRIPT = "check-python-security-floor.py"
|
|
14
|
+
ASSET_STAGE = """FROM node:24.18.0-alpine3.24 AS assets
|
|
15
|
+
WORKDIR /assets
|
|
16
|
+
COPY package.json package-lock.json ./
|
|
17
|
+
RUN npm ci
|
|
18
|
+
COPY . .
|
|
19
|
+
RUN npm run build && test -d dist && mkdir /ship-assets && cp -a dist/. /ship-assets/
|
|
20
|
+
"""
|
|
21
|
+
ASSET_COPY = "COPY --from=assets /ship-assets/ /app/dist/"
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _template(*parts: str) -> str:
|
|
25
|
+
return files("python_ship").joinpath("templates", *parts).read_text(encoding="utf-8")
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def render_dockerfile(config: ShipConfig) -> str:
|
|
29
|
+
return (
|
|
30
|
+
_template("Dockerfile")
|
|
31
|
+
.replace("@UV_VERSION@", UV_VERSION)
|
|
32
|
+
.replace("@ASSET_STAGE@", ASSET_STAGE if config.node_assets else "")
|
|
33
|
+
.replace("@ASSET_COPY@", ASSET_COPY if config.node_assets else "")
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def uses_garage(config: ShipConfig) -> bool:
|
|
38
|
+
return config.services.get("storage") == "garage" or any(
|
|
39
|
+
entry.service == "garage" for entry in config.additional_services
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def publish_stubs(root: Path, config: ShipConfig, *, force: bool) -> bool:
|
|
44
|
+
"""Publish ship/. Returns whether the Dockerfile was (re)published.
|
|
45
|
+
|
|
46
|
+
The Dockerfile is the project's to edit, so it is only replaced with --force.
|
|
47
|
+
The security floor check is Ship's own and always kept current.
|
|
48
|
+
"""
|
|
49
|
+
ship = root / "ship"
|
|
50
|
+
ship.mkdir(exist_ok=True)
|
|
51
|
+
dockerfile = ship / "Dockerfile"
|
|
52
|
+
published = force or not dockerfile.exists()
|
|
53
|
+
if published:
|
|
54
|
+
dockerfile.write_text(render_dockerfile(config), encoding="utf-8", newline="\n")
|
|
55
|
+
(ship / ".ship-version").write_text(__version__ + "\n", encoding="utf-8")
|
|
56
|
+
(ship / FLOOR_SCRIPT).write_text(_template(FLOOR_SCRIPT), encoding="utf-8", newline="\n")
|
|
57
|
+
if uses_garage(config):
|
|
58
|
+
garage = ship / "garage"
|
|
59
|
+
garage.mkdir(exist_ok=True)
|
|
60
|
+
for name in ("Dockerfile", "garage.toml"):
|
|
61
|
+
target = garage / name
|
|
62
|
+
if force or not target.exists():
|
|
63
|
+
target.write_text(_template("garage", name), encoding="utf-8", newline="\n")
|
|
64
|
+
return published
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def published_version(root: Path) -> str | None:
|
|
68
|
+
path = root / "ship" / ".ship-version"
|
|
69
|
+
return path.read_text(encoding="utf-8").strip() if path.is_file() else None
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def stub_version_warning(root: Path) -> str | None:
|
|
73
|
+
version = published_version(root)
|
|
74
|
+
if version != __version__:
|
|
75
|
+
return (
|
|
76
|
+
f"ship/Dockerfile was published by python-ship {version or 'an unknown version'}, "
|
|
77
|
+
f"but {__version__} is installed. Run `ship init --force` to republish it "
|
|
78
|
+
"(commit or back up any edits first)."
|
|
79
|
+
)
|
|
80
|
+
return None
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def _dockerfile(root: Path) -> str | None:
|
|
84
|
+
path = root / "ship" / "Dockerfile"
|
|
85
|
+
return path.read_text(encoding="utf-8") if path.is_file() else None
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def server_stub_problem(root: Path, config: ShipConfig) -> str | None:
|
|
89
|
+
text = _dockerfile(root)
|
|
90
|
+
if text is not None and (config.server or "uvicorn") != "uvicorn" and "SHIP_SERVER_REQUIREMENT" not in text:
|
|
91
|
+
return "ship/Dockerfile cannot install the selected web server; run `ship init --force` to refresh it."
|
|
92
|
+
return None
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def asset_stub_problem(root: Path, config: ShipConfig) -> str | None:
|
|
96
|
+
text = _dockerfile(root)
|
|
97
|
+
if config.node_assets and text is not None and "AS assets" not in text:
|
|
98
|
+
return "nodeAssets requires a refreshed ship/Dockerfile; run `ship init --force`."
|
|
99
|
+
return None
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def stub_problems(root: Path, config: ShipConfig | None) -> list[str]:
|
|
103
|
+
"""Problems with the published ship/ directory that would break a build."""
|
|
104
|
+
text = _dockerfile(root)
|
|
105
|
+
if text is None:
|
|
106
|
+
return ["ship/Dockerfile is missing; run `ship init`."]
|
|
107
|
+
problems = []
|
|
108
|
+
if FLOOR_SCRIPT in text and not (root / "ship" / FLOOR_SCRIPT).is_file():
|
|
109
|
+
problems.append(f"ship/{FLOOR_SCRIPT} is missing; run `ship init` to publish it.")
|
|
110
|
+
if config is None:
|
|
111
|
+
return problems
|
|
112
|
+
for problem in (server_stub_problem(root, config), asset_stub_problem(root, config)):
|
|
113
|
+
if problem:
|
|
114
|
+
problems.append(problem)
|
|
115
|
+
if uses_garage(config):
|
|
116
|
+
problems.extend(
|
|
117
|
+
f"ship/garage/{name} is missing; run `ship init` to publish Garage's build files."
|
|
118
|
+
for name in ("Dockerfile", "garage.toml")
|
|
119
|
+
if not (root / "ship" / "garage" / name).is_file()
|
|
120
|
+
)
|
|
121
|
+
return problems
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def production_stub_problem(root: Path) -> str | None:
|
|
125
|
+
text = _dockerfile(root)
|
|
126
|
+
if text is None or "AS production" not in text:
|
|
127
|
+
return "ship/Dockerfile has no production stage; run `ship init --force` to refresh it."
|
|
128
|
+
return None
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def asset_problems(root: Path, config: ShipConfig) -> list[str]:
|
|
132
|
+
"""What a nodeAssets production build needs from the project itself."""
|
|
133
|
+
if not config.node_assets:
|
|
134
|
+
return []
|
|
135
|
+
problems = []
|
|
136
|
+
package = root / "package.json"
|
|
137
|
+
if not package.is_file():
|
|
138
|
+
problems.append("nodeAssets requires package.json with a build script.")
|
|
139
|
+
else:
|
|
140
|
+
try:
|
|
141
|
+
data = json.loads(package.read_text(encoding="utf-8"))
|
|
142
|
+
scripts = data.get("scripts") if isinstance(data, dict) else None
|
|
143
|
+
if not isinstance(scripts, dict) or not isinstance(scripts.get("build"), str):
|
|
144
|
+
problems.append("nodeAssets requires a \"build\" script in package.json.")
|
|
145
|
+
except (OSError, ValueError):
|
|
146
|
+
problems.append("nodeAssets requires a valid package.json.")
|
|
147
|
+
if not (root / "package-lock.json").is_file():
|
|
148
|
+
problems.append("nodeAssets requires package-lock.json, for a reproducible `npm ci`.")
|
|
149
|
+
return problems
|