File size: 3,086 Bytes
e73ce34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/bin/bash

set -e

COMPOSE_FILE="/mnt/data/cheminformatics-microservice/ops/docker-compose-dev.yml"
API_IMAGE="nfdi4chem/cheminformatics-microservice:api-dev"
WEB_IMAGE="nfdi4chem/cheminformatics-microservice:app-dev"
NEW_CONTAINER_ID=""

# === Basic Logging ===
LOG_FILE="/var/log/cheminformatics-deploy.log"

log() {
    local message="[$(date '+%Y-%m-%d %H:%M:%S')] $1"
    echo "$message"
    echo "$message" >> "$LOG_FILE"
}

# === Functions ===

check_health() {
    HEALTH=$(docker inspect --format='{{json .State.Health.Status}}' "$NEW_CONTAINER_ID")
    if [[ "$HEALTH" == *"healthy"* ]]; then
        log "βœ… Container is healthy."
        return 0
    else
        log "⏳ Container is unhealthy or still starting."
        return 1
    fi
}

deploy_service(){
    local service_name=$1   # e.g., "api"

    IS_CONTAINER_HEALTHY=1

    log "πŸš€ Scaling up $service_name container..."
    docker compose -f "$COMPOSE_FILE" up -d --scale "$service_name=2" --no-recreate

    NEW_CONTAINER_ID=$(docker ps -q -l)
    log "πŸ” New container ID: $NEW_CONTAINER_ID"

    log "⏳ Waiting for new container to pass health check (up to 10 retries)..."
    for i in {1..10}; do
        if check_health; then
            IS_CONTAINER_HEALTHY=0
            break
        else
            log "Retry $i/10: Waiting 60s..."
            sleep 60
        fi
    done

    if [ "$IS_CONTAINER_HEALTHY" == 0 ]; then
        log "🧼 Replacing old $service_name container(s)..."

        # Retrieve and sort containers with matching name prefix
        container_ids=$(docker ps -a --filter "name=$service_name" --format "{{.ID}}")
        sorted_container_ids=$(echo "$container_ids" | xargs docker inspect --format='{{.Created}} {{.ID}}' | sort | awk '{print $2}')
        oldest_container_id=$(echo "$sorted_container_ids" | head -n 1)

        if [[ -z "$oldest_container_id" ]]; then
            log "❌ No containers found with name prefix: ${service_name}"
            exit 1
        fi

        docker stop "$oldest_container_id"
        docker rm "$oldest_container_id"
        docker image prune -af

        log "βœ… Deleted old container ID: $oldest_container_id"
    else
        log "❌ Deployment aborted: new $service_name container is unhealthy."
        docker stop "$NEW_CONTAINER_ID"
        docker rm "$NEW_CONTAINER_ID"
    fi
}

# === Image Update Check ===

log "πŸ” Checking for updated images..."

deploy_api=false
deploy_web=false

if [ "$(docker pull "$API_IMAGE" | grep -c "Status: Image is up to date")" -eq 0 ]; then
    log "πŸ“¦ New API image available."
    deploy_api=true
fi

if [ "$(docker pull "$WEB_IMAGE" | grep -c "Status: Image is up to date")" -eq 0 ]; then
    log "πŸ“¦ New WEB image available."
    deploy_web=true
fi

if [[ "$deploy_api" = false && "$deploy_web" = false ]]; then
    log "βœ… No updates. Deployment not needed."
    exit 0
fi

# === Deployment ===

if [[ "$deploy_api" = true ]]; then
    deploy_service api
fi

if [[ "$deploy_web" = true ]]; then
    deploy_service web
fi

log "πŸŽ‰ Deployment completed successfully!"