2024-08-22 21:03:07 +05:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
INSTALL_ENV="/data/gitea-init/install.env"
|
|
|
|
|
|
|
|
trap 'echo -e "\033[31mgitea-admin.sh: Something went wrong\033[0m"; exit 1' ERR
|
|
|
|
set -e
|
|
|
|
|
|
|
|
source $INSTALL_ENV
|
|
|
|
|
|
|
|
echo " Create admin password..."
|
|
|
|
GITEA_ADMIN_PASSWORD=$(pwgen -s 20 1);
|
|
|
|
echo "Save GITEA_ADMIN_PASSWORD token to $INSTALL_ENV..."
|
|
|
|
cd /data/gitea-init
|
|
|
|
bash env-gen-install.sh GITEA_ADMIN_PASSWORD $GITEA_ADMIN_PASSWORD
|
|
|
|
|
2024-08-22 23:50:33 +05:00
|
|
|
echo "Create Gitea admin user on server $SERVER_REMOTE_HOST..."
|
|
|
|
ssh_cmd="
|
|
|
|
docker exec -i --user git gitea gitea admin user create --username $GITEA_USER_NAME --password $GITEA_ADMIN_PASSWORD --email $ADMIN_EMAIL --admin --must-change-password=false
|
|
|
|
docker exec -i --user git gitea gitea admin user list
|
|
|
|
"
|
|
|
|
|
2024-08-31 02:26:18 +05:00
|
|
|
ssh-keygen -f ~/.ssh/known_hosts -R $GITEA_API_HOST
|
|
|
|
|
2024-08-22 23:50:33 +05:00
|
|
|
echo "Connecting to server and executing command..."
|
|
|
|
echo $ssh_cmd
|
|
|
|
ssh -p $SERVER_SSH_PORT $SERVER_REMOTE_USER@$SERVER_REMOTE_HOST "$ssh_cmd"
|
2024-08-22 21:03:07 +05:00
|
|
|
|
|
|
|
echo " Create admin access token..."
|
|
|
|
TOKEN=$(curl -s -X POST "https://$GITEA_API_HOST/api/v1/users/$GITEA_USER_NAME/tokens" \
|
|
|
|
-H "Content-Type: application/json" \
|
|
|
|
-d '{"name": "WRITE_USER_REPO", "scopes": ["write:repository", "write:user"]}' \
|
|
|
|
-u "$GITEA_USER_NAME:$GITEA_ADMIN_PASSWORD" | jq -r .sha1)
|
|
|
|
|
|
|
|
if [[ -z "$TOKEN" || "$TOKEN" == "null" ]]; then
|
|
|
|
echo "Failed to obtain admin token"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Save GITEA_WRITE_USER_REPO token to $INSTALL_ENV..."
|
|
|
|
cd /data/gitea-init
|
|
|
|
bash env-gen-install.sh GITEA_WRITE_USER_REPO $TOKEN
|
|
|
|
|
|
|
|
echo "Add public SSH key to gitea app..."
|
|
|
|
SSH_PUBLIC_KEY=$(cat ~/.ssh/id_ed25519.pub)
|
|
|
|
|
|
|
|
echo "Get SSH keys list from gitea API..."
|
|
|
|
keys=$(curl -X GET -H "Authorization: token $TOKEN" "https://$GITEA_API_HOST/api/v1/user/keys")
|
|
|
|
|
|
|
|
echo "Search SSH key in gitea..."
|
|
|
|
existing_key_id=$(echo $keys | jq -r ".[] | select(.title == \"$SERVER_NAME\") | .id")
|
|
|
|
|
|
|
|
echo "If already exist SSH key in gitea, remove it..."
|
|
|
|
if [ -n "$existing_key_id" ]; then
|
|
|
|
curl -X DELETE -H "Authorization: token $TOKEN" "https://$GITEA_API_HOST/api/v1/user/keys/$existing_key_id"
|
|
|
|
echo "SSH key removed"
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Add new SSH public key in gitea..."
|
|
|
|
curl -X POST \
|
|
|
|
"https://$GITEA_API_HOST/api/v1/user/keys" \
|
|
|
|
-H "Authorization: token $TOKEN" \
|
|
|
|
-H "Content-Type: application/json" \
|
|
|
|
-d "{
|
|
|
|
\"title\": \"$SERVER_NAME\",
|
|
|
|
\"key\": \"$SSH_PUBLIC_KEY\"
|
|
|
|
}"
|
|
|
|
|
|
|
|
|
|
|
|
trap - ERR
|
|
|
|
echo "New Gitea user and api token created"
|