2016-08-15 15:38:50 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
2017-02-19 22:24:20 +01:00
|
|
|
set -euo pipefail
|
2016-08-15 15:38:50 +02:00
|
|
|
|
|
|
|
# Retrieve arguments
|
2017-04-05 21:06:22 +02:00
|
|
|
domain=$YNH_APP_ARG_DOMAIN
|
|
|
|
path=$YNH_APP_ARG_PATH
|
|
|
|
admin=$YNH_APP_ARG_ADMIN
|
|
|
|
is_public=$YNH_APP_ARG_PUBLIC_SITE
|
2016-08-15 15:38:50 +02:00
|
|
|
|
|
|
|
# Load common variables
|
|
|
|
source ./_common.sh
|
|
|
|
|
|
|
|
# Set app specific variables
|
2016-10-18 00:01:49 +02:00
|
|
|
app=${APPNAME:-gogs}
|
2016-08-15 15:38:50 +02:00
|
|
|
dbname=$app
|
|
|
|
dbuser=$app
|
|
|
|
|
|
|
|
# Source app helpers
|
|
|
|
source /usr/share/yunohost/helpers
|
|
|
|
|
|
|
|
# TODO: Check domain/path availability with app helper
|
|
|
|
sudo yunohost app checkurl "${domain}${path}" -a "$app" \
|
|
|
|
|| ynh_die "The path ${domain}${path} is not available for app installation."
|
|
|
|
|
|
|
|
# Check user parameter
|
|
|
|
ynh_user_exists "$admin" \
|
|
|
|
|| ynh_die "The chosen admin user does not exist."
|
|
|
|
|
|
|
|
# Check destination directory
|
|
|
|
DESTDIR="/opt/$app"
|
|
|
|
[[ -d $DESTDIR ]] && ynh_die \
|
|
|
|
"The destination directory '$DESTDIR' already exists.\
|
|
|
|
You should safely delete it before installing this app."
|
|
|
|
|
|
|
|
# Generate random password and key
|
|
|
|
dbpass=$(ynh_string_random)
|
|
|
|
key=$(ynh_string_random)
|
|
|
|
|
|
|
|
# Initialize database and store mysql password for upgrade
|
|
|
|
ynh_mysql_create_db "$dbname" "$dbuser" "$dbpass"
|
|
|
|
ynh_app_setting_set $app mysqlpwd $dbpass
|
|
|
|
ynh_app_setting_set $app adminusername $admin
|
|
|
|
ynh_app_setting_set $app is_public $is_public
|
|
|
|
ynh_app_setting_set $app secret_key $is_public
|
|
|
|
|
|
|
|
# Add users
|
2016-10-11 22:18:56 +02:00
|
|
|
id -g "$app" &>/dev/null || sudo addgroup "$app" --system --quiet
|
2016-10-16 16:43:48 +02:00
|
|
|
id -u "$app" &>/dev/null || sudo adduser "$app" \
|
2016-10-11 22:18:56 +02:00
|
|
|
--ingroup "$app" --system --quiet --shell /bin/bash
|
2016-08-15 15:38:50 +02:00
|
|
|
|
|
|
|
# create needed directories
|
2016-10-11 22:18:56 +02:00
|
|
|
REPO_PATH=/home/"$app"/repositories
|
|
|
|
DATA_PATH=/home/"$app"/data
|
2016-08-15 15:38:50 +02:00
|
|
|
sudo mkdir -p "$DESTDIR"/custom/conf "$REPO_PATH" "$DATA_PATH"/avatars \
|
2016-10-11 22:18:56 +02:00
|
|
|
"$DATA_PATH"/avatars "$DATA_PATH"/attachments /var/log/"$app"
|
|
|
|
sudo chown -R "$app":"$app" /home/"$app" /var/log/"$app"
|
2016-08-15 15:38:50 +02:00
|
|
|
|
|
|
|
# Install Gogs
|
|
|
|
extract_gogs $DESTDIR
|
|
|
|
|
|
|
|
# Configure gogs with app.ini file
|
|
|
|
sudo cp ../conf/app.ini "$DESTDIR"/custom/conf
|
|
|
|
sudo sed -i "s@yuno_repo_path@"$REPO_PATH"@g" "$DESTDIR"/custom/conf/app.ini
|
|
|
|
if [ "$path" = "/" ]
|
|
|
|
then
|
|
|
|
sudo sed -i "s@yuno_url@$domain@g" "$DESTDIR"/custom/conf/app.ini
|
|
|
|
else
|
|
|
|
sudo sed -i "s@yuno_url@$domain${path%/}@g" "$DESTDIR"/custom/conf/app.ini
|
|
|
|
fi
|
|
|
|
sudo sed -i "s@yuno_dbpdw@$dbpass@g" "$DESTDIR"/custom/conf/app.ini
|
|
|
|
sudo sed -i "s@yuno_dbuser@$dbuser@g" "$DESTDIR"/custom/conf/app.ini
|
|
|
|
sudo sed -i "s@yuno_domain@$domain@g" "$DESTDIR"/custom/conf/app.ini
|
|
|
|
sudo sed -i "s@yuno_key@$key@g" "$DESTDIR"/custom/conf/app.ini
|
|
|
|
sudo sed -i "s@yuno_data_path@$DATA_PATH@g" "$DESTDIR"/custom/conf/app.ini
|
|
|
|
|
|
|
|
# Configure logrotate
|
2016-10-11 22:18:56 +02:00
|
|
|
sudo cp ../conf/logrotate /etc/logrotate.d/"$app"
|
2016-08-15 15:38:50 +02:00
|
|
|
|
|
|
|
# Configure init script
|
|
|
|
sudo cp ../conf/gogs.service /etc/systemd/system/
|
|
|
|
sudo systemctl daemon-reload
|
2016-10-11 22:18:56 +02:00
|
|
|
sudo systemctl enable "$app".service
|
2016-08-15 15:38:50 +02:00
|
|
|
|
|
|
|
# Start gogs for building mysql tables
|
2016-10-11 22:18:56 +02:00
|
|
|
sudo systemctl start "$app".service
|
2016-08-15 15:38:50 +02:00
|
|
|
|
|
|
|
# Wait till login_source mysql table is created
|
|
|
|
while ! $(ynh_mysql_connect_as "$dbuser" "$dbpass" "$dbname" <<< "SELECT * FROM login_source;" &>/dev/null)
|
|
|
|
do
|
|
|
|
sleep 2
|
|
|
|
done
|
|
|
|
|
|
|
|
# Add ldap config
|
|
|
|
sudo sed -i "s@yuno_admin@$admin@g" ../conf/login_source.sql
|
|
|
|
ynh_mysql_connect_as "$dbuser" "$dbpass" "$dbname" < ../conf/login_source.sql
|
|
|
|
|
|
|
|
# Add Gogs to YunoHost's monitored services
|
2016-10-11 22:18:56 +02:00
|
|
|
sudo yunohost service add "$app" --log /var/log/"$app"/"$app".log
|
2016-08-15 15:38:50 +02:00
|
|
|
|
|
|
|
# Modify Nginx configuration file and copy it to Nginx conf directory
|
|
|
|
sed -i "s@PATHTOCHANGE@${path%/}@g" ../conf/nginx.conf
|
|
|
|
if [ "$path" = "/" ]
|
|
|
|
then
|
|
|
|
sed -i "s@COMMENT_IF_ROOT@#@g" ../conf/nginx.conf
|
|
|
|
else
|
|
|
|
sed -i "s@COMMENT_IF_ROOT@@g" ../conf/nginx.conf
|
|
|
|
fi
|
2016-10-11 22:18:56 +02:00
|
|
|
sudo cp ../conf/nginx.conf /etc/nginx/conf.d/$domain.d/"$app".conf
|
2016-08-15 15:38:50 +02:00
|
|
|
|
2017-06-19 20:49:09 +02:00
|
|
|
# Create uploads folder and permissions
|
|
|
|
sudo mkdir /opt/gogs/data
|
2017-06-19 20:53:10 +02:00
|
|
|
sudo chown -R gogs /opt/gogs/data
|
2017-06-19 20:49:09 +02:00
|
|
|
|
2016-08-15 15:38:50 +02:00
|
|
|
# Unprotect root from SSO if public
|
|
|
|
if [ "$is_public" = "Yes" ]
|
|
|
|
then
|
|
|
|
ynh_app_setting_set $app unprotected_uris "/"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Reload services
|
2016-10-11 21:55:26 +02:00
|
|
|
sudo systemctl restart rsyslog.service || true
|
|
|
|
sudo systemctl reload nginx.service || true
|
2016-10-11 22:18:56 +02:00
|
|
|
sudo systemctl restart "$app".service || true
|