jalien-ce.sh 7.47 KB
Newer Older
Kalana Wijethunga's avatar
Kalana Wijethunga committed
1
2
#!/bin/bash

Maarten Litmaath's avatar
Maarten Litmaath committed
3
# JAliEn VObox Startup Scripts - CE
Maarten Litmaath's avatar
Maarten Litmaath committed
4
# v1.3 - 2021-09-19
Maarten Litmaath's avatar
Maarten Litmaath committed
5
6
7
8
# Authors:
#     Kalana Dananjaya <kwijethu@cern.ch>
#     Costin  Grigoras <Costin.Grigoras@cern.ch>
#     Maarten Litmaath <Maarten.Litmaath@cern.ch>
Kalana Wijethunga's avatar
Kalana Wijethunga committed
9
10

ceClassName=alien.site.ComputingElement
11
12
jalienDir=/cvmfs/alice.cern.ch/el[67]-x86_64/Packages/JAliEn   # matches el6 and el7
commonConf="$confDir/version.properties"
Kalana Wijethunga's avatar
Kalana Wijethunga committed
13

14
15
16
export JALIEN_CONF_DIR=${JALIEN_CONF_DIR:-$HOME/.j/config}

########################################################################################
Kalana Wijethunga's avatar
Kalana Wijethunga committed
17
18
19
20
21
# Write log to file
# Globals:
#   setupLogFile: Log file for MonaLisa setup
# Arguments:
#   $1: String to log
22
23
24
########################################################################################
function write_log() {
	echo "$@" >> $setupLogFile
Kalana Wijethunga's avatar
Kalana Wijethunga committed
25
26
}

27
########################################################################################
Kalana Wijethunga's avatar
Kalana Wijethunga committed
28
29
30
# Stop CE
# Globals:
#	ceClassName : Computing Element classname
31
########################################################################################
Kalana Wijethunga's avatar
Kalana Wijethunga committed
32
33
34
35
36
37
38
39
40
41
42
43
44
function stop_ce() {
	echo "Stopping JAliEn CE..."
	pkill -TERM -f $ceClassName
	sleep 3

	status_ce || return 0

	echo "Killing JAliEn CE..."
	pkill -KILL -f $ceClassName

	! status_ce
}

45
########################################################################################
46
47
48
49
50
# CE PID
# Prints:
#   PID if process is running, else nothing
# Returns:
#   0 if process is running, else 1
51
52
########################################################################################
function get_ce_pid() {
53
54
55
	ps uxwww | awk "/[ ]$ceClassName/ { print \$2 }" | grep .
}

56
########################################################################################
Kalana Wijethunga's avatar
Kalana Wijethunga committed
57
58
# CE liveness check
# Returns:
59
#   0 if process is running, else 1
60
61
########################################################################################
function check_liveness_ce() {
62
	if get_ce_pid > /dev/null
Kalana Wijethunga's avatar
Kalana Wijethunga committed
63
64
65
66
67
68
69
	then
		return 0
	else
		return 1
	fi
}

70
########################################################################################
Kalana Wijethunga's avatar
Kalana Wijethunga committed
71
72
73
74
# Check CE status
# Arguments:
#   $command: "status" or "mlstatus"
# Returns: 
75
76
#	0 if process is running, else 1
########################################################################################
Kalana Wijethunga's avatar
Kalana Wijethunga committed
77
78
79
function status_ce() {
	command=$1

80
	pid=$(get_ce_pid)
Kalana Wijethunga's avatar
Kalana Wijethunga committed
81
82
83
84
85
86
	exit_code=$?

	[[ $exit_code == 0 ]] && not= || not=' Not'

	if [[ "$command" == mlstatus ]]
	then
87
88
89
90
91
		ceConfigVersion=$(
			[[ -f "$commonConf" ]] &&
			perl -ne 'print $1 if /^\s*jalien\s*=\s*(\S+)/i' $commonConf
		)

92
		: ${ceConfigVersion:=pro}
93
94
95
96

		if [[ -n "$pid" ]]
		then
			ceEnvRunningVersion=$(
97
98
99
100
101
102
103
104
105
106
107
108
		    		(tr \\0 \\n < /proc/"$pid"/environ) 2> /dev/null |
				perl -ne "
					BEGIN {
						\$v = 'local';
					}
					if (m,^CLASSPATH=.*?$jalienDir/([^/:]+),) {
						\$v=\$1;
					}
					END {
						print \$v if \$.;
					}
				"
109
110
111
112
113
			)
		else
			ceEnvRunningVersion=
		fi

114
115
116
		jaVersion=
		jconf=$JALIEN_CONF_DIR/version.properties

117
		if [[ -e $jconf ]]
118
119
120
121
		then
			jaVersion=$(perl -ne 'print $1 if /^\s*jobagent\.version\s*=\s*(\S+)/i' $jconf)
			jardir=$(perl -ne 'print $1 if /^\s*custom\.jobagent\.jar\.dir\s*=\s*(\S+)/i' $jconf)

122
			if [[ -e "$jardir/alien-users.jar" ]]
123
124
125
126
127
128
129
			then
				jaVersion=custom
			fi
		fi

		jaVersion=${jaVersion:-${ceEnvRunningVersion:-$ceConfigVersion}}

Costin Grigoras's avatar
Costin Grigoras committed
130
		msg="CE\tStatus\t$exit_code\tMessage\tCE$not Running"
131
		msg="$msg\tConfigVersion\t$ceConfigVersion"
132
		msg="$msg\tRunningVersion\t${ceEnvRunningVersion:-n/a}"
133
		msg="$msg\tJAVersion\t$jaVersion"
134
135

		echo -e "$msg"
Kalana Wijethunga's avatar
Kalana Wijethunga committed
136
137
138
139
140
141
142
	else
		echo -e "CE$not Running"
	fi

	return $exit_code
}

143
########################################################################################
Kalana Wijethunga's avatar
Kalana Wijethunga committed
144
145
146
# Start MonaLisa
# Globals:
#	siteConfiguration: Associative array of site configuration parameters in LDAP
147
#	commonConfiguration: Associative array of JAliEn local configuration parameters
Kalana Wijethunga's avatar
Kalana Wijethunga committed
148
# Arguments:
149
#	confDir: AliEn configuration directory
Kalana Wijethunga's avatar
Kalana Wijethunga committed
150
151
152
#	ldapHostname: LDAP hostname
#	ldapPort: LDAP port
#	hostname: Site hostname
153
154
########################################################################################
function start_ce() {
Kalana Wijethunga's avatar
Kalana Wijethunga committed
155
156
157
158
159
160
161
162
163
164
165
166
167
168
	confDir=$1
	ldapHostname=$2
	ldapPort=$3
	hostname=$4
	nl='
	'
	nl=${nl:0:1}

	if check_liveness_ce
	then
		echo "JAliEn CE already running"
		return 0
	fi

169
170
171
172
	#
	# Obtain site configuration from LDAP
	#

Kalana Wijethunga's avatar
Kalana Wijethunga committed
173
174
175
176
177
178
179
180
181
	ldapBase="ou=Sites,o=alice,dc=cern,dc=ch"
	ldapFilter="(&(objectClass=AliEnHostConfig)(host=$hostname))"

	siteLDAPQuery=$(
		ldapsearch -x -LLL -h $ldapHostname -p $ldapPort -b $ldapBase "$ldapFilter" |
		perl -p00e 's/\n //g' | envsubst
	)

	declare -A siteConfiguration
182

Kalana Wijethunga's avatar
Kalana Wijethunga committed
183
184
	while IFS= read -r line
	do
185
186
187
188
		#
		# Create an associative array from the LDAP configuration
		#

Kalana Wijethunga's avatar
Kalana Wijethunga committed
189
190
191
192
193
194
195
196
197
198
199
200
201
202
		if [[ ! -z $line ]]
		then
			key=$(echo "$line" | cut -d ":" -f 1 )
			val=$(echo "$line" | cut -d ":" -f 2- | sed s/.//)

			key=${key^^}
			prev=${siteConfiguration[$key]}
			prev=${prev:+$prev$nl}

			siteConfiguration[$key]=$prev$val
		fi
	done <<< "$siteLDAPQuery"

	baseLogDir=$(echo "${siteConfiguration[LOGDIR]}" | envsubst)
203

Kalana Wijethunga's avatar
Kalana Wijethunga committed
204
205
206
207
208
209
210
	if [[ -z $baseLogDir ]]
	then
		baseLogDir="$HOME/ALICE/alien-logs"
		echo "LDAP doesn't define a particular log location, using the default ($baseLogDir)"
	fi

	logDir="$baseLogDir/CE"
Maarten Litmaath's avatar
Maarten Litmaath committed
211
212
213
214
215
216
217

	if ! mkdir -p $logDir
	then
		echo "Unable to create log directory at $logDir"
		return 1
	fi

Kalana Wijethunga's avatar
Kalana Wijethunga committed
218
219
220
221
222
223
	setupLogFile="$logDir/CE-config-inputs.txt"
	ceEnv="$confDir/CE.env"
	envCommand="/cvmfs/alice.cern.ch/bin/alienv printenv JAliEn"

	> $setupLogFile

224
225
226
227
	#
	# Read JAliEn VObox config file
	#

Kalana Wijethunga's avatar
Kalana Wijethunga committed
228
229
230
231
232
233
	if [[ -f "$commonConf" ]]
	then
		declare -A commonConfiguration

		while IFS= read -r line
		do
234
			if [[ ! "$line" = \#* ]] && [[ "$line" =~ [A-Za-z].*= ]]
Kalana Wijethunga's avatar
Kalana Wijethunga committed
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
			then
				key=$(echo "$line" | cut -d "=" -f 1 )
				val=$(echo "$line" | cut -d "=" -f 2- )

				key=${key^^}
				prev=${commonConfiguration[$key]}
				prev=${prev:+$prev$nl}

				commonConfiguration[$key]=$prev$val
			fi
		done < "$commonConf"
	fi

	write_log ""
	write_log "===================== Local Configuration start ==================="
	for x in "${!commonConfiguration[@]}"
	do
		printf "[%s]=%s\n" "$x" "${commonConfiguration[$x]}" >> $setupLogFile
	done
	write_log "===================== Local Configuration end ==================="
	write_log ""

	envFile="$logDir/CE-env.sh"
	pidFile="$logDir/CE.pid"

260
261
262
263
	#
	# Reset the CE environment
	#

Kalana Wijethunga's avatar
Kalana Wijethunga committed
264
265
	> $envFile

266
267
268
269
	#
	# Bootstrap the CE environment e.g. with the correct X509_USER_PROXY
	#

Kalana Wijethunga's avatar
Kalana Wijethunga committed
270
271
	[[ -f "$ceEnv" ]] && cat "$ceEnv" >> $envFile

272
	#
Kalana Wijethunga's avatar
Kalana Wijethunga committed
273
	# Check for JAliEn version
274
275
	#

276
277
278
279
280
281
	jv=${commonConfiguration[JALIEN]}

	[[ -n "$jv" ]] || jv=pro

	jalienVersion=$(
		p=$jalienDir/$jv
282
		(eval readlink -f "$p" || eval ls -ld "$p") | sed '$!d;s-.*/--'
283
284
285
	)

	if [[ -z "$jalienVersion" ]]
Kalana Wijethunga's avatar
Kalana Wijethunga committed
286
	then
287
288
		echo "Requested JAliEn version '$jv' could not be resolved"
		return 1
Kalana Wijethunga's avatar
Kalana Wijethunga committed
289
290
	fi

291
292
	envCommand="$envCommand/$jalienVersion"

Kalana Wijethunga's avatar
Kalana Wijethunga committed
293
294
	$envCommand | grep . >> $envFile || return 1

295
296
297
298
	#
	# All systems go...
	#

Kalana Wijethunga's avatar
Kalana Wijethunga committed
299
300
	logFile="$logDir/CE-jvm-$(date '+%y%m%d-%H%M%S')-$$-log.txt"

301
302
	echo -e "Starting JAliEn CE..."

Kalana Wijethunga's avatar
Kalana Wijethunga committed
303
	(
304
		#
Kalana Wijethunga's avatar
Kalana Wijethunga committed
305
		# In a subshell, to get the process detached from the parent
306
307
		#

Kalana Wijethunga's avatar
Kalana Wijethunga committed
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
		source $envFile
		cd $logDir
		nohup jalien $ceClassName > "$logFile" 2>&1 < /dev/null &
		echo $! > "$pidFile"
	)

	sleep 3
	status_ce
}

function run_ce() {
	command=$1

	if [[ $command = "start" ]]
	then
		confDir=$2
		ldapHostname=$3
		ldapPort=$4
		hostname=$5
		start_ce $confDir $ldapHostname $ldapPort $hostname

	elif [[ $command = "stop" ]]
	then
		stop_ce

	elif [[ $command = "restart" ]]
	then
		stop_ce
		start_ce $confDir $ldapHostname $ldapPort $hostname

	elif [[ $command =~ "status" ]]
	then
		status_ce $command

	else
		echo "Command must be one of: 'start', 'stop', 'restart' or '(ml)status'"
		return 2
	fi
}