Improving scripts
This commit is contained in:
@@ -5,80 +5,36 @@ echo "Server Setup Script"
|
||||
echo "-------------------------------------"
|
||||
|
||||
echo "Extracting login details from enpass"
|
||||
SCARIF=$(enpass-cli -vault="$HOME/Documents/Enpass/Vaults/primary" -sort show "Scarif space" 2>&1)
|
||||
LABSUN=$(echo "$LABS" | grep -Po "(?<=Admin login: )\w+")
|
||||
LABSPW=$(enpass-cli -vault="$HOME/Documents/Enpass/Vaults/primary" -sort pass "Scarif: Labs")
|
||||
SERVER=$(enpass-cli -vault="$HOME/.local/share/Enpass/Enpass/Vaults/primary" -json -sort show "Scarif space" 'select(.label=="New new admin password")')
|
||||
SERVERUN=$(echo "$LABS" | jq -r '.[].login')
|
||||
SERVERPW=$(echo "$LABS" | jq -r '.[].password')
|
||||
|
||||
SSHPATH="$HOME/.ssh/id_ed25519" # The path to the SSH key file
|
||||
TITLE="$USER@$(cat /etc/hostname)" # The title for the SSH key
|
||||
SERVERIP="scarif.space"
|
||||
SERVERPORT=629
|
||||
|
||||
echo "---"
|
||||
echo "~~~~~~~~"
|
||||
echo "Generating the SSH key if it does not exist"
|
||||
[ ! -f $SSHPATH ] && ssh-keygen -t ed25519 -f "$SSHPATH" -N "" -q
|
||||
|
||||
KEY=$(cat "$SSHPATH.pub")
|
||||
|
||||
# A method to generate the parameters for creating an SSH key on gitea
|
||||
generate_post_data() {
|
||||
cat <<EOF
|
||||
{
|
||||
"key": "$KEY",
|
||||
"read_only": false,
|
||||
"title": "$TITLE"
|
||||
}
|
||||
EOF
|
||||
}
|
||||
|
||||
CREDENTIALS="$LABSUN:$LABSPW" # The credentials to pass to the API
|
||||
KEYS_URL="https://$CREDENTIALS@labs.scarif.space/api/v1/user/keys"
|
||||
|
||||
# Get all the existing keys
|
||||
KEYS=$(curl -X GET -s -S --url "$KEYS_URL")
|
||||
|
||||
KEY_EXISTS=$(echo $KEYS | jq --arg TITLE "$TITLE" 'map(.title|ascii_downcase)|contains([$TITLE|ascii_downcase])')
|
||||
echo "~~~~~~~~"
|
||||
echo "Copying SSH key to server known hosts"
|
||||
ssh-keyscan -p $SERVERPORT $SERVERIP >> ~/.ssh/known_hosts
|
||||
|
||||
echo "~~~~~~~~"
|
||||
echo "Checking if the key exists on the server"
|
||||
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
|
||||
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
|
||||
echo "Removing found keys to replace with this machine"
|
||||
if [ ! -z "$IDS" ]; then
|
||||
for ID in $IDS; do
|
||||
echo "Deleting key with ID $ID"
|
||||
curl -X DELETE \
|
||||
-s -S \
|
||||
-H "Accept: application/json" \
|
||||
-H "Content-Type:application/json" \
|
||||
--url "$KEYS_URL/$ID"
|
||||
done
|
||||
fi
|
||||
echo "Checking if key exists on the server"
|
||||
if sshpass -p "$SERVERPW" ssh -o PasswordAuthentication=yes -p $SERVERPORT "$SERVERUN@$SERVERIP" "grep -q \"$KEY\" ~/.ssh/authorized_keys 2>/dev/null"; then
|
||||
echo "Key already exists on the server. Skipping upload."
|
||||
exit 0
|
||||
fi
|
||||
|
||||
echo "---"
|
||||
echo "Saving the new key"
|
||||
curl -X POST \
|
||||
-s -S \
|
||||
-H "Accept: application/json" \
|
||||
-H "Content-Type:application/json" \
|
||||
--data "$(generate_post_data)" \
|
||||
--url "$KEYS_URL"
|
||||
echo "~~~~~~~~"
|
||||
echo "Uploading the SSH key to the server"
|
||||
sshpass -p "$SERVERPW" ssh -p $SERVERPORT "$SERVERUN@$SERVERIP" "mkdir -p ~/.ssh && echo '$KEY' >> ~/.ssh/authorized_keys && chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys"
|
||||
|
||||
HTTP_REPLACE="s/https:\/\/labs\.scarif\.space\//git@labs.scarif.space:/"
|
||||
|
||||
for dir in "$HOME/.local/nixos"; do
|
||||
dir="$HOME/.local/src/$dir"
|
||||
if [ -d $dir ]; then
|
||||
cd "$dir"
|
||||
SSH_URL=$(git remote get-url origin | sed "$HTTP_REPLACE")
|
||||
git remote set-url origin "$SSH_URL"
|
||||
fi
|
||||
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")
|
||||
git --git-dir "$HOME/.config/dotfiles/.git" --work-tree="$HOME" remote set-url origin "$DOTFILES_SSH_URL"
|
||||
echo "~~~~~~~~"
|
||||
echo "Testing SSH connection"
|
||||
ssh -i "$SSHPATH" -p $SERVERPORT "$SERVERUN@$SERVERIP" "echo 'Successfully connected to $SERVERIP'"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user