setup-forgejo/forgejo-binary.sh
Earl Warren 82ae462c78
do not display confusing error message if logs do not exist
it is expected but the user does not know that and will incorrectly
think it is an error
2024-02-08 10:55:50 +01:00

124 lines
3.4 KiB
Bash
Executable file

#!/bin/bash
# SPDX-License-Identifier: MIT
set -e
SELF_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source $SELF_DIR/forgejo-lib.sh
: ${DIR:=/tmp/forgejo-binary}
if ${VERBOSE:-false}; then set -x; fi
function maybe_sudo() {
if test $(id -u) != 0 ; then
SUDO=sudo
fi
}
function ensure_user() {
local user=$1
dependencies
adduser --quiet --gecos $user --disabled-password $user
adduser $user sudo
adduser $user docker
sed -i -e 's/^%sudo.*/%sudo ALL=(ALL:ALL) NOPASSWD:ALL/' /etc/sudoers
}
function dependencies() {
maybe_sudo
if ! which sudo curl daemon jq git-lfs ip sqlite3 envsubst unzip > /dev/null ; then
$SUDO apt-get update -qq
$SUDO apt-get -q install -y -qq sudo curl daemon jq git-lfs iproute2 sqlite3 gettext-base unzip
fi
}
function cleanup_logs() {
local work_path=$DIR/forgejo-work-path
rm -f $DIR/*.log
rm -f $work_path/log/*.log
}
function download() {
local url="$1"
if ! test -f $DIR/forgejo ; then
curl -sS $url > $DIR/forgejo.tmp
if file $DIR/forgejo.tmp | grep --quiet 'Zip archive' ; then
unzip $DIR/forgejo.tmp -d $DIR
rm -f $DIR/forgejo.tmp
else
mv $DIR/forgejo.tmp $DIR/forgejo
fi
chmod +x $DIR/forgejo
fi
echo $DIR/forgejo --version
$DIR/forgejo --version
}
function create_user() {
local user="$1"
local password="$2"
local work_path=$DIR/forgejo-work-path
if test -f $work_path/forgejo-token; then
return
fi
$DIR/forgejocli admin user create --admin --username "$user" --password "$password" --email "$user@example.com"
$DIR/forgejocli admin user generate-access-token -u "$user" --raw --scopes all > $DIR/forgejo-token
local url="http://$(cat $DIR/forgejo-ip):3000"
echo "$url" > $DIR/forgejo-url
echo "http://${user}:${password}@$(cat $DIR/forgejo-ip):3000" > $DIR/forgejo-auth-url
retry forgejo-curl.sh --user "$user" --password "$password" --token @$DIR/forgejo-token login "$url"
echo forgejo-curl.sh api_json $url/api/forgejo/v1/version
forgejo-curl.sh api_json $url/api/forgejo/v1/version
}
function myip() {
hostname -I | cut -f1 -d' '
}
function start_forgejo() {
local work_path=$DIR/forgejo-work-path
daemon --chdir=$DIR --unsafe --env="TERM=$TERM" --env="HOME=$HOME" --env="PATH=$PATH" --pidfile=$DIR/forgejo-pid --errlog=$DIR/forgejo-err.log --output=$DIR/forgejo-out.log -- $DIR/forgejo --config $work_path/app.ini --work-path $work_path
if ! retry grep --no-messages --quiet 'Starting server on' $work_path/log/forgejo.log ; then
cat $DIR/*.log
cat $work_path/log/*.log
return 1
fi
echo "$DIR/forgejo --config $work_path/app.ini --work-path $work_path" '"$@"' > $DIR/forgejocli
chmod +x $DIR/forgejocli
myip > $DIR/forgejo-ip
}
function reset_forgejo() {
local config=$1
local work_path=$DIR/forgejo-work-path
rm -fr $work_path
mkdir -p $work_path
IP=$(myip) WORK_PATH=$work_path envsubst < $SELF_DIR/$config-app.ini > $work_path/app.ini
}
function teardown() {
stop_daemon forgejo $DIR
cleanup_logs
}
function setup() {
local user="${1:-root}"
local password="${2:-admin1234}"
local url="${3:-https://codeberg.org/forgejo/forgejo/releases/download/v1.21.2-1/forgejo-1.21.2-1-linux-amd64}"
dependencies
download "$url"
reset_forgejo forgejo-binary
start_forgejo
create_user "$user" "$password"
}
mkdir -p $DIR
"$@"