You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

372 lines
12 KiB

#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers
#=================================================
# LOAD SETTINGS
#=================================================
app=$YNH_APP_INSTANCE_NAME
# app may contains owncloud instead of nextcloud in case of migration
# You should be really careful with this variable if you want to set multi-instance installation
domain=$(ynh_app_setting_get $app domain)
path_url=$(ynh_app_setting_get $app path)
admin=$(ynh_app_setting_get $app admin)
final_path=$(ynh_app_setting_get $app final_path)
db_name=$(ynh_app_setting_get $app db_name)
#=================================================
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
migration_name=nextcloud
# If db_name doesn't exist, create it
if [ -z $db_name ]; then
db_name=$(ynh_sanitize_dbid $app)
ynh_app_setting_set $app db_name $db_name
fi
# If final_path doesn't exist, create it
if [ -z $final_path ]; then
final_path=/var/www/$app
ynh_app_setting_set $app final_path $final_path
fi
# Handle old migrations from ownCloud
# Get the database name in the config file
nc_conf="$final_path/config/config.php"
curr_dbname=$(grep dbname "$nc_conf" \
| sed "s|.*=> '\(.*\)'.*|\1|g")
# If the database hasn't the name nextcloud, rename the database
if [ "$curr_dbname" != "$db_name" ]
then
# Get the database user in the config file
curr_dbuser=$(grep dbuser "$nc_conf" \
| sed "s|.*=> '\(.*\)'.*|\1|g")
db_pwd=$(ynh_app_setting_get "$app" mysqlpwd)
# Rename the database
rename_mysql_db "$curr_dbname" "$curr_dbuser" "$db_pwd" "$db_name"
ynh_replace_string "^(\s*'dbname' =>).*," "\1 '${db_name}'," "$nc_conf"
ynh_replace_string "#^(\s*'dbuser' =>).*," "${db_name}'," "$nc_conf"
fi
#=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
#=================================================
# Made a backup only after the version 11.0.0
# Before, the datas will be always saved.
# Get the current version number of nextcloud/owncloud
current_version=$(grep OC_VersionString "$final_path/version.php" | cut -d\' -f2)
if [ "$current_version" \> "11.0.0" ]
then
# Inform the backup/restore process that it should not save the data directory
ynh_app_setting_set $app backup_core_only 1
# Backup the current version of the app
ynh_backup_before_upgrade
ynh_clean_setup () {
# restore it if the upgrade fails
ynh_restore_upgradebackup
}
fi
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
#=================================================
# MIGRATE FROM OWNCLOUD TO NEXTCLOUD
#=================================================
# If the name of the app is not nextcloud ($migration_name), check if it's a migration from owncloud
if [ $YNH_APP_ID != $migration_name ]
then
[ $YNH_APP_ID == owncloud ] \
|| ynh_die "Incompatible application to migrate to Nextcloud"
# Prepare owcloud to migrate to nextcloud
./upgrade.d/owncloud.sh
fi
#=================================================
# CHECK THE PATH
#=================================================
# Normalize the URL path syntax
path_url=$(ynh_normalize_url_path $path_url)
#=================================================
# STANDARD UPGRADE STEPS
#=================================================
# NGINX CONFIGURATION
#=================================================
# Delete current nginx configuration to be able to check if .well-known is already served.
ynh_remove_nginx_config
ynh_app_setting_delete $app "checksum_etc_nginx_conf.d_$domain.d_$app.conf"
# Do not serve .well-known if it's already served on the domain
if is_url_handled "https://${domain}/.well-known/caldav" ; then
sed -ri '/^location = \/\.well\-known\/(caldav|carddav) \{/,/\}/d' \
"../conf/nginx.conf"
fi
# Create a dedicated nginx config
ynh_add_nginx_config
#=================================================
# CREATE DEDICATED USER
#=================================================
# Create a system user
ynh_system_user_create $app
#=================================================
# PHP-FPM CONFIGURATION
#=================================================
# Create a dedicated php-fpm config
ynh_add_fpm_config
#=================================================
# UPGRADE DEPENDENCIES
#=================================================
ynh_install_app_dependencies $dependencies
#=================================================
# SPECIFIC UPGRADE
#=================================================
# MAKE SEQUENTIAL UPGRADES FROM EACH MAJOR
# VERSION TO THE FOLLOWING
#=================================================
# Load the last available version
source upgrade.d/upgrade.last.sh
last_version=$next_version
# Print the current version number of nextcloud
exec_occ -V
# While the current version is not the last version, do a upgrade
while [ "$last_version" != "$current_version" ]
do
# The major version is the first part of the version number
major_version=${next_version%%.*}
current_major_version=${current_version%%.*}
# If the current version have the same major version than the next.
# It's the last upgrade to do
if [ "$major_version" -eq "$current_major_version" ]; then
current_major_version=last
fi
# Load the value for this version
source upgrade.d/upgrade.$current_major_version.sh
echo -e "\nUpdate to nextcloud $next_version" >&2
# Create an app.src for this version of nextcloud
cp ../conf/app.src.default ../conf/app.src
ynh_replace_string "__VERSION__" "$next_version" "../conf/app.src"
ynh_replace_string "__SHA256_SUM__" "$nextcloud_source_sha256" "../conf/app.src"
# Create a temporary directory
tmpdir=$(mktemp -d)
# Install the next nextcloud version in $tmpdir
ynh_setup_source "$tmpdir"
# Enable maintenance mode
exec_occ maintenance:mode --on
# Backup the config file in the temp dir
cp -a "$final_path/config/config.php" "$tmpdir/config/config.php"
# Backup 3rd party applications from the current nextcloud
# But do not overwrite if there any upgrade
cp -a --update "$final_path/apps" "$tmpdir/apps"
# Replace the old nextcloud by the new one
ynh_secure_remove "$final_path"
mv "$tmpdir" "$final_path"
ls -alh "$final_path" >&2
# Set write access for the following commands
chown -R $app: "$final_path" "$datadir"
# Upgrade Nextcloud (SUCCESS = 0, UP_TO_DATE = 3)
exec_occ maintenance:mode --off
exec_occ upgrade \
|| ([[ $? -eq 3 ]] || ynh_die "Unable to upgrade Nextcloud")
# Get the new current version number
current_version=$(grep OC_VersionString "$final_path/version.php" | cut -d\' -f2)
current_major_version=${current_version%%.*}
# Print the current version number of nextcloud
exec_occ -V
done
#=================================================
# CONFIGURE NEXTCLOUD
#=================================================
# Verify the checksum and backup the file if it's different
ynh_backup_if_checksum_is_different "${final_path}/config/config.php"
nc_conf="${final_path}/config.json"
cp ../conf/config.json "$nc_conf"
ynh_replace_string "#DOMAIN#" "$domain" "$nc_conf"
ynh_replace_string "#DATADIR#" "$datadir" "$nc_conf"
# Ensure that UpdateNotification app is disabled
exec_occ app:disable updatenotification
# Enable plugins
exec_occ app:enable user_ldap
# Load the config file in nextcloud
exec_occ config:import "$nc_conf"
# Then remove it
rm -f "$nc_conf"
#=================================================
# ALLOW USERS TO DISCONNECT FROM NEXTCLOUD
#=================================================
# Add dynamic logout URL to the config
exec_occ config:system:get logout_url >/dev/null 2>&1 \
|| echo "
//-YunoHost-
// set logout_url according to main domain
\$main_domain = exec('cat /etc/yunohost/current_host');
\$CONFIG['logout_url'] = 'https://'.\$main_domain.'/yunohost/sso/?action=logout';
//-YunoHost-
" >> "${final_path}/config/config.php"
#=================================================
# MOUNT HOME FOLDERS AS EXTERNAL STORAGE
#=================================================
# Enable External Storage and create local mount to home folder as needed
if [ $user_home -eq 1 ]; then
exec_occ app:enable files_external
exec_occ files_external:list --output=json \
| grep -q '"storage":"\\\\OC\\\\Files\\\\Storage\\\\Local"' \
|| create_home_external_storage
fi
#=================================================
# STORE THE CHECKSUM OF THE CONFIG FILE
#=================================================
# Calculate and store the config file checksum into the app settings
ynh_store_file_checksum "${final_path}/config/config.php"
#=================================================
# UPDATE THE CRON JOB
#=================================================
cron_path="/etc/cron.d/$app"
cp -a ../conf/nextcloud.cron "$cron_path"
ynh_replace_string "#USER#" "$app" "$cron_path"
ynh_replace_string "#DESTDIR#" "$final_path" "$cron_path"
exec_occ background:cron
#=================================================
# UPDATE THE HOOK FILE FOR USER CREATE
#=================================================
# Set system group in hooks
ynh_replace_string "#GROUP#" "$app" ../hooks/post_user_create
#=================================================
# GENERIC FINALIZATION
#=================================================
# SECURE FILES AND DIRECTORIES
#=================================================
# Fix app ownerships & permissions
chown -R $app: "$final_path" "$datadir"
find ${final_path}/ -type f -print0 | xargs -0 chmod 0644
find ${final_path}/ -type d -print0 | xargs -0 chmod 0755
find ${datadir}/ -type f -print0 | xargs -0 chmod 0640
find ${datadir}/ -type d -print0 | xargs -0 chmod 0750
chmod 640 "${final_path}/config/config.php"
chmod 755 /home/yunohost.app
# Iterate over users to extend their home folder permissions - for the external
# storage plugin usage - and create relevant Nextcloud directories
for u in $(ynh_user_list); do
mkdir -p "${datadir}/${u}"
setfacl -m g:$app:rwx "/home/$u" || true
done
#=================================================
# ADD A JOB FOR FINISHING UPGRADING OWNCLOUD
#=================================================
# Finish ownCloud migration
IF [ $YNH_APP_ID == owncloud ]; then
echo "ownCloud has been successfully migrated to Nextcloud! \
A last scheduled operation will run in a couple of minutes to finish the \
migration in YunoHost side. Do not proceed any application operation while \
you don't see Nextcloud as installed." >&2
# Install a cron job and script for final migration step
script_path="/usr/local/sbin/owncloud-migration.sh"
ynh_replace_string "#APP#" "$YNH_APP_ID" ../conf/owncloud-migration.sh
cp ../conf/owncloud-migration.sh "$script_path"
chmod 755 "$script_path"
cron_path="/etc/cron.d/owncloud-migration"
echo "*/1 * * * * root $script_path" | tee "$cron_path" >/dev/null
chmod 644 "$cron_path"
fi
#=================================================
# WARNING ABOUT THIRD-PARTY APPS
#=================================================
# Warn about possible disabled apps
echo "Note that if you've installed some third-parties Nextcloud applications, \
they are probably disabled and you'll have to manually activate them again." >&2
#=================================================
# SETUP LOGROTATE
#=================================================
# Use logrotate to manage app-specific logfile(s)
ynh_use_logrotate
#=================================================
# GENERIC FINALIZATION
#=================================================
# SETUP SSOWAT
#=================================================
ynh_app_setting_set $app unprotected_uris "/"
ynh_app_setting_set $app skipped_regex \
"$(sed 's/[\.\-]/\%&/g' <<< $domain)/%.well%-known/.*"
#=================================================
# RELOAD NGINX
#=================================================
systemctl reload nginx