freedombone/src/freedombone-app-gogs

781 lines
26 KiB
Plaintext
Raw Normal View History

2016-07-03 17:13:34 +02:00
#!/bin/bash
2018-04-08 14:30:21 +02:00
# _____ _ _
# | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# | __| _| -_| -_| . | . | | . | . | | -_|
# |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
2016-07-03 17:13:34 +02:00
#
2018-04-08 14:30:21 +02:00
# Freedom in the Cloud
2016-07-03 17:13:34 +02:00
#
# Gogs functions
#
# License
# =======
#
2018-01-25 19:35:39 +01:00
# Copyright (C) 2014-2018 Bob Mottram <bob@freedombone.net>
2016-07-03 17:13:34 +02:00
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
VARIANTS='developer'
2016-07-06 17:47:55 +02:00
IN_DEFAULT_INSTALL=0
SHOW_ON_ABOUT=1
2016-09-24 17:48:26 +02:00
GOGS_USERNAME='gogs'
2017-09-07 11:13:34 +02:00
GOGS_VERSION='0.11.29'
2016-09-24 17:48:26 +02:00
2016-07-03 17:13:34 +02:00
GIT_DOMAIN_NAME=
GIT_CODE=
GIT_ONION_PORT=8090
GIT_ADMIN_PASSWORD=
2016-09-28 17:11:31 +02:00
GOGS_BIN=
2018-05-15 00:06:41 +02:00
GOGS_PORT=3145
2016-09-24 17:48:26 +02:00
GOGS_SHORT_DESCRIPTION=$'Gogs'
GOGS_DESCRIPTION=$'Gogs git repo management'
GOGS_MOBILE_APP_URL=
2016-10-05 23:33:41 +02:00
gogs_variables=(ONION_ONLY
GIT_ADMIN_PASSWORD
GIT_DOMAIN_NAME
GIT_CODE
GIT_ONION_PORT
MY_USERNAME
DDNS_PROVIDER
ARCHITECTURE)
function logging_on_gogs {
echo -n ''
}
function logging_off_gogs {
echo -n ''
}
2016-11-20 13:37:13 +01:00
function change_password_gogs {
curr_username="$1"
new_user_password="$2"
}
function install_interactive_gogs {
if [[ $ONION_ONLY != "no" ]]; then
2016-10-19 22:17:25 +02:00
GIT_DOMAIN_NAME='gogs.local'
write_config_param "GIT_DOMAIN_NAME" "$GIT_DOMAIN_NAME"
else
function_check interactive_site_details
interactive_site_details git
fi
APP_INSTALLED=1
}
2016-11-13 00:36:55 +01:00
function configure_interactive_gogs {
dialog --title $"Gogs" \
--backtitle $"Freedombone Control Panel" \
--defaultno \
--yesno $"\nAllow registration of new users?" 10 60
sel=$?
case $sel in
0)
sed -i "s|DISABLE_REGISTRATION =.*|DISABLE_REGISTRATION = false|g" /home/gogs/custom/conf/app.ini
sed -i "s|SHOW_REGISTRATION_BUTTON =.*|SHOW_REGISTRATION_BUTTON = true|g" /home/gogs/custom/conf/app.ini
;;
1)
sed -i "s|DISABLE_REGISTRATION =.*|DISABLE_REGISTRATION = true|g" /home/gogs/custom/conf/app.ini
sed -i "s|SHOW_REGISTRATION_BUTTON =.*|SHOW_REGISTRATION_BUTTON = false|g" /home/gogs/custom/conf/app.ini
;;
255) return;;
esac
systemctl restart gogs
}
2016-09-24 17:48:26 +02:00
function gogs_parameters {
2016-10-05 23:33:41 +02:00
if [[ ${ARCHITECTURE} == *"386" || ${ARCHITECTURE} == *"686" ]]; then
2016-09-24 17:48:26 +02:00
CURR_ARCH=386
fi
2016-10-05 23:33:41 +02:00
if [[ ${ARCHITECTURE} == *"amd64" || ${ARCHITECTURE} == "x86_64" ]]; then
2016-09-24 17:48:26 +02:00
CURR_ARCH=amd64
fi
2016-10-05 23:33:41 +02:00
if [[ ${ARCHITECTURE} == *"arm"* ]]; then
2017-05-21 10:57:30 +02:00
CURR_ARCH=armv5
2016-09-24 17:48:26 +02:00
fi
2016-10-05 23:33:41 +02:00
if [ ! ${CURR_ARCH} ]; then
echo $'No architecture specified'
ARCHITECTURE=$(uname -m)
2016-10-05 23:33:41 +02:00
if [[ ${ARCHITECTURE} == "arm"* ]]; then
2017-05-21 10:58:09 +02:00
CURR_ARCH=armv5
echo $"Using $CURR_ARCH"
fi
2016-10-05 23:33:41 +02:00
if [[ ${ARCHITECTURE} == "amd"* || ${ARCHITECTURE} == "x86_64" ]]; then
CURR_ARCH=amd64
echo $"Using $CURR_ARCH"
fi
2016-10-05 23:33:41 +02:00
if [[ ${ARCHITECTURE} == *"386" || ${ARCHITECTURE} == *"686" ]]; then
CURR_ARCH=386
echo $"Using $CURR_ARCH"
fi
2016-09-24 17:48:26 +02:00
fi
2017-05-21 11:05:43 +02:00
GOGS_FILE=linux_${CURR_ARCH}.tar.gz
GOGS_BIN="https://github.com/gogits/gogs/releases/download/v${GOGS_VERSION}/${GOGS_FILE}"
2016-09-24 17:48:26 +02:00
}
2016-07-03 17:13:34 +02:00
function gogs_create_database {
2018-02-27 15:11:56 +01:00
if [ -f "${IMAGE_PASSWORD_FILE}" ]; then
GIT_ADMIN_PASSWORD="$(printf "%s" "$(cat "$IMAGE_PASSWORD_FILE")")"
2016-11-19 10:37:35 +01:00
else
2018-02-27 15:11:56 +01:00
if [ ! "${GIT_ADMIN_PASSWORD}" ]; then
GIT_ADMIN_PASSWORD="$(create_password "${MINIMUM_PASSWORD_LENGTH}")"
2016-08-27 12:52:01 +02:00
fi
fi
2018-02-27 15:11:56 +01:00
if [ ! "$GIT_ADMIN_PASSWORD" ]; then
2016-08-27 12:52:01 +02:00
return
fi
function_check create_database
2017-07-25 11:13:58 +02:00
create_database gogs "$GIT_ADMIN_PASSWORD"
}
2016-07-09 12:36:12 +02:00
function reconfigure_gogs {
2016-08-27 12:52:01 +02:00
echo -n ''
2016-07-09 12:36:12 +02:00
}
2016-07-06 16:01:28 +02:00
function upgrade_gogs {
2018-02-27 15:11:56 +01:00
if ! grep -q 'gogs version:' "$COMPLETION_FILE"; then
2016-09-24 17:48:26 +02:00
return
fi
2016-07-06 16:40:09 +02:00
2018-05-15 23:57:16 +02:00
GOGS_CONFIG_PATH=/home/${GOGS_USERNAME}/custom/conf
GOGS_CONFIG_FILE=$GOGS_CONFIG_PATH/app.ini
# Change port number if necessary
if ! grep -q "HTTP_PORT = ${GOGS_PORT}" "${GOGS_CONFIG_FILE}"; then
sed -i "s|HTTP_PORT =.*|HTTP_PORT = ${GOGS_PORT}|g" "${GOGS_CONFIG_FILE}"
read_config_param GIT_DOMAIN_NAME
sed -i "s|proxy_pass .*|proxy_pass http://localhost:${GOGS_PORT};|g" "/etc/nginx/sites-available/${GIT_DOMAIN_NAME}"
systemctl restart gogs
systemctl restart nginx
fi
2016-10-16 20:50:56 +02:00
CURR_GOGS_VERSION=$(get_completion_param "gogs version")
2017-04-12 19:18:19 +02:00
echo "gogs current version: ${CURR_GOGS_VERSION}"
echo "gogs app version: ${GOGS_VERSION}"
2016-10-05 23:33:41 +02:00
if [[ "${CURR_GOGS_VERSION}" == "${GOGS_VERSION}" ]]; then
2016-09-24 17:48:26 +02:00
return
fi
2018-02-27 15:11:56 +01:00
cp "$GOGS_CONFIG_FILE $INSTALL_DIR/gogs_config.ini"
2018-02-27 15:11:56 +01:00
if [ -d "$INSTALL_DIR/gogs-repositories" ]; then
rm -rf "$INSTALL_DIR/gogs-repositories"
2017-09-07 11:50:18 +02:00
fi
if [ -d /home/${GOGS_USERNAME}/gogs-repositories ]; then
# shellcheck disable=SC2086
mv /home/${GOGS_USERNAME}/gogs-repositories $INSTALL_DIR
2017-09-07 11:50:18 +02:00
fi
2016-09-24 17:48:26 +02:00
gogs_parameters
2017-04-12 16:21:29 +02:00
echo "gogs binary upgrade: ${GOGS_BIN}"
2016-09-24 17:48:26 +02:00
2018-02-27 15:11:56 +01:00
if [ ! -d "${INSTALL_DIR}" ]; then
mkdir -p "${INSTALL_DIR}"
2016-09-24 17:48:26 +02:00
fi
2018-02-27 15:11:56 +01:00
cd "${INSTALL_DIR}" || exit 26784427
if [ -d "${INSTALL_DIR}/gogs" ]; then
rm -rf "${INSTALL_DIR}/gogs"
2016-09-28 18:41:11 +02:00
fi
2016-09-28 18:25:02 +02:00
GOGS_FILE=linux_${CURR_ARCH}.tar.gz
2016-10-05 23:33:41 +02:00
if [ ! -f ${GOGS_FILE} ]; then
2016-09-28 17:19:33 +02:00
wget ${GOGS_BIN}
2016-09-28 18:25:02 +02:00
fi
2016-10-05 23:33:41 +02:00
if [ ! -f ${GOGS_FILE} ]; then
2016-09-28 18:25:02 +02:00
GOGS_FILE=linux_${CURR_ARCH}.zip
GOGS_BIN="https://github.com/gogits/gogs/releases/download/v${GOGS_VERSION}/${GOGS_FILE}"
if [ ! -f ${GOGS_FILE} ]; then
wget ${GOGS_BIN}
fi
if [ ! -f ${GOGS_FILE} ]; then
2016-09-28 17:19:33 +02:00
exit 37836
else
2016-10-23 20:38:14 +02:00
apt-get -yq install unzip
2016-09-28 18:45:46 +02:00
unzip -o ${GOGS_FILE}
2016-09-28 17:19:33 +02:00
fi
else
2018-02-27 15:11:56 +01:00
tar -xzf "${INSTALL_DIR}/${GOGS_FILE}"
2016-09-24 17:48:26 +02:00
fi
2018-02-27 15:11:56 +01:00
if [ ! -d "${INSTALL_DIR}/gogs" ]; then
2016-09-24 17:48:26 +02:00
exit 37823
fi
2018-02-27 15:11:56 +01:00
rm -rf "/home/${GOGS_USERNAME:?}/"*
2018-03-03 12:49:17 +01:00
cp -r "${INSTALL_DIR}/gogs/"* "/home/${GOGS_USERNAME}"
2016-09-28 18:25:02 +02:00
if [ -f ${GOGS_FILE} ]; then
rm ${GOGS_FILE}
2016-09-28 18:14:03 +02:00
fi
2016-09-24 17:48:26 +02:00
if [ ! -d ${GOGS_CONFIG_PATH} ]; then
mkdir -p ${GOGS_CONFIG_PATH}
fi
2018-02-27 15:11:56 +01:00
cp "$INSTALL_DIR/gogs_config.ini" "$GOGS_CONFIG_FILE"
if [ ! -f $GOGS_CONFIG_FILE ]; then
echo $'Gogs ini file not upgraded'
exit 873535
fi
2018-02-27 15:11:56 +01:00
rm "$INSTALL_DIR/gogs_config.ini"
2017-09-07 11:50:18 +02:00
if [ -d /home/${GOGS_USERNAME}/gogs-repositories ]; then
rm -rf /home/${GOGS_USERNAME}/gogs-repositories
fi
2018-02-27 15:11:56 +01:00
if [ -d "$INSTALL_DIR/gogs-repositories" ]; then
# shellcheck disable=SC2086
mv $INSTALL_DIR/gogs-repositories /home/${GOGS_USERNAME}/gogs-repositories
2017-09-07 11:50:18 +02:00
fi
2018-02-27 15:11:56 +01:00
chown -R "${GOGS_USERNAME}":"${GOGS_USERNAME}" "/home/${GOGS_USERNAME}"
2018-02-27 15:11:56 +01:00
sed -i "s|gogs version.*|gogs version:$GOGS_VERSION|g" "${COMPLETION_FILE}"
2017-06-06 15:26:57 +02:00
systemctl restart mariadb
2016-09-24 17:48:26 +02:00
systemctl restart gogs
2016-07-06 16:01:28 +02:00
}
2016-07-06 15:55:09 +02:00
function backup_local_gogs {
2018-02-27 15:11:56 +01:00
if ! grep -q "gogs domain" "${COMPLETION_FILE}"; then
2016-08-27 12:52:01 +02:00
return
fi
2018-02-27 15:11:56 +01:00
if [ ! -d "/home/${GOGS_USERNAME}/gogs-repositories" ]; then
2016-08-27 12:52:01 +02:00
return
fi
2016-07-07 12:33:45 +02:00
2016-08-27 12:52:01 +02:00
echo $"Backing up gogs"
2016-07-07 12:33:45 +02:00
2016-08-27 12:52:01 +02:00
function_check backup_database_to_usb
backup_database_to_usb gogs
2016-07-07 12:33:45 +02:00
2016-08-27 12:52:01 +02:00
function_check backup_directory_to_usb
2018-02-27 15:11:56 +01:00
backup_directory_to_usb "/home/${GOGS_USERNAME}/custom" gogs
backup_directory_to_usb "/home/${GOGS_USERNAME}/gogs-repositories" gogsrepos
backup_directory_to_usb "/home/${GOGS_USERNAME}/.ssh" gogsssh
2016-08-27 12:52:01 +02:00
echo $"Gogs backup complete"
}
function restore_local_gogs {
2018-02-27 15:11:56 +01:00
if ! grep -q "gogs domain" "${COMPLETION_FILE}"; then
2016-08-27 12:52:01 +02:00
return
fi
if [ ${#GIT_DOMAIN_NAME} -gt 2 ]; then
function_check gogs_create_database
gogs_create_database
2018-02-27 15:11:56 +01:00
GOGS_CONFIG_PATH="/home/${GOGS_USERNAME}/custom/conf"
GOGS_CONFIG_FILE="${GOGS_CONFIG_PATH}/app.ini"
2017-09-07 12:11:39 +02:00
2016-08-27 12:52:01 +02:00
function_check restore_database
2018-02-27 15:11:56 +01:00
restore_database gogs "${GIT_DOMAIN_NAME}"
2016-08-27 12:52:01 +02:00
temp_restore_dir=/root/tempgogs
2018-02-27 15:11:56 +01:00
if [ -d "${USB_MOUNT}/backup/gogs" ]; then
2016-08-27 12:52:01 +02:00
echo $"Restoring Gogs settings"
2017-09-07 12:11:39 +02:00
if [ ! -d $GOGS_CONFIG_PATH ]; then
mkdir -p $GOGS_CONFIG_PATH
fi
2018-02-27 15:11:56 +01:00
if [ -d "/root/tempgogs/home/${GOGS_USERNAME}/custom" ]; then
2018-03-03 12:49:17 +01:00
cp -r "/root/tempgogs/home/${GOGS_USERNAME}/custom/"* "/home/${GOGS_USERNAME}/custom/"
2017-09-07 12:11:39 +02:00
else
2018-03-03 12:49:17 +01:00
cp -r "/root/tempgogs/"* "/home/${GOGS_USERNAME}/custom/"
fi
2018-02-27 15:11:56 +01:00
# shellcheck disable=SC2181
2016-08-27 12:52:01 +02:00
if [ ! "$?" = "0" ]; then
function_check set_user_permissions
set_user_permissions
function_check backup_unmount_drive
backup_unmount_drive
exit 981
fi
echo $"Restoring Gogs repos"
function_check restore_directory_from_usb
2018-02-27 15:11:56 +01:00
restore_directory_from_usb "${temp_restore_dir}repos" gogsrepos
if [ ! -d "/home/${GOGS_USERNAME}/gogs-repositories" ]; then
mkdir "/home/${GOGS_USERNAME}/gogs-repositories"
2017-09-07 11:50:18 +02:00
fi
2018-02-27 15:11:56 +01:00
if [ -d "${temp_restore_dir}repos/home/${GOGS_USERNAME}/gogs-repositories" ]; then
2018-03-03 12:49:17 +01:00
cp -r "${temp_restore_dir}repos/home/${GOGS_USERNAME}/gogs-repositories/"* "/home/${GOGS_USERNAME}/gogs-repositories/"
else
2018-03-03 12:49:17 +01:00
cp -r "${temp_restore_dir}repos/"* "/home/${GOGS_USERNAME}/gogs-repositories/"
fi
2018-02-27 15:11:56 +01:00
# shellcheck disable=SC2181
2016-08-27 12:52:01 +02:00
if [ ! "$?" = "0" ]; then
function_check set_user_permissions
set_user_permissions
function_check backup_unmount_drive
backup_unmount_drive
exit 67574
fi
echo $"Restoring Gogs authorized_keys"
function_check restore_directory_from_usb
restore_directory_from_usb ${temp_restore_dir}ssh gogsssh
2016-10-05 23:33:41 +02:00
if [ ! -d /home/${GOGS_USERNAME}/.ssh ]; then
mkdir /home/${GOGS_USERNAME}/.ssh
2016-08-27 12:52:01 +02:00
fi
if [ -d ${temp_restore_dir}ssh/home/${GOGS_USERNAME}/.ssh ]; then
cp -r ${temp_restore_dir}ssh/home/${GOGS_USERNAME}/.ssh/* /home/${GOGS_USERNAME}/.ssh/
else
cp -r ${temp_restore_dir}/* /home/${GOGS_USERNAME}/.ssh/
fi
2018-02-27 15:11:56 +01:00
# shellcheck disable=SC2181
2016-08-27 12:52:01 +02:00
if [ ! "$?" = "0" ]; then
function_check set_user_permissions
set_user_permissions
function_check backup_unmount_drive
backup_unmount_drive
exit 8463
fi
rm -rf ${temp_restore_dir}
rm -rf ${temp_restore_dir}repos
rm -rf ${temp_restore_dir}ssh
2016-10-05 23:33:41 +02:00
chown -R ${GOGS_USERNAME}:${GOGS_USERNAME} /home/${GOGS_USERNAME}
2016-08-27 12:52:01 +02:00
fi
2017-06-25 21:37:25 +02:00
2018-02-27 15:11:56 +01:00
MARIADB_PASSWORD=$("${PROJECT_NAME}-pass" -u root -a mariadb)
2017-06-25 21:37:25 +02:00
sed -i "s|PASSWD =.*|PASSWD = $MARIADB_PASSWORD|g" ${GOGS_CONFIG_FILE}
MARIADB_PASSWORD=
2017-09-07 12:11:39 +02:00
systemctl restart gogs
2016-08-27 12:52:01 +02:00
fi
2016-07-06 15:55:09 +02:00
}
function backup_remote_gogs {
2016-09-24 17:48:26 +02:00
if [ -d /home/$GOGS_USERNAME ]; then
2016-08-27 12:52:01 +02:00
function_check suspend_site
suspend_site ${GIT_DOMAIN_NAME}
2016-07-09 22:26:27 +02:00
2016-08-27 12:52:01 +02:00
function_check backup_database_to_friend
2017-07-25 11:13:58 +02:00
backup_database_to_friend gogs
2016-07-09 22:26:27 +02:00
2016-08-27 12:52:01 +02:00
echo $"Obtaining Gogs settings backup"
2016-07-09 22:26:27 +02:00
2016-08-27 12:52:01 +02:00
function_check backup_directory_to_friend
2016-09-24 17:48:26 +02:00
backup_directory_to_friend /home/$GOGS_USERNAME/custom gogs
2016-07-09 22:26:27 +02:00
2016-08-27 12:52:01 +02:00
echo $"Obtaining Gogs repos backup"
2016-07-09 22:26:27 +02:00
# shellcheck disable=SC2086
2016-09-24 17:48:26 +02:00
mv /home/$GOGS_USERNAME/gogs-repositories/*.git /home/$GOGS_USERNAME/gogs-repositories/bob
backup_directory_to_friend /home/$GOGS_USERNAME/gogs-repositories gogsrepos
2016-07-09 22:26:27 +02:00
2016-08-27 12:52:01 +02:00
echo $"Obtaining Gogs authorized_keys backup"
2016-07-09 22:26:27 +02:00
2016-09-24 17:48:26 +02:00
backup_directory_to_friend /home/$GOGS_USERNAME/.ssh gogsssh
2016-07-09 22:26:27 +02:00
2016-08-27 12:52:01 +02:00
function_check restart_site
restart_site
2016-07-09 22:26:27 +02:00
2016-08-27 12:52:01 +02:00
echo $"Gogs backup complete"
fi
2016-07-04 22:02:22 +02:00
}
function restore_remote_gogs {
2018-02-27 15:11:56 +01:00
if grep -q "gogs domain" "$COMPLETION_FILE"; then
2016-10-16 20:50:56 +02:00
GIT_DOMAIN_NAME=$(get_completion_param "gogs domain")
2016-08-27 12:52:01 +02:00
function_check gogs_create_database
gogs_create_database
2017-09-07 12:11:39 +02:00
GOGS_CONFIG_PATH=/home/${GOGS_USERNAME}/custom/conf
GOGS_CONFIG_FILE=${GOGS_CONFIG_PATH}/app.ini
2016-08-27 12:52:01 +02:00
function_check restore_database_from_friend
2018-02-27 15:11:56 +01:00
restore_database_from_friend gogs "${GIT_DOMAIN_NAME}"
if [ -d "${SERVER_DIRECTORY}/backup/gogs" ]; then
2017-09-07 12:11:39 +02:00
if [ ! -d $GOGS_CONFIG_PATH ]; then
mkdir -p $GOGS_CONFIG_PATH
2016-08-27 12:52:01 +02:00
fi
if [ -d /root/tempgogs/home/${GOGS_USERNAME}/custom ]; then
cp -r /root/tempgogs/home/${GOGS_USERNAME}/custom/* /home/${GOGS_USERNAME}/custom/
else
cp -r /root/tempgogs/* /home/${GOGS_USERNAME}/custom/
fi
2018-02-27 15:11:56 +01:00
# shellcheck disable=SC2181
2016-08-27 12:52:01 +02:00
if [ ! "$?" = "0" ]; then
2016-09-24 17:48:26 +02:00
exit 58852
2016-08-27 12:52:01 +02:00
fi
echo $"Restoring Gogs repos"
restore_directory_from_friend /root/tempgogsrepos gogsrepos
2017-09-07 11:50:18 +02:00
if [ ! -d /home/${GOGS_USERNAME}/gogs-repositories ]; then
mkdir /home/${GOGS_USERNAME}/gogs-repositories
fi
if [ -d /root/tempgogsrepos/home/${GOGS_USERNAME}/gogs-repositories ]; then
cp -r /root/tempgogsrepos/home/${GOGS_USERNAME}/gogs-repositories/* /home/${GOGS_USERNAME}/gogs-repositories/
else
cp -r /root/tempgogsrepos/* /home/${GOGS_USERNAME}/gogs-repositories/
fi
2018-02-27 15:11:56 +01:00
# shellcheck disable=SC2181
2016-08-27 12:52:01 +02:00
if [ ! "$?" = "0" ]; then
exit 7649
fi
echo $"Restoring Gogs authorized_keys"
restore_directory_from_friend /root/tempgogsssh gogsssh
2016-10-05 23:33:41 +02:00
if [ ! -d /home/${GOGS_USERNAME}/.ssh ]; then
mkdir /home/${GOGS_USERNAME}/.ssh
2016-08-27 12:52:01 +02:00
fi
if [ -d /root/tempgogsssh/home/${GOGS_USERNAME}/.ssh ]; then
cp -r /root/tempgogsssh/home/${GOGS_USERNAME}/.ssh/* /home/${GOGS_USERNAME}/.ssh/
else
cp -r /root/tempgogsssh/* /home/${GOGS_USERNAME}/.ssh/
fi
2018-02-27 15:11:56 +01:00
# shellcheck disable=SC2181
2016-08-27 12:52:01 +02:00
if [ ! "$?" = "0" ]; then
exit 74239
fi
rm -rf /root/tempgogs
rm -rf /root/tempgogsrepos
rm -rf /root/tempgogsssh
2016-10-05 23:33:41 +02:00
chown -R ${GOGS_USERNAME}:${GOGS_USERNAME} /home/${GOGS_USERNAME}
2016-08-27 12:52:01 +02:00
echo $"Restore of Gogs complete"
fi
2017-06-25 21:37:25 +02:00
2018-02-27 15:11:56 +01:00
MARIADB_PASSWORD=$("${PROJECT_NAME}-pass" -u root -a mariadb)
2017-06-25 21:37:25 +02:00
sed -i "s|PASSWD =.*|PASSWD = $MARIADB_PASSWORD|g" ${GOGS_CONFIG_FILE}
MARIADB_PASSWORD=
2017-09-07 12:11:39 +02:00
systemctl restart gogs
2016-08-27 12:52:01 +02:00
fi
}
2016-07-03 22:34:22 +02:00
function remove_gogs {
if [ ${#GIT_DOMAIN_NAME} -eq 0 ]; then
return
fi
2016-08-27 12:52:01 +02:00
systemctl stop gogs
systemctl disable gogs
2017-06-10 22:37:50 +02:00
2018-02-27 15:11:56 +01:00
nginx_dissite "${GIT_DOMAIN_NAME}"
remove_certs "${GIT_DOMAIN_NAME}"
if [ -d "/var/www/${GIT_DOMAIN_NAME}" ]; then
rm -rf "/var/www/${GIT_DOMAIN_NAME}"
2016-08-27 12:52:01 +02:00
fi
2018-02-27 15:11:56 +01:00
if [ -f "/etc/nginx/sites-available/${GIT_DOMAIN_NAME}" ]; then
rm "/etc/nginx/sites-available/${GIT_DOMAIN_NAME}"
2016-08-27 12:52:01 +02:00
fi
function_check drop_database
drop_database gogs
rm /etc/systemd/system/gogs.service
2017-06-10 22:37:50 +02:00
systemctl daemon-reload
2018-02-27 15:11:56 +01:00
rm -rf "/home/${GOGS_USERNAME:?}/"*
2016-08-27 12:52:01 +02:00
remove_onion_service gogs ${GIT_ONION_PORT} 9418
2016-10-18 11:43:08 +02:00
remove_completion_param "install_gogs"
2018-02-27 15:11:56 +01:00
sed -i '/gogs /d' "$COMPLETION_FILE"
2016-11-02 18:49:21 +01:00
remove_backup_database_local gogs
2017-06-12 12:33:54 +02:00
groupdel -f gogs
userdel -r gogs
function_check remove_ddns_domain
2018-02-27 15:11:56 +01:00
remove_ddns_domain "$GIT_DOMAIN_NAME"
2016-07-03 22:34:22 +02:00
}
2016-07-03 17:13:34 +02:00
function install_gogs {
2018-02-27 15:11:56 +01:00
if [ ! "$GIT_DOMAIN_NAME" ]; then
2016-08-27 12:52:01 +02:00
return
fi
2016-09-24 17:48:26 +02:00
adduser --disabled-login --gecos 'Gogs' $GOGS_USERNAME
2017-06-10 19:34:45 +02:00
if [ ! -d /home/$GOGS_USERNAME ]; then
echo $"/home/$GOGS_USERNAME directory not created"
exit 783528
fi
2017-06-10 15:57:42 +02:00
groupadd gogs
2016-08-27 12:52:01 +02:00
2016-09-24 17:48:26 +02:00
gogs_parameters
2016-08-27 12:52:01 +02:00
2018-02-27 15:11:56 +01:00
if [ ! -d "${INSTALL_DIR}" ]; then
mkdir -p "${INSTALL_DIR}"
2016-08-27 12:52:01 +02:00
fi
2018-02-27 15:11:56 +01:00
cd "${INSTALL_DIR}" || exit 24682467284
if [ -d "$INSTALL_DIR/gogs" ]; then
rm -rf "$INSTALL_DIR/gogs"
2016-09-28 18:41:11 +02:00
fi
2018-02-27 15:11:56 +01:00
GOGS_FILE="linux_${CURR_ARCH}.tar.gz"
2016-09-28 18:25:02 +02:00
if [ ! -f ${GOGS_FILE} ]; then
wget ${GOGS_BIN}
2016-08-27 12:52:01 +02:00
fi
2016-09-28 18:25:02 +02:00
if [ ! -f ${GOGS_FILE} ]; then
GOGS_FILE=linux_${CURR_ARCH}.zip
GOGS_BIN="https://github.com/gogits/gogs/releases/download/v${GOGS_VERSION}/${GOGS_FILE}"
2016-09-28 17:19:33 +02:00
wget ${GOGS_BIN}
2016-09-28 18:25:02 +02:00
if [ ! -f ${GOGS_FILE} ]; then
2016-09-28 17:19:33 +02:00
exit 37836
else
2016-10-23 20:38:14 +02:00
apt-get -yq install unzip
2016-09-28 18:45:46 +02:00
unzip -o ${GOGS_FILE}
2016-09-28 17:19:33 +02:00
fi
else
2018-02-27 15:11:56 +01:00
tar -xzf "${INSTALL_DIR}/${GOGS_FILE}"
2016-08-27 12:52:01 +02:00
fi
2018-02-27 15:11:56 +01:00
if [ ! -d "${INSTALL_DIR}/gogs" ]; then
2016-09-24 17:48:26 +02:00
exit 37823
2016-08-27 12:52:01 +02:00
fi
2018-02-27 15:11:56 +01:00
rm -rf "/home/${GOGS_USERNAME:?}/"*
2018-03-03 12:49:17 +01:00
cp -r "${INSTALL_DIR}/gogs/"* "/home/${GOGS_USERNAME}"
2016-09-28 18:25:02 +02:00
if [ -f ${GOGS_FILE} ]; then
rm ${GOGS_FILE}
2016-09-28 18:14:03 +02:00
fi
2016-08-27 12:52:01 +02:00
2016-10-05 23:33:41 +02:00
if [ ! -f /home/${GOGS_USERNAME}/gogs ]; then
2016-09-24 17:48:26 +02:00
echo 'Gogs binary not installed'
exit 345562
2016-08-27 12:52:01 +02:00
fi
2018-02-27 15:11:56 +01:00
{ echo "export GOROOT=/home/go";
# shellcheck disable=SC2153
echo "export GOPATH=\${GOROOT}/go${GO_VERSION}/bin";
echo "export PATH=\$PATH:\$GOPATH"; } >> "/home/${GOGS_USERNAME}/.bashrc"
2016-10-05 23:33:41 +02:00
chown -R ${GOGS_USERNAME}:${GOGS_USERNAME} /home/${GOGS_USERNAME}
2016-08-27 12:52:01 +02:00
function_check install_mariadb
install_mariadb
function_check get_mariadb_password
get_mariadb_password
2016-09-28 19:05:33 +02:00
function_check gogs_create_database
2016-08-27 12:52:01 +02:00
gogs_create_database
2016-10-05 23:33:41 +02:00
if [ ! -f /home/${GOGS_USERNAME}/scripts/mysql.sql ]; then
2016-08-27 12:52:01 +02:00
echo $'MySql template for Gogs was not found'
exit 72528
fi
function_check initialise_database
2016-10-05 23:33:41 +02:00
initialise_database gogs /home/${GOGS_USERNAME}/scripts/mysql.sql
2016-08-27 12:52:01 +02:00
2016-10-05 23:33:41 +02:00
chown -R ${GOGS_USERNAME}:${GOGS_USERNAME} /home/${GOGS_USERNAME}
2016-08-27 12:52:01 +02:00
2018-02-27 15:11:56 +01:00
{ echo '[Unit]';
echo 'Description=Gogs (Go Git Service)';
echo 'After=syslog.target';
echo 'After=network.target';
echo 'After=mysqld.service';
echo '';
echo '[Service]';
echo '#LimitMEMLOCK=infinity';
echo '#LimitNOFILE=65535';
echo 'Type=simple';
echo 'User=gogs';
echo 'Group=gogs';
echo "WorkingDirectory=/home/${GOGS_USERNAME}";
echo "ExecStart=/home/${GOGS_USERNAME}/gogs web";
echo 'Restart=always';
echo 'RestartSec=10';
echo "Environment=\"USER=${GOGS_USERNAME}\" \"HOME=/home/${GOGS_USERNAME}\" \"GOPATH=/home/go/go${GO_VERSION}\"";
echo '';
echo '[Install]';
echo 'WantedBy=multi-user.target'; } > /etc/systemd/system/gogs.service
2016-08-27 12:52:01 +02:00
systemctl enable gogs
systemctl daemon-reload
2016-09-29 18:18:36 +02:00
systemctl start gogs
2016-08-27 12:52:01 +02:00
2018-02-27 15:11:56 +01:00
if [ ! -d "/var/www/${GIT_DOMAIN_NAME}" ]; then
mkdir "/var/www/${GIT_DOMAIN_NAME}"
2016-08-27 12:52:01 +02:00
fi
2018-02-27 15:11:56 +01:00
if [ -d "/var/www/${GIT_DOMAIN_NAME}/htdocs" ]; then
rm -rf "/var/www/${GIT_DOMAIN_NAME}/htdocs"
2016-08-27 12:52:01 +02:00
fi
2018-02-27 15:11:56 +01:00
if [[ "${ONION_ONLY}" == "no" ]]; then
2016-08-27 12:52:01 +02:00
function_check nginx_http_redirect
2018-02-27 15:11:56 +01:00
nginx_http_redirect "${GIT_DOMAIN_NAME}"
{ echo 'server {';
echo ' listen 443 ssl;';
echo ' #listen [::]:443 ssl;';
echo " root /var/www/${GIT_DOMAIN_NAME}/htdocs;";
echo " server_name ${GIT_DOMAIN_NAME};";
echo ' access_log /dev/null;';
echo " error_log /dev/null;";
echo ''; } >> "/etc/nginx/sites-available/${GIT_DOMAIN_NAME}"
2016-08-27 12:52:01 +02:00
function_check nginx_ssl
2018-02-27 15:11:56 +01:00
nginx_ssl "${GIT_DOMAIN_NAME}"
2018-03-05 19:15:29 +01:00
function_check nginx_security_options
nginx_security_options "${GIT_DOMAIN_NAME}"
2018-02-27 15:11:56 +01:00
{ echo ' add_header Strict-Transport-Security max-age=0;';
echo '';
echo ' location / {'; } >> "/etc/nginx/sites-available/${GIT_DOMAIN_NAME}"
2016-10-01 13:18:23 +02:00
function_check nginx_limits
2018-02-27 15:11:56 +01:00
nginx_limits "${GIT_DOMAIN_NAME}" '10G'
2018-05-15 00:06:41 +02:00
{ echo " proxy_pass http://localhost:${GOGS_PORT};";
2018-02-27 15:11:56 +01:00
echo ' }';
echo '';
echo ' fastcgi_buffers 64 4K;';
echo '';
echo ' error_page 403 /core/templates/403.php;';
echo ' error_page 404 /core/templates/404.php;';
echo '';
echo ' location = /robots.txt {';
echo ' allow all;';
echo ' log_not_found off;';
echo ' access_log /dev/null;';
echo ' }';
echo '}';
echo ''; } >> "/etc/nginx/sites-available/${GIT_DOMAIN_NAME}"
2016-08-27 12:52:01 +02:00
else
2018-02-27 15:11:56 +01:00
echo -n '' > "/etc/nginx/sites-available/${GIT_DOMAIN_NAME}"
fi
{ echo 'server {';
echo " listen 127.0.0.1:${GIT_ONION_PORT} default_server;";
echo " root /var/www/$GIT_DOMAIN_NAME/htdocs;";
echo " server_name $GIT_DOMAIN_NAME;";
echo ' access_log /dev/null;';
echo " error_log /dev/null;";
echo ''; } >> "/etc/nginx/sites-available/${GIT_DOMAIN_NAME}"
2018-03-05 19:15:29 +01:00
function_check nginx_security_options
nginx_security_options "${GIT_DOMAIN_NAME}"
2018-02-27 15:11:56 +01:00
{ echo ' add_header Strict-Transport-Security max-age=0;';
echo '';
echo ' location / {'; } >> "/etc/nginx/sites-available/${GIT_DOMAIN_NAME}"
2016-10-01 13:18:23 +02:00
function_check nginx_limits
2018-02-27 15:11:56 +01:00
nginx_limits "${GIT_DOMAIN_NAME}" '10G'
2018-05-15 00:06:41 +02:00
{ echo " proxy_pass http://localhost:${GOGS_PORT};";
2018-02-27 15:11:56 +01:00
echo ' }';
echo '';
echo ' fastcgi_buffers 64 4K;';
echo '';
echo ' error_page 403 /core/templates/403.php;';
echo ' error_page 404 /core/templates/404.php;';
echo '';
echo ' location = /robots.txt {';
echo ' allow all;';
echo ' log_not_found off;';
echo ' access_log /dev/null;';
echo ' }';
echo '}'; } >> "/etc/nginx/sites-available/${GIT_DOMAIN_NAME}"
2016-08-27 12:52:01 +02:00
function_check configure_php
configure_php
function_check create_site_certificate
2018-02-27 15:11:56 +01:00
create_site_certificate "${GIT_DOMAIN_NAME}" 'yes'
2016-08-27 12:52:01 +02:00
2018-02-27 15:11:56 +01:00
nginx_ensite "${GIT_DOMAIN_NAME}"
2016-08-27 12:52:01 +02:00
if [ ! -d /var/lib/tor ]; then
echo $'No Tor installation found. Gogs onion site cannot be configured.'
exit 877367
fi
2018-05-01 17:13:57 +02:00
if ! grep -q "hidden_service_gogs" "$ONION_SERVICES_FILE"; then
2018-02-27 15:11:56 +01:00
{ echo 'HiddenServiceDir /var/lib/tor/hidden_service_gogs/';
echo 'HiddenServiceVersion 3';
echo "HiddenServicePort 80 127.0.0.1:${GIT_ONION_PORT}";
2018-05-01 17:13:57 +02:00
echo "HiddenServicePort 9418 127.0.0.1:9418"; } >> "$ONION_SERVICES_FILE"
2016-08-27 12:52:01 +02:00
echo $'Added onion site for Gogs'
fi
2016-10-18 11:48:57 +02:00
onion_update
2016-08-27 12:52:01 +02:00
function_check wait_for_onion_service
wait_for_onion_service 'gogs'
GIT_ONION_HOSTNAME=$(cat /var/lib/tor/hidden_service_gogs/hostname)
2017-06-06 15:26:57 +02:00
systemctl restart mariadb
2017-06-01 20:05:15 +02:00
systemctl restart php7.0-fpm
2016-08-27 12:52:01 +02:00
systemctl restart nginx
2016-10-18 11:43:08 +02:00
set_completion_param "gogs domain" "$GIT_DOMAIN_NAME"
set_completion_param "gogs onion domain" "$GIT_ONION_HOSTNAME"
2016-08-27 12:52:01 +02:00
function_check add_ddns_domain
2018-02-27 15:11:56 +01:00
add_ddns_domain "${GIT_DOMAIN_NAME}"
2016-08-27 12:52:01 +02:00
# obtain the secret key
2018-02-27 15:11:56 +01:00
GOGS_SECRET_KEY="$(create_password "${MINIMUM_PASSWORD_LENGTH}")"
"${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a gogs -p "*"
2016-08-27 12:52:01 +02:00
# create the configuration
2018-02-27 15:11:56 +01:00
GOGS_CONFIG_PATH="/home/${GOGS_USERNAME}/custom/conf"
2016-10-05 23:33:41 +02:00
if [ ! -d ${GOGS_CONFIG_PATH} ]; then
mkdir -p ${GOGS_CONFIG_PATH}
fi
GOGS_CONFIG_FILE=${GOGS_CONFIG_PATH}/app.ini
2018-02-27 15:11:56 +01:00
{ echo "RUN_USER = $GOGS_USERNAME";
echo 'RUN_MODE = prod';
echo '';
echo '[database]';
echo 'DB_TYPE = mysql';
echo 'HOST = 127.0.0.1:3306';
echo 'NAME = gogs';
echo 'USER = root';
echo "PASSWD = $MARIADB_PASSWORD";
echo 'SSL_MODE = disable';
echo 'PATH = data/gogs.db';
echo '';
echo '[repository]';
echo "ROOT = /home/$GOGS_USERNAME/gogs-repositories";
echo '';
echo '[server]'; } >> ${GOGS_CONFIG_FILE}
2016-10-05 23:33:41 +02:00
if [[ ${ONION_ONLY} == 'no' ]]; then
echo "DOMAIN = ${GIT_DOMAIN_NAME}" >> ${GOGS_CONFIG_FILE}
2016-11-02 22:38:10 +01:00
echo "ROOT_URL = https://$GIT_DOMAIN_NAME/" >> ${GOGS_CONFIG_FILE}
2016-08-27 12:52:01 +02:00
else
2016-10-05 23:33:41 +02:00
echo "DOMAIN = ${GIT_ONION_HOSTNAME}" >> ${GOGS_CONFIG_FILE}
2016-11-02 22:38:10 +01:00
echo "ROOT_URL = http://$GIT_DOMAIN_NAME/" >> ${GOGS_CONFIG_FILE}
2016-10-05 23:33:41 +02:00
fi
2018-05-15 00:06:41 +02:00
{ echo "HTTP_PORT = ${GOGS_PORT}";
2018-02-27 15:11:56 +01:00
echo "SSH_PORT = $SSH_PORT";
echo 'SSH_DOMAIN = %(DOMAIN)s';
echo "CERT_FILE = /etc/ssl/certs/${GIT_DOMAIN_NAME}.pem";
echo "KEY_FILE = /etc/ssl/private/${GIT_DOMAIN_NAME}.key";
echo 'DISABLE_ROUTER_LOG = true';
echo '';
echo '[session]';
echo 'PROVIDER = file';
echo '';
echo '[log]';
echo 'MODE = file';
echo 'LEVEL = Info';
echo '';
echo '[security]';
echo 'INSTALL_LOCK = true';
echo "SECRET_KEY = $GOGS_SECRET_KEY";
echo '';
echo '[service]';
echo 'DISABLE_REGISTRATION = false';
echo 'SHOW_REGISTRATION_BUTTON = true';
echo 'REQUIRE_SIGNIN_VIEW = false';
echo 'ENABLE_CAPTCHA = false';
echo '';
echo '[other]';
echo 'SHOW_FOOTER_BRANDING = false';
echo 'SHOW_FOOTER_VERSION = false'; } >> ${GOGS_CONFIG_FILE}
2016-10-05 23:33:41 +02:00
chmod 750 ${GOGS_CONFIG_FILE}
2018-02-27 15:11:56 +01:00
chown -R "${GOGS_USERNAME}":"${GOGS_USERNAME}" "/home/${GOGS_USERNAME}"
2016-08-27 12:52:01 +02:00
systemctl restart gogs
2018-02-27 15:11:56 +01:00
if ! grep -q "gogs domain:" "${COMPLETION_FILE}"; then
echo "gogs domain:${GIT_DOMAIN_NAME}" >> "${COMPLETION_FILE}"
2016-10-02 13:22:33 +02:00
else
2018-02-27 15:11:56 +01:00
sed -i "s|gogs domain.*|gogs domain:${GIT_DOMAIN_NAME}|g" "${COMPLETION_FILE}"
2016-08-27 12:52:01 +02:00
fi
function_check configure_firewall_for_git
configure_firewall_for_git
2018-02-27 15:11:56 +01:00
if ! grep -q "gogs version:" "${COMPLETION_FILE}"; then
echo "gogs version:${GOGS_VERSION}" >> "${COMPLETION_FILE}"
2016-09-24 17:48:26 +02:00
else
2018-02-27 15:11:56 +01:00
sed -i "s|gogs version.*|gogs version:${GOGS_VERSION}|g" "${COMPLETION_FILE}"
2016-09-24 17:48:26 +02:00
fi
APP_INSTALLED=1
2016-07-03 17:13:34 +02:00
}
# NOTE: deliberately no exit 0