scripts/cluster/create.homedir.sh

79 lines
2.7 KiB
Bash
Raw Normal View History

#!/bin/sh
# Vars {{{
DEBUG=1
username="${1}"
homedir_basepath="/mnt/home.ipr"
user_id="$(id -u "${username}" 2>/dev/null)"
user_primary_group="$(id -g "${username}" 2>/dev/null)"
user_homedir_path="${homedir_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 if homedir already exist {{{
if [ $(ls -1d "${user_homedir_path}" 2> /dev/null) ]; then
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: homedir check — ${user_homedir_path} already exists. Abord this script."
exit 1
else
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: homedir check — ${user_homedir_path} doesn't exists. Continue."
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 home base path is available {{{
if [ $(ls -1 -- "${homedir_basepath}" 2> /dev/null| wc -l) -gt 5 ]; then
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: home_base check — ${homedir_basepath} seems available."
else
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: home_base check — ${homedir_basepath} doesn't seems available."
exit 1
fi
# }}}
# Try to create user's homedir {{{
command mkdir -p -- "${user_homedir_path}"
if [ $(ls -1d "${user_homedir_path}" 2> /dev/null) ]; then
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: homedir creation — ${user_homedir_path} ok."
else
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: homedir creation — ${user_homedir_path} error."
exit 1
fi
# }}}
# Try to set permissions on user's homedir {{{
if command chown "${user_id}:${user_primary_group}" -R -- "${user_homedir_path}"; then
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: homedir permissions — set permission for ${user_id}:${user_primary_group} ok."
else
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: homedir permissions — set permission for ${user_id}:${user_primary_group} error."
exit 1
fi
# }}}
exit 0