Skip to content
Snippets Groups Projects
Commit 6ee11bde authored by Trupeshkumar Rajendrabhai Patel's avatar Trupeshkumar Rajendrabhai Patel
Browse files

This is v2 of user-create-getopt on 05/10/19 - 8:48PM

parent 4f97e59d
No related branches found
No related tags found
No related merge requests found
......@@ -2,14 +2,16 @@
#
#
show_help () {
echo;
echo "Usage: `basename $0` <options>";
echo;
echo "Options:";
echo " -h | --help = Help, show this message.";
echo " -w | --warewulf-sync = Enable WareWulf sync for passwd|shadow|groups files.";
echo " -S | --sacct-add = Add user to Slurm account.";
echo " -s | --shell=<SHELL_PATH> = Create user's login shell (By default \"/bin/bash\").";
echo " -H | --create-home=[absent|present] = Do not create the user's home directory (Mandatory).";
echo " -s | --shell=<SHELL_PATH> = Create user's login shell (System shell will be the by default).";
echo " -m | --create-home = Create the user's home directory.";
echo " -M | --no-create-home = Do not create the user's home directory.";
echo " -d | --home-dir=<home_dir_path> = The new user will be created using HOME_DIR as the \"/home/<USER_NAME>\" for the user's login directory.";
echo " -u | --user=<USER_NAME> = Username (Mandatory).";
echo " -c | --comment=\"John Doh\" = Generally the users full name (Mandatory).";
......@@ -17,9 +19,10 @@ show_help () {
echo " -D | --debug = Show what would be changed.";
}
OPTIONS=`getopt -o hwSs::H:d::u:c:UD --long help,warewulf-sync,sacct-add,shell::,create-home:,home-dir::,user:,comment:,user-id,debug -n 'help' -- "$@"`
OPTIONS=`getopt -o hwSs::mMd::u:c:UD --longoptions help,warewulf-sync,sacct-add,shell::,create-home,no-create-home,home-dir::,user:,comment:,user-id,debug -n 'user_create' -- "$@"`
# echo "$OPTIONS"
echo "option: $OPTIONS"
echo;
eval set -- "$OPTIONS"
while true ; do
......@@ -29,24 +32,25 @@ while true ; do
-S | --sacct-add ) sacctmgr_add=true; shift ;;
-s | --shell )
case "$2" in
"" ) shell=false; shift 2 ;;
* ) shell="$2"; shift 2 ;;
esac ;;
-H | --create-home ) create_home="$2"; shift 2 ;;
-d | --home-dir )
"" ) echo "caseNoEcho shell: $shell"; shift 2;;
* ) shell=$1; echo "caseEcho shell: $shell"; shift 2;;
esac;;
-m | --create-home ) create_home=true; shift ;;
-M | --no-create-home ) no_create_home=true; shift ;;
+d | --home-dir )
case "$2" in
"" ) home_dir='/home/' ; shift 2 ;;
* ) home_dir="$2"; shift 2 ;;
"" ) shift 2 ;;
* ) home_dir=$2; echo $home_dir; shift 2 ;;
esac ;;
-u | --user )
case "$2" in
"" ) echo "--user must need to write a name." ; shift 2 ;;
* ) user="$2"; shift 2 ;;
* ) user=$2; shift 2 ;;
esac;;
-c | --comment )
case "$2" in
"" ) echo "--user must need to write a full name." ; shift 2 ;;
* ) comment="$2"; shift 2 ;;
* ) comment=$2; shift 2 ;;
esac;;
-U | --user-id ) user_id=true; shift ;;
-D | --debug ) debug=true; shift ;;
......@@ -67,59 +71,66 @@ if [[ ! "$comment" ]]; then
exit 1
fi
shellPaths=("/bin/bash" "/bin/sh" "/bin/zsh" "/usr/bin/tmux")
# To create LOGIN_SHELL by given path
if [[ "$shell" == "" ]]; then
for i in ${!shellPaths[@]}; do
if [[ "$shell" == "i" ]]; then
shell_path="-s \"$shell\""
break;
else
shell_path=""
fi
done
# Raise error
if [[ "$shell_path" == "" ]]; then
echo "--shell must be from following paths:"
echo ""
printf "1. /bin/bash \n2. /bin/sh \n3. /bin/zsh \n4. /usr/bin/tmux"
show_help
exit 1
fi
if [[ ! -z "${shell}" ]]; then
shellPath="-s \"$shell\""
fi
# To create HOME_DIR
if [[ "$create_home" == "present" ]]; then
# For default HOME_DIR_PATH
if [[ "$home_dir" == "/home/" ]]; then
home_dir_path="-m -d \"$home_dir$user\""
if [ "$create_home" == "true" ] && [[ ! "$no_create_home" ]]; then
# For defined HOME_DIR_PATH
else
if [[ ! -z "${home_dir}" ]]; then
home_dir_path="-m -d \"$home_dir\""
fi
# To omit HOME_DIR
elif [[ "$create_home" == "absent" ]]; then
home_dir_path="-M"
elif [[ ! "$create_home" ]] && [ "$no_create_home" == "true" ]; then
home_dir_path='-M'
# To set default settings
elif [[ -z ${create_home} ]] && [[ -z ${no_create_home} ]]; then
home_dir_path=''
# Raise error
else
echo "--create-home must be either 'absent' or 'present'"
echo "You can only select either --create-home or --no-create-home"
show_help
exit 1
fi
# To execute USERADD command
# Defined LOGIN_SHELL with Defined HOME_DIR
#if [[ "$shellPath" == *"-s"* ]] && [[ "$home_dir_path" == *"-d"* ]]; then
#echo useradd $home_dir_path -U $shellPath -c "$comment" $user
#useradd $home_dir_path -U $shellPath -c "$comment" $user
# Defined LOGIN_SHELL with NO HOME_DIR
#elif [[ "$shellPath" == *"-s"* ]] && [[ "$home_dir_path" == *"-M"* ]]; then
# echo useradd $home_dir_path -U $shellPath -c "$comment" $user
# useradd $home_dir_path -U $shellPath -c "$comment" $user
# Defined LOGIN_SHELL with Default HOME_DIR
#elif [[ "$shellPath" == *"-s"* ]] && [[ "$home_dir_path" = true ]]; then
# echo useradd -U $shellPath -c "$comment" $user
# useradd -U $shellPath -c "$comment" $user
# Default LOGIN_SHELL with Defined HOME_DIR
#elif [[ "$shellPath" == true]] && [[ "$home_dir_path" == *"-d"* ]]; then
# echo useradd $home_dir_path -U -c "$comment" $user
# useradd $home_dir_path -U -c "$comment" $user
# Default LOGIN_SHELL with NO HOME_DIR
#elif [[ "$shellPath" == true]] && [[ "$home_dir_path" == *"-M"* ]]; then
# echo useradd $home_dir_path -U -c "$comment" $user
# useradd $home_dir_path -U -c "$comment" $user
# Default LOGIN_SHELL and HOME_DIR
#else
# echo useradd -U -c "$comment" $user
# useradd -U -c "$comment" $user
#fi
# To do WareWulf sync AND add user to Slurm account
if [ $wwsh_resync ] && [ $sacctmgr_add ]; then
if [[ "$shell" == "false" ]]; then
echo useradd -U "$home_dir_path" -c "\"$comment\"" $user
useradd -U "$home_dir_path" -c "\"$comment\"" $user
else
echo useradd $shell_path -U "$home_dir_path" -c "\"$comment\"" $user
useradd $shell_path -U "$home_dir_path" -c "\"$comment\"" $user
fi
if [ $debug ]; then
echo "--> User Created."
fi
echo useradd $home_dir_path -U $shellPath -c "$comment" $user
useradd $home_dir_path -U $shellPath -c "$comment" $user
wwsh file resync passwd group shadow
if [ $debug ]; then
echo "--> WareWulf Entry Completed."
......@@ -128,23 +139,25 @@ if [ $wwsh_resync ] && [ $sacctmgr_add ]; then
if [ $debug ]; then
echo "--> Slurm Entry Completed."
fi
# To create user WITHOUT WareWulf sync & Slurm account update
elif [ !$wwsh_resync ] && [ ! $sacctmgr_add ]; then
if [[ "$shell" == "false" ]]; then
echo useradd -U "$home_dir_path" -c "\"$comment\"" $user
useradd -U "$home_dir_path" -c "\"$comment\"" $user
else
echo useradd $shell_path -U "$home_dir_path" -c "\"$comment\"" $user
useradd $shell_path -U "$home_dir_path" -c "\"$comment\"" $user
# To do Slurm account update
elif [ -z ${wwsh_resync} ] && [ $sacctmgr_add ]; then
echo useradd $home_dir_path -U $shellPath -c "$comment" $user
useradd $home_dir_path -U $shellPath -c "$comment" $user
sacctmgr -i add user name=$user account="xcbc-users"
if [ $debug ]; then
echo "--> Slurm Entry Completed."
fi
# To do WareWulf sync
elif [ $wwsh_resync ] && [ -z ${sacctmgr_add} ]; then
echo useradd $home_dir_path -U $shellPath -c "$comment" $user
useradd $home_dir_path -U $shellPath -c "$comment" $user
wwsh file resync passwd group shadow
if [ $debug ]; then
echo "--> User Created."
echo "--> WareWulf Entry Completed."
fi
# Raise error
else
echo "--warewulf-sync AND --sacct-add must be either 'absent' or 'present'"
show_help
exit 1
echo useradd $home_dir_path -U $shellPath -c "$comment" $user
useradd $home_dir_path -U $shellPath -c "$comment" $user
fi
#To get UID and GID of given USER_NAME
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment