#!/bin/bash [[ ! $GITEA_DIR ]] && export GITEA_DIR="$(dirname "$(cd "$(dirname "$BASH_SOURCE")"; pwd -P)")" export GITEA_GITHUB_REPO=go-gitea/gitea # finds current installed version function version () { # https://askubuntu.com/a/952479/478337 [[ $1 ]] && export GITEA_BIN=$1 [[ ! $GITEA_BIN ]] && return 3 && echo no bin file path passed [[ ! -f $GITEA_BIN ]] && echo "" && return 2 CUR_VER=$($GITEA_BIN -version | sed -nr 's:.*version ([^ ]+).*:\1:p') [ ! $CUR_VER ] && return 1 echo $CUR_VER } # sets system operating system that is supported function get_OS () { case "$OSTYPE" in # uncomment valid OS darwin*) OS=darwin ;; linux*) OS=linux ;; # msys*) OS=windows ;; *) ;; esac [ ! $OS ] && return 1 echo $OS } # gets system architecture supported function get_arch () { declare -A ARCHES #ARCHES=( ["arm64"]="arm64" ["aarch64"]="arm64" ["x86_64"]="amd64" ["armv61"]="armv6" ["armv71"]="armv7" ["arm32"]="armv7" ["armhf"]="armv7" ) # set valid architectures ARCHES=( ["arm64"]="arm64" ["aarch64"]="arm64" ["x86_64"]="amd64" ) ARCH=${ARCHES[$(uname -m)]} [ ! $ARCH ] && return 1 echo $ARCH } # get list of installed gitea themes in /frontend/$1/public/css function get_theme_list () { local themes="" for theme in $1/theme*.css; do themes+=$(basename $theme .css | cut -c 7-), done themes+=gitea,arc_green echo $themes } # sets the environment variables for gitea managment based on instance name function set_env () { unset GITEA_INSTANCE unset GITEA_BIN unset GITEA_DATA_DIR local Env_file local Dir declare OPTARG declare OPTION declare OPTIND=0 [[ ! $GITEA_DIR ]] && export GITEA_DIR="$(dirname "$(cd "$(readlink -f "$BASH_SOURCE")"; pwd -P)")" [[ ! $GITEA_DIR ]] && echo main directory not set, aborting && return 2 while getopts 'tiud:' OPTION; do case "$OPTION" in d) Dir=$OPTARG ;; *) ;; esac done shift $(( $OPTIND - 1 )) [[ $1 ]] && export GITEA_INSTANCE=$1 [[ ! $GITEA_INSTANCE ]] && echo warning no gitea instance set using \'default\' && export GITEA_INSTANCE=default Env_file=$GITEA_DIR/env/$GITEA_INSTANCE.env [[ -f $Env_file ]] && source $Env_file || echo info: no environment found $Env_file # env | grep GITEA # echo --- [[ ! $GITEA_BIN ]] && GITEA_BIN="$GITEA_DIR/gitea.bin" [[ $GITEA_BIN == */ ]] && GITEA_BIN="${GITEA_BIN}gitea.bin" [[ ! $GITEA_BIN == /* ]] && GITEA_BIN="$GITEA_DIR/$GITEA_BIN" export GITEA_BIN # option overrides environment file GITEA_DATA_DIR=${Dir:-$GITEA_DATA_DIR} GITEA_DATA_DIR=${GITEA_DATA_DIR:-$GITEA_DIR/data} [[ ! $GITEA_DATA_DIR == /* ]] && GITEA_DATA_DIR="$GITEA_DIR/$GITEA_DATA_DIR" export GITEA_DATA_DIR } # generates .ini from corresponding .tmpl file function ini_gen () { [[ ! $GITEA_DIR ]] && export GITEA_DIR="$(dirname "$(cd "$(readlink -f "$BASH_SOURCE")"; pwd -P)")" local Ini local Lock=true declare OPTARG declare OPTION declare OPTIND=0 while getopts 'itud:' OPTION; do case "$OPTION" in i) Lock=false ;; *) ;; esac done shift $(( $OPTIND - 1 )) GITEA_INSTANCE=$1 [[ ! $GITEA_INSTANCE ]] && set_env "$@" echo $Lock echo ini_gen env | grep GITEA Ini=$GITEA_DIR/config/$GITEA_INSTANCE if [[ -f "$Ini.tmpl" ]]; then _Header=";################################################################ ;# THIS FILE IS GENERATED. Do not edit directly # ;# It was created from $Ini.tmpl # ;# Edit this file and run with -t or run ini-gen to regenerate # ;################################################################" # https://stackoverflow.com/a/22819516/4695378 # search and replace data entries in template file echo -e "$_Header" > $Ini.ini cat $Ini.tmpl | \ sed 's:@@_server_dir:'$GITEA_DATA_DIR':'g | \ sed 's:\(INSTALL_LOCK *= \)\(.*\):\1'${Lock}':' | \ sed 's:\(THEMES *= \)\(.*\):\1'"$(get_theme_list $GITEA_DIR/frontend/current/public/css)"':' \ >> $Ini.ini echo info: $Ini.ini file was generated from $Ini.tmpl else echo warning: no file "$Ini.tmpl", no ini file was generated fi } # downloads gitea binary based on OS and Architecture and whether it needs updating # if you have run into github api anonymous access limits which happens during debugging/dev # then run with user and token environment variables or source separate file function download_bin () { # dependencies, jq [[ $(command -v jq &> /dev/null) ]] && echo jq is required, install via \'sudo apt install jq\' && exit echo passed stuff: $@ [[ ! $GITEA_DIR ]] && export GITEA_DIR="$(dirname "$(cd "$(dirname "$BASH_SOURCE")"; pwd -P)")" [[ ! $GITEA_BIN ]] && set_env "$@" [[ ! $GITEA_BIN ]] && echo a Gitea binary path not set, aborting && exit 1 local TokenFile declare OPTARG declare OPTION declare OPTIND=0 echo passed stuff: $@ while getopts 'fg:itud:' OPTION; do case "$OPTION" in g) TokenFile=$OPTARG ;; f) Force=true ;; *) ;; esac done shift $(( $OPTIND - 1 )) echo "#### Gitea Binary Upgrade or Install ###" [[ -f $GITEA_BIN ]] && echo Checking for upgrade of $GITEA_BIN || echo no current gitea binary at $GITEA_BIN OS=$(get_OS) ARCH=$(get_arch) [[ ! $OS ]] && echo non supported OS $OS && exit [[ ! $ARCH ]] && echo non supported architecture $ARCH && exit echo Gitea Binary for: Operating System $OS, Architecture $ARCH RECORD=$(curl -u $GITHUB_USER:$GITHUB_TOKEN -s https://api.github.com/repos/$GITEA_GITHUB_REPO/releases/latest) NEW_VER=$(echo $RECORD | jq -r '.tag_name') [ "$NEW_VER" ] && echo $NEW_VER is latest version available from https://github.com/$GITEA_GITHUB_REPO/releases # echo Checking current installed Gitea binary version VER=$(version) if [[ "$NEW_VER" == "v$VER" ]]; then echo "Gitea binary is up to date" if [[ $Force ]]; then echo Forcing option set. Downloading/Overwriting existing binary with same version else echo "Nothing to upgrade, exiting" return 0 fi else [[ $VER ]] && echo updating v$VER to $NEW_VER || echo no current installed gitea binary, installing $NEW_VER fi URL=$(echo $RECORD | \ jq -r \ --arg os $OS \ --arg arch $ARCH \ --arg archnot "$ARCH." \ '.assets[] | select( .name | contains($os)) | select (.name | contains($arch)) | select (.name | contains($archnot) | not ) .browser_download_url' \ ) [[ -f ${TokenFile:-$HOME/.github} ]] && source ${TokenFile:-$HOME/.github} if [[ $GITHUB_TOKEN ]]; then echo "using access github token with script >>> $GITHUB_USER:$GITHUB_TOKEN" fi if [ $URL ]; then echo "Downloading Gitea Binary at $URL" wget --user=-u $GITHUB_USER --password=$GITHUB_TOKEN -q --show-progress -O $GITEA_BIN $URL if ( [[ $? -eq 0 ]] && [[ "$NEW_VER" == *"$(version)"* ]] ); then chmod +x $GITEA_BIN echo upgrade succeeded, $GITEA_BIN version is $(version) else echo upgrade from $URL failed fi else echo unabled to determine download url fi }