Add a script to create user's work dir

This commit is contained in:
Jeremy Gardais 2018-09-14 11:07:00 +02:00
parent fb40c9d1d2
commit 574d1fc318
1 changed files with 69 additions and 0 deletions

69
cluster/create.workdir.sh Executable file
View File

@ -0,0 +1,69 @@
#!/bin/sh
# Vars {{{
DEBUG=0
username="${1}"
workdir_basepath="/mnt/work"
user_id="$(id -u "${username}" 2>/dev/null)"
user_primary_group="$(id -g "${username}" 2>/dev/null)"
user_workdir_path="${workdir_basepath}/${username}"
# }}}
# Ensure to get one argument {{{
if [ "${#}" -eq 1 ]; then
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: arg check — ${0} get one argument: ${1}."
else
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: arg check — ${0} should get only one argument."
exit 1
fi
# }}}
# Check user exist {{{
if [ "${user_id}" ]; then
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: user check — ${username} exists."
else
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: user check — ${username} doesn't exists."
exit 1
fi
# }}}
# Check group exist {{{
if [ "${user_primary_group}" ]; then
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: group check — group ${user_primary_group} exists."
else
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: group check — ${username}'s primary doesn't seems available on this system."
exit 1
fi
# }}}
# Check work base path is available {{{
if [ $(ls -1 -- "${workdir_basepath}" 2> /dev/null| wc -l) -gt 5 ]; then
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: work_base check — ${workdir_basepath} seems available."
else
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: work_base check — ${workdir_basepath} doesn't seems available."
exit 1
fi
# }}}
# Try to create user's workdir {{{
command mkdir -p -- "${user_workdir_path}"
if [ $(ls -1d "${user_workdir_path}" 2> /dev/null) ]; then
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: workdir creation — ${user_workdir_path} ok."
else
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: workdir creation — ${user_workdir_path} error."
exit 1
fi
# }}}
# Try to set permissions on user's workdir {{{
if command chown "${user_id}:${user_primary_group}" -R -- "${user_workdir_path}"; then
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: workdir permissions — set permission for ${user_id}:${user_primary_group} ok."
else
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: workdir permissions — set permission for ${user_id}:${user_primary_group} error."
exit 1
fi
# }}}
exit 0