2023-03-24 17:42:52 -04:00
|
|
|
#!/bin/bash
|
2023-04-01 05:08:06 -04:00
|
|
|
# SPDX-License-Identifier: MIT
|
2023-03-24 17:42:52 -04:00
|
|
|
|
2023-04-01 08:51:42 -04:00
|
|
|
set -e
|
|
|
|
|
2023-09-24 10:54:52 -04:00
|
|
|
DIR=/tmp
|
2023-04-01 08:51:42 -04:00
|
|
|
if ${VERBOSE:-false}; then set -x; fi
|
|
|
|
: ${CONTAINER:=forgejo}
|
2023-09-24 14:32:40 -04:00
|
|
|
: ${RETRY_DELAYS:=1 1 5 5 15 30}
|
2023-03-24 17:42:52 -04:00
|
|
|
|
2023-09-24 10:54:52 -04:00
|
|
|
function retry() {
|
|
|
|
rm -f $DIR/retry.out
|
2023-03-24 17:42:52 -04:00
|
|
|
success=false
|
2023-09-24 14:32:40 -04:00
|
|
|
for delay in $RETRY_DELAYS ; do
|
2023-09-24 10:54:52 -04:00
|
|
|
if "$@" >> $DIR/retry.out 2>&1 ; then
|
|
|
|
success=true
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
cat $DIR/retry.out
|
|
|
|
echo waiting $delay
|
|
|
|
sleep $delay
|
2023-03-24 17:42:52 -04:00
|
|
|
done
|
|
|
|
if test $success = false ; then
|
2023-09-24 10:54:52 -04:00
|
|
|
cat $DIR/retry.out
|
|
|
|
return 1
|
2023-03-24 17:42:52 -04:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-03-25 09:34:41 -04:00
|
|
|
function run() {
|
|
|
|
local image="$1"
|
2023-04-05 11:40:06 -04:00
|
|
|
local version="$2"
|
|
|
|
|
2023-04-05 17:27:33 -04:00
|
|
|
if test "$version" != "1.19" && dpkg --compare-versions "$version" "lt" "1.19.0-3"; then
|
2023-04-05 11:40:06 -04:00
|
|
|
actions_unit="actions.actions"
|
|
|
|
else
|
|
|
|
actions_unit="repo.actions"
|
|
|
|
fi
|
2023-03-24 17:42:52 -04:00
|
|
|
|
2023-04-01 10:04:26 -04:00
|
|
|
docker run --restart unless-stopped --name ${CONTAINER} \
|
2023-03-24 17:42:52 -04:00
|
|
|
-e "RUN_MODE=dev" \
|
|
|
|
-e "FORGEJO__security__INSTALL_LOCK=true" \
|
|
|
|
-e "FORGEJO__log__LEVEL=debug" \
|
|
|
|
-e "FORGEJO__actions__ENABLED=true" \
|
|
|
|
-e "FORGEJO__repository__ENABLE_PUSH_CREATE_USER=true" \
|
2023-03-25 09:34:41 -04:00
|
|
|
-e "FORGEJO__repository__DEFAULT_PUSH_CREATE_PRIVATE=false" \
|
2023-04-05 11:40:06 -04:00
|
|
|
-e "FORGEJO__repository__DEFAULT_REPO_UNITS=repo.code,repo.releases,repo.issues,repo.pulls,repo.wiki,repo.projects,repo.packages,$actions_unit" \
|
|
|
|
-d $image:$version
|
2023-04-01 08:51:42 -04:00
|
|
|
local ip="$(docker inspect -f "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" ${CONTAINER})"
|
2023-03-25 09:34:41 -04:00
|
|
|
echo $ip > forgejo-ip
|
2023-04-01 08:51:42 -04:00
|
|
|
docker exec ${CONTAINER} sed -i -e "s|localhost|$ip|" /data/gitea/conf/app.ini
|
|
|
|
docker restart ${CONTAINER}
|
2023-03-25 09:34:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function setup() {
|
|
|
|
local user="${1:-root}"
|
|
|
|
local password="${2:-admin1234}"
|
2023-04-05 11:40:06 -04:00
|
|
|
local image="${3:-codeberg.org/forgejo/forgejo}"
|
2023-07-27 14:45:06 -04:00
|
|
|
local version="${4:-1.20}"
|
2023-03-25 09:34:41 -04:00
|
|
|
|
2023-04-05 11:40:06 -04:00
|
|
|
run $image $version
|
2023-03-24 17:42:52 -04:00
|
|
|
|
|
|
|
sleep 5 # for some reason trying to run "forgejo admin" while forgejo is booting will permanently break everything
|
2023-04-01 08:51:42 -04:00
|
|
|
if docker exec --user 1000 ${CONTAINER} forgejo admin user list --admin | grep "$user" ; then
|
|
|
|
docker exec --user 1000 ${CONTAINER} forgejo admin user change-password --username "$user" --password "$password"
|
2023-03-24 17:42:52 -04:00
|
|
|
else
|
2023-09-24 10:54:52 -04:00
|
|
|
retry docker exec --user 1000 ${CONTAINER} forgejo admin user create --admin --username "$user" --password "$password" --email "$user@example.com"
|
2023-03-24 17:42:52 -04:00
|
|
|
fi
|
2023-09-24 10:54:52 -04:00
|
|
|
#
|
|
|
|
# The 'sudo' scope was removed in Forgejo v1.20 and is ignored
|
|
|
|
#
|
2023-04-01 08:51:42 -04:00
|
|
|
docker exec --user 1000 ${CONTAINER} forgejo admin user generate-access-token -u $user --raw --scopes 'all,sudo' > forgejo-token
|
2023-09-24 10:54:52 -04:00
|
|
|
retry forgejo-curl.sh --user "$user" --password "$password" --token @forgejo-token login http://$(cat forgejo-ip):3000
|
|
|
|
#
|
|
|
|
# Redundant with forgejo-curl.sh, kept around for backward compatibility 09/2023
|
|
|
|
#
|
2023-03-24 17:42:52 -04:00
|
|
|
( echo -n 'Authorization: token ' ; cat forgejo-token ) > forgejo-header
|
2023-03-25 09:34:41 -04:00
|
|
|
( echo "#!/bin/sh" ; echo 'curl -sS -H "Content-Type: application/json" -H @'$(pwd)/forgejo-header' "$@"' ) > forgejo-api && chmod +x forgejo-api
|
2023-03-24 17:42:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function teardown() {
|
2023-04-01 08:51:42 -04:00
|
|
|
docker stop ${CONTAINER} >& /dev/null || true
|
|
|
|
docker rm -f ${CONTAINER} >& /dev/null || true
|
2023-03-24 17:42:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
"$@"
|