diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ba00f71 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +install.env \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..d979689 --- /dev/null +++ b/README.md @@ -0,0 +1,62 @@ + +#### Запустить все: + +```sh +cd /data/gitea-init +bash gitea-init.sh +``` + +`gitea-init.sh` выполнит следующие команды: + +```sh +bash gitea-admin.sh +bash create-all.sh +bash push-all.sh +``` + +После успешного выполнения репозиторий Gitea будет готов к работе. + + +#### Первоначальная конфигурация: + +Параметры, необходимые для инициализации Gitea: + +```env +GITEA_API_HOST=git. +GITEA_USER_NAME= +ADMIN_EMAIL= +SERVER_NAME=server- +GIT_REMOTE_PREFIX=- +GIT_BRANCH= +``` + + +#### Создание админа и токена доступа к Gitea: + +```sh +cd /data/gitea-init +bash gitea-admin.sh +``` +Имя токена в Gitea: `WRITE_USER_REPO` +Имя переменной в `/data/secrets/dev.env`: `GITEA_WRITE_USER_REPO` + +Права: +* `write:repository ` +* `write:user` + + + +#### Создать в Gitea репозитории из файла `repos.list`: + +```sh +cd /data/gitea-init +bash create-all.sh +``` + + +#### Push всех репозтиориев из файла `repos.list`: + +```sh +cd /data/gitea-init +bash push-all.sh +``` \ No newline at end of file diff --git a/create-all.sh b/create-all.sh index 9901a24..1f5caa7 100644 --- a/create-all.sh +++ b/create-all.sh @@ -1,36 +1,39 @@ #!/bin/bash -LIST_FILE="./repos.list" -ENV_FILE="/data/secrets/dev.env" +LIST_FILE="/data/gitea-init/repos.list" +ENV_FILE="/data/gitea-init/install.env" -# Checking for a CSV file +trap 'echo -e "\033[31mcreate-all.sh: Something went wrong\033[0m"; exit 1' ERR +set -e + +echo "Checking for a repos.list file..." if [ ! -f "$LIST_FILE" ]; then echo "File $LIST_FILE not found" exit 1 fi -# Including a file with environment variables +echo "Including a file with environment variables..." if [ -f $ENV_FILE ]; then source $ENV_FILE else - echo "File dev.env not found" + echo "File install.env not found" exit 1 fi -# Checking the presence of a variable GITEA_W_USER_REPO -if [ -z "$GITEA_W_USER_REPO" ]; then - echo "The GITEA_W_USER_REPO variable is not set" +echo "Checking the presence of a variable GITEA_WRITE_USER_REPO..." +if [ -z "$GITEA_WRITE_USER_REPO" ]; then + echo "The GITEA_WRITE_USER_REPO variable is not set" exit 1 fi -# Reading a CSV file and creating repositories +echo "Reading a CSV file and creating repositories..." OLDIFS=$IFS IFS=' | ' while read -r NAME_REPO DESCRIPTION_REPO; do if [ "$NAME_REPO" != "NAME_REPO" ]; then # Skip title - RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -X POST "https://git.rozenlab.com/api/v1/user/repos" \ + RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -X POST "https://$GITEA_API_HOST/api/v1/user/repos" \ -H "Content-Type: application/json" \ - -H "Authorization: token $GITEA_W_USER_REPO" \ + -H "Authorization: token $GITEA_WRITE_USER_REPO" \ -d '{ "name": "'"$NAME_REPO"'", "private": true, @@ -44,4 +47,7 @@ while read -r NAME_REPO DESCRIPTION_REPO; do fi fi done < "$LIST_FILE" -IFS=$OLDIFS \ No newline at end of file +IFS=$OLDIFS + +trap - ERR +echo "Create all complete" \ No newline at end of file diff --git a/env-gen-install.sh b/env-gen-install.sh new file mode 100644 index 0000000..d19520c --- /dev/null +++ b/env-gen-install.sh @@ -0,0 +1,42 @@ +#!/bin/bash + +if [ -z "$1" ]; then + echo "Use: $0 [value]" + exit 1 +fi + +VARIABLE_NAME=$1 +ENV_FILE="/data/gitea-init/install.env" + +trap 'echo -e "\033[31menv-gen-install.sh: Something went wrong\033[0m"; exit 1' ERR +set -e + +# Password gen: 20chars,0-9,a-z +generate_random_password() { + pwgen -s 20 1 +} + +# If the second parameter is specified, use it as the value of the variable +if [ -n "$2" ]; then + VALUE=$2 +else + VALUE=$(generate_random_password) +fi + +echo "Creating dir and .env file if it doesn't exist..." +if [ ! -f "$ENV_FILE" ]; then + mkdir -p "$(dirname "$ENV_FILE")" + touch "$ENV_FILE" +fi + +echo "Update or add a variable to the .env file..." +if grep -q "^$VARIABLE_NAME=" "$ENV_FILE"; then + # The variable exists, update its value + sed -i "s/^$VARIABLE_NAME=.*/$VARIABLE_NAME=$VALUE/" "$ENV_FILE" +else + # The variable does not exist, add it to the file + echo "$VARIABLE_NAME=$VALUE" >> "$ENV_FILE" +fi + +trap - ERR +echo "Variable $VARIABLE_NAME successfully updated/added to $ENV_FILE" \ No newline at end of file diff --git a/gitea-admin.sh b/gitea-admin.sh new file mode 100644 index 0000000..9c24edd --- /dev/null +++ b/gitea-admin.sh @@ -0,0 +1,63 @@ +#!/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 + +echo " Create admin user..." +docker exec -it --user git gitea gitea admin user create --username $GIT_USER --password $GITEA_ADMIN_PASSWORD --email $ADMIN_EMAIL --admin +docker exec -it --user git gitea gitea admin user list + +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" \ No newline at end of file diff --git a/gitea-init.sh b/gitea-init.sh new file mode 100644 index 0000000..5f89ddc --- /dev/null +++ b/gitea-init.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +trap 'echo -e "\033[31mgitea-init.sh: Something went wrong\033[0m"; exit 1' ERR +set -e + +bash gitea-admin.sh +bash create-all.sh +bash push-all.sh + +trap - ERR +echo "Gitea inited" \ No newline at end of file diff --git a/push-all.sh b/push-all.sh index 5d4db65..8ec4b4e 100644 --- a/push-all.sh +++ b/push-all.sh @@ -1,90 +1,37 @@ +#!/bin/bash -# notes -# git@rozenlab.com:leo/notes.git -cd /data/notes -git push notes main +LIST_FILE="/data/gitea-init/repos.list" +ENV_FILE="/data/gitea-init/install.env" -# ssl-gen -# git@rozenlab.com:leo/ssl-gen.git -cd /data/ssl-gen -git push ssl-gen main +trap 'echo -e "\033[31mcreate-all.sh: Something went wrong\033[0m"; exit 1' ERR +set -e -# utils -# git@rozenlab.com:leo/utils.git -cd /data/utils -git push utils main +echo "Checking for a repos.list file..." +if [ ! -f "$LIST_FILE" ]; then + echo "File $LIST_FILE not found" + exit 1 +fi -# server-origin -# git@rozenlab.com:leo/server-origin.git -cd /data/server-origin -git push server-origin main +echo "Including a file with environment variables..." +if [ -f $ENV_FILE ]; then + source $ENV_FILE +else + echo "File install.env not found" + exit 1 +fi -# backup -# git@rozenlab.com:leo/backup.git -cd /data/backup -git push backup main +echo "Reading a CSV file and creating repositories..." +OLDIFS=$IFS +IFS=' | ' +while read -r NAME_REPO DESCRIPTION_REPO; do + if [ "$NAME_REPO" != "NAME_REPO" ]; then # Skip title + cd /data/$NAME_REPO + git remote remove ${PREFIX}$NAME_REPO + git remote add ${PREFIX}$NAME_REPO git@$GITEA_API_HOST:$GITEA_USER_NAME/$NAME_REPO.git + git push ${PREFIX}$NAME_REPO $GIT_BRANCH + fi +done < "$LIST_FILE" +IFS=$OLDIFS -# gitea -# git@rozenlab.com:leo/gitea.git -cd /data/gitea -git push gitea main - -# traefik -# git@rozenlab.com:leo/traefik.git -cd /data/traefik -git push traefik main - -# socks5 -# git@rozenlab.com:leo/socks5.git -cd /data/socks5 -git push socks5 main - -# install -# git@rozenlab.com:leo/install.git -cd /data/install -git push install main - -# server-backup -# git@rozenlab.com:leo/server-backup.git -cd /data/server-backup -git push server-backup main - -# tetris -# git@rozenlab.com:Leo/tetris.git -cd /data/tetris -git push tetris main - -# brickgame -# git@rozenlab.com:leo/brickgame.git -cd /data/brickgame -git push brickgame main - -# blog-frontend -# git@rozenlab.com:leo/blog-frontend.git -cd /data/blog-frontend -git push blog-frontend main - -# blog-wp -# git@rozenlab.com:leo/blog-wp.git -cd /data/blog-wp -git push blog-wp main - -# nginx-main -# git@rozenlab.com:leo/nginx-main.git -#cd /data/nginx-main -#git push nginx-main main - -# pgadmin -# git@rozenlab.com:leo/pgadmin.git -cd /data/pgadmin -git push pgadmin main - -# postgres -# git@rozenlab.com:leo/postgres.git -cd /data/postgres -git push postgres main - -# gitea-init -# git@rozenlab.com:leo/gitea-init.git -cd /data/gitea-init -git push gitea-init main \ No newline at end of file +trap - ERR +echo "Create all complete" \ No newline at end of file diff --git a/repos.list b/repos.list index 44ee37f..85d1537 100644 --- a/repos.list +++ b/repos.list @@ -9,6 +9,11 @@ backup | Backup all databases & files server-origin | Head site, repositories, etc notes | Other notes ssl-gen | Self signed SSL certs generator -brickgame | Tetris and other games tetris | Classic Tetris -brickgame | Modular system with several games \ No newline at end of file +brickgame | Modular system with several games +blog-wp | Wordpress dev blog +blog-wp-install | Init wordpress blog +mariadb | Docker Mariadb for Wordpress dev blog +gitea-init | Create all repos of origin in Gitea +postgres | Docker postgres for traefik +pgadmin | Docker pgadmin for traefik