gitea-multi-install/giteainstall

230 lines
7.9 KiB
Bash
Executable File

#!/bin/bash
###################################################################################################
# First Created: 25012024 Author: Allan Desc: Installs Gitea on Ubuntu requires Nginx and MariaDB #
###################################################################################################
#
# Are we root
#
if [[ $(id -u) -ne 0 ]]; then echo "" && echo "Must be root or use sudo" && echo "" ; exit ; fi
#
# Are we in the right directory
#
scriptdir="gitea-multi-install" && whereami=$(pwd |awk -F'/' '{print $NF}')
if [ $whereami != $scriptdir ]; then printf "\nWrong directory! Script must be run from $scriptdir\n\n" ; exit ; fi
#
# Define variables and functions
#
red='\033[0;31m' ; bred='\033[1;31m' ; green='\033[0;92m' ; blue='\033[0;36m' ; bold='\033[1m' ; normal='\033[0m'
line (){ for i in {1..50}; do echo -n "$1"; done && printf "\n"; }
giteauser=$(cat /etc/passwd |grep "gitea" |awk -F':' '{print $1}' | sort -V |sed 's/gitea//g' |tail -n 1)
giteadb="db"
giteaport="3000"
gitealocation="/usr/local/bin/gitea"
giteaversion=$(tail -1 version)
nginxfiles="/etc/nginx/nginxsnippets"
#
# Function title
#
title () { printf "\nGitea multi instance install script V1.0 \n\n"; }
#
# Function usage
#
usage () { clear ; printf -- "\n"
printf -- "${bold}giteamultiinstall${normal} \n\n"
printf -- "${bold}Usage:${normal} \n"
printf -- "./giteainstall [-n] <gitea domain> \n"
printf -- " [-p] <gitea database password> \n"
printf -- " [-help] <this screen> \n\n"
printf -- "${bold}Examples:${normal} \n"
printf -- "./giteamultiinstall -n git.something.xyz -p giteadatabsepwd\n\n"; }
#
# Function to check if a service is already running or not
#
serviceno () { printf "\n$service is not running cannot continue...\n\n"; }
servicedead () { status=$(systemctl is-active $service); if [[ "$status" != "active" ]]; then serviceno ; exit; fi; }
#
# Function input check
#
inputcheck () {
if [[ -z "$hostname" ]]; then usage ; printf "${bold}Error! ${normal}Hostname Empty...\n\n" ; exit; fi
if [[ -z "$giteapwd" ]]; then usage ; printf "${bold}Error! ${normal}Database Password Empty...\n\n" ; exit; fi; }
#
# If Nginx and MariaDB is not running then die
#
service="mariadb" ; servicedead ; service="nginx" ; servicedead
#
# Display title
#
clear ; title
#
# Configure command line options
#
if [[ ! $@ =~ ^\-.+ ]]; then usage; fi
while getopts "n:p:h:" option; do
case $option in
n) # hostname
hostname=$(echo ${OPTARG} | tr '[:upper:]' '[:lower:]');;
p) # database password
giteapwd=$OPTARG;;
h) # display help
usage ; exit;;
\?) # invalid option
printf "Type $0 -help for help\n\n" ; exit;;
esac
done
#
# Check if input conditions are met
#
inputcheck $hostname $giteapwd
#
# Check if a Nginx conf file with the hostname allready exists
#
hostcheck=$(grep -r --exclude="giteainstall" --exclude="postinstall" --exclude="README.md" $hostname /etc/nginx/ *)
if [ ! -z "$hostcheck" ]; then printf "Found a configuration file already containing $hostname in /ect/nginx/* going to abort...\n" ; exit ; fi
#
# Download gitea only if gitea does not allready exists in /etc/gitea
#
if [ ! -f $gitealocation ]
then wget --no-verbose https://dl.gitea.com/gitea/$giteaversion/gitea-$giteaversion-linux-amd64
mv gitea-$giteaversion-linux-amd64 /usr/local/bin/gitea ; chmod +x /usr/local/bin/gitea
else : ; fi
#
# Check if nginxsnippets exist if not download them
#
if [[ ! -d "$nginxfiles" ]]; then
git clone --quiet https://git.x-files.dk/ubuntu-web-server/nginx-install.git ; cp -R nginx-install/nginxsnippets /etc/nginx
fi
#
# Find the next Gitea user number and port
#
if [ -z "$giteauser" ]
then giteauser="gitea1" ; giteaport="3001"
else nextuser=$(($giteauser + 1)) ; giteaport=$(($giteaport + $nextuser)) ; giteauser="gitea$nextuser"
fi
#
# Create Gitea database
#
giteasql="
connect mysql
create database $giteauser$giteadb;
GRANT ALL PRIVILEGES ON $giteauser$giteadb.* TO '$giteauser'@'127.0.0.1' IDENTIFIED BY '$giteapwd';
FLUSH PRIVILEGES;
"
printf '%s\n' "${giteasql[@]}" |sed '1d; $d' > giteasql.sql
mysql < giteasql.sql
#
# Create a Gitea Nginx configuration file
#
curl --silent curl https://git.x-files.dk/ubuntu-web-server/nginx-install/raw/branch/main/cfg-apps/gitea.80.conf > /etc/nginx/conf.d/$hostname.conf
sed -s -i "s/DOMAIN/$hostname/g" /etc/nginx/conf.d/$hostname.conf
#
# Restarting Nginx for changes to take effect
systemctl restart nginx
#
# Create Gitea user
#
adduser --system --group --disabled-password --shell /bin/bash --home /home/$giteauser --gecos 'Git Version Control' $giteauser
#
# Create Gitea standard folders
#
mkdir -p /var/lib/$giteauser/{custom,data,indexers,public,log}
chown $giteauser:$giteauser /var/lib/$giteauser/{data,indexers,log}
chmod 750 /var/lib/$giteauser/{data,indexers,log}
mkdir /etc/$giteauser
chown root:$giteauser /etc/$giteauser
chmod 770 /etc/$giteauser
#
# Create Gitea systemd script
#
giteasystemd="
[Unit]
Description=Gitea (Git with a cup of tea)
After=syslog.target
After=network.target
Requires=mysql.service
[Service]
LimitMEMLOCK=infinity
LimitNOFILE=65535
RestartSec=2s
Type=simple
User=$giteauser
Group=$giteauser
WorkingDirectory=/var/lib/$giteauser/
ExecStart=/usr/local/bin/gitea web -c /etc/$giteauser/app.ini
Restart=always
Environment=USER=$giteauser HOME=/home/$giteauser GITEA_WORK_DIR=/var/lib/$giteauser
#CapabilityBoundingSet=CAP_NET_BIND_SERVICE
#AmbientCapabilities=CAP_NET_BIND_SERVICE
[Install]
WantedBy=multi-user.target
"
printf '%s\n' "${giteasystemd[@]}" |sed '1d; $d' > giteasystemd
cat giteasystemd > /etc/systemd/system/$giteauser.service
#
# Gitea customization uncomment if you need a personal touch to your gitea installation
#
mkdir -p /var/lib/$giteauser/custom/templates
mkdir -p /var/lib/$giteauser/custom/public/assets/img
#
# Start Gitea services
#
systemctl daemon-reload
systemctl enable $giteauser
systemctl start $giteauser
#
# Gitea postinstall notice
#
giteanotice="
RIGHT : Now Go to http://$hostname and modify the following settings:
IMPORTANT: The postinstall script will take care of port configuration of Nginx and Gitea.
DATABASE SETTINGS ---------------------------------------------------------------------------------------
DATABASE USERNAME = $giteauser
DATABASE PASSWORD = $giteapwd
DATABASE NAME = $giteauser$giteadb
GENERAL SETTINGS ----------------------------------------------------------------------------------------
SITE TITLE = YOUR SITE TITLE HERE
SERVER AND THIRD-PARTY SERVICE SETTINGS -----------------------------------------------------------------
HIDE EMAIL ADDRESSES BY DEFAULT = CHECKMARK
ADMINISTRATOR ACCOUNT SETTINGS --------------------------------------------------------------------------
ADMINISTRATOR USERNAME = ADMINUSERNAME
PASSWORD = ADMINPASSWORD
EMAIL ADDRESS = ADMINEMAIL
---------------------------------------------------------------------------------------------------------
IMPORTANT: Once done go back to \"gitea-install\" and run the following as root or using sudo.
./postinstall
NOTE: SSH will be disabled after running postinsall. You can modify this in /etc/$giteauser/app.ini
All other options in /etc/gitea/app.ini are optional and can be modified as you see fit.
---------------------------------------------------------------------------------------------------------
"
#
# Display post install note
#
clear ; printf '%s\n' "${giteanotice[@]}"
#
# Adding gitea user number to cfg for use with postinstall
#
echo $giteauser > giteainfo ; echo $giteaport >> giteainfo ; echo $hostname.conf >> giteainfo
#
# Cleaning up
#
rm -Rf giteasql.sql version giteasystemd nginx-install
#
# End of script
#