From 86ca4900cf0e923777cfa7f57cd3fff9665d1f8f Mon Sep 17 00:00:00 2001 From: Ian Yenien Serrano <63758389+yenienserrano@users.noreply.github.com> Date: Tue, 12 Nov 2024 20:03:50 +0100 Subject: [PATCH] Code improvement --- dev-tools/build-packages/build-packages.sh | 63 +++++++--------------- dev-tools/test-packages/test-packages.sh | 36 ++++--------- dev-tools/utils/retry-operation.sh | 26 +++++++++ 3 files changed, 55 insertions(+), 70 deletions(-) create mode 100644 dev-tools/utils/retry-operation.sh diff --git a/dev-tools/build-packages/build-packages.sh b/dev-tools/build-packages/build-packages.sh index 4bb959f184b0..75f16b28735f 100755 --- a/dev-tools/build-packages/build-packages.sh +++ b/dev-tools/build-packages/build-packages.sh @@ -81,6 +81,8 @@ get_packages(){ cd .. } +source ../utils/retry-operation.sh + build_tar() { log log "Building base package..." @@ -92,21 +94,12 @@ build_tar() { container_name="dashboard-base-builder" cp ./base-builder.sh ${dockerfile_path} cp ./plugins ${dockerfile_path} - for i in {1..5}; do - if docker build -t ${container_name} ${dockerfile_path} && \ - docker run -t --rm \ - -v ${tmp_dir}/:/tmp:Z \ - -v ${output_dir}/:/output:Z \ - ${container_name} ${version} ${revision} ${architecture} ${verbose}; then - break - else - if [ $i -eq 5 ]; then - echo "Failed to build the base package after 5 attempts" - exit 1 - fi - sleep 15 - fi - done + retry_operation "build base package" 5 15 \ + "docker build -t ${container_name} ${dockerfile_path} && \ + docker run -t --rm \ + -v ${tmp_dir}/:/tmp:Z \ + -v ${output_dir}/:/output:Z \ + ${container_name} ${version} ${revision} ${architecture} ${verbose}" || exit 1 cd .. } @@ -118,22 +111,13 @@ build_rpm() { cp -r ${package_config_dir} ${tmp_dir} cp ./rpm-builder.sh ${dockerfile_path} cp ./wazuh-dashboard.spec ${dockerfile_path} - for i in {1..5}; do - if docker build -t ${container_name} ${dockerfile_path} && \ - docker run -t --rm \ + retry_operation "build rpm package" 5 15 \ + "docker build -t ${container_name} ${dockerfile_path} && \ + docker run -t --rm \ -v ${tmp_dir}/:/tmp:Z \ -v ${output_dir}/:/output:Z \ ${container_name} ${version} ${revision} ${architecture} \ - ${commit_sha} ${production} ${verbose}; then - break - else - if [ $i -eq 5 ]; then - echo "Failed to build the rpm package after 5 attempts" - exit 1 - fi - sleep 15 - fi - done + ${commit_sha} ${production} ${verbose}" || exit 1 cd ../ } @@ -147,22 +131,13 @@ build_deb() { cp ./deb-builder.sh ${dockerfile_path} cp -r ./debian ${dockerfile_path} docker build -t ${container_name} ${dockerfile_path} || return 1 - for i in {1..5}; do - if docker build -t ${container_name} ${dockerfile_path} && \ - docker run -t --rm \ - -v ${tmp_dir}/:/tmp:Z \ - -v ${output_dir}/:/output:Z \ - ${container_name} ${version} ${revision} ${architecture} \ - ${commit_sha} ${production} ${verbose}; then - break - else - if [ $i -eq 5 ]; then - echo "Failed to build the deb package after 5 attempts" - exit 1 - fi - sleep 15 - fi - done + retry_operation "build deb package" 5 15 \ + "docker build -t ${container_name} ${dockerfile_path} && \ + docker run -t --rm \ + -v ${tmp_dir}/:/tmp:Z \ + -v ${output_dir}/:/output:Z \ + ${container_name} ${version} ${revision} ${architecture} \ + ${commit_sha} ${production} ${verbose}" || exit 1 cd .. } diff --git a/dev-tools/test-packages/test-packages.sh b/dev-tools/test-packages/test-packages.sh index eb73b63f0501..c40dcbf49e8f 100644 --- a/dev-tools/test-packages/test-packages.sh +++ b/dev-tools/test-packages/test-packages.sh @@ -107,37 +107,21 @@ check_metadata_rpm() { echo "metadata package is correct: $metadataPackage" } +source ../utils/retry-operation.sh + # Run test test() { if [[ $PACKAGE == *".deb" ]]; then - for i in {1..5}; do - if docker build --build-arg PACKAGE=$PACKAGE -t $CONTAINER_NAME ./deb/ && \ - docker run -it --rm -d --name $CONTAINER_NAME $CONTAINER_NAME && \ - check_metadata_deb; then - break - else - if [ $i -eq 5 ]; then - echo "Failed to test the deb package after 5 attempts" - exit 1 - fi - sleep 15 - fi - done + retry_operation "test deb package" 5 15 \ + "docker build --build-arg PACKAGE=$PACKAGE -t $CONTAINER_NAME ./deb/ && \ + docker run -it --rm -d --name $CONTAINER_NAME $CONTAINER_NAME && \ + check_metadata_deb" || exit 1 elif [[ $PACKAGE == *".rpm" ]]; then - for i in {1..5}; do - if docker build --build-arg PACKAGE=$PACKAGE -t $CONTAINER_NAME ./rpm/ && \ - docker run -it --rm -d --name $CONTAINER_NAME $CONTAINER_NAME && \ - check_metadata_rpm; then - break - else - if [ $i -eq 5 ]; then - echo "Failed to test the rpm package after 5 attempts" - exit 1 - fi - sleep 15 - fi - done + retry_operation "test rpm package" 5 15 \ + "docker build --build-arg PACKAGE=$PACKAGE -t $CONTAINER_NAME ./rpm/ && \ + docker run -it --rm -d --name $CONTAINER_NAME $CONTAINER_NAME && \ + check_metadata_rpm" || exit 1 else echo "ERROR: $PACKAGE is not a valid package (valid packages are .deb and .rpm ))" exit 1 diff --git a/dev-tools/utils/retry-operation.sh b/dev-tools/utils/retry-operation.sh new file mode 100644 index 000000000000..e128ff0538cf --- /dev/null +++ b/dev-tools/utils/retry-operation.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +# Function to retry an operation a number of times +# Arguments: +# $1: Name of the operation +# $2: Number of attempts +# $3: Delay between attempts +# $4+: Command to execute +retry_operation() { + local operation_name=$1 + local max_attempts=$2 + local sleep_time=$3 + shift 3 # Delete the first 3 arguments + + for i in $(seq 1 $max_attempts); do + if eval "$@"; then + return 0 + else + if [ $i -eq $max_attempts ]; then + echo "Failed to $operation_name after $max_attempts attempts" + return 1 + fi + sleep $sleep_time + fi + done +}