Blame slurm-setuser.in

Philip Kovacs 9be8f2
#!/bin/bash
Philip Kovacs 9be8f2
#
Philip Kovacs 9be8f2
# Copyright (c) 2017, Philip Kovacs
Philip Kovacs 9be8f2
# All rights reserved.
Philip Kovacs 9be8f2
#
Philip Kovacs 9be8f2
# Redistribution and use in source and binary forms, with or without
Philip Kovacs 9be8f2
# modification, are permitted provided that the following conditions are met:
Philip Kovacs 9be8f2
#
Philip Kovacs 9be8f2
# 1. Redistributions of source code must retain the above copyright notice, this
Philip Kovacs 9be8f2
#    list of conditions and the following disclaimer.
Philip Kovacs 9be8f2
# 2. Redistributions in binary form must reproduce the above copyright notice,
Philip Kovacs 9be8f2
#    this list of conditions and the following disclaimer in the documentation
Philip Kovacs 9be8f2
#    and/or other materials provided with the distribution.
Philip Kovacs 9be8f2
#
Philip Kovacs 9be8f2
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
Philip Kovacs 9be8f2
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
Philip Kovacs 9be8f2
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Philip Kovacs 9be8f2
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
Philip Kovacs 9be8f2
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
Philip Kovacs 9be8f2
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
Philip Kovacs 9be8f2
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
Philip Kovacs 9be8f2
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Philip Kovacs 9be8f2
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Philip Kovacs 9be8f2
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Philip Kovacs 9be8f2
Philip Kovacs 9be8f2
prog=`basename $0`
Philip Kovacs 9be8f2
user=root
Philip Kovacs 9be8f2
group=root
Philip Kovacs 9be8f2
yesno=no
Philip Kovacs 9be8f2
Philip Kovacs 9be8f2
red='\033[0;31m'
Philip Kovacs 9be8f2
green='\033[0;32m'
Philip Kovacs 9be8f2
nc='\033[0m'
Philip Kovacs 9be8f2
pass="${green}[*]${nc}"
Philip Kovacs 9be8f2
fail="${red}[!]${nc}"
Philip Kovacs 9be8f2
ask="[?]"
Philip Kovacs 9be8f2
Philip Kovacs 9be8f2
# the rpm spec configures these directory variables
Philip Kovacs 9be8f2
dir_conf="%{_sysconfdir}/%{name}"
Philip Kovacs 9be8f2
dir_log="%{_var}/log/%{name}"
Philip Kovacs 9be8f2
dir_run="%{_rundir}/%{name}"
Philip Kovacs 9be8f2
dir_spool="%{_var}/spool/%{name}"
Philip Kovacs 9be8f2
dir_tmpfiles_d="%{_tmpfilesdir}"
Philip Kovacs 9be8f2
Philip Kovacs 9be8f2
file_slurm_conf="${dir_conf}/slurm.conf"
Philip Kovacs 9be8f2
file_slurmdbd_conf="${dir_conf}/slurmdbd.conf"
Philip Kovacs 9be8f2
file_tmpfiles_d_conf="${dir_tmpfiles_d}/slurm.conf"
Philip Kovacs 9be8f2
Philip Kovacs 9be8f2
function usage ()
Philip Kovacs 9be8f2
{
Philip Kovacs 9be8f2
	echo  -e "Sets the slurm installation on this node to run as the specified user and group\n"
Philip Kovacs 9be8f2
	echo  "Usage: $prog [-u <user>] [-g <group>] [-h] [-y]"
Philip Kovacs 7245d5
	echo  "   -u <user>   : the slurm file owner and SlurmUser ($user)"
Philip Kovacs 9be8f2
	echo  "   -g <group>  : the slurm file group ($group)"
Philip Kovacs 9be8f2
	echo  "   -y          : answer yes to all questions ($yesno)"
Philip Kovacs 9be8f2
	echo  "   -h          : print help"
Philip Kovacs 9be8f2
}
Philip Kovacs 9be8f2
Philip Kovacs 9be8f2
function answer ()
Philip Kovacs 9be8f2
{
Philip Kovacs 9be8f2
	while true
Philip Kovacs 9be8f2
	do
Philip Kovacs 9be8f2
		if [ "${yesno}" = "yes" ]; then
Philip Kovacs 9be8f2
			echo -e "${ask} $1 [${yesno}]" 
Philip Kovacs 9be8f2
			__answer="yes"
Philip Kovacs 9be8f2
			break;
Philip Kovacs 9be8f2
		fi
Philip Kovacs 9be8f2
Philip Kovacs 9be8f2
		echo -e -n "${ask} "
Philip Kovacs 9be8f2
		read -e -p "$1 [${yesno}] " yn
Philip Kovacs 9be8f2
		case $yn in
Philip Kovacs 9be8f2
			"")     __answer=$yesno
Philip Kovacs 9be8f2
				break;;
Philip Kovacs 9be8f2
			[yY]*)  __answer="yes" 
Philip Kovacs 9be8f2
				break;;
Philip Kovacs 9be8f2
			[nN]*)  __answer="no"
Philip Kovacs 9be8f2
				break;;
Philip Kovacs 9be8f2
			*) 
Philip Kovacs 9be8f2
				echo "Please answer yes or no";;
Philip Kovacs 9be8f2
		esac
Philip Kovacs 9be8f2
	done
Philip Kovacs 9be8f2
}
Philip Kovacs 9be8f2
Philip Kovacs 9be8f2
#
Philip Kovacs 9be8f2
# Parse options
Philip Kovacs 9be8f2
#
Philip Kovacs 9be8f2
while getopts "u:g:yh" opt
Philip Kovacs 9be8f2
do
Philip Kovacs 9be8f2
	case "$opt" in
Philip Kovacs 9be8f2
	h) 
Philip Kovacs 9be8f2
		usage
Philip Kovacs 9be8f2
		exit 2;;
Philip Kovacs 9be8f2
	u)
Philip Kovacs 9be8f2
		user=$OPTARG;;
Philip Kovacs 9be8f2
	g)
Philip Kovacs 9be8f2
		group=$OPTARG;;
Philip Kovacs 9be8f2
	y)
Philip Kovacs 9be8f2
		yesno=yes;;
Philip Kovacs 9be8f2
	*) 
Philip Kovacs 9be8f2
		echo ""
Philip Kovacs 9be8f2
		usage
Philip Kovacs 9be8f2
		exit 2;;
Philip Kovacs 9be8f2
	esac
Philip Kovacs 9be8f2
done
Philip Kovacs 9be8f2
Philip Kovacs 9be8f2
#
Philip Kovacs 9be8f2
# Run this script only as root
Philip Kovacs 9be8f2
#
Philip Kovacs 9be8f2
if [ $UID -eq 0 ]; then
Philip Kovacs 9be8f2
	echo -e "${pass} running as root... good"
Philip Kovacs 9be8f2
else
Philip Kovacs 9be8f2
	echo -e "${fail} $prog must be run as root."
Philip Kovacs 9be8f2
	exit 1
Philip Kovacs 9be8f2
fi
Philip Kovacs 9be8f2
Philip Kovacs 9be8f2
#
Philip Kovacs 9be8f2
# Validate the user and group
Philip Kovacs 9be8f2
#
Philip Kovacs 9be8f2
valid_user="`getent passwd $user || :`"
Philip Kovacs 9be8f2
if [ -n "$valid_user" ]; then
Philip Kovacs 9be8f2
	echo -e "${pass} found user $user... good"
Philip Kovacs 9be8f2
else
Philip Kovacs 9be8f2
	echo -e "${fail} the specified user was not found... ${user}"
Philip Kovacs 9be8f2
	exit 2
Philip Kovacs 9be8f2
fi
Philip Kovacs 9be8f2
valid_group="`getent group $group || :`"
Philip Kovacs 9be8f2
if [ -n "$valid_group" ]; then
Philip Kovacs 9be8f2
	echo -e "${pass} found group $group... good"
Philip Kovacs 9be8f2
else
Philip Kovacs 9be8f2
	echo -e "${fail} the specified group was not found... ${group}"
Philip Kovacs 9be8f2
	exit 2
Philip Kovacs 9be8f2
fi
Philip Kovacs 9be8f2
Philip Kovacs 9be8f2
#
Philip Kovacs 9be8f2
# Slurm services must not be running
Philip Kovacs 9be8f2
#
Philip Kovacs 9be8f2
slurmctld_running_pid="`ps -e | grep slurmctld | grep -v grep | awk '{print $1}'`"
Philip Kovacs 9be8f2
if [ -z "$slurmctld_running_pid" ]; then
Philip Kovacs 9be8f2
	echo -e "${pass} slurmctld is not running... good"
Philip Kovacs 9be8f2
else
Philip Kovacs 9be8f2
	echo -e "${fail} slurmctld is running... stop it with [systemctl stop slurmctld]"
Philip Kovacs 9be8f2
	exit 2
Philip Kovacs 9be8f2
fi
Philip Kovacs 9be8f2
slurmd_running_pid="`ps -e | grep slurmd | grep -v grep | awk '{print $1}'`"
Philip Kovacs 9be8f2
if [ -z "$slurmd_running_pid" ]; then
Philip Kovacs 9be8f2
	echo -e "${pass} slurmd is not running... good"
Philip Kovacs 9be8f2
else
Philip Kovacs 9be8f2
	echo -e "${fail} slurmd is running... stop it with [systemctl stop slurmd]"
Philip Kovacs 9be8f2
	exit 2
Philip Kovacs 9be8f2
fi
Philip Kovacs 9be8f2
slurmdbd_running_pid="`ps -e | grep slurmdbd | grep -v grep | awk '{print $1}'`"
Philip Kovacs 9be8f2
if [ -z "$slurmdbd_running_pid" ]; then
Philip Kovacs 9be8f2
	echo -e "${pass} slurmdbd is not running... good"
Philip Kovacs 9be8f2
else
Philip Kovacs 9be8f2
	echo -e "${fail} slurmdbd is running... stop it with [systemctl stop slurmdbd]"
Philip Kovacs 9be8f2
	exit 2
Philip Kovacs 9be8f2
fi
Philip Kovacs 9be8f2
Philip Kovacs 9be8f2
#
Philip Kovacs 9be8f2
# Update SlurmUser in the slurm configs
Philip Kovacs 9be8f2
#
Philip Kovacs 9be8f2
for file in "$file_slurm_conf" "$file_slurmdbd_conf"
Philip Kovacs 9be8f2
do
Philip Kovacs 9be8f2
	if [ -f "$file" ]; then
Philip Kovacs 9be8f2
		answer "update SlurmUser in $file ?"
Philip Kovacs 9be8f2
		if [ "$__answer" = "yes" ]; then
Philip Kovacs 9be8f2
			sed -i "s|^SlurmUser=.*|SlurmUser=${user}|g" $file
Philip Kovacs 9be8f2
			if [ $? -eq 0 ]; then
Philip Kovacs 9be8f2
				echo -e "${pass} $file updated successfully"
Philip Kovacs 9be8f2
			else
Philip Kovacs 9be8f2
				echo -e "${fail} error updating $file"
Philip Kovacs 9be8f2
				exit 1
Philip Kovacs 9be8f2
			fi
Philip Kovacs 9be8f2
		fi
Philip Kovacs 9be8f2
	fi
Philip Kovacs 9be8f2
done
Philip Kovacs 9be8f2
Philip Kovacs 9be8f2
#
Philip Kovacs 9be8f2
# Update ownership of slurm directories that must be owned by the slurm user
Philip Kovacs 9be8f2
#
Philip Kovacs 9be8f2
for dir in "$dir_log" "$dir_run" "$dir_spool" "$dir_spool/ctld"
Philip Kovacs 9be8f2
do
Philip Kovacs 9be8f2
	if [ -d "$dir" ]; then
Philip Kovacs 9be8f2
		answer "update ownership of $dir ?"
Philip Kovacs 9be8f2
		if [ "$__answer" = "yes" ]; then
Philip Kovacs 9be8f2
			chown ${user}:${group} $dir
Philip Kovacs 9be8f2
			if [ $? -eq 0 ]; then
Philip Kovacs 9be8f2
				echo -e "${pass} $dir updated successfully"
Philip Kovacs 9be8f2
			else
Philip Kovacs 9be8f2
				echo -e "${fail} error updating $dir"
Philip Kovacs 9be8f2
				exit 1
Philip Kovacs 9be8f2
			fi
Philip Kovacs 9be8f2
		fi
Philip Kovacs 9be8f2
	fi
Philip Kovacs 9be8f2
done
Philip Kovacs 9be8f2
Philip Kovacs 9be8f2
#
Philip Kovacs 9be8f2
# Update ownership of slurmctld spool files which are owned by the slurm user
Philip Kovacs 9be8f2
#
Philip Kovacs 9be8f2
if [ -d "${dir_spool}/ctld" ]; then
Philip Kovacs 9be8f2
	answer "update ownership of files in ${dir_spool}/ctld ?"
Philip Kovacs 9be8f2
	if [ "$__answer" = "yes" ]; then
Philip Kovacs 9be8f2
		if [ $? -eq 0 ]; then
Philip Kovacs 9be8f2
			find ${dir_spool}/ctld -mindepth 1 -exec chown ${user}:${group} {} \; 
Philip Kovacs 9be8f2
			echo -e "${pass} ${dir_spool}/ctld files updated successfully"
Philip Kovacs 9be8f2
		else
Philip Kovacs 9be8f2
			echo -e "${fail} error updating ${dir_spool}/ctld files"
Philip Kovacs 9be8f2
			exit 1
Philip Kovacs 9be8f2
		fi
Philip Kovacs 9be8f2
	fi
Philip Kovacs 9be8f2
fi
Philip Kovacs 9be8f2
Philip Kovacs 9be8f2
#
Philip Kovacs 9be8f2
# Update the tmpfiles.d config file to the new slurm user
Philip Kovacs 9be8f2
#
Philip Kovacs 9be8f2
if [ -f "$file_tmpfiles_d_conf" ]; then
Philip Kovacs 9be8f2
	answer "update tmpfiles.d config $file_tmpfiles_d_conf ?"
Philip Kovacs 9be8f2
	if [ "$__answer" = "yes" ]; then
Philip Kovacs 9be8f2
		sed -i "s|0755 \(.*\) -$|0755 ${user} ${group} -|g" $file_tmpfiles_d_conf
Philip Kovacs 9be8f2
		if [ $? -eq 0 ]; then
Philip Kovacs 9be8f2
			echo -e "${pass} $file_tmpfiles_d_conf updated successfully"
Philip Kovacs 9be8f2
		else
Philip Kovacs 9be8f2
			echo -e "${fail} error updating $file_tmpfiles_d_conf"
Philip Kovacs 9be8f2
			exit 1
Philip Kovacs 9be8f2
		fi
Philip Kovacs 9be8f2
	fi
Philip Kovacs 9be8f2
fi
Philip Kovacs 9be8f2
Philip Kovacs 9be8f2
exit 0