seafile_ynh/scripts/install
2019-05-08 18:57:06 +02:00

197 lines
7 KiB
Bash

#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# Source YunoHost helpers
source /usr/share/yunohost/helpers
# Stop script if errors
ynh_abort_if_errors
# Import common cmd
source ./experimental_helper.sh
source ./_common.sh
# Retrieve arguments
domain=$YNH_APP_ARG_DOMAIN
path_url=$(ynh_normalize_url_path $YNH_APP_ARG_PATH)
server_name="$YNH_APP_ARG_SERVER_NAME"
admin=$YNH_APP_ARG_ADMIN
admin_password=$YNH_APP_ARG_ADMIN_PASSWORD
is_public=$YNH_APP_ARG_PUBLIC_SITE
seafile_data=/home/yunohost.app/seafile-data
final_path=/opt/yunohost/$app
seafile_user=seafile
# Create special path with / at the end
if [[ $path_url == '/' ]]
then
path_url2=$path_url
else
path_url2=$path_url"/"
fi
# Check domain/path availability
test $(ynh_webpath_available $domain $path_url) == 'True' || ynh_die "$domain$path_url is not available, please use an other domain or path."
ynh_webpath_register $app $domain $path_url
# Check Final Path availability
test ! -e "$final_path" || ynh_die "This path already contains a folder"
# Find available ports
seahub_port=$(ynh_find_port 8000)
fileserver_port=$(ynh_find_port 8082)
webdav_port=$(ynh_find_port 8080)
# store config in yunohost
ynh_app_setting_set $app server_name "$server_name"
ynh_app_setting_set $app final_path $final_path
ynh_app_setting_set $app seafile_user $seafile_user
ynh_app_setting_set $app admin $admin
ynh_app_setting_set $app seahub_port $seahub_port
ynh_app_setting_set $app fileserver_port $fileserver_port
ynh_app_setting_set $app webdav_port $webdav_port
ynh_app_setting_set $app is_public $is_public
ynh_app_setting_set $app installed_version $seafile_version
# Retrieve admin email
admin_email=$(yunohost user info $admin | grep mail: | sed "s/mail: //g")
#=================================================
# STANDARD MODIFICATIONS
#=================================================
# Create User
ynh_system_user_create $seafile_user $final_path
# Check dependencies
install_dependance
# Copy files to the right place
test -e $seafile_data && ynh_secure_remove "$seafile_data"
mkdir -p $final_path
mkdir -p $final_path/installed
mkdir -p $final_path/logs
mkdir -p $final_path/seafile-data
# Download new version from sources
install_source
# init databases
dbuser=seafile
db_pwd=$(ynh_string_random 15)
ynh_app_setting_set "$app" mysqlpwd "$db_pwd"
ynh_mysql_create_db ccnetdb "$dbuser" "$db_pwd"
ynh_mysql_create_db seafiledb "$dbuser" "$db_pwd"
ynh_mysql_create_db seahubdb "$dbuser" "$db_pwd"
# Run install script
chmod +x expect_scripts/install.exp
chmod +x $final_path/seafile-server-$seafile_version/setup-seafile-mysql.sh
expect_scripts/install.exp "$final_path/seafile-server-$seafile_version" "$server_name" "$domain" "$seafile_data" "$fileserver_port" "$db_pwd"
# Update seafile config
ynh_replace_string http:// https:// $final_path/conf/ccnet.conf
ynh_replace_string :8000 $path_url $final_path/conf/ccnet.conf
echo 'FILE_SERVER_ROOT = "https://'$domain'/seafhttp"' | tee -a $final_path/conf/seahub_settings.py
echo 'SITE_ROOT = "'$path_url2'"' | tee -a $final_path/conf/seahub_settings.py
echo 'SERVE_STATIC = False' | tee -a $final_path/conf/seahub_settings.py
echo 'MEDIA_URL = "'$path_url2'media/"' | tee -a $final_path/conf/seahub_settings.py
echo 'COMPRESS_URL = MEDIA_URL' | tee -a $final_path/conf/seahub_settings.py
echo "STATIC_URL = MEDIA_URL + 'assets/'" | tee -a $final_path/conf/seahub_settings.py
echo "LOGIN_URL = '"$path_url2"accounts/login/'" | tee -a $final_path/conf/seahub_settings.py
echo "ALLOWED_HOSTS = ['"$domain"']" | tee -a $final_path/conf/seahub_settings.py
# Email configuration
echo 'EMAIL_USE_TLS = False' | tee -a $final_path/conf/seahub_settings.py
echo 'EMAIL_HOST = "localhost"' | tee -a $final_path/conf/seahub_settings.py
echo 'EMAIL_HOST_USER = "seafile@'$domain'"' | tee -a $final_path/conf/seahub_settings.py
echo 'EMAIL_HOST_PASSWORD = ""' | tee -a $final_path/conf/seahub_settings.py
echo 'EMAIL_PORT = "25"' | tee -a $final_path/conf/seahub_settings.py
echo 'DEFAULT_FROM_EMAIL = "seafile@'$domain'"' | tee -a $final_path/conf/seahub_settings.py
echo 'SERVER_EMAIL = "seafile@'$domain'"' | tee -a $final_path/conf/seahub_settings.py
# LDAP configuration
echo '[LDAP]' | tee -a $final_path/conf/ccnet.conf
echo 'HOST = ldap://localhost:389' | tee -a $final_path/conf/ccnet.conf
echo 'BASE = ou=users,dc=yunohost,dc=org' | tee -a $final_path/conf/ccnet.conf
echo 'LOGIN_ATTR = mail' | tee -a $final_path/conf/ccnet.conf
# Enable manually wiki
echo 'ENABLE_WIKI = True' | tee -a $final_path/conf/seahub_settings.py
# Fix local warning
ynh_replace_string en_US.UTF-8 ${LANG:-'en_US.UTF-8'} $final_path/seafile-server-$seafile_version/seahub.sh
# Update gunicorn config
sed --in-place -r "s@bind = \"0\.0\.0\.0:[[:digit:]]+\"@bind = \"0.0.0.0:$seahub_port\"@g" $final_path/conf/gunicorn.conf
# Add webdav
cp ../conf/seafdav.conf $final_path/conf/seafdav.conf
ynh_replace_string WEBDAV_PORT $webdav_port $final_path/conf/seafdav.conf
# Add Seafile Server to startup
cp ../conf/seafile-server /etc/init.d
ynh_replace_string SEAHUB_PORT $seahub_port /etc/init.d/seafile-server
ynh_replace_string SEAFILE_DIR $final_path /etc/init.d/seafile-server
ynh_replace_string SEAFILE_USER $seafile_user /etc/init.d/seafile-server
chmod +x /etc/init.d/seafile-server
systemctl daemon-reload
update-rc.d seafile-server defaults
# Config nginx
config_nginx
# Copy first launch script
cp expect_scripts/first_launch.exp $final_path
chmod +x $final_path/first_launch.exp
# Set permissions to seafile directory
chown -R $seafile_user:$seafile_user $final_path
chown -R $seafile_user:$seafile_user $seafile_data
# Start seafile, seahub and populate admin account
su - $seafile_user -s /bin/bash -c "$final_path/seafile-server-$seafile_version/seafile.sh start"
# We escape all char witch needed.
ynh_replace_special_string "__ADMIN_PASSWORD__" $admin_password "$final_path/first_launch.exp"
su - $seafile_user -s /bin/bash -c "$final_path/first_launch.exp $final_path/seafile-server-$seafile_version $admin_email"
ynh_secure_remove "$final_path/first_launch.exp"
#=================================================
# GENERIC FINALIZATION
#=================================================
# Add sso config to unprotect domain.tld/seafhttp + domain.tld/seafdav do in /etc/ssowat/conf.json.persistent
cp ../conf/add_sso_conf.py $final_path
cp ../conf/remove_sso_conf.py $final_path
python3 $final_path/add_sso_conf.py
# unprotect media
ynh_app_setting_set seafile unprotected_uris "/media"
if [ "$is_public" = "0" ]
then
ynh_app_setting_delete seafile unprotected_uris
else
ynh_app_setting_set seafile unprotected_uris "/"
fi
# Add logrotate
ynh_use_logrotate $final_path/logs
ln -s $final_path/logs /var/log/seafile
# register yunohost service
yunohost service add seafile-server
# Restart seafile
su - $seafile_user -s /bin/bash -c "$final_path/seafile-server-latest/seahub.sh stop"
systemctl stop seafile-server.service
sleep 2
# We kill all process lunched by the script
pkill -f seafile-controller || true
pkill -f seaf-server || true
system_reload seafile-server start