Simplified bootstrapping
This commit is contained in:
72
scripts/gitea_keys.sh
Normal file
72
scripts/gitea_keys.sh
Normal file
@@ -0,0 +1,72 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
# Check if required environment variables are set
|
||||
if [ -z "$SIA_GITEA_API_KEY" ]; then
|
||||
echo "Error: SIA_GITEA_API_KEY environment variable is not set"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ -z "$SIA_REPO_URL" ]; then
|
||||
echo "Error: SIA_REPO_URL environment variable is not set"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Extract domain and port from repo URL
|
||||
if [[ "$SIA_REPO_URL" == *":"*"/"* ]]; then
|
||||
# Format: git@domain:port/path or domain:port/path
|
||||
DOMAIN_PORT=$(echo "$SIA_REPO_URL" | cut -d'/' -f1)
|
||||
DOMAIN=$(echo "$DOMAIN_PORT" | cut -d':' -f1)
|
||||
PORT=$(echo "$DOMAIN_PORT" | cut -d':' -f2)
|
||||
else
|
||||
# Format: git@domain/path or domain/path (no port specified)
|
||||
DOMAIN=$(echo "$SIA_REPO_URL" | cut -d'/' -f1)
|
||||
PORT=22
|
||||
fi
|
||||
|
||||
# If domain includes username (like git@domain), extract just the domain
|
||||
if [[ "$DOMAIN" == *"@"* ]]; then
|
||||
DOMAIN=$(echo "$DOMAIN" | cut -d'@' -f2)
|
||||
fi
|
||||
|
||||
echo "Gitea domain: $DOMAIN"
|
||||
echo "Gitea port: $PORT"
|
||||
|
||||
# Create SSH directory if it doesn't exist
|
||||
mkdir -p ~/.ssh
|
||||
chmod 700 ~/.ssh
|
||||
|
||||
# Generate SSH key if it doesn't exist
|
||||
if [ ! -f ~/.ssh/sia_repo ]; then
|
||||
echo "Generating new SSH key..."
|
||||
ssh-keygen -t ed25519 -f ~/.ssh/sia_repo -N ""
|
||||
fi
|
||||
|
||||
# Get the public key
|
||||
SSH_PUBLIC_KEY=$(cat ~/.ssh/sia_repo.pub)
|
||||
|
||||
# Add host key to known_hosts to avoid verification prompt
|
||||
echo "Adding host key to known_hosts..."
|
||||
ssh-keyscan -t rsa -p $PORT $DOMAIN >> ~/.ssh/known_hosts
|
||||
chmod 600 ~/.ssh/known_hosts
|
||||
|
||||
# Check if a key with title "sia" already exists and delete it
|
||||
echo "Checking for existing SSH key in Gitea..."
|
||||
EXISTING_KEYS=$(curl -s -X GET "https://$DOMAIN/api/v1/user/keys" \
|
||||
-H "accept: application/json" \
|
||||
-H "Authorization: token $SIA_GITEA_API_KEY")
|
||||
KEY_ID=$(echo "$EXISTING_KEYS" | jq -r '.[] | select(.title=="sia") | .id')
|
||||
if [ ! -z "$KEY_ID" ]; then
|
||||
echo "Found existing key with ID $KEY_ID, deleting..."
|
||||
curl -X DELETE "https://$DOMAIN/api/v1/user/keys/$KEY_ID" \
|
||||
-H "accept: application/json" \
|
||||
-H "Authorization: token $SIA_GITEA_API_KEY"
|
||||
fi
|
||||
|
||||
# Add the SSH key to Gitea using the API
|
||||
echo "Adding SSH key to Gitea using API..."
|
||||
curl -X POST "https://$DOMAIN/api/v1/user/keys" \
|
||||
-H "accept: application/json" \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Authorization: token $SIA_GITEA_API_KEY" \
|
||||
-d "{\"title\":\"sia\", \"key\":\"$SSH_PUBLIC_KEY\"}"
|
||||
Reference in New Issue
Block a user