Implement self-deletion of setup and customization scripts after successful execution. Enhance user password setting process with retry logic for both sysadmin and additional users, improving error handling and user feedback during setup.

This commit is contained in:
2025-09-07 21:34:37 +01:00
parent a25d98fdeb
commit 0083186826
2 changed files with 46 additions and 6 deletions

View File

@@ -247,6 +247,11 @@ main() {
echo ""
echo -e "${GREEN}Server customization completed!${NC}"
echo -e "${YELLOW}You may need to log out and back in to see the hostname change.${NC}"
# Self-delete the script after successful completion
log "Cleaning up customization script..."
rm -f "$0"
log "Customization script deleted successfully"
}
main "$@"

View File

@@ -329,9 +329,24 @@ create_sysadmin_user() {
log "User $SYSADMIN_USER created successfully"
SYSADMIN_USER_CREATED="yes"
# Set password for sysadmin user
echo "Please set a password for user $SYSADMIN_USER:"
passwd "$SYSADMIN_USER"
# Set password for sysadmin user with retry logic
local max_attempts=3
local attempt=1
while [ $attempt -le $max_attempts ]; do
echo "Please set a password for user $SYSADMIN_USER (attempt $attempt of $max_attempts):"
if passwd "$SYSADMIN_USER"; then
log "Password set successfully for $SYSADMIN_USER"
break
else
warn "Failed to set password for $SYSADMIN_USER"
if [ $attempt -eq $max_attempts ]; then
error "Failed to set password after $max_attempts attempts. Exiting."
fi
echo "Please try again..."
((attempt++))
fi
done
fi
# Add sysadmin to sudo group
@@ -352,9 +367,24 @@ create_additional_user() {
log "User $ADDITIONAL_USER created successfully"
ADDITIONAL_USER_CREATED="yes"
# Set password for additional user
echo "Please set a password for user $ADDITIONAL_USER:"
passwd "$ADDITIONAL_USER"
# Set password for additional user with retry logic
local max_attempts=3
local attempt=1
while [ $attempt -le $max_attempts ]; do
echo "Please set a password for user $ADDITIONAL_USER (attempt $attempt of $max_attempts):"
if passwd "$ADDITIONAL_USER"; then
log "Password set successfully for $ADDITIONAL_USER"
break
else
warn "Failed to set password for $ADDITIONAL_USER"
if [ $attempt -eq $max_attempts ]; then
error "Failed to set password after $max_attempts attempts. Exiting."
fi
echo "Please try again..."
((attempt++))
fi
done
fi
# Add additional user to sudo group (existing or newly created)
@@ -629,6 +659,11 @@ main() {
# Finalize
finalize_setup
# Self-delete the script after successful completion
log "Cleaning up setup script..."
rm -f "$0"
log "Setup script deleted successfully"
}
main "$@"