gitea-install/giteainstall

204 lines
6.5 KiB
Bash
Executable File

#!/bin/bash
###################################################################################################
# First Created: 12032021 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-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"; }
giteaversion=$(tail -1 version)
nginxfiles="/etc/nginx/nginxsnippets"
#
# Function title
#
title () { printf "\nGitea install script V1.0\n\n"; }
#
# Function usage
#
usage () { clear ; printf -- "\n"
printf -- "${bold}giteainstall${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 -- "./giteainstall -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
#
# Download and install Gitea
#
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
#
# Check if nginxsnippets exist if not download them
#
if [[ ! -d "$nginxfiles" ]]; then git clone --quiet https://git.x-files.dk/ubuntu-server-files/nginxsnippets.git $nginxfiles; fi
#
# Create Gitea database
#
giteasql="
connect mysql
create database gitea;
GRANT ALL PRIVILEGES ON gitea.* TO 'gitea'@'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 https://git.x-files.dk/ubuntu-server-files/nginx-config-examples/raw/branch/main/gitea.80.conf > /etc/nginx/conf.d/$hostname.conf
sed -s -i "s/DOMAIN/$hostname/g" /etc/nginx/conf.d/$hostname.conf
#
# Restarting Nginx and Phpfpm for changes to take effect
systemctl restart nginx && systemctl restart php8.1-fpm
#
# Create Gitea user
#
adduser --system --group --disabled-password --shell /bin/bash --home /home/git --gecos 'Git Version Control' git
#
# Create Gitea standard folders
#
mkdir -p /var/lib/gitea/{custom,data,indexers,public,log}
chown git:git /var/lib/gitea/{data,indexers,log}
chmod 750 /var/lib/gitea/{data,indexers,log}
mkdir /etc/gitea
chown root:git /etc/gitea
chmod 770 /etc/gitea
#
# 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=git
Group=git
WorkingDirectory=/var/lib/gitea/
ExecStart=/usr/local/bin/gitea web -c /etc/gitea/app.ini
Restart=always
Environment=USER=git HOME=/home/git GITEA_WORK_DIR=/var/lib/gitea
#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/gitea.service
#
# Create Gitea customization directories
#
mkdir -p /var/lib/gitea/custom/templates
mkdir -p /var/lib/gitea/custom/public/assets/img
#
# Start Gitea services
#
systemctl daemon-reload
systemctl enable gitea
systemctl start gitea
#
# Gitea postinstall notice
#
giteanotice="
---------------------------------------------------------------------------------------------------
IMPORTANT: Go to http://$hostname and modify the following settings:
DATABASE SETTINGS ---------------------------------------------------------------------------------
DATABASE PASSWORD = $giteapwd
GENERAL SETTINGS ----------------------------------------------------------------------------------
SITE TITLE = YOUR SITE TITLE HERE
SERVER AND THIRD-PARTY SERVICE SETTINGS -----------------------------------------------------------
ALLOW CREATION OF ORGANIZATIONS BY DEFAULT = CHECKMARK
HIDE EMAIL ADDRESSES BY DEFAULT = CHECKMARK
ADMINISTRATOR ACCOUNT SETTINGS --------------------------------------------------------------------
ADMINISTRATOR USERNAME = ADMINUSERNAME
PASSWORD = ADMINPASSWORD
EMAIL ADDRESS = ADMINEMAIL
---------------------------------------------------------------------------------------------------
IMPORTANT : SSH will be disabled while runnung postinsall. You can modify this in /etc/gitea/app.ini
NOTE : All other options are optional and can be set as you see fit.
Go back to \"gitea-multi-install\" and run the following:
./postinstall
---------------------------------------------------------------------------------------------------
"
#
# Display post install note
#
clear ; printf '%s\n' "${giteanotice[@]}"
#
# Cleaning up
#
rm -Rf giteasql.sql version giteasystemd nginx-config-examples
#
# End of script
#