feat: delete repo with the same unix user

This commit is contained in:
bsourisse 2023-08-20 16:40:23 +02:00 committed by Ravinou
parent 071e9733d2
commit 7b04e8dc4c
No known key found for this signature in database
GPG key ID: EEEE670C40F6A4D7

View file

@ -1,32 +1,49 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Shell created by Raven for BorgWarehouse. # Shell created by Raven for BorgWarehouse.
# This shell takes 1 arg : [user] with 8 char. length only. # This shell takes 1 arg : [repositoryName] with 8 char. length only.
# This shell **delete the user** in arg and **all his data**. # This shell **delete the repository** in arg and **all his data** and the line associated in the authorized_keys file.
# Exit when any command fails # Exit when any command fails
set -e set -e
# Load .env if exists
if [[ -f .env ]]; then
source .env
fi
# Default value if .env not exists
: "${home:=/home/borgwarehouse}"
# Some variables
pool="${home}/repos"
authorized_keys="${home}/.ssh/authorized_keys"
# Check arg # Check arg
if [[ $# -ne 1 || $1 = "" ]]; then if [[ $# -ne 1 || $1 = "" ]]; then
echo "You must provide a username in argument." echo "You must provide a repositoryName in argument."
exit 1 exit 1
fi fi
# Check if username length is 8 char. With createRepo.sh our randoms have a length of 8 characters. # Check if the repositoryName length is 8 char. With createRepo.sh our randoms have a length of 8 characters.
# If we receive another length there is necessarily a problem. # If we receive another length there is necessarily a problem.
username=$1 repositoryName=$1
if [ ${#username} != 8 ] if [ ${#repositoryName} != 8 ]; then
then echo "Error with the length of the repositoryName."
echo "Error with the length of the username."
exit 2 exit 2
fi fi
# Delete the user if it exists # Delete the repository and the line associated in the authorized_keys file
if id "$1" &>/dev/null; then if [ -d "$1" ]; then
sudo userdel -rf "$1" # Delete the repository
echo "The user $1 and all his data have been deleted" rm -rf "${pool}/${repositoryName}"
# Delete the line in the authorized_keys file
sed -i "/${repositoryName}/d" "${authorized_keys}"
echo "The folder "${pool}/${repositoryName}" and all its data have been deleted. The line associated in the authorized_keys file has been deleted."
exit 3
else else
echo "The user $1 does not exist" # Delete the line in the authorized_keys file
exit 3 sed -i "/${repositoryName}/d" "${authorized_keys}"
fi echo "The folder "${pool}/${repositoryName}" did not exist (repository never initialized or used). The line associated in the authorized_keys file has been deleted."
exit 3
fi