seafile_ynh/scripts/install
2024-04-21 20:45:23 +02:00

232 lines
9.3 KiB
Bash

#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# Import common cmd
source ./experimental_helper.sh
source ./_common.sh
# Source YunoHost helpers
source /usr/share/yunohost/helpers
# Stop script if errors
ynh_abort_if_errors
ynh_script_progression --message="Validating installation parameters..."
# Retrieve arguments
domain=$YNH_APP_ARG_DOMAIN
path_url=$YNH_APP_ARG_PATH
server_name="$YNH_APP_ARG_SERVER_NAME"
admin=$YNH_APP_ARG_ADMIN
is_public=$YNH_APP_ARG_IS_PUBLIC
seafile_data=/home/yunohost.app/seafile-data
final_path=/opt/yunohost/$app
seafile_user=$app
admin_password=$YNH_APP_ARG_ADMIN_PASSWORD
seafile_version=$(ynh_app_upstream_version)
architecture=$(ynh_detect_arch)
# Create special path with / at the end
if [[ $path_url == '/' ]]
then
path_url2=$path_url
else
path_url2=$path_url"/"
fi
ynh_apt install pkgconf
# Register (book) web path
ynh_webpath_register --app $app --domain $domain --path_url $path_url
# Check Final Path availability
test ! -e "$final_path" || ynh_die --message "This path already contains a folder"
if [ -e "$seafile_data" ]; then
old_data_dir_path="$seafile_data$(date '+%Y%m%d.%H%M%S')"
ynh_print_warn "A data directory already exist. Data was renamed to $old_data_dir_path"
mv "$seafile_data" "$old_data_dir_path"
fi
# Find available ports
ynh_script_progression --message="Finding available ports..."
seahub_port=$(ynh_find_port --port 8000)
fileserver_port=$(ynh_find_port --port 8082)
webdav_port=$(ynh_find_port --port 8080)
if [ $fileserver_port -eq $webdav_port ]; then
webdav_port=$(ynh_find_port --port $((fileserver_port + 1)))
fi
# store config in yunohost
ynh_script_progression --message="Storing installation settings..."
ynh_app_setting_set --app $app --key server_name --value "$server_name"
ynh_app_setting_set --app $app --key final_path --value $final_path
ynh_app_setting_set --app $app --key seafile_user --value $seafile_user
ynh_app_setting_set --app $app --key admin --value $admin
ynh_app_setting_set --app $app --key seahub_port --value $seahub_port
ynh_app_setting_set --app $app --key fileserver_port --value $fileserver_port
ynh_app_setting_set --app $app --key webdav_port --value $webdav_port
ynh_app_setting_set --app $app --key is_public --value $is_public
#=================================================
# STANDARD MODIFICATIONS
#=================================================
ynh_script_progression --message="Creating base directory..."
mkdir -p $final_path
mkdir -p $final_path/installed
mkdir -p $final_path/logs
mkdir -p $seafile_data
ln -s $seafile_data $final_path/seafile-data
# Create User
ynh_script_progression --message="Configuring system user..."
ynh_system_user_create --username $seafile_user --home_dir $final_path
# Check dependencies
ynh_script_progression --message="Installing dependencies..." --weight=7
install_dependance
# Download new version from sources
ynh_script_progression --message="Installing sources files..." --weight=7
install_source
# init databases
ynh_script_progression --message="Configuring MySQL database..."
db_user=seafile
ynh_mysql_setup_db --db_user $db_user --db_name ccnetdb
ynh_mysql_setup_db --db_user $db_user --db_name seafiledb --db_pwd "$db_pwd"
ynh_mysql_setup_db --db_user $db_user --db_name seahubdb --db_pwd "$db_pwd"
ynh_script_progression --message="Configuring application..." --weight=3
# Run install script
mv_expect_scripts
chmod +x $final_path/seafile-server-$seafile_version/setup-seafile-mysql.sh
$expect_scripts_dir/install.exp "$final_path/seafile-server-$seafile_version" "$server_name" "$domain" "$fileserver_port" "$db_pwd"
sleep 3
# Update seafile config
ynh_replace_string --match_string http:// --replace_string https:// --target_file $final_path/conf/ccnet.conf
ynh_replace_string --match_string :8000 --replace_string $path_url --target_file $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_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
echo 'EMAIL_HOST_PASSWORD = ""' | tee -a $final_path/conf/seahub_settings.py
echo "TIME_ZONE = \"$(cat /etc/timezone)\"" | tee -a $final_path/conf/seahub_settings.py
# SSO authentication
echo 'ENABLE_REMOTE_USER_AUTHENTICATION = True' | tee -a $final_path/conf/seahub_settings.py
echo "REMOTE_USER_HEADER = 'HTTP_EMAIL'" | tee -a $final_path/conf/seahub_settings.py
echo 'REMOTE_USER_CREATE_UNKNOWN_USER = False' | tee -a $final_path/conf/seahub_settings.py
echo "REMOTE_USER_PROTECTED_PATH = ['$path_url', '$path_url/accounts/login']" | 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
echo "FILTER = permission=cn=$app.main,ou=permission,dc=yunohost,dc=org" | tee -a $final_path/conf/ccnet.conf
# Enable manually wiki
echo 'ENABLE_WIKI = True' | tee -a $final_path/conf/seahub_settings.py
# Enable memcached
cat >> $final_path/conf/seahub_settings.py <<EOF
CACHES = {
'default': {
'BACKEND': 'django_pylibmc.memcached.PyLibMCCache',
'LOCATION': '127.0.0.1:11211',
},
}
EOF
# Configure admin info
# It will be used the first start
admin_email=$(ynh_user_get_info --username $admin --key 'mail')
cp ../conf/create_admin.json $final_path/conf/admin.txt
ynh_replace_string --match_string __ADMIN__ --replace_string $admin_email --target_file $final_path/conf/admin.txt
ynh_replace_special_string --match_string __PASSWORD__ --replace_string $admin_password --target_file $final_path/conf/admin.txt
# Fix local warning
ynh_replace_string --match_string en_US.UTF-8 --replace_string ${LANG:-'en_US.UTF-8'} --target_file $final_path/seafile-server-$seafile_version/seahub.sh
# Update gunicorn config
sed --in-place -r "s@bind = \"127\.0\.0\.1:[[:digit:]]+\"@bind = \"127.0.0.1:$seahub_port\"@g" $final_path/conf/gunicorn.conf.py
# Add webdav
cp ../conf/seafdav.conf $final_path/conf/seafdav.conf
ynh_replace_string --match_string __WEBDAV_PORT__ --replace_string $webdav_port --target_file $final_path/conf/seafdav.conf
# Add Seafile Server to startup
ynh_script_progression --message="Configuring a systemd service..." --weight=2
ynh_add_systemd_config --service seafile --template seafile.service
ynh_add_systemd_config --service seahub --template seahub.service
# Config nginx
ynh_script_progression --message="Configuring nginx..." --weight=1
ynh_add_nginx_config 'seahub_port fileserver_port webdav_port'
#=================================================
# GENERIC FINALIZATION
#=================================================
# Set all permissions
ynh_script_progression --message="Protecting directory..."
set_permission
# Add sso config to unprotect domain.tld/seafhttp + domain.tld/seafdav
ynh_script_progression --message="Configuring permissions..."
ynh_permission_create --permission=file_server --url=$domain/seafhttp --auth_header=false \
--label="File server" --protected=true --allowed=visitors
ynh_permission_create --permission=webdav --url=$domain/seafdav --auth_header=true \
--label="Webdav" --protected=true --allowed=visitors
# unprotect media
ynh_permission_create --permission=media --url=/media --auth_header=true \
--label="Media" --protected=true --allowed=visitors
if [ "$is_public" == '1' ];
then
ynh_permission_update --permission "main" --add "visitors"
fi
# Add logrotate
ynh_script_progression --message="Configuring log rotation..."
ynh_use_logrotate --logfile $final_path/logs
ln -s $final_path/logs /var/log/seafile
# register yunohost service
yunohost service add seafile
yunohost service add seahub
ynh_script_progression --message="Stoping services..." --weight=3
# Start service
ynh_script_progression --message="Starting seafile services..." --weight=3
ynh_systemd_action --service_name seafile -l "spawned seaf-server, pid " -p /var/log/seafile/controller.log
sleep 2
ynh_systemd_action --service_name seahub -l "Started Seafile hub." -p "systemd"
sleep 2
# Add fail2ban
ynh_script_progression --message="Configuring fail2ban..." --weight=10
ynh_add_fail2ban_config --use_template
ynh_script_progression --message="Installation of $app completed" --last