Skip to content
Snippets Groups Projects
freedombone-app-gogs 24.3 KiB
Newer Older
Bob Mottram's avatar
Bob Mottram committed
#!/bin/bash
Bob Mottram's avatar
Bob Mottram committed
#  _____               _           _
# |   __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# |   __|  _| -_| -_| . | . |     | . | . |   | -_|
# |__|  |_| |___|___|___|___|_|_|_|___|___|_|_|___|
Bob Mottram's avatar
Bob Mottram committed
#
Bob Mottram's avatar
Bob Mottram committed
#                              Freedom in the Cloud
Bob Mottram's avatar
Bob Mottram committed
#
# Gogs functions
#
# License
# =======
#
Bob Mottram's avatar
Bob Mottram committed
# Copyright (C) 2014-2019 Bob Mottram <bob@freedombone.net>
Bob Mottram's avatar
Bob Mottram committed
#
# 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'
APP_CATEGORY=organization
Bob Mottram's avatar
Bob Mottram committed

REQUIRES_APP=
Bob Mottram's avatar
Bob Mottram committed
GOGS_USERNAME='gogs'
Bob Mottram's avatar
Bob Mottram committed
GOGS_VERSION='0.11.66'
GOGS_DOMAIN_NAME=
GOGS_CODE=
Bob Mottram's avatar
Bob Mottram committed
GIT_ONION_PORT=8090
GIT_ADMIN_PASSWORD=
Bob Mottram's avatar
Bob Mottram committed
GOGS_BIN=
Bob Mottram's avatar
Bob Mottram committed
GOGS_PORT=3145
Bob Mottram's avatar
Bob Mottram committed
GOGS_SHORT_DESCRIPTION=$'Git repo management'
GOGS_DESCRIPTION=$'Git repo management'
GOGS_MOBILE_APP_URL=

gogs_os_type='linux'

Bob Mottram's avatar
Bob Mottram committed
gogs_variables=(ONION_ONLY
                GIT_ADMIN_PASSWORD
                GOGS_DOMAIN_NAME
                GOGS_CODE
Bob Mottram's avatar
Bob Mottram committed
                MY_USERNAME
                DDNS_PROVIDER
                ARCHITECTURE)

function upgrade_distro_gogs {
    target_distro="$1"

    if [[ "$target_distro" == 'buster' ]]; then
        echo -n ''
    fi
}

function gogs_setting_registration {
    allow_registrations="$1"

    if [[ "$allow_registrations" == '1' ]]; then
        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
        systemctl restart gogs
    fi

    if [[ "$allow_registrations" == '0' ]]; then
        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
        systemctl restart gogs
    fi
}

function logging_on_gogs {
    echo -n ''
}

function logging_off_gogs {
    echo -n ''
}

Bob Mottram's avatar
Bob Mottram committed
function change_password_gogs {
    curr_username="$1"
    new_user_password="$2"
}

function install_interactive_gogs {
    if [[ $ONION_ONLY != "no" ]]; then
        GOGS_DOMAIN_NAME='gogs.local'
        write_config_param "GOGS_DOMAIN_NAME" "$GOGS_DOMAIN_NAME"
        function_check interactive_site_details
        interactive_site_details git
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
}

Bob Mottram's avatar
Bob Mottram committed
function gogs_parameters {
Bob Mottram's avatar
Bob Mottram committed
    if [[ ${ARCHITECTURE} == *"386" || ${ARCHITECTURE} == *"686" ]]; then
Bob Mottram's avatar
Bob Mottram committed
        CURR_ARCH=386
    fi
Bob Mottram's avatar
Bob Mottram committed
    if [[ ${ARCHITECTURE} == *"amd64" || ${ARCHITECTURE} == "x86_64" ]]; then
Bob Mottram's avatar
Bob Mottram committed
        CURR_ARCH=amd64
    fi
Bob Mottram's avatar
Bob Mottram committed
    if [[ ${ARCHITECTURE} == *"arm"* ]]; then
        CURR_ARCH=armv6
        gogs_os_type=raspi2
Bob Mottram's avatar
Bob Mottram committed
    fi
Bob Mottram's avatar
Bob Mottram committed
    if [ ! ${CURR_ARCH} ]; then
        echo $'No architecture specified'
        ARCHITECTURE=$(uname -m)
Bob Mottram's avatar
Bob Mottram committed
        if [[ ${ARCHITECTURE} == "arm"* ]]; then
            CURR_ARCH=armv6
            gogs_os_type=raspi2
            echo $"Using $CURR_ARCH"
Bob Mottram's avatar
Bob Mottram committed
        if [[ ${ARCHITECTURE} == "amd"* || ${ARCHITECTURE} == "x86_64" ]]; then
            CURR_ARCH=amd64
            echo $"Using $CURR_ARCH"
Bob Mottram's avatar
Bob Mottram committed
        if [[ ${ARCHITECTURE} == *"386" || ${ARCHITECTURE} == *"686" ]]; then
            echo $"Using $CURR_ARCH"
    GOGS_FILE=gogs_${GOGS_VERSION}_${gogs_os_type}_${CURR_ARCH}.tar.gz
Bob Mottram's avatar
Bob Mottram committed
    GOGS_BIN="https://dl.gogs.io/${GOGS_VERSION}/${GOGS_FILE}"
Bob Mottram's avatar
Bob Mottram committed

function gogs_create_database {
Bob Mottram's avatar
Bob Mottram committed
    if [ -f "${IMAGE_PASSWORD_FILE}" ]; then
        GIT_ADMIN_PASSWORD="$(printf "%s" "$(cat "$IMAGE_PASSWORD_FILE")")"
Bob Mottram's avatar
Bob Mottram committed
        if [ ! "${GIT_ADMIN_PASSWORD}" ]; then
            GIT_ADMIN_PASSWORD="$(create_password "${MINIMUM_PASSWORD_LENGTH}")"
Bob Mottram's avatar
Bob Mottram committed
        fi
    fi
Bob Mottram's avatar
Bob Mottram committed
    if [ ! "$GIT_ADMIN_PASSWORD" ]; then
Bob Mottram's avatar
Bob Mottram committed
        return
    fi

    function_check create_database
Bob Mottram's avatar
Bob Mottram committed
    create_database gogs "$GIT_ADMIN_PASSWORD"
function reconfigure_gogs {
Bob Mottram's avatar
Bob Mottram committed
    echo -n ''
function upgrade_gogs {
Bob Mottram's avatar
Bob Mottram committed
    if ! grep -q 'gogs version:' "$COMPLETION_FILE"; then
Bob Mottram's avatar
Bob Mottram committed
        return
    fi
Bob Mottram's avatar
Bob Mottram committed
    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 GOGS_DOMAIN_NAME
        sed -i "s|proxy_pass .*|proxy_pass http://localhost:${GOGS_PORT};|g" "/etc/nginx/sites-available/${GOGS_DOMAIN_NAME}"
Bob Mottram's avatar
Bob Mottram committed
        systemctl restart gogs
        systemctl restart nginx
    fi

Bob Mottram's avatar
Bob Mottram committed
    CURR_GOGS_VERSION=$(get_completion_param "gogs version")
Bob Mottram's avatar
Bob Mottram committed
    echo "gogs current version: ${CURR_GOGS_VERSION}"
    echo "gogs app version: ${GOGS_VERSION}"
Bob Mottram's avatar
Bob Mottram committed
    if [[ "${CURR_GOGS_VERSION}" == "${GOGS_VERSION}" ]]; then
Bob Mottram's avatar
Bob Mottram committed
    cp "$GOGS_CONFIG_FILE" "$INSTALL_DIR/gogs_config.ini"
Bob Mottram's avatar
Bob Mottram committed
    if [ -d "$INSTALL_DIR/gogs-repositories" ]; then
        rm -rf "$INSTALL_DIR/gogs-repositories"
    fi
    if [ -d /home/${GOGS_USERNAME}/gogs-repositories ]; then
Bob Mottram's avatar
Bob Mottram committed
        # shellcheck disable=SC2086
        mv /home/${GOGS_USERNAME}/gogs-repositories $INSTALL_DIR
Bob Mottram's avatar
Bob Mottram committed
    gogs_parameters
Bob Mottram's avatar
Bob Mottram committed
    echo "gogs binary upgrade: ${GOGS_BIN}"
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -d "${INSTALL_DIR}" ]; then
        mkdir -p "${INSTALL_DIR}"
Bob Mottram's avatar
Bob Mottram committed
    fi
    cd "${INSTALL_DIR}" || exit 26
Bob Mottram's avatar
Bob Mottram committed
    if [ -d "${INSTALL_DIR}/gogs" ]; then
        rm -rf "${INSTALL_DIR}/gogs"
    GOGS_FILE=gogs_${GOGS_VERSION}_${gogs_os_type}_${CURR_ARCH}.tar.gz
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -f ${GOGS_FILE} ]; then
        wget ${GOGS_BIN}
Bob Mottram's avatar
Bob Mottram committed
    fi
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -f ${GOGS_FILE} ]; then
        GOGS_FILE=gogs_${GOGS_VERSION}_${gogs_os_type}_${CURR_ARCH}.zip
Bob Mottram's avatar
Bob Mottram committed
        GOGS_BIN="https://dl.gogs.io/${GOGS_VERSION}/${GOGS_FILE}"
Bob Mottram's avatar
Bob Mottram committed
        if [ ! -f ${GOGS_FILE} ]; then
            wget ${GOGS_BIN}
        fi
        if [ ! -f ${GOGS_FILE} ]; then
            $INSTALL_PACKAGES unzip
Bob Mottram's avatar
Bob Mottram committed
            unzip -o ${GOGS_FILE}
Bob Mottram's avatar
Bob Mottram committed
        tar -xzf "${INSTALL_DIR}/${GOGS_FILE}"
Bob Mottram's avatar
Bob Mottram committed
    fi
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -d "${INSTALL_DIR}/gogs" ]; then
Bob Mottram's avatar
Bob Mottram committed
    fi
Bob Mottram's avatar
Bob Mottram committed
    rm -rf "/home/${GOGS_USERNAME:?}/"*
Bob Mottram's avatar
Bob Mottram committed
    cp -r "${INSTALL_DIR}/gogs/"* "/home/${GOGS_USERNAME}"
Bob Mottram's avatar
Bob Mottram committed
    if [ -f ${GOGS_FILE} ]; then
        rm ${GOGS_FILE}
Bob Mottram's avatar
Bob Mottram committed
    fi
    if [ ! -d ${GOGS_CONFIG_PATH} ]; then
        mkdir -p ${GOGS_CONFIG_PATH}
    fi

Bob Mottram's avatar
Bob Mottram committed
    cp "$INSTALL_DIR/gogs_config.ini" "$GOGS_CONFIG_FILE"
    if [ ! -f $GOGS_CONFIG_FILE ]; then
        echo $'Gogs ini file not upgraded'
Bob Mottram's avatar
Bob Mottram committed
    rm "$INSTALL_DIR/gogs_config.ini"
    if [ -d /home/${GOGS_USERNAME}/gogs-repositories ]; then
        rm -rf /home/${GOGS_USERNAME}/gogs-repositories
    fi
Bob Mottram's avatar
Bob Mottram committed
    if [ -d "$INSTALL_DIR/gogs-repositories" ]; then
Bob Mottram's avatar
Bob Mottram committed
        # shellcheck disable=SC2086
        mv $INSTALL_DIR/gogs-repositories /home/${GOGS_USERNAME}/gogs-repositories
Bob Mottram's avatar
Bob Mottram committed
    chown -R "${GOGS_USERNAME}":"${GOGS_USERNAME}" "/home/${GOGS_USERNAME}"
Bob Mottram's avatar
Bob Mottram committed
    sed -i "s|gogs version.*|gogs version:$GOGS_VERSION|g" "${COMPLETION_FILE}"
    systemctl restart mariadb
Bob Mottram's avatar
Bob Mottram committed
    systemctl restart gogs
Bob Mottram's avatar
Bob Mottram committed
function backup_local_gogs {
Bob Mottram's avatar
Bob Mottram committed
    if ! grep -q "gogs domain" "${COMPLETION_FILE}"; then
Bob Mottram's avatar
Bob Mottram committed
        return
    fi
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -d "/home/${GOGS_USERNAME}/gogs-repositories" ]; then
Bob Mottram's avatar
Bob Mottram committed
        return
    fi
Bob Mottram's avatar
Bob Mottram committed
    echo $"Backing up gogs"
Bob Mottram's avatar
Bob Mottram committed
    function_check backup_database_to_usb
    backup_database_to_usb gogs
Bob Mottram's avatar
Bob Mottram committed
    function_check backup_directory_to_usb
Bob Mottram's avatar
Bob Mottram committed
    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
Bob Mottram's avatar
Bob Mottram committed
    echo $"Gogs backup complete"
}

function restore_local_gogs {
Bob Mottram's avatar
Bob Mottram committed
    if ! grep -q "gogs domain" "${COMPLETION_FILE}"; then
Bob Mottram's avatar
Bob Mottram committed
        return
    fi

    if [ ${#GOGS_DOMAIN_NAME} -gt 2 ]; then
Bob Mottram's avatar
Bob Mottram committed
        function_check gogs_create_database
        gogs_create_database

Bob Mottram's avatar
Bob Mottram committed
        GOGS_CONFIG_PATH="/home/${GOGS_USERNAME}/custom/conf"
        GOGS_CONFIG_FILE="${GOGS_CONFIG_PATH}/app.ini"
Bob Mottram's avatar
Bob Mottram committed
        function_check restore_database
        restore_database gogs "${GOGS_DOMAIN_NAME}"
Bob Mottram's avatar
Bob Mottram committed
        temp_restore_dir=/root/tempgogs
Bob Mottram's avatar
Bob Mottram committed
        if [ -d "${USB_MOUNT}/backup/gogs" ]; then
Bob Mottram's avatar
Bob Mottram committed
            echo $"Restoring Gogs settings"
Bob Mottram's avatar
Bob Mottram committed
            if [ ! -d $GOGS_CONFIG_PATH ]; then
                mkdir -p $GOGS_CONFIG_PATH
            fi
Bob Mottram's avatar
Bob Mottram committed
            if [ -d "/root/tempgogs/home/${GOGS_USERNAME}/custom" ]; then
Bob Mottram's avatar
Bob Mottram committed
                cp -r "/root/tempgogs/home/${GOGS_USERNAME}/custom/"* "/home/${GOGS_USERNAME}/custom/"
Bob Mottram's avatar
Bob Mottram committed
            else
Bob Mottram's avatar
Bob Mottram committed
                cp -r "/root/tempgogs/"* "/home/${GOGS_USERNAME}/custom/"
Bob Mottram's avatar
Bob Mottram committed
            # shellcheck disable=SC2181
Bob Mottram's avatar
Bob Mottram committed
            if [ ! "$?" = "0" ]; then
                function_check set_user_permissions
                set_user_permissions
                function_check backup_unmount_drive
                backup_unmount_drive
Bob Mottram's avatar
Bob Mottram committed
            fi
            echo $"Restoring Gogs repos"
            function_check restore_directory_from_usb
Bob Mottram's avatar
Bob Mottram committed
            restore_directory_from_usb "${temp_restore_dir}repos" gogsrepos
            if [ ! -d "/home/${GOGS_USERNAME}/gogs-repositories" ]; then
                mkdir "/home/${GOGS_USERNAME}/gogs-repositories"
Bob Mottram's avatar
Bob Mottram committed
            if [ -d "${temp_restore_dir}repos/home/${GOGS_USERNAME}/gogs-repositories" ]; then
Bob Mottram's avatar
Bob Mottram committed
                cp -r "${temp_restore_dir}repos/home/${GOGS_USERNAME}/gogs-repositories/"* "/home/${GOGS_USERNAME}/gogs-repositories/"
Bob Mottram's avatar
Bob Mottram committed
                cp -r "${temp_restore_dir}repos/"* "/home/${GOGS_USERNAME}/gogs-repositories/"
Bob Mottram's avatar
Bob Mottram committed
            # shellcheck disable=SC2181
Bob Mottram's avatar
Bob Mottram committed
            if [ ! "$?" = "0" ]; then
                function_check set_user_permissions
                set_user_permissions
                function_check backup_unmount_drive
                backup_unmount_drive
Bob Mottram's avatar
Bob Mottram committed
            fi
            echo $"Restoring Gogs authorized_keys"
            function_check restore_directory_from_usb
            restore_directory_from_usb ${temp_restore_dir}ssh gogsssh
Bob Mottram's avatar
Bob Mottram committed
            if [ ! -d /home/${GOGS_USERNAME}/.ssh ]; then
                mkdir /home/${GOGS_USERNAME}/.ssh
Bob Mottram's avatar
Bob Mottram committed
            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
Bob Mottram's avatar
Bob Mottram committed
            # shellcheck disable=SC2181
Bob Mottram's avatar
Bob Mottram committed
            if [ ! "$?" = "0" ]; then
                function_check set_user_permissions
                set_user_permissions
                function_check backup_unmount_drive
                backup_unmount_drive
Bob Mottram's avatar
Bob Mottram committed
            fi
            rm -rf ${temp_restore_dir}
            rm -rf ${temp_restore_dir}repos
            rm -rf ${temp_restore_dir}ssh
Bob Mottram's avatar
Bob Mottram committed
            chown -R ${GOGS_USERNAME}:${GOGS_USERNAME} /home/${GOGS_USERNAME}
Bob Mottram's avatar
Bob Mottram committed
        fi
Bob Mottram's avatar
Bob Mottram committed
        MARIADB_PASSWORD=$("${PROJECT_NAME}-pass" -u root -a mariadb)
        sed -i "s|PASSWD =.*|PASSWD = $MARIADB_PASSWORD|g" ${GOGS_CONFIG_FILE}
        MARIADB_PASSWORD=
Bob Mottram's avatar
Bob Mottram committed
        systemctl restart gogs
Bob Mottram's avatar
Bob Mottram committed
    fi
Bob Mottram's avatar
Bob Mottram committed
}

function backup_remote_gogs {
    echo -n ''
function restore_remote_gogs {
    echo -n ''
Bob Mottram's avatar
Bob Mottram committed
function remove_gogs {
    if [ ${#GOGS_DOMAIN_NAME} -eq 0 ]; then
Bob Mottram's avatar
Bob Mottram committed
    systemctl stop gogs
    systemctl disable gogs
    nginx_dissite "${GOGS_DOMAIN_NAME}"
    remove_certs "${GOGS_DOMAIN_NAME}"
    if [ -d "/var/www/${GOGS_DOMAIN_NAME}" ]; then
        rm -rf "/var/www/${GOGS_DOMAIN_NAME}"
Bob Mottram's avatar
Bob Mottram committed
    fi
    if [ -f "/etc/nginx/sites-available/${GOGS_DOMAIN_NAME}" ]; then
        rm "/etc/nginx/sites-available/${GOGS_DOMAIN_NAME}"
Bob Mottram's avatar
Bob Mottram committed
    fi
    function_check drop_database
    drop_database gogs
    rm /etc/systemd/system/gogs.service
Bob Mottram's avatar
Bob Mottram committed
    systemctl daemon-reload
Bob Mottram's avatar
Bob Mottram committed
    rm -rf "/home/${GOGS_USERNAME:?}/"*
Bob Mottram's avatar
Bob Mottram committed
    remove_onion_service gogs ${GIT_ONION_PORT} 9418
Bob Mottram's avatar
Bob Mottram committed
    remove_completion_param "install_gogs"
Bob Mottram's avatar
Bob Mottram committed
    sed -i '/gogs /d' "$COMPLETION_FILE"
    remove_backup_database_local gogs
    groupdel -f gogs
    userdel -r gogs

    function_check remove_ddns_domain
    remove_ddns_domain "$GOGS_DOMAIN_NAME"
Bob Mottram's avatar
Bob Mottram committed
function install_gogs {
    if [ ! "$GOGS_DOMAIN_NAME" ]; then
Bob Mottram's avatar
Bob Mottram committed
        return
    fi

    increment_app_install_progress

Bob Mottram's avatar
Bob Mottram committed
    adduser --disabled-login --gecos 'Gogs' $GOGS_USERNAME
    increment_app_install_progress

Bob Mottram's avatar
Bob Mottram committed
    if [ ! -d /home/$GOGS_USERNAME ]; then
        echo $"/home/$GOGS_USERNAME directory not created"
    increment_app_install_progress

Bob Mottram's avatar
Bob Mottram committed
    groupadd gogs
Bob Mottram's avatar
Bob Mottram committed

    increment_app_install_progress

Bob Mottram's avatar
Bob Mottram committed
    gogs_parameters
Bob Mottram's avatar
Bob Mottram committed

    increment_app_install_progress

Bob Mottram's avatar
Bob Mottram committed
    if [ ! -d "${INSTALL_DIR}" ]; then
        mkdir -p "${INSTALL_DIR}"
Bob Mottram's avatar
Bob Mottram committed
    fi
    cd "${INSTALL_DIR}" || exit 246
Bob Mottram's avatar
Bob Mottram committed
    if [ -d "$INSTALL_DIR/gogs" ]; then
        rm -rf "$INSTALL_DIR/gogs"
    GOGS_FILE="gogs_${GOGS_VERSION}_${gogs_os_type}_${CURR_ARCH}.tar.gz"
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -f ${GOGS_FILE} ]; then
        wget ${GOGS_BIN}
Bob Mottram's avatar
Bob Mottram committed
    fi
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -f ${GOGS_FILE} ]; then
        GOGS_FILE="gogs_${GOGS_VERSION}_${gogs_os_type}_${CURR_ARCH}.zip"
Bob Mottram's avatar
Bob Mottram committed
        GOGS_BIN="https://dl.gogs.io/${GOGS_VERSION}/${GOGS_FILE}"
        wget ${GOGS_BIN}
Bob Mottram's avatar
Bob Mottram committed
        if [ ! -f ${GOGS_FILE} ]; then
            $INSTALL_PACKAGES unzip
Bob Mottram's avatar
Bob Mottram committed
            unzip -o ${GOGS_FILE}
Bob Mottram's avatar
Bob Mottram committed
        tar -xzf "${INSTALL_DIR}/${GOGS_FILE}"
Bob Mottram's avatar
Bob Mottram committed
    fi
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -d "${INSTALL_DIR}/gogs" ]; then
Bob Mottram's avatar
Bob Mottram committed
    fi
Bob Mottram's avatar
Bob Mottram committed
    rm -rf "/home/${GOGS_USERNAME:?}/"*
Bob Mottram's avatar
Bob Mottram committed
    cp -r "${INSTALL_DIR}/gogs/"* "/home/${GOGS_USERNAME}"
Bob Mottram's avatar
Bob Mottram committed
    if [ -f ${GOGS_FILE} ]; then
        rm ${GOGS_FILE}
Bob Mottram's avatar
Bob Mottram committed
    fi
Bob Mottram's avatar
Bob Mottram committed

Bob Mottram's avatar
Bob Mottram committed
    if [ ! -f /home/${GOGS_USERNAME}/gogs ]; then
Bob Mottram's avatar
Bob Mottram committed
        echo 'Gogs binary not installed'
Bob Mottram's avatar
Bob Mottram committed
    fi

    increment_app_install_progress

Bob Mottram's avatar
Bob Mottram committed
    { 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"
Bob Mottram's avatar
Bob Mottram committed
    chown -R ${GOGS_USERNAME}:${GOGS_USERNAME} /home/${GOGS_USERNAME}
Bob Mottram's avatar
Bob Mottram committed

    increment_app_install_progress

Bob Mottram's avatar
Bob Mottram committed
    function_check install_mariadb
    install_mariadb

    increment_app_install_progress

Bob Mottram's avatar
Bob Mottram committed
    function_check get_mariadb_password
    get_mariadb_password

    increment_app_install_progress

Bob Mottram's avatar
Bob Mottram committed
    function_check gogs_create_database
Bob Mottram's avatar
Bob Mottram committed
    gogs_create_database

    increment_app_install_progress

Bob Mottram's avatar
Bob Mottram committed
    if [ ! -f /home/${GOGS_USERNAME}/scripts/mysql.sql ]; then
Bob Mottram's avatar
Bob Mottram committed
        echo $'MySql template for Gogs was not found'
Bob Mottram's avatar
Bob Mottram committed
    fi

Bob Mottram's avatar
Bob Mottram committed
    chown -R ${GOGS_USERNAME}:${GOGS_USERNAME} /home/${GOGS_USERNAME}
Bob Mottram's avatar
Bob Mottram committed

    increment_app_install_progress

Bob Mottram's avatar
Bob Mottram committed
    { 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 'PrivateTmp=true';
      echo 'PrivateDevices=false';
      echo 'NoNewPrivileges=true';
      echo 'CapabilityBoundingSet=~CAP_SYS_ADMIN';
Bob Mottram's avatar
Bob Mottram committed
      echo '';
      echo '[Install]';
      echo 'WantedBy=multi-user.target'; } > /etc/systemd/system/gogs.service
Bob Mottram's avatar
Bob Mottram committed

    increment_app_install_progress

Bob Mottram's avatar
Bob Mottram committed
    systemctl enable gogs
    systemctl daemon-reload
Bob Mottram's avatar
Bob Mottram committed
    systemctl start gogs
Bob Mottram's avatar
Bob Mottram committed

    increment_app_install_progress

    if [ ! -d "/var/www/${GOGS_DOMAIN_NAME}" ]; then
        mkdir "/var/www/${GOGS_DOMAIN_NAME}"
Bob Mottram's avatar
Bob Mottram committed
    fi
    if [ -d "/var/www/${GOGS_DOMAIN_NAME}/htdocs" ]; then
        rm -rf "/var/www/${GOGS_DOMAIN_NAME}/htdocs"
Bob Mottram's avatar
Bob Mottram committed
    fi

Bob Mottram's avatar
Bob Mottram committed
    if [[ "${ONION_ONLY}" == "no" ]]; then
Bob Mottram's avatar
Bob Mottram committed
        function_check nginx_http_redirect
        nginx_http_redirect "${GOGS_DOMAIN_NAME}"
Bob Mottram's avatar
Bob Mottram committed
        { echo 'server {';
          echo '    listen 443 ssl;';
          echo '    #listen [::]:443 ssl;';
          echo "    root /var/www/${GOGS_DOMAIN_NAME}/htdocs;";
          echo "    server_name ${GOGS_DOMAIN_NAME};";
Bob Mottram's avatar
Bob Mottram committed
          echo '    access_log /dev/null;';
          echo "    error_log /dev/null;";
          echo ''; } >> "/etc/nginx/sites-available/${GOGS_DOMAIN_NAME}"
Bob Mottram's avatar
Bob Mottram committed
        function_check nginx_ssl
        nginx_ssl "${GOGS_DOMAIN_NAME}"
Bob Mottram's avatar
Bob Mottram committed
        function_check nginx_security_options
        nginx_security_options "${GOGS_DOMAIN_NAME}"
Bob Mottram's avatar
Bob Mottram committed
        nginx_robots "${GOGS_DOMAIN_NAME}"
Bob Mottram's avatar
Bob Mottram committed
        { echo '    add_header Strict-Transport-Security max-age=0;';
          echo '';
          echo '    location / {'; } >> "/etc/nginx/sites-available/${GOGS_DOMAIN_NAME}"
Bob Mottram's avatar
Bob Mottram committed
        function_check nginx_limits
        nginx_limits "${GOGS_DOMAIN_NAME}" '10G'
Bob Mottram's avatar
Bob Mottram committed
        { echo "        proxy_pass http://localhost:${GOGS_PORT};";
Bob Mottram's avatar
Bob Mottram committed
          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/${GOGS_DOMAIN_NAME}"
Bob Mottram's avatar
Bob Mottram committed
    else
        echo -n '' > "/etc/nginx/sites-available/${GOGS_DOMAIN_NAME}"
Bob Mottram's avatar
Bob Mottram committed
    fi
    { echo 'server {';
      echo "    listen 127.0.0.1:${GIT_ONION_PORT} default_server;";
      echo '    port_in_redirect off;';
      echo "    root /var/www/$GOGS_DOMAIN_NAME/htdocs;";
      echo "    server_name $GOGS_DOMAIN_NAME;";
Bob Mottram's avatar
Bob Mottram committed
      echo '    access_log /dev/null;';
      echo "    error_log /dev/null;";
      echo ''; } >> "/etc/nginx/sites-available/${GOGS_DOMAIN_NAME}"
Bob Mottram's avatar
Bob Mottram committed
    function_check nginx_security_options
    nginx_security_options "${GOGS_DOMAIN_NAME}"
Bob Mottram's avatar
Bob Mottram committed
    { echo '    add_header Strict-Transport-Security max-age=0;';
      echo '';
      echo '    location / {'; } >> "/etc/nginx/sites-available/${GOGS_DOMAIN_NAME}"
Bob Mottram's avatar
Bob Mottram committed
    function_check nginx_limits
    nginx_limits "${GOGS_DOMAIN_NAME}" '10G'
Bob Mottram's avatar
Bob Mottram committed
    { echo "        proxy_pass http://localhost:${GOGS_PORT};";
Bob Mottram's avatar
Bob Mottram committed
      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/${GOGS_DOMAIN_NAME}"
Bob Mottram's avatar
Bob Mottram committed

    function_check configure_php
    configure_php

    increment_app_install_progress

Bob Mottram's avatar
Bob Mottram committed
    function_check create_site_certificate
    create_site_certificate "${GOGS_DOMAIN_NAME}" 'yes'
Bob Mottram's avatar
Bob Mottram committed

    increment_app_install_progress

    nginx_ensite "${GOGS_DOMAIN_NAME}"
Bob Mottram's avatar
Bob Mottram committed

    if [ ! -d /var/lib/tor ]; then
        echo $'No Tor installation found. Gogs onion site cannot be configured.'
Bob Mottram's avatar
Bob Mottram committed
    fi
Bob Mottram's avatar
Bob Mottram committed
    if ! grep -q "hidden_service_gogs" "$ONION_SERVICES_FILE"; then
Bob Mottram's avatar
Bob Mottram committed
        { echo 'HiddenServiceDir /var/lib/tor/hidden_service_gogs/';
          echo 'HiddenServiceVersion 3';
          echo "HiddenServicePort 80 127.0.0.1:${GIT_ONION_PORT}";
Bob Mottram's avatar
Bob Mottram committed
          echo "HiddenServicePort 9418 127.0.0.1:9418"; } >> "$ONION_SERVICES_FILE"
Bob Mottram's avatar
Bob Mottram committed
        echo $'Added onion site for Gogs'
    fi

    onion_update
Bob Mottram's avatar
Bob Mottram committed

    increment_app_install_progress

Bob Mottram's avatar
Bob Mottram committed
    function_check wait_for_onion_service
    wait_for_onion_service 'gogs'

    increment_app_install_progress

Bob Mottram's avatar
Bob Mottram committed
    GIT_ONION_HOSTNAME=$(cat /var/lib/tor/hidden_service_gogs/hostname)

    systemctl restart mariadb

    increment_app_install_progress

    # shellcheck disable=SC2086
    systemctl restart php${PHP_VERSION}-fpm

    increment_app_install_progress

Bob Mottram's avatar
Bob Mottram committed
    systemctl restart nginx

    set_completion_param "gogs domain" "$GOGS_DOMAIN_NAME"
Bob Mottram's avatar
Bob Mottram committed
    set_completion_param "gogs onion domain" "$GIT_ONION_HOSTNAME"
Bob Mottram's avatar
Bob Mottram committed

    function_check add_ddns_domain
    add_ddns_domain "${GOGS_DOMAIN_NAME}"
Bob Mottram's avatar
Bob Mottram committed

    # obtain the secret key
Bob Mottram's avatar
Bob Mottram committed
    GOGS_SECRET_KEY="$(create_password "${MINIMUM_PASSWORD_LENGTH}")"
    "${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a gogs -p "*"
Bob Mottram's avatar
Bob Mottram committed

    # create the configuration
Bob Mottram's avatar
Bob Mottram committed
    GOGS_CONFIG_PATH="/home/${GOGS_USERNAME}/custom/conf"
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -d ${GOGS_CONFIG_PATH} ]; then
        mkdir -p ${GOGS_CONFIG_PATH}
    fi
    GOGS_CONFIG_FILE=${GOGS_CONFIG_PATH}/app.ini
Bob Mottram's avatar
Bob Mottram committed
    { 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}
Bob Mottram's avatar
Bob Mottram committed
    if [[ ${ONION_ONLY} == 'no' ]]; then
        echo "DOMAIN = ${GOGS_DOMAIN_NAME}" >> ${GOGS_CONFIG_FILE}
        echo "ROOT_URL = https://$GOGS_DOMAIN_NAME/" >> ${GOGS_CONFIG_FILE}
Bob Mottram's avatar
Bob Mottram committed
    else
Bob Mottram's avatar
Bob Mottram committed
        echo "DOMAIN = ${GIT_ONION_HOSTNAME}" >> ${GOGS_CONFIG_FILE}
        echo "ROOT_URL = http://$GOGS_DOMAIN_NAME/" >> ${GOGS_CONFIG_FILE}
Bob Mottram's avatar
Bob Mottram committed
    fi
Bob Mottram's avatar
Bob Mottram committed
    { echo "HTTP_PORT = ${GOGS_PORT}";
Bob Mottram's avatar
Bob Mottram committed
      echo "SSH_PORT = $SSH_PORT";
      echo 'SSH_DOMAIN = %(DOMAIN)s';
      echo "CERT_FILE = /etc/ssl/certs/${GOGS_DOMAIN_NAME}.pem";
      echo "KEY_FILE = /etc/ssl/private/${GOGS_DOMAIN_NAME}.key";
Bob Mottram's avatar
Bob Mottram committed
      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}
Bob Mottram's avatar
Bob Mottram committed

    chmod 750 ${GOGS_CONFIG_FILE}
Bob Mottram's avatar
Bob Mottram committed
    chown -R "${GOGS_USERNAME}":"${GOGS_USERNAME}" "/home/${GOGS_USERNAME}"
Bob Mottram's avatar
Bob Mottram committed

    increment_app_install_progress

Bob Mottram's avatar
Bob Mottram committed
    systemctl restart gogs

    increment_app_install_progress

    function_check initialise_database
    initialise_database gogs /home/${GOGS_USERNAME}/scripts/mysql.sql

    systemctl restart gogs

    increment_app_install_progress

Bob Mottram's avatar
Bob Mottram committed
    if ! grep -q "gogs domain:" "${COMPLETION_FILE}"; then
        echo "gogs domain:${GOGS_DOMAIN_NAME}" >> "${COMPLETION_FILE}"
Bob Mottram's avatar
Bob Mottram committed
    else
        sed -i "s|gogs domain.*|gogs domain:${GOGS_DOMAIN_NAME}|g" "${COMPLETION_FILE}"
Bob Mottram's avatar
Bob Mottram committed
    fi

    function_check configure_firewall_for_git
    configure_firewall_for_git
Bob Mottram's avatar
Bob Mottram committed
    if ! grep -q "gogs version:" "${COMPLETION_FILE}"; then
        echo "gogs version:${GOGS_VERSION}" >> "${COMPLETION_FILE}"
Bob Mottram's avatar
Bob Mottram committed
    else
Bob Mottram's avatar
Bob Mottram committed
        sed -i "s|gogs version.*|gogs version:${GOGS_VERSION}|g" "${COMPLETION_FILE}"
Bob Mottram's avatar
Bob Mottram committed
    fi
Bob Mottram's avatar
Bob Mottram committed
}

# NOTE: deliberately no exit 0