implement gitlab blue-green ci cd
This commit is contained in:
1 parent
a353b80a15
commit
0f2dee08cb
13 files changed
+686
-230
No files matched your search
@@ -1,3 +1,4 @@
|
||||
APP_VERSION=latest
|
||||
WA_INVENTORY_ADMIN_TOKEN=replace-with-a-long-random-secret
|
||||
|
||||
ACTIVE_SLOT=blue
|
||||
BLUE_IMAGE=local/warehouse-allocation:replace-with-commit-sha
|
||||
GREEN_IMAGE=local/warehouse-allocation:replace-with-commit-sha
|
||||
+79
-93
@@ -1,119 +1,105 @@
|
||||
# Production deployment: `warehouse-allocation.mvphub.ir`
|
||||
|
||||
## Current layout
|
||||
## Runtime layout
|
||||
|
||||
| Item | Value |
|
||||
|---|---|
|
||||
| SSH host | `ict` |
|
||||
| Public URL | `https://warehouse-allocation.mvphub.ir` |
|
||||
| Release root | `/srv/apps/warehouse-allocation` |
|
||||
| Current release | `/srv/apps/warehouse-allocation/current` |
|
||||
| Shared environment | `/srv/apps/warehouse-allocation/shared/.env` |
|
||||
| Compose project | `warehouse-allocation` |
|
||||
| API loopback address | `127.0.0.1:18081` |
|
||||
| Nginx site | `/etc/nginx/sites-available/warehouse-allocation.mvphub.ir` |
|
||||
| Certificate | `/etc/letsencrypt/live/warehouse-allocation.mvphub.ir` |
|
||||
| ACME webroot | `/var/www/letsencrypt` |
|
||||
| Blue slot | `127.0.0.1:18081` |
|
||||
| Green slot | `127.0.0.1:18082` |
|
||||
| Shared Redis | `warehouse-allocation-redis-1`, no host port |
|
||||
| Nginx upstream | `/etc/nginx/conf.d/10-warehouse-allocation-upstream.conf` |
|
||||
| Restricted account | `warehouse-deploy` |
|
||||
| Deployment program | `/usr/local/sbin/warehouse-allocation-deploy` |
|
||||
|
||||
The API and Redis belong to a dedicated Compose project. Redis has no host port, and
|
||||
the API port is bound only to loopback. Public traffic can reach it only through
|
||||
the hostname-specific Nginx reverse proxy.
|
||||
Only one API slot receives traffic after a deployment. During cutover both slots
|
||||
run briefly, allowing Nginx to switch without an API interruption. Redis and its
|
||||
named volume remain shared between releases.
|
||||
|
||||
## GitLab pipeline
|
||||
|
||||
The authoritative pipeline is [`.gitlab-ci.yml`](../.gitlab-ci.yml). It runs
|
||||
quality checks, tests with a real Redis service, coverage, optional SonarQube,
|
||||
container build validation, release packaging, and automatic protected-`main`
|
||||
deployment.
|
||||
|
||||
Required protected GitLab variables:
|
||||
|
||||
| Variable | Type/value |
|
||||
|---|---|
|
||||
| `DEPLOY_HOST` | `188.137.233.91` |
|
||||
| `DEPLOY_PORT` | `22` |
|
||||
| `DEPLOY_USER` | `warehouse-deploy` |
|
||||
| `DEPLOY_SSH_PRIVATE_KEY` | File variable containing the dedicated private key |
|
||||
| `DEPLOY_KNOWN_HOSTS` | File variable containing the pinned server host key |
|
||||
|
||||
Optional SonarQube variables are `SONAR_HOST_URL` and masked `SONAR_TOKEN`. If both
|
||||
exist, the quality gate becomes mandatory for deployment.
|
||||
|
||||
The CI account has no Docker-group membership and no general sudo access. It may
|
||||
invoke only the root-owned deployment program through `/etc/sudoers.d`.
|
||||
|
||||
## Deployment transaction
|
||||
|
||||
The deploy job uploads:
|
||||
|
||||
```text
|
||||
/home/warehouse-deploy/incoming/release-<40-character-commit-sha>.tar.gz
|
||||
```
|
||||
|
||||
It then invokes:
|
||||
|
||||
```bash
|
||||
sudo /usr/local/sbin/warehouse-allocation-deploy <commit-sha> <sha256>
|
||||
```
|
||||
|
||||
The program:
|
||||
|
||||
1. serializes deployments with `flock`;
|
||||
2. validates the commit ID, checksum, and archive paths;
|
||||
3. extracts an immutable release;
|
||||
4. builds an exact commit-tagged image while the old slot serves traffic;
|
||||
5. starts and checks the inactive slot;
|
||||
6. switches only the warehouse-allocation Nginx upstream;
|
||||
7. runs public health, OpenAPI, and exact Swagger-example tests twice;
|
||||
8. commits the active-slot metadata and removes the old API container;
|
||||
9. retains five recent releases and application images.
|
||||
|
||||
If any step before commit fails, the upstream is restored, Nginx is reloaded,
|
||||
the failed candidate is removed, and the previous release remains current.
|
||||
|
||||
## Status and logs
|
||||
|
||||
```bash
|
||||
ssh ict
|
||||
|
||||
docker compose \
|
||||
--env-file /srv/apps/warehouse-allocation/shared/.env \
|
||||
-f /srv/apps/warehouse-allocation/current/deploy/compose.server.yaml \
|
||||
ps
|
||||
source /srv/apps/warehouse-allocation/shared/.env
|
||||
echo "$ACTIVE_SLOT"
|
||||
readlink -f /srv/apps/warehouse-allocation/current
|
||||
|
||||
docker logs --tail 100 warehouse-allocation-api-1
|
||||
docker ps --filter name=warehouse-allocation
|
||||
docker logs --tail 100 warehouse-allocation-api-blue-1
|
||||
docker logs --tail 100 warehouse-allocation-api-green-1
|
||||
docker logs --tail 100 warehouse-allocation-redis-1
|
||||
|
||||
curl -fsS http://127.0.0.1:18081/health/ready
|
||||
curl -fsS https://warehouse-allocation.mvphub.ir/health/ready
|
||||
nginx -t
|
||||
```
|
||||
|
||||
Do not print or commit `/srv/apps/warehouse-allocation/shared/.env`. It contains the
|
||||
inventory administration token.
|
||||
Do not print or commit the shared `.env`; it contains the inventory administration
|
||||
token. Do not use the root development `compose.yaml` for production.
|
||||
|
||||
## Starting or reconciling the current release
|
||||
## Manual rollback
|
||||
|
||||
```bash
|
||||
docker compose \
|
||||
--env-file /srv/apps/warehouse-allocation/shared/.env \
|
||||
-f /srv/apps/warehouse-allocation/current/deploy/compose.server.yaml \
|
||||
up -d --build
|
||||
```
|
||||
|
||||
The production Compose file is
|
||||
[`compose.server.yaml`](compose.server.yaml). It must be used instead of the local
|
||||
root `compose.yaml`, because it provides the isolated loopback port, restart
|
||||
policy, resource limits, persistent Redis volume, and production environment.
|
||||
|
||||
## Safe release process
|
||||
|
||||
1. Run tests and quality checks locally.
|
||||
2. Package tracked and required untracked source files without `.env`, `.git`, or
|
||||
virtual environments.
|
||||
3. Upload the archive to the server and verify its SHA-256 checksum.
|
||||
4. Extract it into a new immutable directory under
|
||||
`/srv/apps/warehouse-allocation/releases/<release-id>`.
|
||||
5. Validate the new Compose file using the shared environment.
|
||||
6. Point `/srv/apps/warehouse-allocation/current` at the new release.
|
||||
7. Run Compose with `up -d --build`.
|
||||
8. Wait for both containers to become healthy.
|
||||
9. Verify loopback health, public HTTPS health, OpenAPI, and one allocation smoke
|
||||
test.
|
||||
|
||||
Never modify another application's Compose project, loopback port, Nginx site, or
|
||||
volume as part of this release.
|
||||
|
||||
## Nginx and TLS
|
||||
|
||||
The repository contains:
|
||||
|
||||
- [`nginx/warehouse-allocation.bootstrap.conf`](nginx/warehouse-allocation.bootstrap.conf)
|
||||
for initial HTTP/ACME validation;
|
||||
- [`nginx/warehouse-allocation.mvphub.ir.conf`](nginx/warehouse-allocation.mvphub.ir.conf)
|
||||
for the final HTTP-to-HTTPS redirect and reverse proxy.
|
||||
|
||||
Always run `nginx -t` before `systemctl reload nginx`. A reload is graceful and
|
||||
must only happen after the full server configuration passes validation.
|
||||
|
||||
The certificate uses Certbot's webroot authenticator. The existing
|
||||
`certbot.timer` renews it automatically.
|
||||
|
||||
## Rollback
|
||||
|
||||
Identify the prior release first:
|
||||
|
||||
```bash
|
||||
find /srv/apps/warehouse-allocation/releases -mindepth 1 -maxdepth 1 -type d -print
|
||||
readlink -f /srv/apps/warehouse-allocation/current
|
||||
```
|
||||
|
||||
Point `current` at the explicit prior release, validate Compose, and start it:
|
||||
|
||||
```bash
|
||||
ln -sfn /srv/apps/warehouse-allocation/releases/<prior-release-id> \
|
||||
/srv/apps/warehouse-allocation/current
|
||||
|
||||
docker compose \
|
||||
--env-file /srv/apps/warehouse-allocation/shared/.env \
|
||||
-f /srv/apps/warehouse-allocation/current/deploy/compose.server.yaml \
|
||||
config --quiet
|
||||
|
||||
docker compose \
|
||||
--env-file /srv/apps/warehouse-allocation/shared/.env \
|
||||
-f /srv/apps/warehouse-allocation/current/deploy/compose.server.yaml \
|
||||
up -d --build
|
||||
```
|
||||
|
||||
Then verify `/health/ready` on loopback and HTTPS. The Redis volume is deliberately
|
||||
shared between releases, so rolling application code back does not discard
|
||||
inventory data.
|
||||
Normal failures roll back automatically before the old slot is stopped. For a
|
||||
post-deployment application regression, redeploy the desired older commit
|
||||
artifact through the same deployment program. This performs the same health,
|
||||
traffic-switch, and rollback protections instead of manually editing Nginx.
|
||||
|
||||
The deployment program is scoped to warehouse-allocation release directories,
|
||||
images, two API containers, and its dedicated upstream file. It never prunes or
|
||||
restarts bank-flow containers, unrelated images, Nginx sites, certificates, or
|
||||
volumes.
|
||||
+42
-37
@@ -1,45 +1,50 @@
|
||||
name: warehouse-allocation
|
||||
|
||||
x-api-common: &api-common
|
||||
environment:
|
||||
WA_ENVIRONMENT: production
|
||||
WA_INVENTORY_BACKEND: redis
|
||||
WA_REDIS_URL: redis://redis:6379/0
|
||||
WA_REDIS_SOCKET_TIMEOUT_SECONDS: "0.08"
|
||||
WA_MAX_CANDIDATE_WAREHOUSES: "33"
|
||||
WA_INVENTORY_ADMIN_TOKEN: ${WA_INVENTORY_ADMIN_TOKEN:?inventory admin token is required}
|
||||
restart: unless-stopped
|
||||
read_only: true
|
||||
tmpfs:
|
||||
- /tmp:size=32m,mode=1777
|
||||
security_opt:
|
||||
- no-new-privileges:true
|
||||
mem_limit: 384m
|
||||
healthcheck:
|
||||
test:
|
||||
- CMD
|
||||
- python
|
||||
- -c
|
||||
- "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=1)"
|
||||
interval: 5s
|
||||
timeout: 2s
|
||||
retries: 10
|
||||
start_period: 10s
|
||||
logging:
|
||||
driver: json-file
|
||||
options:
|
||||
max-size: 10m
|
||||
max-file: "3"
|
||||
|
||||
services:
|
||||
api:
|
||||
build:
|
||||
context: ..
|
||||
dockerfile: Dockerfile
|
||||
image: local/warehouse-allocation:${APP_VERSION:-latest}
|
||||
environment:
|
||||
WA_ENVIRONMENT: production
|
||||
WA_INVENTORY_BACKEND: redis
|
||||
WA_REDIS_URL: redis://redis:6379/0
|
||||
WA_REDIS_SOCKET_TIMEOUT_SECONDS: "0.08"
|
||||
WA_MAX_CANDIDATE_WAREHOUSES: "33"
|
||||
WA_INVENTORY_ADMIN_TOKEN: ${WA_INVENTORY_ADMIN_TOKEN:?inventory admin token is required}
|
||||
api-blue:
|
||||
<<: *api-common
|
||||
image: ${BLUE_IMAGE:?blue image is required}
|
||||
profiles: [blue]
|
||||
ports:
|
||||
- "127.0.0.1:18081:8080"
|
||||
depends_on:
|
||||
redis:
|
||||
condition: service_healthy
|
||||
restart: unless-stopped
|
||||
read_only: true
|
||||
tmpfs:
|
||||
- /tmp:size=32m,mode=1777
|
||||
security_opt:
|
||||
- no-new-privileges:true
|
||||
mem_limit: 512m
|
||||
healthcheck:
|
||||
test:
|
||||
- CMD
|
||||
- python
|
||||
- -c
|
||||
- "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=1)"
|
||||
interval: 10s
|
||||
timeout: 2s
|
||||
retries: 5
|
||||
start_period: 10s
|
||||
logging:
|
||||
driver: json-file
|
||||
options:
|
||||
max-size: 10m
|
||||
max-file: "3"
|
||||
|
||||
api-green:
|
||||
<<: *api-common
|
||||
image: ${GREEN_IMAGE:?green image is required}
|
||||
profiles: [green]
|
||||
ports:
|
||||
- "127.0.0.1:18082:8080"
|
||||
|
||||
redis:
|
||||
image: redis:7.4-alpine
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
upstream warehouse_allocation_backend {
|
||||
server 127.0.0.1:18081 max_fails=3 fail_timeout=5s;
|
||||
keepalive 32;
|
||||
}
|
||||
@@ -38,7 +38,6 @@ server {
|
||||
location / {
|
||||
include snippets/proxy-application.conf;
|
||||
proxy_connect_timeout 2s;
|
||||
proxy_pass http://127.0.0.1:18081;
|
||||
proxy_pass http://warehouse_allocation_backend;
|
||||
}
|
||||
}
|
||||
|
||||
Executable
+325
@@ -0,0 +1,325 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
readonly PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
|
||||
readonly APP_ROOT="/srv/apps/warehouse-allocation"
|
||||
readonly RELEASES_DIR="${APP_ROOT}/releases"
|
||||
readonly SHARED_ENV="${APP_ROOT}/shared/.env"
|
||||
readonly INCOMING_DIR="/home/warehouse-deploy/incoming"
|
||||
readonly IMAGE_REPOSITORY="local/warehouse-allocation"
|
||||
readonly NGINX_UPSTREAM="/etc/nginx/conf.d/10-warehouse-allocation-upstream.conf"
|
||||
readonly PUBLIC_URL="https://warehouse-allocation.mvphub.ir"
|
||||
readonly LOCK_FILE="/run/lock/warehouse-allocation-deploy.lock"
|
||||
|
||||
release_id="${1:-}"
|
||||
expected_checksum="${2:-}"
|
||||
|
||||
if [[ ! "$release_id" =~ ^[0-9a-f]{40}$ ]]; then
|
||||
echo "release id must be a full 40-character lowercase commit SHA" >&2
|
||||
exit 64
|
||||
fi
|
||||
if [[ ! "$expected_checksum" =~ ^[0-9a-f]{64}$ ]]; then
|
||||
echo "checksum must be a lowercase SHA-256 digest" >&2
|
||||
exit 64
|
||||
fi
|
||||
|
||||
readonly release_id expected_checksum
|
||||
readonly archive="${INCOMING_DIR}/release-${release_id}.tar.gz"
|
||||
readonly release_dir="${RELEASES_DIR}/${release_id}"
|
||||
readonly candidate_image="${IMAGE_REPOSITORY}:${release_id}"
|
||||
|
||||
exec 9>"$LOCK_FILE"
|
||||
if ! flock -n 9; then
|
||||
echo "another warehouse-allocation deployment is running" >&2
|
||||
exit 75
|
||||
fi
|
||||
|
||||
umask 077
|
||||
work_dir="$(mktemp -d "${APP_ROOT}/shared/.deploy.XXXXXX")"
|
||||
archive_listing="${work_dir}/archive.list"
|
||||
upstream_backup="${work_dir}/upstream.conf"
|
||||
openapi_file="${work_dir}/openapi.json"
|
||||
example_file="${work_dir}/example.json"
|
||||
response_file="${work_dir}/response.json"
|
||||
|
||||
candidate_started=0
|
||||
upstream_changed=0
|
||||
deployment_complete=0
|
||||
current_changed=0
|
||||
previous_release=""
|
||||
active_container=""
|
||||
candidate=""
|
||||
candidate_port=""
|
||||
active=""
|
||||
blue_image=""
|
||||
green_image=""
|
||||
staging_dir=""
|
||||
|
||||
compose() {
|
||||
env \
|
||||
BLUE_IMAGE="$blue_image" \
|
||||
GREEN_IMAGE="$green_image" \
|
||||
docker compose \
|
||||
--env-file "$SHARED_ENV" \
|
||||
-f "${release_dir}/deploy/compose.server.yaml" \
|
||||
"$@"
|
||||
}
|
||||
|
||||
env_value() {
|
||||
local key="$1"
|
||||
awk -F= -v key="$key" '$1 == key {sub(/^[^=]*=/, ""); print; exit}' "$SHARED_ENV"
|
||||
}
|
||||
|
||||
upsert_env() {
|
||||
local key="$1"
|
||||
local value="$2"
|
||||
local temporary
|
||||
temporary="$(mktemp "${APP_ROOT}/shared/.env.XXXXXX")"
|
||||
awk -v key="$key" -v value="$value" '
|
||||
BEGIN { found = 0 }
|
||||
index($0, key "=") == 1 {
|
||||
print key "=" value
|
||||
found = 1
|
||||
next
|
||||
}
|
||||
{ print }
|
||||
END {
|
||||
if (!found) {
|
||||
print key "=" value
|
||||
}
|
||||
}
|
||||
' "$SHARED_ENV" >"$temporary"
|
||||
chmod 0600 "$temporary"
|
||||
chown root:root "$temporary"
|
||||
mv -f "$temporary" "$SHARED_ENV"
|
||||
}
|
||||
|
||||
write_upstream() {
|
||||
local port="$1"
|
||||
local temporary
|
||||
temporary="$(mktemp "/etc/nginx/conf.d/.warehouse-allocation-upstream.XXXXXX")"
|
||||
cat >"$temporary" <<EOF
|
||||
upstream warehouse_allocation_backend {
|
||||
server 127.0.0.1:${port} max_fails=3 fail_timeout=5s;
|
||||
keepalive 32;
|
||||
}
|
||||
EOF
|
||||
chmod 0644 "$temporary"
|
||||
chown root:root "$temporary"
|
||||
mv -f "$temporary" "$NGINX_UPSTREAM"
|
||||
}
|
||||
|
||||
rollback() {
|
||||
local exit_code=$?
|
||||
trap - EXIT
|
||||
|
||||
if ((deployment_complete == 0)); then
|
||||
echo "deployment failed; preserving the previous production slot" >&2
|
||||
|
||||
if ((upstream_changed == 1)) && [[ -s "$upstream_backup" ]]; then
|
||||
cp -f "$upstream_backup" "$NGINX_UPSTREAM"
|
||||
if nginx -t; then
|
||||
systemctl reload nginx || true
|
||||
fi
|
||||
fi
|
||||
|
||||
if ((current_changed == 1)) && [[ -n "$previous_release" ]]; then
|
||||
ln -sfn "$previous_release" "${APP_ROOT}/current"
|
||||
fi
|
||||
|
||||
if ((candidate_started == 1)) && [[ -n "$candidate" ]]; then
|
||||
compose --profile "$candidate" stop "api-${candidate}" || true
|
||||
compose --profile "$candidate" rm -f "api-${candidate}" || true
|
||||
fi
|
||||
|
||||
if [[ -n "$staging_dir" && -d "$staging_dir" ]]; then
|
||||
rm -rf "$staging_dir"
|
||||
fi
|
||||
fi
|
||||
|
||||
rm -rf "$work_dir"
|
||||
exit "$exit_code"
|
||||
}
|
||||
trap rollback EXIT
|
||||
|
||||
if [[ ! -f "$archive" ]]; then
|
||||
echo "release archive is missing: $archive" >&2
|
||||
exit 66
|
||||
fi
|
||||
if [[ ! -f "$SHARED_ENV" ]]; then
|
||||
echo "shared production environment is missing: $SHARED_ENV" >&2
|
||||
exit 66
|
||||
fi
|
||||
|
||||
actual_checksum="$(sha256sum "$archive" | awk '{print $1}')"
|
||||
if [[ "$actual_checksum" != "$expected_checksum" ]]; then
|
||||
echo "release archive checksum mismatch" >&2
|
||||
exit 65
|
||||
fi
|
||||
|
||||
tar -tzf "$archive" >"$archive_listing"
|
||||
if grep -Eq '(^/|(^|/)\.\.(/|$))' "$archive_listing"; then
|
||||
echo "release archive contains an unsafe path" >&2
|
||||
exit 65
|
||||
fi
|
||||
|
||||
if [[ -d "$release_dir" ]]; then
|
||||
if [[ ! -f "${release_dir}/.release-checksum" ]] ||
|
||||
[[ "$(<"${release_dir}/.release-checksum")" != "$expected_checksum" ]]; then
|
||||
echo "existing release directory does not match the artifact checksum" >&2
|
||||
exit 65
|
||||
fi
|
||||
else
|
||||
staging_dir="${RELEASES_DIR}/.${release_id}.staging"
|
||||
if [[ -e "$staging_dir" ]]; then
|
||||
echo "staging directory already exists: $staging_dir" >&2
|
||||
exit 73
|
||||
fi
|
||||
install -d -m 0755 "$staging_dir"
|
||||
tar -xzf "$archive" -C "$staging_dir"
|
||||
printf '%s\n' "$expected_checksum" >"${staging_dir}/.release-checksum"
|
||||
chmod 0444 "${staging_dir}/.release-checksum"
|
||||
mv "$staging_dir" "$release_dir"
|
||||
staging_dir=""
|
||||
fi
|
||||
|
||||
active="$(env_value ACTIVE_SLOT)"
|
||||
if [[ -z "$active" ]]; then
|
||||
active="blue"
|
||||
fi
|
||||
if [[ "$active" != "blue" && "$active" != "green" ]]; then
|
||||
echo "ACTIVE_SLOT must be blue or green" >&2
|
||||
exit 78
|
||||
fi
|
||||
|
||||
if [[ "$active" == "blue" ]]; then
|
||||
candidate="green"
|
||||
candidate_port="18082"
|
||||
else
|
||||
candidate="blue"
|
||||
candidate_port="18081"
|
||||
fi
|
||||
|
||||
active_container="warehouse-allocation-api-${active}-1"
|
||||
if ! docker inspect "$active_container" >/dev/null 2>&1; then
|
||||
active_container="warehouse-allocation-api-1"
|
||||
fi
|
||||
if ! docker inspect "$active_container" >/dev/null 2>&1; then
|
||||
echo "active API container could not be identified" >&2
|
||||
exit 69
|
||||
fi
|
||||
|
||||
active_image="$(docker inspect --format '{{.Config.Image}}' "$active_container")"
|
||||
if [[ "$active" == "blue" ]]; then
|
||||
blue_image="$active_image"
|
||||
green_image="$candidate_image"
|
||||
else
|
||||
blue_image="$candidate_image"
|
||||
green_image="$active_image"
|
||||
fi
|
||||
|
||||
compose config --quiet
|
||||
docker build \
|
||||
--label "ir.mvphub.service=warehouse-allocation" \
|
||||
--label "org.opencontainers.image.revision=${release_id}" \
|
||||
--tag "$candidate_image" \
|
||||
"$release_dir"
|
||||
|
||||
compose --profile "$candidate" up -d --no-deps "api-${candidate}"
|
||||
candidate_started=1
|
||||
|
||||
for _attempt in $(seq 1 24); do
|
||||
if curl -fsS --max-time 2 "http://127.0.0.1:${candidate_port}/health/ready" \
|
||||
>/dev/null; then
|
||||
break
|
||||
fi
|
||||
sleep 2
|
||||
done
|
||||
curl -fsS --max-time 5 "http://127.0.0.1:${candidate_port}/health/ready" >/dev/null
|
||||
|
||||
docker exec warehouse-allocation-redis-1 \
|
||||
redis-cli HSETNX inventory:demo-tehran DEMO-SKU-001 10000 >/dev/null
|
||||
docker exec warehouse-allocation-redis-1 \
|
||||
redis-cli HSETNX inventory:demo-karaj DEMO-SKU-001 10000 >/dev/null
|
||||
|
||||
cp -f "$NGINX_UPSTREAM" "$upstream_backup"
|
||||
write_upstream "$candidate_port"
|
||||
upstream_changed=1
|
||||
nginx -t
|
||||
systemctl reload nginx
|
||||
|
||||
curl -fsS --max-time 10 "${PUBLIC_URL}/health/ready" >/dev/null
|
||||
curl -fsS --max-time 10 "${PUBLIC_URL}/openapi.json" >"$openapi_file"
|
||||
python3 - "$openapi_file" "$example_file" <<'PY'
|
||||
import json
|
||||
import sys
|
||||
|
||||
source, destination = sys.argv[1:]
|
||||
with open(source, encoding="utf-8") as stream:
|
||||
document = json.load(stream)
|
||||
example = document["paths"]["/v1/allocations"]["post"]["requestBody"]["content"][
|
||||
"application/json"
|
||||
]["examples"]["demoQuote"]["value"]
|
||||
with open(destination, "w", encoding="utf-8") as stream:
|
||||
json.dump(example, stream)
|
||||
PY
|
||||
|
||||
for _attempt in 1 2; do
|
||||
curl -fsS --max-time 10 \
|
||||
-H "Content-Type: application/json" \
|
||||
--data-binary "@${example_file}" \
|
||||
"${PUBLIC_URL}/v1/allocations" >"$response_file"
|
||||
python3 - "$response_file" <<'PY'
|
||||
import json
|
||||
import sys
|
||||
|
||||
with open(sys.argv[1], encoding="utf-8") as stream:
|
||||
response = json.load(stream)
|
||||
assert response["warehouseId"] in {"demo-tehran", "demo-karaj"}
|
||||
assert response["inventoryReserved"] is False
|
||||
assert 0 <= response["routeOptimizationScore"] <= 100
|
||||
PY
|
||||
done
|
||||
|
||||
previous_release="$(readlink -f "${APP_ROOT}/current" || true)"
|
||||
upsert_env BLUE_IMAGE "$blue_image"
|
||||
upsert_env GREEN_IMAGE "$green_image"
|
||||
|
||||
new_current="${APP_ROOT}/.current-${release_id}"
|
||||
ln -s "$release_dir" "$new_current"
|
||||
mv -Tf "$new_current" "${APP_ROOT}/current"
|
||||
current_changed=1
|
||||
upsert_env ACTIVE_SLOT "$candidate"
|
||||
|
||||
deployment_complete=1
|
||||
candidate_started=0
|
||||
upstream_changed=0
|
||||
|
||||
docker rm -f "$active_container" >/dev/null 2>&1 || true
|
||||
rm -f "$archive"
|
||||
|
||||
current_release="$(readlink -f "${APP_ROOT}/current")"
|
||||
mapfile -t old_releases < <(
|
||||
find "$RELEASES_DIR" -mindepth 1 -maxdepth 1 -type d -printf '%T@ %p\n' |
|
||||
sort -nr |
|
||||
tail -n +6 |
|
||||
cut -d ' ' -f 2-
|
||||
)
|
||||
for old_release in "${old_releases[@]}"; do
|
||||
if [[ "$old_release" != "$current_release" ]]; then
|
||||
rm -rf "$old_release"
|
||||
fi
|
||||
done
|
||||
|
||||
mapfile -t old_images < <(
|
||||
docker image ls "$IMAGE_REPOSITORY" --format '{{.Repository}}:{{.Tag}}' |
|
||||
tail -n +6
|
||||
)
|
||||
for old_image in "${old_images[@]}"; do
|
||||
docker image rm "$old_image" >/dev/null 2>&1 || true
|
||||
done
|
||||
|
||||
rm -rf "$work_dir"
|
||||
trap - EXIT
|
||||
|
||||
echo "warehouse-allocation ${release_id} is active in the ${candidate} slot"
|
||||
Reference in new issue
Block a user