Improving scripts

This commit is contained in:
2026-03-14 19:15:38 +00:00
parent 23bbbbdbe8
commit 2de91ed77b
4 changed files with 76 additions and 104 deletions

View File

@@ -9,9 +9,19 @@ echo """
""" """
"$HOME/.local/bin/install/password.sh" export MASTERPW=$(gum input --header="Enpass Master password" --password)
"$HOME/.local/bin/install/password-manager.sh"
"$HOME/.local/bin/install/ssh.sh" "$HOME/.local/bin/station-install/password.sh"
"$HOME/.local/bin/install/server.sh" "$HOME/.local/bin/station-install/password-manager.sh"
"$HOME/.local/bin/install/firefox.sh"
"$HOME/.local/bin/install/nextcloud.sh" CHECKPW=$(enpass-cli -vault="$HOME/.local/share/Enpass/Enpass/Vaults/primary" -sort list 2>&1)
while [[ "$CHECKPW" == *level=error* ]]; do
export MASTERPW=$(gum input --header="Enpass Master password (incorrect, try again)" --password)
CHECKPW=$(enpass-cli -vault="$VAULT_DIR" -sort list)
done
"$HOME/.local/bin/station-install/ssh.sh"
"$HOME/.local/bin/station-install/server.sh"
# "$HOME/.local/bin/station-install/firefox.sh"
# "$HOME/.local/bin/station-install/nextcloud.sh"

View File

@@ -4,38 +4,39 @@ echo "-------------------------------------"
echo "Enpass Setup Script" echo "Enpass Setup Script"
echo "-------------------------------------" echo "-------------------------------------"
export MASTERPW=$(gum input --header="Enpass Master password" --password) if [[ -z $MASTERPW ]]; then
export MASTERPW=$(gum input --header="Enpass Master password" --password)
fi
echo "stofflees@gmail.com" | wl-copy VAULT_DIR="$HOME/.local/share/Enpass/Enpass/Vaults/primary"
station-launch-password-manager if [ -f "$VAULT_DIR/vault.enpassdb" ]; then
echo "Vault already exists, skipping Enpass setup."
else
echo -n "stofflees@gmail.com" | wl-copy
zenity --title="Enpass Setup" \ station-launch-password-manager
--info \
--text="Email copied.\nPress OK when it asks for webdav URL" \
|| gum confirm "You closed the dialog, do you want to continue?" \
|| exit 1
echo "https://tower.scarif.space" | wl-copy zenity --title="Enpass Setup" \
--info \
--text="Email copied.\nPress OK when it asks for webdav URL" \
|| gum confirm "You closed the dialog, do you want to continue?" \
|| exit 1
zenity --title="Enpass Setup" \ echo -n "https://tower.scarif.space" | wl-copy
--info \
--text="Tower URL copied.\nPress OK when it asks for master password" \
|| gum confirm "You closed the dialog, do you want to continue?" \
|| exit 1
echo "$MASTERPW" | wl-copy zenity --title="Enpass Setup" \
--info \
--text="Tower URL copied.\nPress OK when it asks for master password" \
|| gum confirm "You closed the dialog, do you want to continue?" \
|| exit 1
read -n 1 -r -s -p $'Press enter when you have synced enpass...\n' echo -n "$MASTERPW" | wl-copy
wl-copy --clear read -n 1 -r -s -p $'Press enter when you have synced enpass...\n'
CHECKPW=$(enpass-cli -vault="$HOME/.local/share/Enpass/Enpass/Vaults/primary" -sort list 2>&1) wl-copy --clear
while [[ "$CHECKPW" == *level=fatal* ]]; do echo "Enpass setup complete."
export MASTERPW=$(gum input --header="Enpass Master password (incorrect, try again)" --password") fi
CHECKPW=$(enpass-cli -vault="$HOME/.local/share/Enpass/Enpass/Vaults/primary" -sort list)
done
echo "Password set successfuly"

View File

@@ -5,80 +5,36 @@ echo "Server Setup Script"
echo "-------------------------------------" echo "-------------------------------------"
echo "Extracting login details from enpass" echo "Extracting login details from enpass"
SCARIF=$(enpass-cli -vault="$HOME/Documents/Enpass/Vaults/primary" -sort show "Scarif space" 2>&1) SERVER=$(enpass-cli -vault="$HOME/.local/share/Enpass/Enpass/Vaults/primary" -json -sort show "Scarif space" 'select(.label=="New new admin password")')
LABSUN=$(echo "$LABS" | grep -Po "(?<=Admin login: )\w+") SERVERUN=$(echo "$LABS" | jq -r '.[].login')
LABSPW=$(enpass-cli -vault="$HOME/Documents/Enpass/Vaults/primary" -sort pass "Scarif: Labs") SERVERPW=$(echo "$LABS" | jq -r '.[].password')
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 SERVERIP="scarif.space"
SERVERPORT=629
echo "---" echo "~~~~~~~~"
echo "Generating the SSH key if it does not exist" echo "Generating 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")
# A method to generate the parameters for creating an SSH key on gitea echo "~~~~~~~~"
generate_post_data() { echo "Copying SSH key to server known hosts"
cat <<EOF ssh-keyscan -p $SERVERPORT $SERVERIP >> ~/.ssh/known_hosts
{
"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 "~~~~~~~~"
echo "Checking if the key exists on the server" echo "Checking if key exists on the server"
if [[ $KEY_EXISTS == true ]]; then if sshpass -p "$SERVERPW" ssh -o PasswordAuthentication=yes -p $SERVERPORT "$SERVERUN@$SERVERIP" "grep -q \"$KEY\" ~/.ssh/authorized_keys 2>/dev/null"; then
echo "Found keys with the same title as this machine." echo "Key already exists on the server. Skipping upload."
# Extract the ids of the keys with the same title as this machine exit 0
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
fi fi
echo "---" echo "~~~~~~~~"
echo "Saving the new key" echo "Uploading the SSH key to the server"
curl -X POST \ sshpass -p "$SERVERPW" ssh -p $SERVERPORT "$SERVERUN@$SERVERIP" "mkdir -p ~/.ssh && echo '$KEY' >> ~/.ssh/authorized_keys && chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys"
-s -S \
-H "Accept: application/json" \
-H "Content-Type:application/json" \
--data "$(generate_post_data)" \
--url "$KEYS_URL"
HTTP_REPLACE="s/https:\/\/labs\.scarif\.space\//git@labs.scarif.space:/" echo "~~~~~~~~"
echo "Testing SSH connection"
for dir in "$HOME/.local/nixos"; do ssh -i "$SSHPATH" -p $SERVERPORT "$SERVERUN@$SERVERIP" "echo 'Successfully connected to $SERVERIP'"
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"

View File

@@ -5,9 +5,9 @@ echo "SSH Setup Script"
echo "-------------------------------------" echo "-------------------------------------"
echo "Extracting login details from enpass" echo "Extracting login details from enpass"
LABS=$(enpass-cli -vault="$HOME/Documents/Enpass/Vaults/primary" -sort show "Scarif: Labs" 2>&1) LABS=$(enpass-cli -vault="$HOME/.local/share/Enpass/Enpass/Vaults/primary" -json -sort show "Scarif: Labs")
LABSUN=$(echo "$LABS" | grep -Po "(?<=login: )\w+") LABSUN=$(echo "$LABS" | jq -r '.[].login')
LABSPW=$(enpass-cli -vault="$HOME/Documents/Enpass/Vaults/primary" -sort pass "Scarif: Labs") LABSPW=$(echo "$LABS" | jq -r '.[].password')
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
@@ -42,19 +42,23 @@ echo "Checking if the key exists on the server"
if [[ $KEY_EXISTS == true ]]; then if [[ $KEY_EXISTS == true ]]; then
echo "Found keys with the same title as this machine." 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|ascii_downcase == ($TITLE|ascii_downcase)))[].id') MATCH=$(echo "$KEYS" | jq --arg TITLE "$TITLE" 'map(select(.title|ascii_downcase == ($TITLE|ascii_downcase)))[]')
# Loop through the keys and remove them from gitea to be replaced by the new one if [[ -n "$MATCH" ]]; then
echo "Removing found keys to replace with this machine" ID=$(echo "$MATCH" | jq -r '.id')
if [ ! -z "$IDS" ]; then EXISTING_KEY=$(echo "$MATCH" | jq -r '.key')
for ID in $IDS; do
echo "Deleting key with ID $ID" if [[ "$KEY" == "$EXISTING_KEY" ]]; then
echo "Key already exists and is correct. Nothing to do."
exit 0;
else
echo "Key exists but differs. Deleting existing key ID $ID"
curl -X DELETE \ curl -X DELETE \
-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" --url "$KEYS_URL/$ID"
done fi
fi fi
fi fi
@@ -83,3 +87,4 @@ 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"