install/setup.sh
2019-06-25 23:03:46 +03:00

140 lines
4.6 KiB
Bash
Executable File

#!/usr/bin/env bash
set -e
curp="$(cd "$(dirname "$0")" && pwd)"
# prefer gitea executable in project folder and fallback to $PATH
[[ -x "$curp/gitea" ]] && giteae="$curp/gitea" \
|| giteae=$(command -v gitea 2>/dev/null) \
|| { echo "gitea executable not found!" 1>&2;exit 1; }
set_in_file() {
# ensure there is a line with $1=$2 in file $3
grep -q "^$1=" "$3" && sed "s/^$1=.*/$1=$2/" -i "$3" \
|| echo "$1=$2" >> "$3"
}
gen_app_ini() {
source "$curp/.defaults.sh"
for x in $(find "$curp/.defaults" -type f -name "*.sh"); do
source "$x"
done
truncate -s 0 "$curp/custom/conf/app.ini"
# first build the template
for x in $( ( set -o posix ; set ) |grep -o '^A_DEFAULT_SETTINGS.*=' | cut -d '=' -f1); do
echo -e "${!x}\n" >> "$curp/custom/conf/app.ini"
done
# then replace variables in the template
for x in $(cat "$curp/custom/conf/app.ini" | grep -o '{{%.*%}}' | sed -e 's/^{{%//' -e 's/%}}//' | sort | uniq); do
sed -i -e "s#{{%$x%}}#$(echo -n "${!x}"|sed -z 's/\n/\\n/g')#" "$curp/custom/conf/app.ini"
done
}
create_admin() {
if [ -z "${ADMIN_PASSWORD}" ]; then
ADMIN_PASSWORD=$(head /dev/urandom | tr -dc 'A-Za-z0-9_' | head -c 32)
echo "pass: ${ADMIN_PASSWORD}"
set_in_file "ADMIN_PASSWORD" "${ADMIN_PASSWORD}" "$curp/.custom_defaults.sh"
fi
set +e
output=$("$giteae" admin create-user \
--username "${ADMIN_USER}" \
--email "${ADMIN_EMAIL}" \
--password "${ADMIN_PASSWORD}" \
--admin \
--access-token)
ecode="$?"
set -e
echo "$output" 1>&2
if [ "$ecode" -ne 0 ]; then
echo "command exited with status code: $ecode"
return "$ecode"
else
ADMIN_TOKEN=$(echo -n "$output" | grep 'Access token was successfully created' | rev | cut -d ' ' -f1 | rev)
echo "token: ${ADMIN_TOKEN}"
set_in_file "ADMIN_TOKEN" "${ADMIN_TOKEN}" "$curp/.custom_defaults.sh"
fi
}
create_org() {
curl --silent -X POST "${apiurl}/orgs" \
-H "Authorization: token ${ADMIN_TOKEN}" \
-H "accept: application/json" \
-H "Content-Type: application/json" \
-d "{ \"username\": \"$1\", \"visibility\": \"limited\"}" 1>&2
}
create_repo() {
curl --silent -X POST "${apiurl}$2" \
-H "Authorization: token ${ADMIN_TOKEN}" \
-H "accept: application/json" \
-H "Content-Type: application/json" \
-d "{\"auto_init\": false, \"private\": true, \"name\": \"$1\"}" 1>&2
}
# load local file
touch "$curp/.custom_defaults.sh" && chmod 0600 "$curp/.custom_defaults.sh"
source "$curp/.custom_defaults.sh"
if [ -z "${ADMIN_EMAIL}" ]; then
while [ -z "${ADMIN_EMAIL}" ]; do
read -p "ADMIN_EMAIL=" ADMIN_EMAIL
done
set_in_file "ADMIN_EMAIL" "${ADMIN_EMAIL}" "$curp/.custom_defaults.sh"
fi
mkdir -p "$curp/custom/conf/" "$curp/log"
[ -z "$ADMIN_USER" ] && ADMIN_USER="$USER"
# generate app.ini
gen_app_ini
# migrate database
"$giteae" migrate 1>>"$curp/log/db_init.log" 2>>"$curp/log/db_init.log"
# create admin user
[ -z "${ADMIN_TOKEN}" ] && admininit=$(create_admin 2>>"$curp/log/admin_init.log")
SCREEN_SES_NAME=${SCREEN_SES_NAME:-gitea-dev}
# run gitea in screen
if ! screen -S ${SCREEN_SES_NAME} -Q "select" . > /dev/null 2>&1; then
screen -S "${SCREEN_SES_NAME}" -d -m -- sh -c "\"$giteae\" web 2>&1 | tee \"$curp/log/web.log\""
SLEEP_SECS=10
echo "Sleeping for ${SLEEP_SECS} seconds for web service to start!" 1>&2
for x in $(seq ${SLEEP_SECS} -1 1); do
echo -n "$x " 1>&2
sleep 1
done
echo "" 1>&2
fi
# reload .custom_defaults as they may have been changed
source "$curp/.custom_defaults.sh"
serverurl="$(cat custom/conf/app.ini | grep '^ROOT_URL.*=' | rev | cut -d '=' -f1 | rev | tr -d '[:space:]')"
pushuri="$(echo "$serverurl" | perl -pe "s#(https?://)#\${1}${ADMIN_USER}:${ADMIN_PASSWORD}@#" )"
apiurl="$serverurl/api/v1"
# init ripos
for REPO in ${REPOS}; do
repoPath="$(echo "$REPO" | cut -d ':' -f1 | tr -d '[:space:]')"
repoOrg="$(echo "$REPO" | cut -d ':' -f2 | tr -d '[:space:]')"
repoName="$(echo "$REPO" | cut -d ':' -f3 | tr -d '[:space:]')"
[ -z "$repoName" ] && repoName="$(basename "$repoPath")"
if [ -z "$repoOrg" ]; then
repoAPIEP="/user/repos"
repoOwner="${ADMIN_USER}"
else
create_org "$repoOrg"
sleep 1
repoAPIEP="/org/$repoOrg/repos"
repoOwner="$repoOrg"
fi
create_repo "${repoName}" "${repoAPIEP}" 1>&2
(
cd "$repoPath"
git remote add gitea-debug-dev "$pushuri/${repoOwner}/${repoName}.git" || true
git push "gitea-debug-dev" master
)
exit
done