Skip to content

Commit

Permalink
Code improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
yenienserrano committed Nov 12, 2024
1 parent 52ffa72 commit 86ca490
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 70 deletions.
63 changes: 19 additions & 44 deletions dev-tools/build-packages/build-packages.sh
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ get_packages(){
cd ..
}

source ../utils/retry-operation.sh

build_tar() {
log
log "Building base package..."
Expand All @@ -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 ..
}

Expand All @@ -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 ../
}

Expand All @@ -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 ..
}

Expand Down
36 changes: 10 additions & 26 deletions dev-tools/test-packages/test-packages.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions dev-tools/utils/retry-operation.sh
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 86ca490

Please sign in to comment.