## ## install_func ## ### # Change user/group ownership. Only does someting if you are root. # $1 File # $2 User # $3 Group # $4 Opts ### Chown() { test $(id -u) -eq 0 -a -n "$2" -a -n "$3" && chown $4 $2:$3 "$1" || true } ##============================================================================= ## TestDir() ## Function to see if a directory exists. If it does not exist, we call ## mkdir -p on the argument passed. Will call chmod on the directory whether ## it exists or not. ## Parameters: ## $1 - Directory ## $2 - Mode to set directory ## $3 - User name to set ownership ## $4 - Group name to set ownership ##============================================================================= TestDir() { [ ! -d "$1" ] && mkdir -p "$1" && echo "Created $1" chmod $2 "$1" Chown "$1" "$3" "$4" } ##============================================================================= ## CheckStatus() ## Function to call after running a command that should return 0. If the ## command's exit status was not zero, it will print out your specified ## error message and exit. ## Parameters: ## $1 - A message to print ##============================================================================= CheckStatus() { if [ $? -ne 0 ] ; then echo "$1" exit 1 fi } ##============================================================================ ## CopyFile() ## Generic function to copy file(s) to a given location and set the ## permissions and ownership. If $1 is a file list, then $6 should not be ## set. ## Parameters ## $1 - Source file(s) ## $2 - Destination directory ## $3 - Mode to set the file ## $4 - User name to set ownership ## $5 - Group name to set ownership ## $6 - (Opt) An alternate filename than the source ## $7 - (Opt) Overwrite. If false, do not overwrite the file if it exists. ##============================================================================ CopyFile() { local current_dir=`pwd` TestDir "$2" "777" "$4" "$5" if [ "$7" = "false" ] ; then ## Newer versions of cp support a '-n' flag which means to not clobber ## the file if it exists. However, the cp on EL 5 does not, so we work ## around it by using the interfactive flag with false piped in. false | cp -i "$1" "$2" >/dev/null 2>&1 else cp "$1" "$2" fi cd "$2" chmod "$3" "$1" Chown "$1" "$4" "$5" if [ "$6" != "" ] ; then mv "$1" "$6" fi cd "$current_dir" } ## ## If the current user is "root", issue an error and exit. ## ValidateNonRootUser() { test $(id -u) -ne 0 CheckStatus " Must not be \"root\" (super user)!" } ## ## If the current user is not "root", issue an error and exit. ## ValidateRootUser() { test $(id -u) -eq 0 CheckStatus " Must be \"root\" (super user)!" } ## ## If the variable given by "$1" is empty, issue an error and exit. ## ValidateEmptyVar() { if [ "$1" = "" ] ; then echo " Variable \"$2\" is not defined!" ; exit 1 fi } ## ## If the variable given by "$1" is not defined or not a valid directory, ## issue an error and exit. ## ValidateDir() { [ -d "$1" ] CheckStatus " \"$1\" is not a valid directory!" } ## # Recursively copy a directory and do not follow symbolic links. # $1 Source directory # $2 Destination directory # $3 Permissions # $4 User # $5 Group ## CopyDir() { [ -d "$1" ] CheckStatus " Directory \"$1\" does not exist!" TestDir "$2" "777" "$4" "$5" if [ -d "$2/$1" ] ; then rm -rf "$2/$1" fi cp -R "$1" "$2" CheckStatus " Could not copy directory \"$1\" to \"$2\"!" chmod $3 "$2/$1" Chown "$2/$1" "$4" "$5" "-R" } ## ## Adds a new user to the operating system. ## add_user() { UserName=${1} UserNum=${2} GroupName=${3} HomeDir=${4} Password=${5} NoLogin=${6} if [ "$Password" = "" ] ; then Password="$UserName" fi echo "Creating user name and password for \"${UserName}\"..." grep -q "^${UserName}:" /etc/passwd if [ ${?} -eq 0 ] ; then echo " \"${UserName}\" user already exists!" else if [ "$NoLogin" = "yes" ] ; then /usr/sbin/useradd -u ${UserNum} -M -g ${GroupName} -G ${GroupName} -s /sbin/nologin -d ${HomeDir} ${UserName} CheckStatus " Failed to add user \"${UserName}\"!" else /usr/sbin/useradd -u ${UserNum} -M -g ${GroupName} -G ${GroupName} -d ${HomeDir} ${UserName} CheckStatus " Failed to add user \"${UserName}\"!" fi echo ${UserName} |passwd --stdin ${Password} > /dev/null 2>&1 CheckStatus " Failed to create password for \"${UserName}\"!" fi } ## ## Add a new group to the operating system. ## add_group() { GroupName=${1} GroupNum=${2} echo "Creating group \"${GroupName}\"..." grep -q "^${GroupName}:" /etc/group if [ ${?} -eq 0 ] ; then echo " \"${GroupName}\" group already exists!" else /usr/sbin/groupadd -g ${GroupNum} ${GroupName} CheckStatus " Failed to add group \"${GroupName}\"!" fi } ## ## Checks the component name passed to this function and reports its status. ## check_comp() { CompName="${1}" echo "${CompName}" shift for CompPath ; do if [ -x "${CompPath}" ] ; then return fi done echo " ${CompName} not found! Please install." } ## ## Create a backup directory for this current distribution. ## create_backup_dir() { DirectoryToBackup=${1} BackupDirPath=${2} Description=${3} ## Set backup date extension. Y=%Y ; m=%m ; d=%d DateExt=`date +${Y}${m}${d}` unset Y m d DirectoryName=`basename "${DirectoryToBackup}"` BackupDirectory="${BackupDirPath}/${DirectoryName}.${DateExt}" if [ -e "${DirectoryToBackup}" ] ; then echo " ${Description} already exists - Backing up." if [ -e "${BackupDirectory}" ] ; then echo " \"${BackupDirectory}\" already exists!" ; exit 1 fi cp -rp "${DirectoryToBackup}" "${BackupDirectory}" CheckStatus " Could not backup \"${DirectoryToBackup}\"!" fi } ## ## Removes the old dist directories on the system. ## cleanup_old_dist() { if [ "${1}" != "" ] ; then ls -1d ${1}.[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] > /dev/null 2>&1 if [ $? -eq 0 ] ; then echo " Old versions of \"${2}\" found - cleanup." for cleanup_dir in `ls -1d ${1}.[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]` ; do if [ -d "${cleanup_dir}" ] ; then echo -n "Cleanup \"${cleanup_dir}\"? " read response if [ "${response}" = "Y" -o "${response}" = "y" ] ; then echo "Cleanup: \"${cleanup_dir}\"..." rm -rf "${cleanup_dir}" > /dev/null 2>&1 else echo "Skipping: \"${cleanup_dir}\"..." fi fi done fi fi } GetApplicationArea() { read -e -p "Enter the C.A.S. application: " SpApp SpApp=`echo "$SpApp" | tr -cd '[:upper:][:lower:][:digit:]_'` if [ $SpApp = "all" ] ; then SpAppDir="/u/scs" else SpAppDir="/u/scs_$SpApp" fi ValidateDir "$SpAppDir" export SCS_WEB_ROOT=$SpAppDir/web read -e -p "This will install C.A.S. in $SpAppDir [Y/N] " Answer Answer=`echo $Answer | tr "[a-z]" "[A-Z]"` if [ "${Answer}" != "Y" ] ; then echo "Install canceled" exit 1 fi export SPICE_APP=$SpApp . /u/scs/spicedef } GetWebDirectories() { local DEFAULT_HTML_ROOT=/var/www/html local DEFAULT_CGI_ROOT=/var/www/cgi-bin local APP_DIR="scs" if [ "$1" = "web_bs" ] ; then APP_DIR="scs_bs" fi printf "Enter the root HTML dir containing the \"scs\" dir [$DEFAULT_HTML_ROOT]: " read -e HTML_ROOT if [ "$HTML_ROOT" = "" ] ; then HTML_ROOT=$DEFAULT_HTML_ROOT fi export SCS_HTML_ROOT="$HTML_ROOT/$APP_DIR" ValidateDir "$SCS_HTML_ROOT" printf "Enter the root cgi-bin dir containing the \"scs\" dir [$DEFAULT_CGI_ROOT]: " read -e CGI_ROOT if [ "$CGI_ROOT" = "" ] ; then CGI_ROOT=$DEFAULT_CGI_ROOT fi export SCS_CGI_ROOT="$CGI_ROOT/$APP_DIR" ValidateDir "$SCS_CGI_ROOT" } ### # Stop a bspice service on a given port on localhost. # @param $1 The port ### StopBspiceService() { /usr/sbin/lsof -i :$1 > /dev/null 2>&1 if [ $? -eq 0 ] ; then if [ -x /usr/bin/ncat ] ; then echo "z4-stop" | ncat -4 --send-only localhost $1 > /dev/null 2>&1 else echo "z4-stop" | nc localhost $1 > /dev/null 2>&1 fi fi } ### # Stop an inotifywait process. # @param $1 The directory it is waiting on. ### StopInotifyProcess() { local INOTIFY_PID=`ps -efwww | grep "[i]notifywait -m $1" | awk '{ print $2 }'` [ -n "$INOTIFY_PID" ] && kill -9 $INOTIFY_PID } ## END OF FILE