This repository has been archived by the owner on Jul 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcleanup-job.sh
executable file
·127 lines (114 loc) · 4.54 KB
/
cleanup-job.sh
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/bin/bash
set -o pipefail
function output_help {
echo "Create custom cleanup jobs and excute Nexus pre-configured tasks";
echo "";
echo "Example: `basename $0` -p "7 ^v2\/.*\/cache\/.*$ last_downloaded" -t "ab12c229-424a-4a69-8318-22dc21876c3b"";
echo "";
echo "Options:";
echo " -p parameters for setting up custom cleanup script ";
echo " Should come as a string with spaced seperated values";
echo " Values should come from OrientDb query, please look into image README for more info";
echo " -t Nexus tasks IDs to run after custom cleanup";
echo "";
}
function load_script {
# check if the script is not already on Nexus
output=$(curl -u ${NEXUS_AUTH} -X GET "${NEXUS_URL}/service/rest/v1/script" -H "accept: application/json" | grep dockerCleanup)
if [ -z "$output" ];
then
local BODY;
BODY=$(cat /scripts/dockerCleanup.groovy | tr -d '\n');
echo $BODY;
curl -i -u ${NEXUS_AUTH} -X POST "${NEXUS_URL}/service/rest/v1/script" -H "accept: application/json" -H "Content-Type: application/json" -d "{ \"name\": \"dockerCleanup\", \"content\": \"${BODY}\", \"type\": \"groovy\"}"
fi
}
# If you want to delete this script, run this function.
function delete_script {
curl -i -u ${NEXUS_AUTH} -X DELETE "${NEXUS_URL}/service/rest/v1/script/dockerCleanup" -H "accept: application/json"
if [ $? == 0 ];
then
echo 'The script deleted succesfully'
else
echo 'There was an error while deleting the script, please try deleting the script from Nexus ui'
fi
}
function adjust_dates {
local d;
d=$(date --date='-'${1}' day' +%Y-%m-%d)
echo "$d"
}
function escape_url {
escape=$(echo "${1}" | sed -e 's:\\:\\\\:g' )
echo "$escape"
}
function run_final_cleanup {
# Triggering needed tasks including hard cleanup task.
last=${#@};
i=0;
prevtask=0;
status='"RUNNING"'
for d in "$@" ;
do
if [ $prevtask == '0' ];
then
echo "Running Task with ID $d";
curl -i -u ${NEXUS_AUTH} -X POST "${NEXUS_URL}/service/rest/v1/tasks/$d/run" -H "accept: application/json";
prevtask=$d
else
while [ $status == '"RUNNING"' ]; do
echo "Checking Previous Task status";
status=$(curl -i -u ${NEXUS_AUTH} -X GET "${NEXUS_URL}/service/rest/v1/tasks/$prevtask" -H "accept: application/json" | grep -Eo '"currentState" : "[A-Z]*"' | sed -e 's/"currentState" : //');
echo "Status is $status"
if [ $status == '"RUNNING"' ]; then
sleeptime=$((5*60)); # wait for 5 minutes until next status check
sleep ${sleeptime};
else
echo "Running Task with ID $d";
curl -i -u ${NEXUS_AUTH} -X POST "${NEXUS_URL}/service/rest/v1/tasks/$d/run" -H "accept: application/json";
prevtask=$d
status='"RUNNING"'
break;
fi
done
fi
done
}
function create_curl {
DATE=$(adjust_dates ${1});
URL=$(escape_url ${2});
TIMEFILTER="${3}";
if [ -z "${4}" ]
then
NOTDOWNLOADED="false"
else
NOTDOWNLOADED="${4}"
fi
curl -i -u ${NEXUS_AUTH} -X POST "${NEXUS_URL}/service/rest/v1/script/dockerCleanup/run" -H "accept: application/json" -H "Content-Type: text/plain" -d "{\"repoName\":\"${NEXUS_REPO}\",\"startDate\":\"${DATE}\",\"url\":\"${URL}\",\"timeFilter\":\"${TIMEFILTER}\",\"notDownloaded\":\"${NOTDOWNLOADED}\"}"
}
delete_script;
echo "==> Loading Cleanup script" & load_script;
echo "==> Running Custom Cleanup";
while getopts ":p:t:h" opt; do
case $opt in
p ) set -f # disable glob
IFS=' ' # split on space characters
array=($OPTARG) # use the split+glob operator
if [ "${#array[@]}" = 3 ]|| [ "${#array[@]}" = 4 ] ;
then
create_curl ${array[@]}
else
echo "${array[@]} is not valid, please have 3 parameters per query seperated by space ,/n format \"1 '^v2\/.*\/cache\/.*$' 'blob_created' \" "
fi;;
t ) set -f # disable glob
IFS=' ' # split on space characters
tasks=($OPTARG) ;; # use the split+glob operator
h ) output_help;
exit 0 ;;
* ) echo "-$OPTARG is not supported "
exit 1 ;;
esac
done
echo "==> Running final Cleanup"
run_final_cleanup ${tasks[@]}
printf "\n Cleanup Ended succesfully!"