Group deployment files by deployment method
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
output="$PWD/target/bedrock-release"
|
||||
mkdir -p "$output"
|
||||
docker run --rm \
|
||||
--mount "type=bind,source=$PWD,target=/work,readonly" \
|
||||
--mount "type=bind,source=$output,target=/out" \
|
||||
-w /work -e CARGO_TARGET_DIR=/tmp/bedrock-target \
|
||||
rust:1.91.1-bookworm sh -ec '
|
||||
cargo build --locked --release --bin bedrock
|
||||
install -m 755 /tmp/bedrock-target/release/bedrock /out/bedrock
|
||||
'
|
||||
@@ -0,0 +1,50 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
umask 077
|
||||
|
||||
binary=${1:?Path to the built Bedrock executable is required}
|
||||
revision=${2:?Git commit is required}
|
||||
if [[ ! $revision =~ ^[0-9a-f]{40}$ || ! -x $binary ]]; then
|
||||
printf 'Invalid release commit or executable\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
infra=/opt/endoleap-infra
|
||||
root=$infra/bedrock-rust
|
||||
network=endoleap-dev_default
|
||||
test -d "$infra"
|
||||
test ! -L "$root"
|
||||
docker network inspect "$network" >/dev/null
|
||||
install -d -m 700 "$root/releases"
|
||||
|
||||
stage=$(mktemp -d "$root/releases/.staging.XXXXXXXX")
|
||||
cleanup() { rm -rf -- "$stage"; }
|
||||
trap cleanup EXIT
|
||||
install -m 755 "$binary" "$stage/bedrock"
|
||||
release="$revision.${stage##*.}"
|
||||
python3 "$(dirname "$0")/render-postgres.py" "$stage/bedrock.yaml" \
|
||||
--deployment production --config-version "$release"
|
||||
|
||||
# The current binary is a one-shot storage initializer. It publishes no ports.
|
||||
docker run --rm --network "$network" \
|
||||
--user "$(id -u):$(id -g)" --read-only --cap-drop ALL --security-opt no-new-privileges \
|
||||
--mount "type=bind,source=$stage,target=/release,readonly" \
|
||||
-e BEDROCK_DB_CONFIG=/release/bedrock.yaml \
|
||||
debian:bookworm-slim /release/bedrock
|
||||
|
||||
# Keep the final release private; only switch current after initialization succeeds.
|
||||
python3 - "$stage" "$root" "$release" <<'PY'
|
||||
import os
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import tempfile
|
||||
|
||||
stage, root, release = Path(sys.argv[1]), Path(sys.argv[2]), sys.argv[3]
|
||||
destination = Path("releases") / release
|
||||
stage.rename(root / destination)
|
||||
with tempfile.TemporaryDirectory(prefix=".activate.", dir=root) as activation:
|
||||
link = Path(activation) / "current"
|
||||
link.symlink_to(destination)
|
||||
os.replace(link, root / "current")
|
||||
PY
|
||||
printf 'Published Bedrock Rust release %s\n' "$release"
|
||||
@@ -0,0 +1,78 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Render the private Bedrock PostgreSQL config at deploy time."""
|
||||
|
||||
import argparse
|
||||
import ipaddress
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import tempfile
|
||||
from urllib.parse import quote
|
||||
|
||||
|
||||
def required(variable):
|
||||
value = os.environ.get(variable)
|
||||
if not value:
|
||||
raise SystemExit(f"Missing {variable}")
|
||||
return value
|
||||
|
||||
|
||||
def database_url():
|
||||
host = required("BEDROCK_PG_HOST").removeprefix("[").removesuffix("]")
|
||||
if ":" in host:
|
||||
try:
|
||||
host = f"[{ipaddress.IPv6Address(host)}]"
|
||||
except ValueError:
|
||||
raise SystemExit("Invalid BEDROCK_PG_HOST") from None
|
||||
elif not re.fullmatch(r"[A-Za-z0-9.-]+", host):
|
||||
raise SystemExit("Invalid BEDROCK_PG_HOST")
|
||||
|
||||
try:
|
||||
port = int(required("BEDROCK_PG_PORT"))
|
||||
if not 1 <= port <= 65535:
|
||||
raise ValueError
|
||||
except ValueError:
|
||||
raise SystemExit("Invalid BEDROCK_PG_PORT") from None
|
||||
|
||||
user = quote(required("BEDROCK_PG_USER"), safe="")
|
||||
password = quote(required("BEDROCK_PG_PASSWORD"), safe="")
|
||||
database = quote(required("BEDROCK_PG_DATABASE"), safe="")
|
||||
return f"postgres://{user}:{password}@{host}:{port}/{database}"
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("output", type=Path)
|
||||
parser.add_argument("--deployment", default="default")
|
||||
parser.add_argument("--config-version", default="local")
|
||||
options = parser.parse_args()
|
||||
if not re.fullmatch(r"[A-Za-z0-9._-]{1,128}", options.deployment):
|
||||
parser.error("invalid deployment name")
|
||||
if not re.fullmatch(r"[A-Za-z0-9._-]{1,128}", options.config_version):
|
||||
parser.error("invalid config version")
|
||||
# JSON quoting is valid for scalars in YAML and handles special characters.
|
||||
config = (
|
||||
f"deployment: {json.dumps(options.deployment)}\n"
|
||||
f"config_version: {json.dumps(options.config_version)}\n"
|
||||
"bindings:\n"
|
||||
" documents: postgres\n"
|
||||
"resources:\n"
|
||||
" postgres:\n"
|
||||
" kind: sql\n"
|
||||
" driver: postgres\n"
|
||||
f" primary: {json.dumps(database_url())}\n"
|
||||
)
|
||||
|
||||
# Write with owner-only permissions, then replace the destination atomically.
|
||||
file = tempfile.NamedTemporaryFile(mode="w", dir=options.output.parent, delete=False)
|
||||
try:
|
||||
with file:
|
||||
file.write(config)
|
||||
os.replace(file.name, options.output)
|
||||
finally:
|
||||
Path(file.name).unlink(missing_ok=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user