updates...
This commit is contained in:
80
homelab/applications/app-template-generator/create-user.sh
Normal file
80
homelab/applications/app-template-generator/create-user.sh
Normal file
@ -0,0 +1,80 @@
|
||||
#!/bin/zsh
|
||||
while [[ "$#" -gt 0 ]]
|
||||
do
|
||||
case $1 in
|
||||
--app_name) app_name="$2"
|
||||
shift;;
|
||||
--id) desired_id="$2"
|
||||
shift;;
|
||||
*) echo "Unknown parameter passed: $1"
|
||||
exit 1;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
|
||||
# Validate desired_id is a number
|
||||
if ! [[ "$desired_id" =~ ^[0-9]+$ ]]; then
|
||||
echo "Error: Invalid UID/GID. Please enter a numeric value."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
user_name="${app_name}-user"
|
||||
group_name="${app_name}-group"
|
||||
|
||||
echo "--- Checking/Creating User and Group for ${app_name} ---"
|
||||
|
||||
# --- Handle Group ---
|
||||
echo "Checking group: ${group_name}"
|
||||
existing_gid=$(getent group "${group_name}" | cut -d: -f3)
|
||||
|
||||
if [[ -n "$existing_gid" ]]; then
|
||||
if [[ "$existing_gid" -eq "$desired_id" ]]; then
|
||||
echo "Group '${group_name}' already exists with the correct GID (${desired_id})."
|
||||
else
|
||||
echo "Group '${group_name}' exists with GID ${existing_gid}, but desired GID is ${desired_id}."
|
||||
echo "Attempting to modify group GID..."
|
||||
if sudo groupmod -g "$desired_id" "${group_name}"; then
|
||||
echo "Successfully adjusted group '${group_name}' to GID ${desired_id}."
|
||||
else
|
||||
echo "Failed to adjust group '${group_name}' GID. Please check permissions or try manually."
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
else
|
||||
echo "Group '${group_name}' does not exist. Creating..."
|
||||
if sudo groupadd -g "$desired_id" "${group_name}"; then
|
||||
echo "Successfully created group '${group_name}' with GID ${desired_id}."
|
||||
else
|
||||
echo "Failed to create group '${group_name}'. Please check permissions or try manually."
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# --- Handle User ---
|
||||
echo "Checking user: ${user_name}"
|
||||
existing_uid=$(getent passwd "${user_name}" | cut -d: -f3)
|
||||
|
||||
if [[ -n "$existing_uid" ]]; then
|
||||
if [[ "$existing_uid" -eq "$desired_id" ]]; then
|
||||
echo "User '${user_name}' already exists with the correct UID (${desired_id})."
|
||||
else
|
||||
echo "User '${user_name}' exists with UID ${existing_uid}, but desired UID is ${desired_id}."
|
||||
echo "Attempting to modify user UID..."
|
||||
if sudo usermod -u "$desired_id" -g "$desired_id" "${user_name}"; then
|
||||
echo "Successfully adjusted user '${user_name}' to UID ${desired_id} and primary GID ${desired_id}."
|
||||
else
|
||||
echo "Failed to adjust user '${user_name}' UID/GID. Please check permissions or try manually."
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
else
|
||||
echo "User '${user_name}' does not exist. Creating..."
|
||||
if sudo useradd -u "$desired_id" -g "$desired_id" -s /sbin/nologin -c "Application User for ${app_name}" "${user_name}"; then
|
||||
echo "Successfully created user '${user_name}' with UID ${desired_id} and primary GID ${desired_id}."
|
||||
else
|
||||
echo "Failed to create user '${user_name}'. Please check permissions or try manually."
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
echo "--- Operation complete for ${app_name} ---"
|
||||
Reference in New Issue
Block a user