From 9a20a2d3741d78ab8d086b6b700ba850433cdd0f Mon Sep 17 00:00:00 2001 From: Martin Winkler Date: Wed, 9 Feb 2022 16:01:42 +0100 Subject: [PATCH] Gitea - Now configurable with default pathes in one place for better backup/migration --- seqs/gitea.cfg.example | 57 +++++++++++++++++ seqs/gitea.sh | 142 ++++++++++++++++++++++++++--------------- 2 files changed, 149 insertions(+), 50 deletions(-) create mode 100644 seqs/gitea.cfg.example diff --git a/seqs/gitea.cfg.example b/seqs/gitea.cfg.example new file mode 100644 index 0000000..c80fb47 --- /dev/null +++ b/seqs/gitea.cfg.example @@ -0,0 +1,57 @@ +#!/bin/bash + +SEQ_GITEA_USER="git" +SEQ_GITEA_BIN_LOC="/usr/local/bin/gitea" +SEQ_GITEA_BASE_DIR="/var/lib/gitea" +SEQ_GITEA_WORK_DIR="$SEQ_GITEA_BASE_DIR/work" +SEQ_GITEA_HOME_DIR="$SEQ_GITEA_BASE_DIR/home" +SEQ_GITEA_CONF_DIR="$SEQ_GITEA_BASE_DIR/config" +SEQ_GITEA_BACKUP_DIR="$SEQ_GITEA_BASE_DIR/backup" + +# Service Derived from +# https://raw.githubusercontent.com/go-gitea/gitea/master/contrib/systemd/gitea.service +SEQ_GITEA_SERVICE="[Unit] +Description=Gitea (Git with a cup of tea) +After=syslog.target +After=network.target + +Requires=mysql.service + +[Service] +# Modify these two values and uncomment them if you have +# repos with lots of files and get an HTTP error 500 because +# of that +### +#LimitMEMLOCK=infinity +#LimitNOFILE=65535 + +RestartSec=2s +Type=simple +User=$SEQ_GITEA_USER +Group=$SEQ_GITEA_USER +WorkingDirectory=$SEQ_GITEA_WORK_DIR + +# If using Unix socket: tells systemd to create the /run/gitea folder, which will contain the gitea.sock f +# (manually creating /run/gitea doesn't work, because it would not persist across reboots) +#RuntimeDirectory=gitea +ExecStart=/usr/local/bin/gitea web --config \"$SEQ_GITEA_CONF_DIR/app.ini\" + +Restart=always +Environment=USER=\"$SEQ_GITEA_USER\" HOME=\"$SEQ_GITEA_HOME_DIR\" GITEA_WORK_DIR=\"$SEQ_GITEA_WORK_DIR\" + +# If you install Git to directory prefix other than default PATH (which happens +# for example if you install other versions of Git side-to-side with +# distribution version), uncomment below line and add that prefix to PATH +# Don't forget to place git-lfs binary on the PATH below if you want to enable +# Git LFS support +#Environment=PATH=/path/to/git/bin:/bin:/sbin:/usr/bin:/usr/sbin + +# If you want to bind Gitea to a port below 1024, uncomment +# the two values below, or use socket activation to pass Gitea its ports as above +### +#CapabilityBoundingSet=CAP_NET_BIND_SERVICE +#AmbientCapabilities=CAP_NET_BIND_SERVICE +### + +[Install] +WantedBy=multi-user.target" diff --git a/seqs/gitea.sh b/seqs/gitea.sh index 008e2d0..ea69fea 100755 --- a/seqs/gitea.sh +++ b/seqs/gitea.sh @@ -4,20 +4,68 @@ ## Installation of self hosted git service Gitea toolName="gitea" +# Get script working directory +# (when called from a different directory) +WDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >>/dev/null 2>&1 && pwd)" +APTOPT= +CONFIG=0 +SCRIPT_FILE=$(basename -- $0) +SCRIPT_NAME=${SCRIPT_FILE%%.*} +CONFIG_FILE_NAME="${SCRIPT_NAME}.cfg" +CONFIG_FILE_TEMPLATE="$WDIR/${CONFIG_FILE_NAME}.example" + giteaLatestUrl="https://api.github.com/repos/go-gitea/gitea/releases/latest" giteaVersion=$(curl --silent "$giteaLatestUrl" | grep -Po '"tag_name": "v\K.*?(?=")') -giteaDownload="https://dl.gitea.io/gitea/${giteaVersion}/gitea-${giteaVersion}-linux-arm-6" +giteaArch= +giteaDownloadEval='https://dl.gitea.io/gitea/${giteaVersion}/gitea-${giteaVersion}-linux-${giteaArch}' +giteaDownload=$(eval echo $giteaDownloadEval) giteaDir="/usr/local/bin" -giteaLoc="${giteaDir}/gitea" -giteaService="https://raw.githubusercontent.com/go-gitea/gitea/master/contrib/systemd/gitea.service" giteaServiceLoc="/etc/systemd/system/gitea.service" -giteaConfigDir="/etc/gitea" -giteaIniLoc="${giteaConfigDir}/app.ini" +giteaLogDir="/var/log/gitea" +giteaIniLoc= +giteaDownFile="/tmp/giteaDown" +giteaUser="git" +versionNow= step_config() { + initSeqConfig "$CONFIG_FILE_NAME" "$CONFIG_FILE_TEMPLATE" + if [ $? -eq 0 ] ; then + CONFIG=1 + else + # End if no configuration file exists + [ $DRY -eq 0 ] && return -1 + fi + + ## Apt cmdline option to suppress user interaction + [ $QUIET -ne 0 ] && APTOPT="-y" + if [ -z $giteaVersion ] ; then echoerr " [E] Couldn't determine latest version of $toolName" fi + [ ! -z "$(command -v gitea)" ] && versionNow=$(gitea --version | sed 's/.*version \([0-9.]\+\).*/\1/') + + checkArchitecture + giteaIniLoc="${SEQ_GITEA_CONF_DIR}/app.ini" + + echoseq " [I] Gitea work: $SEQ_GITEA_WORK_DIR" + echoseq " Gitea config: $SEQ_GITEA_CONF_DIR" + echoseq " Git user home: $SEQ_GITEA_HOME_DIR" + echoseq + outColor yellow + echoseq " [W] Don't forget to adapt $SEQ_GITEA_CONF_DIR/app.ini" + outColor + + ## Return of non zero value will abort the sequence + return 0 +} + +checkArchitecture() { + [ ! -z "$(command -v dpkg)" ] && giteaArch=$(dpkg --print-architecture) + case $giteaArch in + armhf) + giteaArch="arm-6";; + esac + giteaDownload=$(eval echo $giteaDownloadEval) } step_1_info() { echo "Updating apt"; } @@ -26,17 +74,18 @@ step_1() { exe apt update } -step_2_info() { +step_2_info() { + [ -z "$giteaArch" ] && checkArchitecture echo "Downloading $toolName to user home from:" echoinfo "$giteaDownload" } step_2() { - exe wget -O ~/gitea $giteaDownload + exe wget -O "$giteaDownFile" $giteaDownload saveReturn $? endReturn } -step_3_info() { echo "Adding user for $toolName (git:git)"; } +step_3_info() { echo "Adding user for $toolName ($giteaUser)"; } step_3() { exe adduser \ --system \ @@ -44,48 +93,39 @@ step_3() { --gecos 'Git Version Control' \ --group \ --disabled-password \ - --home /home/git \ - git + --home "$SEQ_GITEA_HOME_DIR" \ + "$SEQ_GITEA_USER" saveReturn $? endReturn } step_4_info() { echo "Create required directory structure"; } step_4() { - exe mkdir -p /var/lib/gitea/{custom,data,log} - exe chown -R git: /var/lib/gitea/ - exe chmod -R 750 /var/lib/gitea/ - exe mkdir /etc/gitea - exe chown root:git /etc/gitea - exe chmod 770 /etc/gitea - echo "Creating /var/log/gitea" - exe mkdir -p /var/log/gitea - exe chown root:git /var/log/gitea - exe chmod 770 /var/log/gitea - echo -n "Copying gitea to global location and making it executable..." - exe chmod +x ~/gitea - exe cp -ar ~/gitea "$giteaLoc" && echo "ok" - saveReturn $? - endReturn + exe install -o $SEQ_GITEA_USER -g $SEQ_GITEA_USER -m 750 \ + -d "$SEQ_GITEA_BACKUP_DIR" "$SEQ_GITEA_CONF_DIR" \ + "$SEQ_GITEA_HOME_DIR" "$SEQ_GITEA_WORK_DIR"/{custom,data,log} + exe chown root:$SEQ_GITEA_USER "$SEQ_GITEA_CONF_DIR" + exe chmod 770 $SEQ_GITEA_CONF_DIR + echoseq "Creating $giteaLogDir" + exe install -g $SEQ_GITEA_USER -m 770 -d "$giteaLogDir" + echoseq -n "Copying gitea to global location and making it executable..." + exe install -b -m 755 -T "$giteaDownFile" "$SEQ_GITEA_BIN_LOC" && echo "ok" + endReturn "Failed to install $SEQ_GITEA_BIN_LOC" } step_5_info() { echo "Creating systemd service"; } step_5() { - exe wget -O "$giteaServiceLoc" "$giteaService" - echo -en "Uncomment needed services mysql (enter to continue): " - exe read - exe vi $giteaServiceLoc + addConf -c "$SEQ_GITEA_SERVICE" "$giteaServiceLoc" } step_6_info() { echo "Starting $toolName service"; } step_6() { - exe systemctl enable gitea - exe systemctl start gitea - echo "Before proceeding to installation you may need to create a database first with step 10" - echo - echo "Goto http://[yourip]:3000/install and complete installation" - echo - echo "Afterwards please execute step 20 to secure configuration" + exe systemctl enable gitea --now + echoseq "Before proceeding to installation you may need to create a database first with step 10" + echoseq + echoseq "Goto http://[yourip]:3000/install and complete installation" + echoseq + echoseq "Afterwards please execute step 20 to secure configuration" } step_7_info() { echo "Show configuration notes"; } @@ -122,7 +162,7 @@ step_10() { } step_12_info() { - if [ ! -z $versionNow ] ; then + if [ ! -z "$versionNow" ] ; then if [ "$giteaVersion" == "$versionNow" ] ; then echo "No upgrade available. Already on latest: $versionNow" else @@ -130,37 +170,39 @@ step_12_info() { echoinfo " - installed version: $versionNow -" fi else - echo "Download new version $giteaVersion to /usr/local/bin" + echo "Upgrade existing $toolName installation" fi } step_12_alias() { ALIAS="upgrade"; } step_12() { - exe wget -O "$giteaDownLoc" $giteaDownload + endCheckEmpty versionNow "Please install $toolName first" + exe wget -O "$giteaDownFile" $giteaDownload endReturn -o $? "Download failed" - if [ -f "$giteaLoc" ] ; then - local toolBackup="${giteaDir}/gitea_${versionNow}" + if [ -f "$SEQ_GITEA_BIN_LOC" ] ; then + local toolBackup="${SEQ_GITEA_BACKUP_DIR}/gitea_${versionNow}" exe service gitea stop saveReturn $? endReturn echoseq -n "Backing up existing executable to ${toolBackup}..." - exe cp -ar "$giteaLoc" "$toolBackup" && echoseq "ok" || echoseq "nok" + exe cp -ar "$SEQ_GITEA_BIN_LOC" "$toolBackup" && echoseq "ok" || echoseq "nok" fi - exe mv "$giteaDownLoc" "$giteaLoc" - exe chmod +x "$giteaLoc" + exe install -m 755 -T "$giteaDownFile" "$SEQ_GITEA_BIN_LOC" + endReturn -o $? "Upgrade failed" exe service gitea start } -versionNow=$(gitea --version | sed 's/.*version \([0-9.]\+\).*/\1/') -giteaDownLoc="/tmp/giteaDown" step_20_info() { echo "Secure settings after installation"; } step_20() { - exe chmod 750 "$giteaConfigDir" + exe chmod 750 "$SEQ_GITEA_CONF_DIR" exe chmod 644 "$giteaIniLoc" } -# Sequence Revision -VERSION_SEQREV=12 +step_22_info() { echo "Open $toolName config file"; } +step_22_alias() { ALIAS="config"; } +step_22() { + exe vi "$giteaIniLoc" +} -# Path to sequencer +VERSION_SEQREV=14 . /usr/local/bin/sequencer.sh