2020-06-22 11:33:31 +02:00
#!/bin/sh
# Vars {{{
readonly PROGNAME = $( basename " ${ 0 } " )
readonly PROGDIR = $( readlink -m $( dirname " ${ 0 } " ) )
readonly ARGS = " ${ * } "
readonly NBARGS = " ${# } "
## Test if DEBUG is already defined (by parent script,…)
2020-06-22 13:48:26 +02:00
[ -z " ${ DEBUG } " ] && DEBUG = 1
2020-06-22 11:33:31 +02:00
2020-06-22 14:04:57 +02:00
# If output message should be displayed
[ -z " ${ OUTPUT_MESSAGE } " ] && OUTPUT_MESSAGE = 0
2020-06-22 17:12:14 +02:00
# If SGE queues should be re-enable even if disabled manually (by a user)
[ -z " ${ FORCE_MODE } " ] && FORCE_MODE = 1
2020-06-22 12:22:01 +02:00
# Maco temp file
readonly MACO_TMP_FILE = "/tmp/.maco.upgrade"
readonly MACO_TMP_URGENT_FILE = "/tmp/.maco.urgent.upgrade"
# APT temp file to monitor
readonly APT_TMP_FILE = "/tmp/.apt.upgrade"
2020-06-22 13:48:26 +02:00
# If the scrip need to check pending upgrade before enable a queue
CHECK_UPGRADE = "0"
2020-06-22 11:33:31 +02:00
## Colors
readonly PURPLE = '\033[1;35m'
readonly RED = '\033[0;31m'
readonly RESET = '\033[0m'
readonly COLOR_DEBUG = " ${ PURPLE } "
# }}}
usage( ) { # {{{
cat <<- EOF
2020-06-22 17:12:14 +02:00
usage: $PROGNAME [ --help] [ -d| -f| -h| -q] [ hostname]
2020-06-22 11:33:31 +02:00
2020-06-22 14:04:57 +02:00
Try to enable all SGE queues of the current host ( default) ,
if no pending upgrades ( Maco, APT) are present,
or to the one passed as first argument ( no upgrades checking) .
2020-06-22 11:33:31 +02:00
EXAMPLES :
- Enable SGE' s queue( s) of the current host
${ PROGNAME }
- Enable SGE' s queue( s) of "marvin.domain.tld" host
${ PROGNAME } marvin.domain.tld
2020-06-22 14:20:45 +02:00
${ PROGNAME } -h marvin.domain.tld
2020-06-22 14:04:57 +02:00
OPTIONS :
2020-06-22 14:20:45 +02:00
-d,--debug
Enable debug messages.
2020-06-22 17:12:14 +02:00
-f,--force
Force to ( re) enable a queue even if it was
previously manually disabled ( by a user) .
2020-06-22 14:04:57 +02:00
--help
Print this help message.
2020-06-22 14:20:45 +02:00
-h,--host,--hostname SGE_HOST_TO_MANAGE
Manage SGE' s queue( s) of "SGE_HOST_TO_MANAGE" host.
2020-06-22 14:04:57 +02:00
-q,--quiet
Disable messages on standard output ( except for error) .
2020-06-22 17:12:14 +02:00
EOF
2020-06-22 11:33:31 +02:00
}
# }}}
debug_message( ) { # {{{
local_debug_message = " ${ 1 } "
## Print message if DEBUG is enable (=0)
[ " ${ DEBUG } " -eq "0" ] && printf '\e[1;35m%-6b\e[m\n' " DEBUG − ${ PROGNAME } : ${ local_debug_message } "
2020-06-22 14:04:57 +02:00
}
# }}}
message( ) { # {{{
local_message = " ${ 1 } "
## Print message if OUTPUT_MESSAGE is enable (=0)
[ " ${ OUTPUT_MESSAGE } " -eq "0" ] && printf '%b\n' " ${ local_message } "
return 0
2020-06-22 11:33:31 +02:00
}
# }}}
2020-06-22 12:22:01 +02:00
is_apt_upgrade_present( ) { # {{{
## No pending upgrade by default
return_apt_upgrade_present = "1"
## If we need to watch for upgrades
if [ " ${ CHECK_UPGRADE } " -eq "0" ] ; then
### Check if temp APT upgrade file exists
if [ -f " ${ APT_TMP_FILE } " ] ; then
return_apt_upgrade_present = "0"
debug_message " is_apt_upgrade_absent − \
APT upgrade seems available for this system."
else
return_apt_upgrade_present = "1"
debug_message " is_apt_upgrade_absent − \
NO APT upgrade available for this system."
fi
fi
return " ${ return_apt_upgrade_present } "
}
# }}}
is_maco_upgrade_present( ) { # {{{
## No pending upgrades by default
return_maco_upgrade_present = "1"
## If we need to watch for upgrades
if [ " ${ CHECK_UPGRADE } " -eq "0" ] ; then
## Check if temp Maco upgrade file is present
if [ -f " ${ MACO_TMP_FILE } " ] ; then
return_maco_upgrade_present = "0"
debug_message " is_maco_upgrade_present − \
Maco upgrade seems available."
## Check if temp Maco urgent upgrade file is present
elif [ -f " ${ MACO_TMP_URGENT_FILE } " ] ; then
return_maco_upgrade_present = "0"
debug_message " is_maco_upgrade_present − \
Maco urgent upgrade seems available."
else
debug_message " is_maco_upgrade_present − \
No Maco upgrade require."
fi
fi
return " ${ return_maco_upgrade_present } "
}
# }}}
is_queue_enable( ) { # {{{
2020-06-22 11:33:31 +02:00
local_queue_enable_hostname = " ${ 1 } "
local_queue_enable_name = " ${ 2 } "
## List all queues with 'disable' state and filter to the expected queue name
## with a fake_user to avoid pending jobs for this queue
### And count returned lines
local_queue_enable_test = $( qstat -f -qs d -q " ${ local_queue_enable_name : =/dev/null } @ ${ local_queue_enable_hostname : =/dev/null } " -u fake_user \
| wc -l)
case " ${ local_queue_enable_test } " in
0 ) ## No result so the queue is enable
local_sge_queue_state = "enable"
return_queue_enable = "0"
; ;
3 ) ## Results (header + queue name) so the queue is disable
local_sge_queue_state = "disable"
return_queue_enable = "1"
; ;
* ) ## Unexpected result
printf '%b\n' " ${ RED } Not able to determine the state of ${ local_sge_queue_name : =/dev/null } @ ${ local_queue_enable_hostname : =/dev/null } queue (command return ${ local_queue_enable_test } lines). ${ RESET } "
exit 2
; ;
esac
## Simple debug message to valid current variables
debug_message " is_queue_enable − \
SGE queue: ${ RED } ${ local_queue_enable_name : =/dev/null } ${ COLOR_DEBUG } \
state is: ${ RED } ${ local_sge_queue_state : =/dev/null } ${ COLOR_DEBUG } ."
return " ${ return_queue_enable } "
}
# }}}
is_queue_disable( ) { # {{{
local_queue_disable_hostname = " ${ 1 } "
local_queue_disable_name = " ${ 2 } "
## List all queues with 'disable' state and filter to the expected queue name
## add a fake_user to avoid pending jobs for this queue
### And count returned lines
local_queue_disable_test = $( qstat -f -qs d -q " ${ local_queue_disable_name : =/dev/null } @ ${ local_queue_disable_hostname : =/dev/null } " -u fake_user \
| wc -l)
case " ${ local_queue_disable_test } " in
0 ) ## No result so the queue is enable
local_sge_queue_state = "enable"
return_queue_disable = "1"
2020-06-22 17:12:14 +02:00
## Ensure to remove any previously setted file
rm -f -- " ${ sge_queue_deactivator } "
2020-06-22 11:33:31 +02:00
; ;
3 ) ## Results (header + queue name) so the queue is disable
local_sge_queue_state = "disable"
return_queue_disable = "0"
; ;
* ) ## Unexpected result
printf '%b\n' " ${ RED } Not able to determine the state of ${ local_queue_disable_name : =/dev/null } @ ${ local_queue_disable_hostname : =/dev/null } queue (command return ${ local_queue_disable_test } lines). ${ RESET } "
exit 3
; ;
esac
## Simple debug message to valid current variables
debug_message " is_queue_disable − \
SGE queue: ${ RED } ${ local_queue_disable_name : =/dev/null } ${ COLOR_DEBUG } \
state is: ${ RED } ${ local_sge_queue_state : =/dev/null } ${ COLOR_DEBUG } ."
return " ${ return_queue_disable } "
}
# }}}
2020-06-22 12:22:01 +02:00
is_all_queue_enable( ) { # {{{
2020-06-22 11:33:31 +02:00
local_all_queue_enable_hostname = " ${ 1 } "
local_all_queue_enable_name = " ${ 2 } "
## By default, all queues are enable
return_all_queue_enable = "0"
## Test all queues one by one
for loop_enable_queue in ${ local_all_queue_enable_name } ; do
### If a queue is not enable
#### Change the return value
is_queue_enable " ${ local_all_queue_enable_hostname } " " ${ loop_enable_queue } " \
|| return_all_queue_enable = "1"
done
return " ${ return_all_queue_enable } "
}
# }}}
2020-06-22 12:22:01 +02:00
enable_sge_queue( ) { # {{{
2020-06-22 11:33:31 +02:00
local_sge_hostname = " ${ 1 } "
local_sge_queue_name = " ${ 2 } "
2020-06-22 17:12:14 +02:00
## If the queue was previously disabled by another script OR if FORCE_MODE is enable
if [ -f " ${ sge_queue_deactivator } " ] || [ " ${ FORCE_MODE } " -eq "0" ] ; then
2020-06-22 13:25:34 +02:00
debug_message " enable_sge_queue − \
2020-06-22 17:12:14 +02:00
Previously disabled by a script ( or FORCE is enable ) , try to enable SGE queue: ${ RED } ${ local_sge_queue_name : =/dev/null } @${ local_sge_hostname : =/dev/null } ${ COLOR_DEBUG } ."
2020-06-22 13:25:34 +02:00
## SGE command to enable the queue
qmod --enable " ${ local_sge_queue_name } @ ${ local_sge_hostname } " \
&& return_enable_queue = " ${ ? } "
else
2020-06-22 17:12:14 +02:00
message " SGE queue: ${ RED } ${ local_sge_queue_name : =/dev/null } @ ${ local_sge_hostname : =/dev/null } ${ RESET } was manually disabled, please re-enable it ${ RED } manually ${ RESET } (or use --force option). "
2020-06-22 13:25:34 +02:00
return_enable_queue = "1"
fi
2020-06-22 11:33:31 +02:00
return " ${ return_enable_queue } "
}
# }}}
main( ) { # {{{
sge_queues_name = " $( qhost -h " ${ sge_hostname : =/dev/null } " -q -xml \
| grep "queue name" \
| cut -d"'" -f2 ) "
sge_queues_name_print = " $( qhost -h " ${ sge_hostname : =/dev/null } " -q -xml \
| grep "queue name" \
| cut -d"'" -f2 \
| tr -s '\n' ' ' ) "
2020-06-22 12:22:01 +02:00
## If APT package upgrade is available
### Exit (wait for APT upgrade to be applied)
is_apt_upgrade_present \
&& exit 0
## If Maco upgrade is present
### Exit (wait for Maco upgrade to be applied)
is_maco_upgrade_present \
&& exit 0
2020-06-22 11:33:31 +02:00
## Simple debug message with color to valid current variables
debug_message " main − Try to manage \
SGE queue( s) : ${ RED } ${ sge_queues_name_print : =/dev/null } ${ COLOR_DEBUG } \
for host: ${ RED } ${ sge_hostname : =/dev/null } ${ COLOR_DEBUG } ."
## If the queue(s) are already enable
### Exit
is_all_queue_enable " ${ sge_hostname } " " ${ sge_queues_name } " \
&& exit 0
## Test all queues one by one
for loop_queue in ${ sge_queues_name } ; do
2020-06-22 17:12:14 +02:00
## File previously set if the queue was disabled by a script
## "automatically disabled" for an upgrade
sge_queue_deactivator = " /etc/.sge. ${ local_sge_queue_name } .disable "
2020-06-22 11:33:31 +02:00
## If the queue is disable
### Try to enable it
is_queue_disable " ${ sge_hostname } " " ${ loop_queue } " \
&& enable_sge_queue " ${ sge_hostname } " " ${ loop_queue } "
2020-06-22 17:12:14 +02:00
## Don't consider manually disabled queue as an error except if FORCE_MODE was specified
if [ -f " ${ sge_queue_deactivator } " ] || [ " ${ FORCE_MODE } " -eq "0" ] ; then
## If the queue is still disable
### Exit with error
is_queue_disable " ${ sge_hostname } " " ${ loop_queue } " \
&& printf '%b\n' " ${ RED } ERROR ${ loop_queue } @ ${ sge_hostname } is still disable. ${ RESET } " \
&& exit 4
fi
2020-06-22 11:33:31 +02:00
done
}
# }}}
2020-06-22 13:48:26 +02:00
# Manage arguments # {{{
# This code can't be in a function due to arguments
if [ ! " ${ NBARGS } " -eq "0" ] ; then
manage_arg = "0"
## If the first argument is not an option
if ! printf -- '%s' " ${ 1 } " | grep -q -E -- "^-*" ;
then
## Use this argument for sge_hostname
sge_hostname = " ${ 1 } "
## No way to monitor pending upgrades on a remote host
CHECK_UPGRADE = "1"
2020-06-22 14:20:45 +02:00
## Switch to the next argument
2020-06-22 13:48:26 +02:00
shift
manage_arg = $(( manage_arg+1))
else
## Use local host for sge_hostname
sge_hostname = " $( hostname -f) "
fi
# Parse all options (start with a "-") one by one
while printf -- '%s' " ${ 1 } " | grep -q -E -- "^-*" ; do
case " ${ 1 } " in
2020-06-22 14:20:45 +02:00
-d| --debug ) ## debug
DEBUG = 0
; ;
2020-06-22 17:12:14 +02:00
-f| --force ) ## Force to enable SGE queue
FORCE_MODE = 0
; ;
2020-06-22 14:20:45 +02:00
--help ) ## help
2020-06-22 13:48:26 +02:00
usage
## Exit after help informations
exit 0
; ;
2020-06-22 14:20:45 +02:00
-h| --host| --hostname ) ## Specify a different host to manage
## Move to the next argument
shift
## Override previous definition of sge_hostname
sge_hostname = " ${ 1 } "
2020-06-22 14:04:57 +02:00
; ;
2020-06-22 14:20:45 +02:00
-q| --quiet ) ## Silent mode
2020-06-22 14:04:57 +02:00
## Avoid to display any message on standard output
OUTPUT_MESSAGE = 1
2020-06-22 13:48:26 +02:00
; ;
2020-06-22 14:20:45 +02:00
-- ) ## End of options list
2020-06-22 13:48:26 +02:00
## End the while loop
break
; ;
2020-06-22 14:20:45 +02:00
* ) ## unknow option
2020-06-22 13:48:26 +02:00
printf '%b\n' " ${ RED } Invalid option: ${ 1 } ${ RESET } "
printf '%b\n' "---"
usage
exit 1
; ;
esac
debug_message " Arguments management − \
${ RED } ${ 1 } ${ COLOR_DEBUG } option managed."
2020-06-22 14:20:45 +02:00
## Move to the next argument
2020-06-22 13:48:26 +02:00
shift
manage_arg = $(( manage_arg+1))
done
debug_message " Arguments management − \
${ RED } ${ manage_arg } ${ COLOR_DEBUG } argument( s) successfully managed."
else
debug_message " Arguments management − \
No arguments/options to manage."
## Use local host for sge_hostname
sge_hostname = " $( hostname -f) "
fi
# }}}
2020-06-22 11:33:31 +02:00
main
exit 0