Fix script
This commit is contained in:
@@ -10,7 +10,7 @@ LABSPW=$(enpass-cli -vault="$HOME/Documents/Enpass/Vaults/primary" -sort pass "S
|
|||||||
SSHPATH="$HOME/.ssh/id_ed25519" # The path to the SSH key file
|
SSHPATH="$HOME/.ssh/id_ed25519" # The path to the SSH key file
|
||||||
TITLE="$USER@$(cat /etc/hostname)" # The title for the SSH key
|
TITLE="$USER@$(cat /etc/hostname)" # The title for the SSH key
|
||||||
|
|
||||||
# Generate the SSH key if it does not exist
|
echo "Generate the SSH key if it does not exist"
|
||||||
[ ! -f $SSHPATH ] && ssh-keygen -t ed25519 -f "$SSHPATH" -N "" -q
|
[ ! -f $SSHPATH ] && ssh-keygen -t ed25519 -f "$SSHPATH" -N "" -q
|
||||||
|
|
||||||
KEY=$(cat "$SSHPATH.pub")
|
KEY=$(cat "$SSHPATH.pub")
|
||||||
@@ -32,13 +32,15 @@ KEYS_URL="https://$CREDENTIALS@labs.scarif.space/api/v1/user/keys"
|
|||||||
# Get all the existing keys
|
# Get all the existing keys
|
||||||
KEYS=$(curl -X GET -s -S --url "$KEYS_URL")
|
KEYS=$(curl -X GET -s -S --url "$KEYS_URL")
|
||||||
|
|
||||||
KEY_EXISTS=$(echo $KEYS | jq --arg TITLE "$TITLE" 'map(.title)|contains([$TITLE])')
|
KEY_EXISTS=$(echo $KEYS | jq --arg TITLE "$TITLE" 'map(.title|ascii_downcase)|contains([$TITLE|ascii_downcase])')
|
||||||
|
|
||||||
if [[ $KEY_EXISTS == true ]]; then
|
if [[ $KEY_EXISTS == true ]]; then
|
||||||
|
echo "Found keys with the same title as this machine."
|
||||||
# Extract the ids of the keys with the same title as this machine
|
# Extract the ids of the keys with the same title as this machine
|
||||||
IDS=$(echo "$KEYS" | jq --arg TITLE "$TITLE" 'map(select(.title == $TITLE))[].id')
|
IDS=$(echo "$KEYS" | jq --arg TITLE "$TITLE" 'map(select(.title|ascii_downcase == ($TITLE|ascii_downcase)))[].id')
|
||||||
|
|
||||||
# Loop through the keys and remove them from gitea to be replaced by the new one
|
# Loop through the keys and remove them from gitea to be replaced by the new one
|
||||||
|
echo "Removing found keys to replace with this machine"
|
||||||
if [ ! -z "$IDS" ]; then
|
if [ ! -z "$IDS" ]; then
|
||||||
for ID in $IDS; do
|
for ID in $IDS; do
|
||||||
echo "Deleting key with ID $ID"
|
echo "Deleting key with ID $ID"
|
||||||
@@ -46,19 +48,18 @@ if [[ $KEY_EXISTS == true ]]; then
|
|||||||
-s -S \
|
-s -S \
|
||||||
-H "Accept: application/json" \
|
-H "Accept: application/json" \
|
||||||
-H "Content-Type:application/json" \
|
-H "Content-Type:application/json" \
|
||||||
--url "$KEYS_URL/$ID" > /dev/null
|
--url "$KEYS_URL/$ID"
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
echo "Saving the new key"
|
||||||
# Save the new key in gitea
|
|
||||||
curl -X POST \
|
curl -X POST \
|
||||||
-s -S \
|
-s -S \
|
||||||
-H "Accept: application/json" \
|
-H "Accept: application/json" \
|
||||||
-H "Content-Type:application/json" \
|
-H "Content-Type:application/json" \
|
||||||
--data "$(generate_post_data)" \
|
--data "$(generate_post_data)" \
|
||||||
--url "$KEYS_URL" > /dev/null
|
--url "$KEYS_URL"
|
||||||
|
|
||||||
HTTP_REPLACE="s/https:\/\/labs\.scarif\.space\//git@labs.scarif.space:/"
|
HTTP_REPLACE="s/https:\/\/labs\.scarif\.space\//git@labs.scarif.space:/"
|
||||||
|
|
||||||
@@ -71,6 +72,7 @@ HTTP_REPLACE="s/https:\/\/labs\.scarif\.space\//git@labs.scarif.space:/"
|
|||||||
# fi
|
# fi
|
||||||
# done
|
# done
|
||||||
|
|
||||||
|
echo "Replacing remote URL to use SSH key"
|
||||||
DOTFILES_SSH_URL=$(git --git-dir "$HOME/.config/dotfiles/.git" --work-tree="$HOME" remote get-url origin | sed "$HTTP_REPLACE")
|
DOTFILES_SSH_URL=$(git --git-dir "$HOME/.config/dotfiles/.git" --work-tree="$HOME" remote get-url origin | sed "$HTTP_REPLACE")
|
||||||
git --git-dir "$HOME/.config/dotfiles/.git" --work-tree="$HOME" remote set-url origin "$DOTFILES_SSH_URL"
|
git --git-dir "$HOME/.config/dotfiles/.git" --work-tree="$HOME" remote set-url origin "$DOTFILES_SSH_URL"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user