fix: deploy verified images atomically
This commit is contained in:
@@ -65,9 +65,8 @@ jobs:
|
|||||||
cp lab4.env.example .env
|
cp lab4.env.example .env
|
||||||
docker compose config --quiet
|
docker compose config --quiet
|
||||||
docker compose up -d --build --scale api=2 --wait --wait-timeout 150
|
docker compose up -d --build --scale api=2 --wait --wait-timeout 150
|
||||||
network=$(docker inspect --format '{{range $name, $_ := .NetworkSettings.Networks}}{{$name}}{{end}}' "$(docker compose ps -q nginx)")
|
docker network connect "${COMPOSE_PROJECT_NAME}_application" "$HOSTNAME"
|
||||||
docker network connect "$network" "$HOSTNAME"
|
docker network connect "${COMPOSE_PROJECT_NAME}_data" "$HOSTNAME"
|
||||||
printf '%s\n' "$network" > .ci-network
|
|
||||||
|
|
||||||
- name: Integration checks
|
- name: Integration checks
|
||||||
run: |
|
run: |
|
||||||
@@ -89,5 +88,6 @@ jobs:
|
|||||||
- name: Stop local stack
|
- name: Stop local stack
|
||||||
if: always()
|
if: always()
|
||||||
run: |
|
run: |
|
||||||
if [ -f .ci-network ]; then docker network disconnect "$(cat .ci-network)" "$HOSTNAME"; fi
|
docker network disconnect "${COMPOSE_PROJECT_NAME}_application" "$HOSTNAME" 2>/dev/null || true
|
||||||
|
docker network disconnect "${COMPOSE_PROJECT_NAME}_data" "$HOSTNAME" 2>/dev/null || true
|
||||||
if [ -f .env ]; then docker compose --profile tests down -v --remove-orphans; fi
|
if [ -f .env ]; then docker compose --profile tests down -v --remove-orphans; fi
|
||||||
|
|||||||
@@ -13,12 +13,28 @@ jobs:
|
|||||||
if: github.ref == 'refs/heads/main'
|
if: github.ref == 'refs/heads/main'
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
timeout-minutes: 45
|
timeout-minutes: 45
|
||||||
|
outputs:
|
||||||
|
api_image: ${{ steps.publish.outputs.api_image }}
|
||||||
|
nginx_image: ${{ steps.publish.outputs.nginx_image }}
|
||||||
|
swagger_image: ${{ steps.publish.outputs.swagger_image }}
|
||||||
|
prometheus_image: ${{ steps.publish.outputs.prometheus_image }}
|
||||||
|
grafana_image: ${{ steps.publish.outputs.grafana_image }}
|
||||||
|
s3_image: ${{ steps.publish.outputs.s3_image }}
|
||||||
|
s3_bootstrap_image: ${{ steps.publish.outputs.s3_bootstrap_image }}
|
||||||
env:
|
env:
|
||||||
COMPOSE_PROJECT_NAME: mtusi2026lab4-deploy-${{ github.run_id }}
|
COMPOSE_PROJECT_NAME: mtusi2026lab4-deploy-${{ github.run_id }}
|
||||||
|
COMPOSE_FILE: compose.yaml:deploy/compose.images.yaml
|
||||||
API_BIND_ADDRESS: 127.0.0.1:0
|
API_BIND_ADDRESS: 127.0.0.1:0
|
||||||
SWAGGER_BIND_ADDRESS: 127.0.0.1:0
|
SWAGGER_BIND_ADDRESS: 127.0.0.1:0
|
||||||
PROMETHEUS_BIND_ADDRESS: 127.0.0.1:0
|
PROMETHEUS_BIND_ADDRESS: 127.0.0.1:0
|
||||||
GRAFANA_BIND_ADDRESS: 127.0.0.1:0
|
GRAFANA_BIND_ADDRESS: 127.0.0.1:0
|
||||||
|
API_IMAGE: gitea.privcloud.tech/${{ github.repository_owner }}/mtusi2026lab4-api:${{ github.sha }}
|
||||||
|
NGINX_IMAGE: gitea.privcloud.tech/${{ github.repository_owner }}/mtusi2026lab4-nginx:${{ github.sha }}
|
||||||
|
SWAGGER_IMAGE: gitea.privcloud.tech/${{ github.repository_owner }}/mtusi2026lab4-swagger:${{ github.sha }}
|
||||||
|
PROMETHEUS_IMAGE: gitea.privcloud.tech/${{ github.repository_owner }}/mtusi2026lab4-prometheus:${{ github.sha }}
|
||||||
|
GRAFANA_IMAGE: gitea.privcloud.tech/${{ github.repository_owner }}/mtusi2026lab4-grafana:${{ github.sha }}
|
||||||
|
S3_IMAGE: gitea.privcloud.tech/${{ github.repository_owner }}/mtusi2026lab4-minio:${{ github.sha }}
|
||||||
|
S3_BOOTSTRAP_IMAGE: gitea.privcloud.tech/${{ github.repository_owner }}/mtusi2026lab4-minio-client:${{ github.sha }}
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout exact commit
|
- name: Checkout exact commit
|
||||||
run: |
|
run: |
|
||||||
@@ -63,9 +79,8 @@ jobs:
|
|||||||
cp lab4.env.example .env
|
cp lab4.env.example .env
|
||||||
docker compose config --quiet
|
docker compose config --quiet
|
||||||
docker compose up -d --build --scale api=2 --wait --wait-timeout 150
|
docker compose up -d --build --scale api=2 --wait --wait-timeout 150
|
||||||
network=$(docker inspect --format '{{range $name, $_ := .NetworkSettings.Networks}}{{$name}}{{end}}' "$(docker compose ps -q nginx)")
|
docker network connect "${COMPOSE_PROJECT_NAME}_application" "$HOSTNAME"
|
||||||
docker network connect "$network" "$HOSTNAME"
|
docker network connect "${COMPOSE_PROJECT_NAME}_data" "$HOSTNAME"
|
||||||
printf '%s\n' "$network" > .ci-network
|
|
||||||
|
|
||||||
- name: Integration checks
|
- name: Integration checks
|
||||||
run: |
|
run: |
|
||||||
@@ -79,6 +94,33 @@ jobs:
|
|||||||
make test-persistence ACTION_SCRIPT=scripts/restart.sh BASE_URL=http://nginx:8080
|
make test-persistence ACTION_SCRIPT=scripts/restart.sh BASE_URL=http://nginx:8080
|
||||||
make test-failover ACTION_SCRIPT=scripts/stop-one.sh BASE_URL=http://nginx:8080
|
make test-failover ACTION_SCRIPT=scripts/stop-one.sh BASE_URL=http://nginx:8080
|
||||||
|
|
||||||
|
- name: Publish verified images
|
||||||
|
id: publish
|
||||||
|
env:
|
||||||
|
REGISTRY_USERNAME: ${{ secrets.REGISTRY_USERNAME }}
|
||||||
|
REGISTRY_PASSWORD: ${{ secrets.REGISTRY_PASSWORD }}
|
||||||
|
run: |
|
||||||
|
printf '%s' "$REGISTRY_PASSWORD" | docker login gitea.privcloud.tech --username "$REGISTRY_USERNAME" --password-stdin
|
||||||
|
|
||||||
|
publish() {
|
||||||
|
output=$1
|
||||||
|
image=$2
|
||||||
|
push_output=$(docker push "$image")
|
||||||
|
printf '%s\n' "$push_output"
|
||||||
|
digest=$(printf '%s\n' "$push_output" | sed -n 's/^.*digest: \(sha256:[0-9a-f]*\).*$/\1/p' | tail -n 1)
|
||||||
|
test -n "$digest"
|
||||||
|
repository=${image%:*}
|
||||||
|
printf '%s=%s@%s\n' "$output" "$repository" "$digest" >> "$GITHUB_OUTPUT"
|
||||||
|
}
|
||||||
|
|
||||||
|
publish api_image "$API_IMAGE"
|
||||||
|
publish nginx_image "$NGINX_IMAGE"
|
||||||
|
publish swagger_image "$SWAGGER_IMAGE"
|
||||||
|
publish prometheus_image "$PROMETHEUS_IMAGE"
|
||||||
|
publish grafana_image "$GRAFANA_IMAGE"
|
||||||
|
publish s3_image "$S3_IMAGE"
|
||||||
|
publish s3_bootstrap_image "$S3_BOOTSTRAP_IMAGE"
|
||||||
|
|
||||||
- name: Container logs
|
- name: Container logs
|
||||||
if: failure()
|
if: failure()
|
||||||
run: if [ -f .env ]; then docker compose logs --no-color --tail=200; fi
|
run: if [ -f .env ]; then docker compose logs --no-color --tail=200; fi
|
||||||
@@ -86,7 +128,8 @@ jobs:
|
|||||||
- name: Stop local stack
|
- name: Stop local stack
|
||||||
if: always()
|
if: always()
|
||||||
run: |
|
run: |
|
||||||
if [ -f .ci-network ]; then docker network disconnect "$(cat .ci-network)" "$HOSTNAME"; fi
|
docker network disconnect "${COMPOSE_PROJECT_NAME}_application" "$HOSTNAME" 2>/dev/null || true
|
||||||
|
docker network disconnect "${COMPOSE_PROJECT_NAME}_data" "$HOSTNAME" 2>/dev/null || true
|
||||||
if [ -f .env ]; then docker compose --profile tests down -v --remove-orphans; fi
|
if [ -f .env ]; then docker compose --profile tests down -v --remove-orphans; fi
|
||||||
|
|
||||||
deploy:
|
deploy:
|
||||||
@@ -99,6 +142,8 @@ jobs:
|
|||||||
DEPLOY_USER: ${{ secrets.DEPLOY_USER }}
|
DEPLOY_USER: ${{ secrets.DEPLOY_USER }}
|
||||||
DEPLOY_SSH_KEY: ${{ secrets.DEPLOY_SSH_KEY }}
|
DEPLOY_SSH_KEY: ${{ secrets.DEPLOY_SSH_KEY }}
|
||||||
DEPLOY_KNOWN_HOSTS: ${{ secrets.DEPLOY_KNOWN_HOSTS }}
|
DEPLOY_KNOWN_HOSTS: ${{ secrets.DEPLOY_KNOWN_HOSTS }}
|
||||||
|
REGISTRY_USERNAME: ${{ secrets.REGISTRY_USERNAME }}
|
||||||
|
REGISTRY_PASSWORD: ${{ secrets.REGISTRY_PASSWORD }}
|
||||||
DEPLOY_SHA: ${{ github.sha }}
|
DEPLOY_SHA: ${{ github.sha }}
|
||||||
steps:
|
steps:
|
||||||
- name: Configure SSH
|
- name: Configure SSH
|
||||||
@@ -108,6 +153,24 @@ jobs:
|
|||||||
printf '%s\n' "$DEPLOY_SSH_KEY" > ~/.ssh/id_ed25519
|
printf '%s\n' "$DEPLOY_SSH_KEY" > ~/.ssh/id_ed25519
|
||||||
printf '%s\n' "$DEPLOY_KNOWN_HOSTS" > ~/.ssh/known_hosts
|
printf '%s\n' "$DEPLOY_KNOWN_HOSTS" > ~/.ssh/known_hosts
|
||||||
|
|
||||||
|
- name: Prepare immutable image manifest
|
||||||
|
run: |
|
||||||
|
cat > deploy-images.env <<EOF
|
||||||
|
API_IMAGE=${{ needs.verify.outputs.api_image }}
|
||||||
|
NGINX_IMAGE=${{ needs.verify.outputs.nginx_image }}
|
||||||
|
SWAGGER_IMAGE=${{ needs.verify.outputs.swagger_image }}
|
||||||
|
PROMETHEUS_IMAGE=${{ needs.verify.outputs.prometheus_image }}
|
||||||
|
GRAFANA_IMAGE=${{ needs.verify.outputs.grafana_image }}
|
||||||
|
S3_IMAGE=${{ needs.verify.outputs.s3_image }}
|
||||||
|
S3_BOOTSTRAP_IMAGE=${{ needs.verify.outputs.s3_bootstrap_image }}
|
||||||
|
EOF
|
||||||
|
|
||||||
|
- name: Authenticate registry on deployment host
|
||||||
|
run: printf '%s' "$REGISTRY_PASSWORD" | ssh "$DEPLOY_USER@$DEPLOY_HOST" "docker login gitea.privcloud.tech --username '$REGISTRY_USERNAME' --password-stdin"
|
||||||
|
|
||||||
|
- name: Upload image manifest
|
||||||
|
run: scp deploy-images.env "$DEPLOY_USER@$DEPLOY_HOST:/tmp/mtusi2026lab4-${{ github.run_id }}.env"
|
||||||
|
|
||||||
- name: Deploy and verify
|
- name: Deploy and verify
|
||||||
run: |
|
run: |
|
||||||
ssh "$DEPLOY_USER@$DEPLOY_HOST" "
|
ssh "$DEPLOY_USER@$DEPLOY_HOST" "
|
||||||
@@ -116,8 +179,14 @@ jobs:
|
|||||||
git fetch origin main
|
git fetch origin main
|
||||||
git cat-file -e '$DEPLOY_SHA^{commit}'
|
git cat-file -e '$DEPLOY_SHA^{commit}'
|
||||||
git reset --hard '$DEPLOY_SHA'
|
git reset --hard '$DEPLOY_SHA'
|
||||||
git clean -fd
|
manifest='/tmp/mtusi2026lab4-${{ github.run_id }}.env'
|
||||||
scripts/rolling-update.sh
|
if scripts/rolling-update.sh \"\$manifest\"; then
|
||||||
|
rm -f \"\$manifest\"
|
||||||
|
else
|
||||||
|
status=\$?
|
||||||
|
rm -f \"\$manifest\"
|
||||||
|
exit \"\$status\"
|
||||||
|
fi
|
||||||
test \"\$(git rev-parse HEAD)\" = '$DEPLOY_SHA'
|
test \"\$(git rev-parse HEAD)\" = '$DEPLOY_SHA'
|
||||||
docker compose ps
|
docker compose ps
|
||||||
"
|
"
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ services:
|
|||||||
cpus: ${POSTGRES_CPUS}
|
cpus: ${POSTGRES_CPUS}
|
||||||
mem_limit: ${POSTGRES_MEMORY}
|
mem_limit: ${POSTGRES_MEMORY}
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
networks: [data]
|
||||||
|
|
||||||
valkey:
|
valkey:
|
||||||
image: valkey/valkey:8.1.4-alpine
|
image: valkey/valkey:8.1.4-alpine
|
||||||
@@ -31,6 +32,7 @@ services:
|
|||||||
cpus: ${VALKEY_CPUS}
|
cpus: ${VALKEY_CPUS}
|
||||||
mem_limit: ${VALKEY_MEMORY}
|
mem_limit: ${VALKEY_MEMORY}
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
networks: [data]
|
||||||
|
|
||||||
s3:
|
s3:
|
||||||
build:
|
build:
|
||||||
@@ -57,6 +59,7 @@ services:
|
|||||||
cpus: ${S3_CPUS}
|
cpus: ${S3_CPUS}
|
||||||
mem_limit: ${S3_MEMORY}
|
mem_limit: ${S3_MEMORY}
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
networks: [data]
|
||||||
|
|
||||||
s3-bootstrap:
|
s3-bootstrap:
|
||||||
build:
|
build:
|
||||||
@@ -80,6 +83,7 @@ services:
|
|||||||
cpus: ${S3_BOOTSTRAP_CPUS}
|
cpus: ${S3_BOOTSTRAP_CPUS}
|
||||||
mem_limit: ${S3_BOOTSTRAP_MEMORY}
|
mem_limit: ${S3_BOOTSTRAP_MEMORY}
|
||||||
restart: "no"
|
restart: "no"
|
||||||
|
networks: [data]
|
||||||
|
|
||||||
migrate:
|
migrate:
|
||||||
image: ${API_IMAGE:-mtusi-messenger:lab4}
|
image: ${API_IMAGE:-mtusi-messenger:lab4}
|
||||||
@@ -101,6 +105,7 @@ services:
|
|||||||
cpus: ${MIGRATE_CPUS}
|
cpus: ${MIGRATE_CPUS}
|
||||||
mem_limit: ${MIGRATE_MEMORY}
|
mem_limit: ${MIGRATE_MEMORY}
|
||||||
restart: "no"
|
restart: "no"
|
||||||
|
networks: [data]
|
||||||
|
|
||||||
api:
|
api:
|
||||||
build: .
|
build: .
|
||||||
@@ -140,6 +145,7 @@ services:
|
|||||||
S3_TIMEOUT_SECONDS: ${S3_TIMEOUT_SECONDS}
|
S3_TIMEOUT_SECONDS: ${S3_TIMEOUT_SECONDS}
|
||||||
ATTACHMENT_MAX_BYTES: ${ATTACHMENT_MAX_BYTES}
|
ATTACHMENT_MAX_BYTES: ${ATTACHMENT_MAX_BYTES}
|
||||||
ATTACHMENT_MAX_PIXELS: ${ATTACHMENT_MAX_PIXELS}
|
ATTACHMENT_MAX_PIXELS: ${ATTACHMENT_MAX_PIXELS}
|
||||||
|
ATTACHMENT_BODY_MEMORY_BYTES: ${ATTACHMENT_BODY_MEMORY_BYTES}
|
||||||
ATTACHMENT_DECODE_MEMORY_BYTES: ${ATTACHMENT_DECODE_MEMORY_BYTES}
|
ATTACHMENT_DECODE_MEMORY_BYTES: ${ATTACHMENT_DECODE_MEMORY_BYTES}
|
||||||
expose:
|
expose:
|
||||||
- "8080"
|
- "8080"
|
||||||
@@ -162,6 +168,7 @@ services:
|
|||||||
mem_limit: ${API_MEMORY}
|
mem_limit: ${API_MEMORY}
|
||||||
stop_grace_period: 8s
|
stop_grace_period: 8s
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
networks: [application, data]
|
||||||
|
|
||||||
nginx:
|
nginx:
|
||||||
build:
|
build:
|
||||||
@@ -181,6 +188,7 @@ services:
|
|||||||
cpus: ${NGINX_CPUS}
|
cpus: ${NGINX_CPUS}
|
||||||
mem_limit: ${NGINX_MEMORY}
|
mem_limit: ${NGINX_MEMORY}
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
networks: [edge, application, monitoring]
|
||||||
|
|
||||||
swagger:
|
swagger:
|
||||||
build:
|
build:
|
||||||
@@ -195,6 +203,7 @@ services:
|
|||||||
cpus: ${SWAGGER_CPUS}
|
cpus: ${SWAGGER_CPUS}
|
||||||
mem_limit: ${SWAGGER_MEMORY}
|
mem_limit: ${SWAGGER_MEMORY}
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
networks: [edge]
|
||||||
|
|
||||||
tests:
|
tests:
|
||||||
profiles: ["tests"]
|
profiles: ["tests"]
|
||||||
@@ -211,6 +220,7 @@ services:
|
|||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
cpus: ${TEST_CPUS}
|
cpus: ${TEST_CPUS}
|
||||||
mem_limit: ${TEST_MEMORY}
|
mem_limit: ${TEST_MEMORY}
|
||||||
|
networks: [data]
|
||||||
|
|
||||||
postgres-exporter:
|
postgres-exporter:
|
||||||
image: quay.io/prometheuscommunity/postgres-exporter:v0.20.1
|
image: quay.io/prometheuscommunity/postgres-exporter:v0.20.1
|
||||||
@@ -225,6 +235,7 @@ services:
|
|||||||
cpus: ${EXPORTER_CPUS}
|
cpus: ${EXPORTER_CPUS}
|
||||||
mem_limit: ${EXPORTER_MEMORY}
|
mem_limit: ${EXPORTER_MEMORY}
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
networks: [data, monitoring]
|
||||||
|
|
||||||
valkey-exporter:
|
valkey-exporter:
|
||||||
image: oliver006/redis_exporter:v1.89.0-alpine
|
image: oliver006/redis_exporter:v1.89.0-alpine
|
||||||
@@ -238,6 +249,7 @@ services:
|
|||||||
cpus: ${EXPORTER_CPUS}
|
cpus: ${EXPORTER_CPUS}
|
||||||
mem_limit: ${EXPORTER_MEMORY}
|
mem_limit: ${EXPORTER_MEMORY}
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
networks: [data, monitoring]
|
||||||
|
|
||||||
nginx-exporter:
|
nginx-exporter:
|
||||||
image: nginx/nginx-prometheus-exporter:1.5.1
|
image: nginx/nginx-prometheus-exporter:1.5.1
|
||||||
@@ -248,6 +260,7 @@ services:
|
|||||||
cpus: ${EXPORTER_CPUS}
|
cpus: ${EXPORTER_CPUS}
|
||||||
mem_limit: ${EXPORTER_MEMORY}
|
mem_limit: ${EXPORTER_MEMORY}
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
networks: [monitoring]
|
||||||
|
|
||||||
prometheus:
|
prometheus:
|
||||||
build:
|
build:
|
||||||
@@ -276,6 +289,7 @@ services:
|
|||||||
cpus: ${PROMETHEUS_CPUS}
|
cpus: ${PROMETHEUS_CPUS}
|
||||||
mem_limit: ${PROMETHEUS_MEMORY}
|
mem_limit: ${PROMETHEUS_MEMORY}
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
networks: [edge, application, data, monitoring]
|
||||||
|
|
||||||
grafana:
|
grafana:
|
||||||
build:
|
build:
|
||||||
@@ -295,6 +309,7 @@ services:
|
|||||||
cpus: ${GRAFANA_CPUS}
|
cpus: ${GRAFANA_CPUS}
|
||||||
mem_limit: ${GRAFANA_MEMORY}
|
mem_limit: ${GRAFANA_MEMORY}
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
networks: [edge, monitoring]
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
postgres_data:
|
postgres_data:
|
||||||
@@ -302,3 +317,12 @@ volumes:
|
|||||||
prometheus_data:
|
prometheus_data:
|
||||||
grafana_data:
|
grafana_data:
|
||||||
s3_data:
|
s3_data:
|
||||||
|
|
||||||
|
networks:
|
||||||
|
edge:
|
||||||
|
application:
|
||||||
|
internal: true
|
||||||
|
data:
|
||||||
|
internal: true
|
||||||
|
monitoring:
|
||||||
|
internal: true
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
services:
|
||||||
|
api:
|
||||||
|
image: ${API_IMAGE:?API_IMAGE is required}
|
||||||
|
|
||||||
|
migrate:
|
||||||
|
image: ${API_IMAGE:?API_IMAGE is required}
|
||||||
|
|
||||||
|
nginx:
|
||||||
|
image: ${NGINX_IMAGE:?NGINX_IMAGE is required}
|
||||||
|
|
||||||
|
swagger:
|
||||||
|
image: ${SWAGGER_IMAGE:?SWAGGER_IMAGE is required}
|
||||||
|
|
||||||
|
prometheus:
|
||||||
|
image: ${PROMETHEUS_IMAGE:?PROMETHEUS_IMAGE is required}
|
||||||
|
|
||||||
|
grafana:
|
||||||
|
image: ${GRAFANA_IMAGE:?GRAFANA_IMAGE is required}
|
||||||
|
|
||||||
|
s3:
|
||||||
|
image: ${S3_IMAGE:?S3_IMAGE is required}
|
||||||
|
|
||||||
|
s3-bootstrap:
|
||||||
|
image: ${S3_BOOTSTRAP_IMAGE:?S3_BOOTSTRAP_IMAGE is required}
|
||||||
@@ -40,6 +40,7 @@ S3_USE_TLS=false
|
|||||||
S3_TIMEOUT_SECONDS=5
|
S3_TIMEOUT_SECONDS=5
|
||||||
ATTACHMENT_MAX_BYTES=10485760
|
ATTACHMENT_MAX_BYTES=10485760
|
||||||
ATTACHMENT_MAX_PIXELS=40000000
|
ATTACHMENT_MAX_PIXELS=40000000
|
||||||
|
ATTACHMENT_BODY_MEMORY_BYTES=67108864
|
||||||
ATTACHMENT_DECODE_MEMORY_BYTES=402653184
|
ATTACHMENT_DECODE_MEMORY_BYTES=402653184
|
||||||
|
|
||||||
# Размер пула и обслуживание соединений PostgreSQL.
|
# Размер пула и обслуживание соединений PostgreSQL.
|
||||||
|
|||||||
+110
-27
@@ -2,18 +2,44 @@
|
|||||||
|
|
||||||
set -eu
|
set -eu
|
||||||
|
|
||||||
|
if [ "$#" -ne 1 ] || [ ! -f "$1" ]; then
|
||||||
|
printf 'usage: %s <image-manifest>\n' "$0" >&2
|
||||||
|
exit 2
|
||||||
|
fi
|
||||||
|
|
||||||
|
manifest=$1
|
||||||
|
deploy_dir=${DEPLOY_STATE_DIR:-"$HOME/.local/state/mtusi2026lab4"}
|
||||||
|
current_manifest=$deploy_dir/current-images.env
|
||||||
|
previous_manifest=$deploy_dir/previous-images.env
|
||||||
|
replicas=${API_REPLICAS:-2}
|
||||||
|
rollback_ready=false
|
||||||
|
switched=false
|
||||||
|
|
||||||
|
mkdir -p "$deploy_dir"
|
||||||
|
|
||||||
|
load_manifest() {
|
||||||
|
set -a
|
||||||
|
# shellcheck disable=SC1090
|
||||||
|
. "$1"
|
||||||
|
set +a
|
||||||
|
}
|
||||||
|
|
||||||
|
compose() {
|
||||||
|
docker compose -f compose.yaml -f deploy/compose.images.yaml "$@"
|
||||||
|
}
|
||||||
|
|
||||||
wait_for_replicas() {
|
wait_for_replicas() {
|
||||||
expected=$1
|
expected=$1
|
||||||
deadline=$(($(date +%s) + 90))
|
deadline=$(($(date +%s) + 120))
|
||||||
|
|
||||||
while [ "$(date +%s)" -lt "$deadline" ]; do
|
while [ "$(date +%s)" -lt "$deadline" ]; do
|
||||||
containers=$(docker compose ps -q api)
|
containers=$(compose ps -q api)
|
||||||
count=0
|
count=0
|
||||||
healthy=0
|
healthy=0
|
||||||
for container in $containers; do
|
for container in $containers; do
|
||||||
count=$((count + 1))
|
count=$((count + 1))
|
||||||
status=$(docker inspect --format '{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}' "$container")
|
status=$(docker inspect --format '{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}' "$container")
|
||||||
if [ "$status" = "healthy" ]; then
|
if [ "$status" = healthy ]; then
|
||||||
healthy=$((healthy + 1))
|
healthy=$((healthy + 1))
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
@@ -23,36 +49,93 @@ wait_for_replicas() {
|
|||||||
sleep 1
|
sleep 1
|
||||||
done
|
done
|
||||||
|
|
||||||
printf '%s\n' "API replicas did not become healthy: expected $expected" >&2
|
printf 'API replicas did not become healthy: expected %s\n' "$expected" >&2
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
old_containers=$(docker compose ps -q api)
|
smoke_check() {
|
||||||
old_count=0
|
compose exec -T nginx wget -q -O /dev/null http://127.0.0.1:8080/health/ready
|
||||||
for container in $old_containers; do
|
}
|
||||||
old_count=$((old_count + 1))
|
|
||||||
done
|
|
||||||
|
|
||||||
docker compose build
|
running_image() {
|
||||||
docker compose up -d postgres valkey s3
|
containers=$(compose ps -q "$1")
|
||||||
docker compose run --rm s3-bootstrap
|
for container in $containers; do
|
||||||
docker compose run --rm migrate
|
docker inspect --format '{{.Config.Image}}' "$container"
|
||||||
|
return 0
|
||||||
|
done
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
if [ "$old_count" -lt 2 ]; then
|
capture_running_images() {
|
||||||
docker compose up -d --scale api=2 --wait --wait-timeout 120
|
output=$1
|
||||||
exit 0
|
api_image=$(running_image api) || return 1
|
||||||
|
nginx_image=$(running_image nginx) || return 1
|
||||||
|
swagger_image=$(running_image swagger) || swagger_image=$SWAGGER_IMAGE
|
||||||
|
prometheus_image=$(running_image prometheus) || prometheus_image=$PROMETHEUS_IMAGE
|
||||||
|
grafana_image=$(running_image grafana) || grafana_image=$GRAFANA_IMAGE
|
||||||
|
s3_image=$(running_image s3) || s3_image=$S3_IMAGE
|
||||||
|
s3_bootstrap_image=$(running_image s3-bootstrap) || s3_bootstrap_image=$S3_BOOTSTRAP_IMAGE
|
||||||
|
|
||||||
|
{
|
||||||
|
printf 'API_IMAGE=%s\n' "$api_image"
|
||||||
|
printf 'NGINX_IMAGE=%s\n' "$nginx_image"
|
||||||
|
printf 'SWAGGER_IMAGE=%s\n' "$swagger_image"
|
||||||
|
printf 'PROMETHEUS_IMAGE=%s\n' "$prometheus_image"
|
||||||
|
printf 'GRAFANA_IMAGE=%s\n' "$grafana_image"
|
||||||
|
printf 'S3_IMAGE=%s\n' "$s3_image"
|
||||||
|
printf 'S3_BOOTSTRAP_IMAGE=%s\n' "$s3_bootstrap_image"
|
||||||
|
} > "$output"
|
||||||
|
}
|
||||||
|
|
||||||
|
rollback() {
|
||||||
|
status=$?
|
||||||
|
trap - EXIT INT TERM
|
||||||
|
|
||||||
|
if [ "$switched" = true ] && [ "$rollback_ready" = true ]; then
|
||||||
|
printf '%s\n' 'Deployment failed; restoring the previous images.' >&2
|
||||||
|
compose stop nginx api >/dev/null 2>&1 || true
|
||||||
|
compose rm -f nginx api >/dev/null 2>&1 || true
|
||||||
|
load_manifest "$previous_manifest"
|
||||||
|
compose up -d --no-build --no-deps --force-recreate --scale api="$replicas" api
|
||||||
|
wait_for_replicas "$replicas"
|
||||||
|
compose up -d --no-build --no-deps --force-recreate nginx
|
||||||
|
smoke_check
|
||||||
|
elif [ "$switched" = true ]; then
|
||||||
|
printf '%s\n' 'Deployment failed and no previous image manifest is available.' >&2
|
||||||
|
fi
|
||||||
|
|
||||||
|
exit "$status"
|
||||||
|
}
|
||||||
|
|
||||||
|
trap rollback EXIT
|
||||||
|
trap 'exit 130' INT
|
||||||
|
trap 'exit 143' TERM
|
||||||
|
|
||||||
|
load_manifest "$manifest"
|
||||||
|
|
||||||
|
if [ -f "$current_manifest" ]; then
|
||||||
|
cp "$current_manifest" "$previous_manifest"
|
||||||
|
rollback_ready=true
|
||||||
|
elif capture_running_images "$previous_manifest"; then
|
||||||
|
rollback_ready=true
|
||||||
fi
|
fi
|
||||||
|
|
||||||
docker compose up -d --no-deps --no-recreate --scale api=3 api
|
compose pull api nginx swagger prometheus grafana s3 s3-bootstrap
|
||||||
wait_for_replicas 3
|
compose up -d --no-build --wait --wait-timeout 120 postgres valkey s3
|
||||||
|
compose run --rm --no-deps s3-bootstrap
|
||||||
|
compose run --rm --no-deps migrate
|
||||||
|
|
||||||
for container in $old_containers; do
|
# Stop the entry point and every old API replica before starting the new pool.
|
||||||
docker stop --time 6 "$container" >/dev/null
|
# This gives an atomic version boundary at the cost of a short maintenance window.
|
||||||
docker rm "$container" >/dev/null
|
switched=true
|
||||||
docker compose up -d --no-deps --no-recreate --scale api=3 api
|
compose stop nginx api
|
||||||
wait_for_replicas 3
|
compose rm -f nginx api
|
||||||
done
|
compose up -d --no-build --no-deps --force-recreate --scale api="$replicas" api
|
||||||
|
wait_for_replicas "$replicas"
|
||||||
|
compose up -d --no-build --no-deps --force-recreate nginx
|
||||||
|
smoke_check
|
||||||
|
|
||||||
docker compose up -d --no-deps --no-recreate --scale api=2 api
|
compose up -d --no-build swagger postgres-exporter valkey-exporter nginx-exporter prometheus grafana
|
||||||
wait_for_replicas 2
|
|
||||||
docker compose up -d --no-build --scale api=2 --wait --wait-timeout 120
|
cp "$manifest" "$current_manifest"
|
||||||
|
trap - EXIT INT TERM
|
||||||
|
|||||||
Reference in New Issue
Block a user