Code development platform for open source projects from the European Union institutions :large_blue_circle: EU Login authentication by SMS has been phased out. To see alternatives please check here

Skip to content
Snippets Groups Projects

Feature/ci upgrade container

Merged Magdalena GOMEZ requested to merge feature/CI_upgrade_container into main

Files

+ 147
15
@@ -36,10 +36,28 @@ spec:
- if: $CI_COMMIT_TAG
when: on_success
.bash_functions: &bash_functions
# curl GET info from netbox
# syntax: function_name 1-GET/POST/PATH/DELETE 2-api_enpoint 3-json_content 4-output_file
# cmd e.g.: curl_netbox_function "POST" "$[[ inputs.NETBOX_URL ]]/api/plugins/docker/hosts/" '{}' "res.json"
- |
curl_netbox_function () {
# print command && squeeze-repeats
echo "curl -s -X ${1} $[[ inputs.NETBOX_URL ]]/api/${2} -d ${3}" |tr -s ' '
# execute
curl -s -X ${1} "$[[ inputs.NETBOX_URL ]]/api/${2}" \
-H 'accept: application/json; indent=4' \
-H 'Content-Type: application/json' \
-H "Authorization: Token ${NETBOX_TOKEN}" \
-d "${3}" \
> "$CI_PROJECT_DIR/opt_tmp/${4}"
}
deploy-img-job:
stage: $[[ inputs.stage ]]
before_script:
# requirements
- *bash_functions
- mkdir -p "$CI_PROJECT_DIR/opt_tmp/"
- sudo apt install jq -y
# decide list of netbox to target
@@ -71,13 +89,11 @@ deploy-img-job:
# declare variables
NETBOX_URL=$(echo ${str1} | jq -r '.netbox')
NETBOX_TOKEN=$(echo ${str1} | jq -r '.token')
echo ${str1} | jq -r '.description'
netbox_description=$(echo ${str1} | jq -r '.description')
echo "Iterate over ${netbox_description}: ${NETBOX_URL}"
# curl GET info from netbox
curl -X 'GET' "$[[ inputs.NETBOX_URL ]]/api/plugins/docker/hosts/" \
-H 'accept: application/json; indent=4' \
-H "Authorization: Token ${NETBOX_TOKEN}" \
> "$CI_PROJECT_DIR/opt_tmp/res.json"
curl_netbox_function "GET" "plugins/docker/hosts/" '{}' "res.json"
# iterate over VMs with a Netbox inside.
for str2 in $(cat "$CI_PROJECT_DIR/opt_tmp/res.json" | \
@@ -86,23 +102,139 @@ deploy-img-job:
do
# VMs with a netbox (host_id).
NETBOX_PLUGIN_HOST_ID="${str2}"
echo $NETBOX_PLUGIN_HOST_ID
echo "netbox_plugin_host_id to connect to: $NETBOX_PLUGIN_HOST_ID"
# recolect image info
#e.g.: curl -X 'GET' "mylab.d.publicvm.com:8082/api/plugins/docker/hosts/" -H 'accept: application/json; indent=4' -H "Authorization: Token b63.." | jq '.results[] | select(.id | contains(19)) | .images[] | select(.display | contains("code.europa.eu:4567/digit-c4/netbox/netbox-backup-agent:v1.2.1") ) | .size'
# save image JSON.
cat "$CI_PROJECT_DIR/opt_tmp/res.json" | jq '.results[] | select(.id | contains('${NETBOX_PLUGIN_HOST_ID}')) | .images[] | select(.display | contains("'$[[ inputs.REGISTRY_IMAGE_DEPLOY ]]':'$[[ inputs.COMMIT_TAG_DEPLOY ]]'") ) ' > "$CI_PROJECT_DIR/opt_tmp/img.json"
# set variables
imgSize=$(cat "$CI_PROJECT_DIR/opt_tmp/img.json" | jq '.size')
imgId=$(cat "$CI_PROJECT_DIR/opt_tmp/img.json" | jq '.id')
echo "imgSize: ${imgSize}, imgId: ${imgId}"
# check if image already exist.
if [[ ${imgSize} > 0 ]]; then echo "WARNING: Good image already uploaded. Nothing to do here."; exit 1; fi
if [[ ${imgId} != '' ]]; then echo "WARNING: Bad image already uploaded. Try to remove that manually."; exit 1; fi
# curl POST image to netbox
curl -X 'POST' "$[[ inputs.NETBOX_URL ]]/api/plugins/docker/images/" \
-H 'accept: application/json; indent=4' \
-H 'Content-Type: application/json' \
-H "Authorization: Token ${NETBOX_TOKEN}" \
-d "{ \
curl_netbox_function "POST" "plugins/docker/images/" "{ \
\"name\": \"$[[ inputs.REGISTRY_IMAGE_DEPLOY ]]\", \
\"version\": \"$[[ inputs.COMMIT_TAG_DEPLOY ]]\", \
\"host\": \"${NETBOX_PLUGIN_HOST_ID}\", \
\"registry\": {\"serveraddress\": \"$[[ inputs.REGISTRY_CHECK ]]\"} \
}" \
> "$CI_PROJECT_DIR/opt_tmp/res2.json"
}" "res2.json"
# wait until image is pulled
for ((i=1; i<10; i++)); do
# curl recolect image info
#e.g.: curl -X 'GET' "mylab.d.publicvm.com:8082/api/plugins/docker/hosts/" -H 'accept: application/json; indent=4' -H "Authorization: Token b63.." | jq '.results[] | select(.id | contains(19)) | .images[] | select(.display | contains("code.europa.eu:4567/digit-c4/netbox/netbox-backup-agent:v1.2.1") ) | .size'
sleep 20 && curl_netbox_function "GET" "plugins/docker/hosts/" '{}' "res3.json"
# save image JSON.
cat "$CI_PROJECT_DIR/opt_tmp/res3.json" | jq '.results[] | select(.id | contains('${NETBOX_PLUGIN_HOST_ID}')) | .images[] | select(.display | contains("'$[[ inputs.REGISTRY_IMAGE_DEPLOY ]]':'$[[ inputs.COMMIT_TAG_DEPLOY ]]'") ) ' > "$CI_PROJECT_DIR/opt_tmp/img.json"
# set variables
imgSize=$(cat "$CI_PROJECT_DIR/opt_tmp/img.json" | jq '.size')
imgId=$(cat "$CI_PROJECT_DIR/opt_tmp/img.json" | jq '.id')
echo "imgSize: ${imgSize}, imgId: ${imgId}"
# check that image has been uploaded
if [[ ${imgSize} > 0 ]];
then echo "image uploaded."; break;
else echo "image not yet uploaded, sleep for 20 seconds..";
fi
if [[ $i == 10 ]];
then echo "It seems not able to upload image to ${NETBOX_URL}, host_id: ${NETBOX_PLUGIN_HOST_ID}."; exit 1;
fi
done
# curl recolect container info
#e.g.: curl -X 'GET' "mylab.d.publicvm.com:8082/api/plugins/docker/containers/" -H 'accept: application/json; indent=4' -H "Authorization: Token b63.." -H 'Content-Type: application/json' | jq '.results[] |select(.image.name | contains("code.europa.eu:4567/digit-c4/netbox/netbox-backup-agent"))'
curl_netbox_function "GET" "plugins/docker/containers/" '{}' "res_containers.json"
# save container to use to JSON.
cat "$CI_PROJECT_DIR/opt_tmp/res_containers.json" | jq '[.results[] |select(.image.name | contains("'$[[ inputs.REGISTRY_IMAGE_DEPLOY ]]'") ) ]' > "$CI_PROJECT_DIR/opt_tmp/containers_to_update.json"
# Check how many containers need to be updated
container_count=$(jq '. | length' "$CI_PROJECT_DIR/opt_tmp/containers_to_update.json")
if [[ $container_count -eq 0 ]]; then
echo "No containers need an update."
continue
fi
echo "$container_count containers need to be updated."
jq -c '.[]' "$CI_PROJECT_DIR/opt_tmp/containers_to_update.json" | while read -r container; do
# set variables
contName=$(echo $container | jq '.name')
contId=$(echo $container | jq '.id')
contImgId=$(echo $container | jq '.image.id')
echo "contName: ${contName}, contId: ${contId}, contImgId: ${contImgId}"
# check variables results
if [[ ${imgSize} == null || ${imgId} == null || ${imgId} == '' ]]; then echo "could not find some image info." ; exit 1; fi
if [[ ${contImgId} == null || ${contId} == null || ${contName} == null || ${contId} == '' ]]; then echo "could not find some container info."; exit 1; fi
# check that container is not using new image already
if [[ $contImgId == $imgId ]]; then
echo "Container $contName is already using the new image. Skipping..."
continue
fi
# check if new tag is bigger that older tag
current_tag=$(echo $container | jq -r '.image.version')
new_tag="$[[ inputs.COMMIT_TAG_DEPLOY ]]"
# If current tag is invalid or missing, force update
if [[ -z "$current_tag" || "$current_tag" == "null" ]]; then
echo "Current tag is invalid or missing. Proceeding with update..."
else
# Compare versions
if [[ $(echo -e "$current_tag\n$new_tag" | sort -V | tail -n 1) != "$new_tag" ]]; then
echo "New tag ($new_tag) is not greater than current tag ($current_tag). Skipping update."
continue
fi
fi
echo "New tag ($new_tag) is greater than current tag ($current_tag). Proceeding with update..."
echo "Recreating container $contName with new image..."
# curl update&&recreate container, TO-DO: might need to include external ports!!!!
#e.g.: curl -X 'PATCH' "mylab.d.publicvm.com:8082/api/plugins/docker/containers/4/" -H 'accept: application/json; indent=4' -H "Authorization: Token b6.." -H 'Content-Type: application/json' -d "{ \"name\": \"netbox-gxy-bckp-agt-0\", \"image\": \"11\", \"operation\": \"recreate\" }"
curl_netbox_function "PATCH" "plugins/docker/containers/${contId}/" \
"{ \"name\": ${contName}, \"image\": \"${imgId}\", \"operation\": \"recreate\" }" "res_update.json"
# wait until container is restarted
for ((i=1; i<10; i++)); do
# curl recolect container info
#e.g.: curl -X 'GET' "mylab.d.publicvm.com:8082/api/plugins/docker/containers/" -H 'accept: application/json; indent=4' -H "Authorization: Token b63.." -H 'Content-Type: application/json' | jq '.results[] |select(.image.name | contains("code.europa.eu:4567/digit-c4/netbox/netbox-backup-agent"))'
sleep 20 && curl_netbox_function "GET" "plugins/docker/containers/${contId}/" '{}' "res_container_updated.json"
# save container JSON.
# cat "$CI_PROJECT_DIR/opt_tmp/res_container_updated.json" | jq '.results[] |select(.image.name | contains("'$[[ inputs.REGISTRY_IMAGE_DEPLOY ]]'") ) ' > "$CI_PROJECT_DIR/opt_tmp/cont.json"
# set variables
contOperation=$(cat "$CI_PROJECT_DIR/opt_tmp/res_container_updated.json" | jq '.operation')
contImgId=$(cat "$CI_PROJECT_DIR/opt_tmp/res_container_updated.json" | jq '.image.id')
echo "contOperation: ${contOperation}, contImgId: ${contImgId}"
# check that container has been recreated
if [[ ${contOperation:1:4} == "none" ]];
then echo "container recreated and ready."; break;
else echo "container not yet recreated, sleep for 20 seconds..";
fi
if [[ $i == 10 ]];
then echo "It seems not able to upload image to ${NETBOX_URL}, host_id: ${NETBOX_PLUGIN_HOST_ID}."; exit 1;
fi
done
# check variables results
if [[ ${contImgId} == null || ${contOperation} == null ]]; then echo "could not find some container info. Skipping update."; continue; fi
# check that container is using new image.
if [[ ${imgId} != ${contImgId} || ${contOperation:1:4} != "none" ]]; then echo "container not using new image. Skipping update."; continue; fi
# print extra output
if [[ $[[ inputs.VERBOSITY ]] == true ]]; then cat $CI_PROJECT_DIR/opt_tmp/res2.json; fi
# print extra output
if [[ $[[ inputs.VERBOSITY ]] == true ]]; then cat $CI_PROJECT_DIR/opt_tmp/res2.json;
fi
done
done
done
- echo "Ended."
Loading