Commit 91220acc authored by Jorge Moratinos's avatar Jorge Moratinos
Browse files

Added pre-pulling images at run.sh

parent 8874ae7f
Loading
Loading
Loading
Loading
Loading
+1 −1
Changes for services/docker-compose-capif.yml: 1 added line, 1 removed line.
Original line number Diff line number Diff line
services:
  redis:
    container_name: ${REDIS_CONTAINER_NAME:-redis}
    image: "labs.etsi.org:5050/ocf/capif/redis:7.4.2-alpine"
    image: labs.etsi.org:5050/ocf/capif/redis:7.4.2-alpine
    command: redis-server
    # ports:
    #   - "6379:6379"
+130 −16
Changes for services/run.sh: 130 added lines, 16 removed lines.
Original line number Diff line number Diff line
@@ -9,7 +9,11 @@ fi

# Load variables for the selected environment
source "$(dirname "$0")/variables.sh" "$ENVIRONMENT"
# source $(dirname "$(readlink -f "$0")")/variables.sh

# ============= NEW VARIABLES FOR BASE IMAGE RETRY =============
MAX_RETRIES=3
RETRY_DELAY=5
# ==============================================================

help() {
  echo "Usage: $0 [environment] [options]"
@@ -29,10 +33,94 @@ help() {
  echo "       -f : Services directory. (Default $SERVICES_DIR)"
  echo "       -g : Gitlab base URL. (Default $REGISTRY_BASE_URL)"
  echo "       -b : Build docker images. Default TRUE"
  echo "       -t : Number of retries for pulling base images. (Default 3)"
  echo "       -d : Retry delay in seconds. (Default 5)"
  echo "       -p : Pull base images with retries before building. Default TRUE"
  echo "       -h : show this help"
  exit 1
}

# ============= FUNCTION: Pull base images with retries =============
pull_base_images() {
  local services_dir="$1"
  local max_retries="$2"
  local retry_delay="$3"
  
  # Find all FROM statements in Dockerfiles
  local dockerfile_images=$(find "$services_dir" -name "Dockerfile*" -type f | \
                            xargs grep -h "^FROM " 2>/dev/null | \
                            awk '{print $2}' | \
                            grep -v "^#" | \
                            grep -v "^$")
  
  # Find all image: entries in docker-compose files
  local compose_images=$(find "$services_dir" -name "docker-compose*.yml" -type f | \
                         xargs grep -h "image:" 2>/dev/null | \
                         sed 's/.*image:[[:space:]]*//g' | \
                         sed 's/[[:space:]]*#.*//g' | \
                         sed 's/[[:space:]]*${.*}.*//g' | \
                         grep -v "^#" | \
                         grep -v "^$")
  
  # Combine all images and remove duplicates
  local all_images=$(echo -e "$dockerfile_images\n$compose_images" | sort -u | grep -v "^$")
  
  if [ -z "$all_images" ]; then
    echo "⚠️  No images found in Dockerfiles or docker-compose files"
    return 0
  fi
  
  echo "========================================"
  echo "📦 Images found (Dockerfiles + docker-compose):"
  echo "$all_images" | sed 's/^/   /'
  echo "========================================"
  echo ""
  
  local failed_images=()
  
  while IFS= read -r image; do
    [ -z "$image" ] && continue
    
    echo "⬇️  Pulling: $image"
    
    local attempt=1
    local success=false
    
    while [ $attempt -le $max_retries ]; do
      if docker pull "$image" 2>&1 | grep -E "(Downloaded|Digest|Status: Downloaded|already exists)"; then
        echo "✓ $image pulled successfully"
        success=true
        break
      fi
      
      if [ $attempt -lt $max_retries ]; then
        local wait=$((attempt * retry_delay))
        echo "✗ Attempt $attempt/$max_retries failed. Waiting ${wait}s before retry..."
        sleep $wait
      else
        echo "✗ $image failed after $max_retries attempts"
        failed_images+=("$image")
      fi
      
      ((attempt++))
    done
    
    [ "$success" = true ] && echo ""
    
  done <<< "$all_images"
  
  if [ ${#failed_images[@]} -gt 0 ]; then
    echo "❌ The following images could not be pulled:"
    printf '   %s\n' "${failed_images[@]}"
    return 1
  fi
  
  echo "✓ All images pulled successfully"
  echo "========================================"
  return 0
}
# ====================================================================

# Get docker compose version
docker_version=$(docker compose version --short | cut -d',' -f1)
IFS='.' read -ra version_components <<< "$docker_version"
@@ -51,8 +139,11 @@ then
    exit 1
fi

# Default value for base image pulling
PULL_BASE_IMAGES="true"

# Read params
while getopts ":c:a:l:ms:hrv:f:g:b:" opt; do
while getopts ":c:a:R:l:ms:hrv:f:g:b:t:d:p:" opt; do
  case $opt in
    c)
      CAPIF_HOSTNAME="$OPTARG"
@@ -81,6 +172,15 @@ while getopts ":c:a:l:ms:hrv:f:g:b:" opt; do
    b)
      BUILD_DOCKER_IMAGES="$OPTARG"
      ;;
    t)
      MAX_RETRIES="$OPTARG"
      ;;
    d)
      RETRY_DELAY="$OPTARG"
      ;;
    p)
      PULL_BASE_IMAGES="$OPTARG"
      ;;
    h)
      help
      ;;
@@ -103,6 +203,23 @@ done

echo Nginx hostname will be $CAPIF_HOSTNAME, Vault $CAPIF_VAULT, Register Hostname $CAPIF_REGISTER, deploy $DEPLOY, monitoring $MONITORING_STATE

# ========== PRE-PULL BASE IMAGES WITH RETRIES ==========
if [ "$PULL_BASE_IMAGES" == "true" ] ; then
    echo ""
    echo "🔄 Pre-pulling base images with retries..."
    echo "   Retries: $MAX_RETRIES | Delay: ${RETRY_DELAY}s"
    echo ""
    
    if ! pull_base_images "$SERVICES_DIR" "$MAX_RETRIES" "$RETRY_DELAY"; then
        echo ""
        echo "❌ Error: Could not pull base images"
        echo "   Try manually: docker pull <image>"
        exit 1
    fi
    echo ""
fi
# ========================================================

if [ "$BUILD_DOCKER_IMAGES" == "true" ] ; then
    echo '***Building Docker images set as true***'
    BUILD="--build"
@@ -111,6 +228,13 @@ else
    BUILD="--no-build"
fi

# Display cache status
if [ -n "$CACHED_INFO" ]; then
    echo "***Cache disabled: $CACHED_INFO***"
else
    echo "***Using Docker cache***"
fi

# Deploy Monitoring stack
if [ "$MONITORING_STATE" == "true" ] ; then
    echo '***Monitoring set as true***'
@@ -119,7 +243,7 @@ if [ "$MONITORING_STATE" == "true" ] ; then

    status=$?
    if [ $status -eq 0 ]; then
        echo "*** Monitoring Stack Runing ***"
        echo "*** Monitoring Stack Running ***"
    else
        echo "*** Monitoring Stack failed to start ***"
        exit $status
@@ -128,13 +252,13 @@ fi

docker network create ${CAPIF_NETWORK_NAME}

## Deploy Vault service
# Deploy Vault service
if [ "$DEPLOY_VAULT" == "true" ] ; then
  REGISTRY_BASE_URL=$REGISTRY_BASE_URL OCF_VERSION=$OCF_VERSION CAPIF_HOSTNAME=$CAPIF_HOSTNAME docker compose -f "$SERVICES_DIR/docker-compose-vault.yml" up --detach $BUILD $CACHED_INFO

  status=$?
  if [ $status -eq 0 ]; then
      echo "*** Vault Service Runing ***"
      echo "*** Vault Service Running ***"
  else
      echo "*** Vault failed to start ***"
      exit $status
@@ -152,16 +276,6 @@ else
    exit $status
fi

# # Path to the register config.yaml file
# REGISTER_CONFIG_FILE="$SERVICES_DIR/register/config.yaml"
# # Backup Original config.yaml file
# cp $REGISTER_CONFIG_FILE $REGISTER_CONFIG_FILE.bak
# # Mark the file as assume-unchanged
# git update-index --assume-unchanged "$REGISTER_CONFIG_FILE"

# # Edit Register Service URL within ccf in the config.yaml file
# yq eval ".ccf.url = \"$CAPIF_HOSTNAME\"" -i "$REGISTER_CONFIG_FILE" -P

# Deploy Register service
CAPIF_PRIV_KEY_BASE_64=$(echo "$(cat ${SERVICES_DIR}/nginx/certs/server.key)")
REGISTRY_BASE_URL=$REGISTRY_BASE_URL SERVICES_DIR=$SERVICES_DIR OCF_VERSION=$OCF_VERSION CAPIF_PRIV_KEY=$CAPIF_PRIV_KEY_BASE_64 LOG_LEVEL=$LOG_LEVEL CAPIF_REGISTER=$CAPIF_REGISTER CAPIF_HOSTNAME=$CAPIF_HOSTNAME docker compose -f "$SERVICES_DIR/docker-compose-register.yml" up --detach $BUILD $CACHED_INFO
@@ -182,7 +296,7 @@ if [ "$ROBOT_MOCK_SERVER" == "true" ] ; then
    REGISTRY_BASE_URL=$REGISTRY_BASE_URL SERVICES_DIR=$SERVICES_DIR OCF_VERSION=$OCF_VERSION IP=$MOCK_SERVER_IP PORT=$MOCK_SERVER_PORT docker compose -f "$SERVICES_DIR/docker-compose-mock-server.yml" up --detach $BUILD $CACHED_INFO
    status=$?
    if [ $status -eq 0 ]; then
        echo "*** Mock Server Runing ***"
        echo "*** Mock Server Running ***"
    else
        echo "*** Mock Server failed to start ***"
        exit $status