Project

General

Profile

blacklist.sh

Marco Mambelli, 11/27/2018 04:04 PM

 
1
#!/bin/bash
2
# Script to send in with glide-ins to blacklist slots per VO when needed
3

    
4
glidein_config="$1"
5

    
6
function info (
7
    echo "$@" 1>&2
8
}
9

    
10
function advertise {
11
    # atype is the type of the value as defined by GlideinWMS:
12
    #   I - integer
13
    #   S - quoted string
14
    #   C - unquoted string (i.e. Condor keyword or expression)
15
    local key="$1"
16
    local value="$2"
17
    local atype="$3"
18

    
19
    if [ "$glidein_config" != "NONE" ]; then
20
        add_config_line $key "$value"
21
        add_condor_vars_line $key "$atype" "-" "+" "Y" "Y" "+" 
22
    fi  
23

    
24
    if [ "$atype" = "S" ]; then
25
        echo "$key = \"$value\""
26
    else
27
        echo "$key = $value"
28
    fi  
29
}
30

    
31
# import add_config_line and add_condor_vars_line functions 
32

    
33
add_config_line_source=`grep '^ADD_CONFIG_LINE_SOURCE ' "${glidein_config}" | awk '{print $2}'`
34
condor_vars_file=`grep -i '^CONDOR_VARS_FILE ' "${glidein_config}" | awk '{print $2}'` 
35
error_gen=`grep '^ERROR_GEN_PATH ' "${glidein_config}" | awk '{print $2}'`
36

    
37
source ${add_config_line_source} 
38

    
39
#----------MAIN SCRIPT----------#
40

    
41
MYKERNEL=`uname -r`
42
MYSITE=`grep -w "GLIDEIN_Site" "${glidein_config}" |awk '{print $2}'`
43

    
44
TMPFILE=./blacklist.local.$$
45
ITBFEGROUP="fife_test"
46

    
47
# Get the Glideclient so we know if we're in dev, itb, or prod
48
glideclientgroup=`grep '^GLIDECLIENT_Group' "${glidein_config}" | awk '{print $2}'`
49

    
50
# Find which one matches, set which url we want to use
51

    
52
# WAS: if [[ -n ${glideclientgroup+x} ]] && [[ $glideclientgroup == $ITBFEGROUP ]] ; 
53
# Not sure I interpreted it correctly: glideclientgroup not empty and equal to ...
54
# Also: if [[ -n "${glideclientgroup}" ]] && [[ "$glideclientgroup" == $ITBFEGROUP ]] ; 
55
if [ "x$glideclientgroup" = "x$ITBFEGROUP" ];
56
then
57
	info "This is the itb frontend group.  Using the itb blacklist"
58
	urlpat="-itb"
59
else
60
	info "Using the production blacklist"
61
	urlpat=""
62
fi  
63

    
64
blacklist_url="https://fife-service.fnal.gov/blacklist${urlpat}.txt"
65
info $blacklist_url
66

    
67
# Max 5 retries
68
n=0
69
until [ $n -ge 5 ]
70
do
71
	#Replace the next line with the real webserver/blacklist file
72
	curl -s --insecure $blacklist_url > $TMPFILE && break
73
	n=$[$n+1]
74
	sleep 60
75
done
76

    
77
# Parse the blacklist file, store it in the BAD_JOBSUB_GROUPS variable
78
info "Parsing blacklist file"
79

    
80
awkcmd='
81
function checklines(valuestring, checkvar) {
82
        n = split(valuestring, vals, ",");
83
        for (i=1; i<=n; i++) {
84
                if (checkvar ~ vals[i]) {
85
                        return "true";
86
                }   
87
        }   
88
        return "false";
89
}
90

    
91
{
92
        firstfield = $1; 
93
        target = $2; 
94
        if (substr(firstfield, 1, 1) != "#")  {   
95
                split(target, labelvalues, ":"); 
96
                label = labelvalues[1];
97
                values = labelvalues[2];
98
    
99
                if (label == "host") {
100
                        result = checklines(values, myhost);
101
                        if (result == "true") {
102
                                print firstfield;
103
                        }   
104
                } else if (label == "site") {
105
                        result = checklines(values, mysite);
106
                        if (result == "true") {
107
                                print firstfield;
108
                        }   
109
                } else if (label == "kernel") {
110
                        result = checklines(values, mykernel);
111
                        if (result == "true") {
112
                                print firstfield;
113
                        }   
114
                } else {
115
                        print ""; 
116
                }   
117
        }   
118
}
119
'
120

    
121
BAD_JOBSUB_GROUPS=`awk -v myhost=$HOSTNAME -v mysite=$MYSITE -v mykernel=$MYKERNEL "$awkcmd" $TMPFILE | tr '\n' ',' | sed  's/,*$//g;s/^,*//g' ` 
122

    
123
info "blacklisted VOs are: $BAD_JOBSUB_GROUPS"
124

    
125

    
126
# Also to check if empty: [ -z "$BAD_JOBSUB_GROUPS" ] && BAD_JOBSUB_GROUPS="EMPTY"
127
if [ ${#BAD_JOBSUB_GROUPS} == 0 ]
128
then
129
	BAD_JOBSUB_GROUPS="EMPTY"	
130
fi
131

    
132
BADATTR="SLOT_BAD_JOBSUB_GROUPS"
133

    
134
# Advertise back the VOs for which this slot won't work
135
advertise $BADATTR "${BAD_JOBSUB_GROUPS}" "S" 
136

    
137
if [ -f $TMPFILE ]
138
then
139
	rm $TMPFILE
140
fi
141

    
142
# error_gen will log the forst execution in the gwms scripts results log. For periodic scripts the wrapper will do it for you.
143
#"$error_gen" -ok  "blacklist.sh"
144
exit 0