" mail_to_send 137 | ynh_replace_string "__PRE_TAG2__" "<\pre>" mail_to_send 138 | 139 | # Insert finishing HTML tags 140 | echo -e "\n\n" >> mail_to_send 141 | 142 | # Otherwise, remove tags to keep a plain text. 143 | else 144 | # Remove URL tags 145 | ynh_replace_string "__URL_TAG[1,3]__" "" mail_to_send 146 | ynh_replace_string "__URL_TAG2__" ": " mail_to_send 147 | 148 | # Remove PRE tags 149 | ynh_replace_string "__PRE_TAG[1-2]__" "" mail_to_send 150 | fi 151 | 152 | # Define binary to use for mail command 153 | if [ -e /usr/bin/bsd-mailx ] 154 | then 155 | local mail_bin=/usr/bin/bsd-mailx 156 | else 157 | local mail_bin=/usr/bin/mail.mailutils 158 | fi 159 | 160 | if [ "$admin_mail_html" -eq 1 ] 161 | then 162 | content_type="text/html" 163 | else 164 | content_type="text/plain" 165 | fi 166 | 167 | # Send the email to the recipients 168 | cat mail_to_send | $mail_bin -a "Content-Type: $content_type; charset=UTF-8" -s "$mail_subject" "$recipients" 169 | } 170 | 171 | #================================================= 172 | 173 | ynh_maintenance_mode_ON () { 174 | # Load value of $path_url and $domain from the config if their not set 175 | if [ -z $path_url ]; then 176 | path_url=$(ynh_app_setting_get $app path) 177 | fi 178 | if [ -z $domain ]; then 179 | domain=$(ynh_app_setting_get $app domain) 180 | fi 181 | 182 | mkdir -p /var/www/html/ 183 | 184 | # Create an html to serve as maintenance notice 185 | echo " 186 | 187 | 188 | 189 |Your app $app is currently under maintenance! 190 | 196 | 197 | 198 |Your app $app is currently under maintenance!
199 |This app has been put under maintenance by your administrator at $(date)
200 |Please wait until the maintenance operation is done. This page will be reloaded as soon as your app will be back.
201 | 202 | 203 | " > "/var/www/html/maintenance.$app.html" 204 | 205 | # Create a new nginx config file to redirect all access to the app to the maintenance notice instead. 206 | echo "# All request to the app will be redirected to ${path_url}_maintenance and fall on the maintenance notice 207 | rewrite ^${path_url}/(.*)$ ${path_url}_maintenance/? redirect; 208 | # Use another location, to not be in conflict with the original config file 209 | location ${path_url}_maintenance/ { 210 | alias /var/www/html/ ; 211 | 212 | try_files maintenance.$app.html =503; 213 | 214 | # Include SSOWAT user panel. 215 | include conf.d/yunohost_panel.conf.inc; 216 | }" > "/etc/nginx/conf.d/$domain.d/maintenance.$app.conf" 217 | 218 | # The current config file will redirect all requests to the root of the app. 219 | # To keep the full path, we can use the following rewrite rule: 220 | # rewrite ^${path_url}/(.*)$ ${path_url}_maintenance/\$1? redirect; 221 | # The difference will be in the $1 at the end, which keep the following queries. 222 | # But, if it works perfectly for a html request, there's an issue with any php files. 223 | # This files are treated as simple files, and will be downloaded by the browser. 224 | # Would be really be nice to be able to fix that issue. So that, when the page is reloaded after the maintenance, the user will be redirected to the real page he was. 225 | 226 | systemctl reload nginx 227 | } 228 | 229 | ynh_maintenance_mode_OFF () { 230 | # Load value of $path_url and $domain from the config if their not set 231 | if [ -z $path_url ]; then 232 | path_url=$(ynh_app_setting_get $app path) 233 | fi 234 | if [ -z $domain ]; then 235 | domain=$(ynh_app_setting_get $app domain) 236 | fi 237 | 238 | # Rewrite the nginx config file to redirect from ${path_url}_maintenance to the real url of the app. 239 | echo "rewrite ^${path_url}_maintenance/(.*)$ ${path_url}/\$1 redirect;" > "/etc/nginx/conf.d/$domain.d/maintenance.$app.conf" 240 | systemctl reload nginx 241 | 242 | # Sleep 4 seconds to let the browser reload the pages and redirect the user to the app. 243 | sleep 4 244 | 245 | # Then remove the temporary files used for the maintenance. 246 | rm "/var/www/html/maintenance.$app.html" 247 | rm "/etc/nginx/conf.d/$domain.d/maintenance.$app.conf" 248 | 249 | systemctl reload nginx 250 | } 251 | 252 | #================================================= 253 | 254 | # Create a changelog for an app after an upgrade. 255 | # 256 | # The changelog is printed into the file ./changelog for the time of the upgrade. 257 | # 258 | # In order to create a changelog, ynh_app_changelog will get info from /etc/yunohost/apps/$app/status.json 259 | # In order to find the current commit use by the app. 260 | # The remote repository, and the branch. 261 | # The changelog will be only the commits since the current revision. 262 | # 263 | # Because of the need of those info, ynh_app_changelog works only 264 | # with apps that have been installed from a list. 265 | # 266 | # usage: ynh_app_changelog 267 | ynh_app_changelog () { 268 | get_value_from_settings () 269 | { 270 | local value="$1" 271 | # Extract a value from the status.json file of an installed app. 272 | 273 | grep "$value\": \"" /etc/yunohost/apps/$app/status.json | sed "s/.*$value\": \"\([^\"]*\).*/\1/" 274 | } 275 | 276 | local current_revision="$(get_value_from_settings revision)" 277 | local repo="$(get_value_from_settings url)" 278 | local branch="$(get_value_from_settings branch)" 279 | # ynh_app_changelog works only with an app installed from a list. 280 | if [ -z "$current_revision" ] || [ -z "$repo" ] || [ -z "$branch" ] 281 | then 282 | ynh_print_warn "Unable to build the changelog..." 283 | touch changelog 284 | return 0 285 | fi 286 | 287 | # Fetch the history of the repository, without cloning it 288 | mkdir git_history 289 | (cd git_history 290 | ynh_exec_warn_less git init 291 | ynh_exec_warn_less git remote add -f origin $repo 292 | # Get the line of the current commit of the installed app in the history. 293 | local line_to_head=$(git log origin/$branch --pretty=oneline | grep --line-number "$current_revision" | cut -d':' -f1) 294 | # Cut the history before the current commit, to keep only newer commits. 295 | # Then use sed to reorganise each lines and have a nice list of commits since the last upgrade. 296 | # This list is redirected into the file changelog 297 | git log origin/$branch --pretty=oneline | head --lines=$(($line_to_head-1)) | sed 's/^\([[:alnum:]]*\)\(.*\)/*(\1) -> \2/g' > ../changelog) 298 | # Remove 'Merge pull request' commits 299 | sed -i '/Merge pull request #[[:digit:]]* from/d' changelog 300 | # As well as conflict resolving commits 301 | sed -i '/Merge branch .* into/d' changelog 302 | 303 | # Get the value of admin_mail_html 304 | admin_mail_html=$(ynh_app_setting_get $app admin_mail_html) 305 | admin_mail_html="${admin_mail_html:-0}" 306 | 307 | # If a html email is required. Apply html to the changelog. 308 | if [ "$admin_mail_html" -eq 1 ] 309 | then 310 | sed -in-place "s@\*(\([[:alnum:]]*\)) -> \(.*\)@* __URL_TAG1__\2__URL_TAG2__${repo}/commit/\1__URL_TAG3__@g" changelog 311 | fi 312 | } 313 | 314 | #================================================= 315 | 316 | # Check the amount of available RAM 317 | # 318 | # usage: ynh_check_ram [--required=RAM required in Mb] [--no_swap|--only_swap] [--free_ram] 319 | # | arg: -r, --required= - Amount of RAM required in Mb. The helper will return 0 is there's enough RAM, or 1 otherwise. 320 | # If --required isn't set, the helper will print the amount of RAM, in Mb. 321 | # | arg: -s, --no_swap - Ignore swap 322 | # | arg: -o, --only_swap - Ignore real RAM, consider only swap. 323 | # | arg: -f, --free_ram - Count only free RAM, not the total amount of RAM available. 324 | ynh_check_ram () { 325 | # Declare an array to define the options of this helper. 326 | declare -Ar args_array=( [r]=required= [s]=no_swap [o]=only_swap [f]=free_ram ) 327 | local required 328 | local no_swap 329 | local only_swap 330 | # Manage arguments with getopts 331 | ynh_handle_getopts_args "$@" 332 | required=${required:-} 333 | no_swap=${no_swap:-0} 334 | only_swap=${only_swap:-0} 335 | 336 | local total_ram=$(vmstat --stats --unit M | grep "total memory" | awk '{print $1}') 337 | local total_swap=$(vmstat --stats --unit M | grep "total swap" | awk '{print $1}') 338 | local total_ram_swap=$(( total_ram + total_swap )) 339 | 340 | local free_ram=$(vmstat --stats --unit M | grep "free memory" | awk '{print $1}') 341 | local free_swap=$(vmstat --stats --unit M | grep "free swap" | awk '{print $1}') 342 | local free_ram_swap=$(( free_ram + free_swap )) 343 | 344 | # Use the total amount of ram 345 | local ram=$total_ram_swap 346 | if [ $free_ram -eq 1 ] 347 | then 348 | # Use the total amount of free ram 349 | ram=$free_ram_swap 350 | if [ $no_swap -eq 1 ] 351 | then 352 | # Use only the amount of free ram 353 | ram=$free_ram 354 | elif [ $only_swap -eq 1 ] 355 | then 356 | # Use only the amount of free swap 357 | ram=$free_swap 358 | fi 359 | else 360 | if [ $no_swap -eq 1 ] 361 | then 362 | # Use only the amount of free ram 363 | ram=$total_ram 364 | elif [ $only_swap -eq 1 ] 365 | then 366 | # Use only the amount of free swap 367 | ram=$total_swap 368 | fi 369 | fi 370 | 371 | if [ -n "$required" ] 372 | then 373 | # Return 1 if the amount of ram isn't enough. 374 | if [ $ram -lt $required ] 375 | then 376 | return 1 377 | else 378 | return 0 379 | fi 380 | 381 | # If no RAM is required, return the amount of available ram. 382 | else 383 | echo $ram 384 | fi 385 | } 386 | -------------------------------------------------------------------------------- /scripts/actions/clean_backups: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | #================================================= 4 | # GENERIC STARTING 5 | #================================================= 6 | # IMPORT GENERIC HELPERS 7 | #================================================= 8 | 9 | source scripts/_common.sh 10 | source /usr/share/yunohost/helpers 11 | 12 | #================================================= 13 | # MANAGE SCRIPT FAILURE 14 | #================================================= 15 | 16 | # Exit if an error occurs during the execution of the script 17 | ynh_abort_if_errors 18 | 19 | #================================================= 20 | # RETRIEVE ARGUMENTS 21 | #================================================= 22 | 23 | app=${YNH_APP_INSTANCE_NAME:-$YNH_APP_ID} 24 | 25 | final_path=$(ynh_app_setting_get --app=$app --key=final_path) 26 | install_type=$(ynh_app_setting_get --app=$app --key=install_type) 27 | 28 | #================================================= 29 | # CHECK IF ARGUMENTS ARE CORRECT 30 | #================================================= 31 | 32 | #================================================= 33 | # CHECK IF AN ACTION HAS TO BE DONE 34 | #================================================= 35 | 36 | if [ "$install_type" == "fallback" ] 37 | then 38 | ynh_die --message="This action can be performed only on a Main server type installation of the app." --ret_code=0 39 | fi 40 | 41 | #================================================= 42 | # SPECIFIC ACTION 43 | #================================================= 44 | # CLEAN ALL BACKUP FILES 45 | #================================================= 46 | ynh_script_progression --message="Cleaning all backup files" --weight=9 47 | 48 | # Get the backup directory from the config file 49 | backup_dir="$(grep "^main_storage_dir=" "$final_path/send_process/config.conf" | cut -d= -f2)" 50 | 51 | while read backup_file <&3 52 | do 53 | if [ -n "$backup_file" ] 54 | then 55 | ynh_print_info --message="Clean backup file $backup_file" 56 | ynh_secure_remove --file="$backup_dir/backup/$backup_file" 57 | fi 58 | done 3<<< $(ls -1 "$backup_dir/backup" | grep ".tar.gz$") 59 | 60 | #================================================= 61 | # END OF SCRIPT 62 | #================================================= 63 | 64 | ynh_script_progression --message="Execution completed" --last 65 | -------------------------------------------------------------------------------- /scripts/actions/close_fallback: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | #================================================= 4 | # GENERIC STARTING 5 | #================================================= 6 | # IMPORT GENERIC HELPERS 7 | #================================================= 8 | 9 | source scripts/_common.sh 10 | source /usr/share/yunohost/helpers 11 | 12 | #================================================= 13 | # MANAGE SCRIPT FAILURE 14 | #================================================= 15 | 16 | # Exit if an error occurs during the execution of the script 17 | ynh_abort_if_errors 18 | 19 | #================================================= 20 | # RETRIEVE ARGUMENTS 21 | #================================================= 22 | 23 | app=${YNH_APP_INSTANCE_NAME:-$YNH_APP_ID} 24 | 25 | final_path=$(ynh_app_setting_get --app=$app --key=final_path) 26 | install_type=$(ynh_app_setting_get --app=$app --key=install_type) 27 | 28 | #================================================= 29 | # CHECK IF ARGUMENTS ARE CORRECT 30 | #================================================= 31 | 32 | #================================================= 33 | # CHECK IF AN ACTION HAS TO BE DONE 34 | #================================================= 35 | 36 | if [ "$install_type" == "main" ] 37 | then 38 | ynh_die --message="This action can be performed only on a Fallback server type installation of the app." --ret_code=0 39 | fi 40 | 41 | #================================================= 42 | # SPECIFIC ACTION 43 | #================================================= 44 | # FORCE A NEW BACKUP 45 | #================================================= 46 | ynh_script_progression --message="Closing the fallback..." --weight=9 47 | 48 | if IS_PACKAGE_CHECK 49 | then 50 | ynh_print_info --message="Do not alter the status of the fallback in Package_check." 51 | else 52 | ynh_exec_warn $final_path/deploy_process/close_fallback.sh auto 53 | fi 54 | 55 | #================================================= 56 | # END OF SCRIPT 57 | #================================================= 58 | 59 | ynh_script_progression --message="Execution completed" --last 60 | -------------------------------------------------------------------------------- /scripts/actions/deploy_fallback: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | #================================================= 4 | # GENERIC STARTING 5 | #================================================= 6 | # IMPORT GENERIC HELPERS 7 | #================================================= 8 | 9 | source scripts/_common.sh 10 | source /usr/share/yunohost/helpers 11 | 12 | #================================================= 13 | # MANAGE SCRIPT FAILURE 14 | #================================================= 15 | 16 | # Exit if an error occurs during the execution of the script 17 | ynh_abort_if_errors 18 | 19 | #================================================= 20 | # RETRIEVE ARGUMENTS 21 | #================================================= 22 | 23 | app=${YNH_APP_INSTANCE_NAME:-$YNH_APP_ID} 24 | 25 | final_path=$(ynh_app_setting_get --app=$app --key=final_path) 26 | install_type=$(ynh_app_setting_get --app=$app --key=install_type) 27 | 28 | #================================================= 29 | # CHECK IF ARGUMENTS ARE CORRECT 30 | #================================================= 31 | 32 | #================================================= 33 | # CHECK IF AN ACTION HAS TO BE DONE 34 | #================================================= 35 | 36 | if [ "$install_type" == "main" ] 37 | then 38 | ynh_die --message="This action can be performed only on a Fallback server type installation of the app." --ret_code=0 39 | fi 40 | 41 | #================================================= 42 | # SPECIFIC ACTION 43 | #================================================= 44 | # FORCE A NEW BACKUP 45 | #================================================= 46 | ynh_script_progression --message="Deploying the fallback..." --weight=9 47 | 48 | if IS_PACKAGE_CHECK 49 | then 50 | ynh_print_info --message="Do not actually deploy the fallback in Package_check." 51 | else 52 | ynh_exec_warn $final_path/deploy_process/deploy_fallback.sh auto 53 | fi 54 | 55 | #================================================= 56 | # END OF SCRIPT 57 | #================================================= 58 | 59 | ynh_script_progression --message="Execution completed" --last 60 | -------------------------------------------------------------------------------- /scripts/actions/force_backup: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | #================================================= 4 | # GENERIC STARTING 5 | #================================================= 6 | # IMPORT GENERIC HELPERS 7 | #================================================= 8 | 9 | source scripts/_common.sh 10 | source /usr/share/yunohost/helpers 11 | 12 | #================================================= 13 | # MANAGE SCRIPT FAILURE 14 | #================================================= 15 | 16 | # Exit if an error occurs during the execution of the script 17 | ynh_abort_if_errors 18 | 19 | #================================================= 20 | # RETRIEVE ARGUMENTS 21 | #================================================= 22 | 23 | app=${YNH_APP_INSTANCE_NAME:-$YNH_APP_ID} 24 | 25 | final_path=$(ynh_app_setting_get --app=$app --key=final_path) 26 | install_type=$(ynh_app_setting_get --app=$app --key=install_type) 27 | 28 | #================================================= 29 | # CHECK IF ARGUMENTS ARE CORRECT 30 | #================================================= 31 | 32 | #================================================= 33 | # CHECK IF AN ACTION HAS TO BE DONE 34 | #================================================= 35 | 36 | if [ "$install_type" == "fallback" ] 37 | then 38 | ynh_die --message="This action can be performed only on a Main server type installation of the app." --ret_code=0 39 | fi 40 | 41 | #================================================= 42 | # SPECIFIC ACTION 43 | #================================================= 44 | # FORCE A NEW BACKUP 45 | #================================================= 46 | ynh_script_progression --message="Forcing a new backup..." --weight=9 47 | 48 | ynh_exec_warn nice -n10 $final_path/send_process/send_backup.sh 49 | 50 | #================================================= 51 | # END OF SCRIPT 52 | #================================================= 53 | 54 | ynh_script_progression --message="Execution completed" --last 55 | -------------------------------------------------------------------------------- /scripts/actions/reset_default_config: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | #================================================= 4 | # GENERIC STARTING 5 | #================================================= 6 | # IMPORT GENERIC HELPERS 7 | #================================================= 8 | 9 | source scripts/_common.sh 10 | source /usr/share/yunohost/helpers 11 | 12 | #================================================= 13 | # MANAGE SCRIPT FAILURE 14 | #================================================= 15 | 16 | # Exit if an error occurs during the execution of the script 17 | ynh_abort_if_errors 18 | 19 | #================================================= 20 | # RETRIEVE ARGUMENTS 21 | #================================================= 22 | 23 | app=${YNH_APP_INSTANCE_NAME:-$YNH_APP_ID} 24 | 25 | final_path=$(ynh_app_setting_get --app=$app --key=final_path) 26 | ssh_host=$(ynh_app_setting_get --app=$app --key=ssh_host) 27 | ssh_port=$(ynh_app_setting_get --app=$app --key=ssh_port) 28 | encrypt=$(ynh_app_setting_get --app=$app --key=encrypt) 29 | delay_before_incident=$(ynh_app_setting_get --app=$app --key=delay_before_incident) 30 | contact_mail=$(ynh_app_setting_get --app=$app --key=contact_mail) 31 | auto_deploy=$(ynh_app_setting_get --app=$app --key=auto_deploy) 32 | install_type=$(ynh_app_setting_get --app=$app --key=install_type) 33 | 34 | #================================================= 35 | # SORT OUT THE CONFIG FILE TO HANDLE 36 | #================================================= 37 | 38 | file="$1" 39 | 40 | if [ "$file" = "both" ]; then 41 | if [ "$install_type" = "main" ]; then 42 | config_file="$final_path/send_process/config.conf" 43 | else 44 | config_file="$final_path/deploy_process/auto_check.conf" 45 | fi 46 | fi 47 | 48 | #================================================= 49 | # SPECIFIC ACTION 50 | #================================================= 51 | # RESET THE CONFIG FILE 52 | #================================================= 53 | ynh_script_progression --message="Reseting the config file..." 54 | 55 | # Verify the checksum and backup the file if it's different 56 | ynh_backup_if_checksum_is_different --file="$config_file" 57 | 58 | if [ "$file" = "both" ] 59 | then 60 | if [ "$install_type" = "main" ] 61 | then 62 | # Get the default file and overwrite the current config 63 | cp "${config_file}.modele" "$config_file" 64 | 65 | # Recreate the default config 66 | ynh_replace_string --match_string="ssh_user=.*" --replace_string="ssh_user=$app" --target_file="$config_file" 67 | ynh_replace_string --match_string="ssh_host=.*" --replace_string="ssh_host=$ssh_host" --target_file="$config_file" 68 | ynh_replace_string --match_string="ssh_port=.*" --replace_string="ssh_port=$ssh_port" --target_file="$config_file" 69 | 70 | data_dir="/home/yunohost.app/$app/fallback_backup" 71 | ynh_replace_string --match_string="main_storage_dir=.*" --replace_string="main_storage_dir=$data_dir" --target_file="$config_file" 72 | ynh_replace_string --match_string="encrypt=.*" --replace_string="encrypt=$encrypt" --target_file="$config_file" 73 | 74 | ssh_key="$final_path/send_process/ssh_key" 75 | ynh_replace_string --match_string="ssh_key=.*" --replace_string="ssh_key=$ssh_key" --target_file="$config_file" 76 | 77 | passkey="$final_path/send_process/passkey" 78 | ynh_replace_string --match_string="pass_file=.*" --replace_string="pass_file=$passkey" --target_file="$config_file" 79 | else 80 | # Get the default file and overwrite the current config 81 | cp "${config_file}.modele" "$config_file" 82 | 83 | # Recreate the default config 84 | ynh_replace_string --match_string="delay_before_incident=.*" --replace_string="delay_before_incident=$delay_before_incident" --target_file="$config_file" 85 | ynh_replace_string --match_string="contact_mail=.*" --replace_string="contact_mail=$contact_mail" --target_file="$config_file" 86 | ynh_replace_string --match_string="auto_deploy=.*" --replace_string="auto_deploy=$auto_deploy" --target_file="$config_file" 87 | ynh_replace_string --match_string="__FINAL_PATH__" --replace_string="$final_path" --target_file="$config_file" 88 | fi 89 | fi 90 | 91 | # Calculate and store the config file checksum into the app settings 92 | ynh_store_file_checksum --file="$config_file" 93 | 94 | #================================================= 95 | # END OF SCRIPT 96 | #================================================= 97 | 98 | ynh_script_progression --message="Execution completed" --last 99 | -------------------------------------------------------------------------------- /scripts/actions/update_dns: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | #================================================= 4 | # GENERIC STARTING 5 | #================================================= 6 | # IMPORT GENERIC HELPERS 7 | #================================================= 8 | 9 | source scripts/_common.sh 10 | source /usr/share/yunohost/helpers 11 | 12 | #================================================= 13 | # MANAGE SCRIPT FAILURE 14 | #================================================= 15 | 16 | # Exit if an error occurs during the execution of the script 17 | ynh_abort_if_errors 18 | 19 | #================================================= 20 | # RETRIEVE ARGUMENTS 21 | #================================================= 22 | 23 | app=${YNH_APP_INSTANCE_NAME:-$YNH_APP_ID} 24 | 25 | final_path=$(ynh_app_setting_get --app=$app --key=final_path) 26 | install_type=$(ynh_app_setting_get --app=$app --key=install_type) 27 | 28 | #================================================= 29 | # CHECK IF ARGUMENTS ARE CORRECT 30 | #================================================= 31 | 32 | #================================================= 33 | # CHECK IF AN ACTION HAS TO BE DONE 34 | #================================================= 35 | 36 | if [ "$install_type" == "main" ] 37 | then 38 | ynh_die --message="This action can be performed only on a Fallback server type installation of the app." --ret_code=0 39 | fi 40 | 41 | if [ $(grep "^auto_update_DNS=" "$final_path/deploy_process/auto_check.conf" | cut -d'=' -f2) -ne 1 ] 42 | then 43 | ynh_die --message="Please set the setting auto_update_DNS to 1 into your $final_path/deploy_process/auto_check.conf in order to use this action." --ret_code=0 44 | fi 45 | 46 | #================================================= 47 | # SPECIFIC ACTION 48 | #================================================= 49 | # GET DNS CONFIGURATION 50 | #================================================= 51 | ynh_script_progression --message="Checking DNS configuration of the fallback..." --weight=1 52 | 53 | # And the script to use to do it 54 | auto_update_script=$(grep "^auto_update_script=" "$final_path/deploy_process/auto_check.conf" | cut -d'=' -f2) 55 | 56 | if [ "auto_update_script" == "$final_path/DynHost/..." ] 57 | then 58 | ynh_die --message="Your current configuration in \"$final_path/deploy_process/auto_check.conf\" does not specify the script to use to update your DNS. Please configure a script to use and retry." --ret_code=0 59 | fi 60 | 61 | #================================================= 62 | # FORCE A NEW BACKUP 63 | #================================================= 64 | ynh_script_progression --message="Updating the DNS..." --weight=9 65 | 66 | ynh_exec_warn "$auto_update_script" 67 | 68 | #================================================= 69 | # END OF SCRIPT 70 | #================================================= 71 | 72 | ynh_script_progression --message="Execution completed" --last 73 | -------------------------------------------------------------------------------- /scripts/actions/update_from_fallback: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | #================================================= 4 | # GENERIC STARTING 5 | #================================================= 6 | # IMPORT GENERIC HELPERS 7 | #================================================= 8 | 9 | source scripts/_common.sh 10 | source /usr/share/yunohost/helpers 11 | 12 | #================================================= 13 | # MANAGE SCRIPT FAILURE 14 | #================================================= 15 | 16 | # Exit if an error occurs during the execution of the script 17 | ynh_abort_if_errors 18 | 19 | #================================================= 20 | # RETRIEVE ARGUMENTS 21 | #================================================= 22 | 23 | app=${YNH_APP_INSTANCE_NAME:-$YNH_APP_ID} 24 | 25 | final_path=$(ynh_app_setting_get --app=$app --key=final_path) 26 | install_type=$(ynh_app_setting_get --app=$app --key=install_type) 27 | 28 | #================================================= 29 | # CHECK IF ARGUMENTS ARE CORRECT 30 | #================================================= 31 | 32 | #================================================= 33 | # CHECK IF AN ACTION HAS TO BE DONE 34 | #================================================= 35 | 36 | if [ "$install_type" == "fallback" ] 37 | then 38 | ynh_die --message="This action can be performed only on a Main server type installation of the app." --ret_code=0 39 | fi 40 | 41 | #================================================= 42 | # SPECIFIC ACTION 43 | #================================================= 44 | # UPDATE THE SERVER FROM FALLBACK 45 | #================================================= 46 | ynh_script_progression --message="Updating the server from fallback..." --weight=9 47 | 48 | if IS_PACKAGE_CHECK 49 | then 50 | ynh_print_info --message="Do not alter the status of the fallback in Package_check." 51 | else 52 | ynh_exec_warn nice -n10 $final_path/update_from_fallback_process/update_from_fallback.sh auto 53 | fi 54 | 55 | #================================================= 56 | # END OF SCRIPT 57 | #================================================= 58 | 59 | ynh_script_progression --message="Execution completed" --last 60 | -------------------------------------------------------------------------------- /scripts/backup: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | #================================================= 4 | # GENERIC START 5 | #================================================= 6 | # IMPORT GENERIC HELPERS 7 | #================================================= 8 | 9 | # Keep this path for calling _common.sh inside the execution's context of backup and restore scripts 10 | source ../settings/scripts/_common.sh 11 | source /usr/share/yunohost/helpers 12 | 13 | #================================================= 14 | # MANAGE SCRIPT FAILURE 15 | #================================================= 16 | 17 | # Exit if an error occurs during the execution of the script 18 | ynh_abort_if_errors 19 | 20 | #================================================= 21 | # LOAD SETTINGS 22 | #================================================= 23 | ynh_print_info --message="Loading installation settings..." 24 | 25 | app=$YNH_APP_INSTANCE_NAME 26 | 27 | install_type=$(ynh_app_setting_get --app=$app --key=install_type) 28 | final_path=$(ynh_app_setting_get --app=$app --key=final_path) 29 | auto_detect_failure=$(ynh_app_setting_get --app=$app --key=auto_detect_failure) 30 | datadir=$(ynh_app_setting_get --app=$app --key=datadir) 31 | 32 | #================================================= 33 | # DECLARE DATA AND CONF FILES TO BACKUP 34 | #================================================= 35 | ynh_print_info --message="Declaring files to be backed up..." 36 | 37 | #================================================= 38 | # BACKUP THE APP MAIN DIR 39 | #================================================= 40 | 41 | ynh_backup --src_path="$final_path" 42 | 43 | #================================================= 44 | # BACKUP THE DATA DIR 45 | #================================================= 46 | 47 | ynh_backup --src_path="$datadir" --is_big 48 | 49 | #================================================= 50 | # SPECIFIC BACKUP 51 | #================================================= 52 | 53 | if [ "$install_type" == "main" ] 54 | then 55 | 56 | #================================================= 57 | # BACKUP LOGROTATE 58 | #================================================= 59 | 60 | ynh_backup --src_path="/etc/logrotate.d/$app" 61 | 62 | #================================================= 63 | # BACKUP THE CRON FILE 64 | #================================================= 65 | 66 | ynh_backup --src_path="/etc/cron.d/$app" 67 | else 68 | #================================================= 69 | # BACKUP USER DIRECTORIES 70 | #================================================= 71 | 72 | ynh_backup --src_path="/home/$app/bin" 73 | ynh_backup --src_path="/home/$app/lib" 74 | ynh_backup --src_path="/home/$app/lib64" 75 | ynh_backup --src_path="/home/$app/.ssh" 76 | 77 | #================================================= 78 | # BACKUP THE CRON FILE 79 | #================================================= 80 | 81 | if [ $auto_detect_failure -eq 1 ] 82 | then 83 | ynh_backup --src_path="/etc/cron.d/auto_deploy_fallback" 84 | fi 85 | fi 86 | 87 | #================================================= 88 | # END OF SCRIPT 89 | #================================================= 90 | 91 | ynh_print_info --message="Backup script completed for $app. (YunoHost will then actually copy those files to the archive)." 92 | -------------------------------------------------------------------------------- /scripts/config: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | #================================================= 4 | # GENERIC STARTING 5 | #================================================= 6 | # IMPORT GENERIC HELPERS 7 | #================================================= 8 | 9 | source _common.sh 10 | source /usr/share/yunohost/helpers 11 | 12 | #================================================= 13 | # RETRIEVE ARGUMENTS 14 | #================================================= 15 | 16 | app=${YNH_APP_INSTANCE_NAME:-$YNH_APP_ID} 17 | 18 | final_path=$(ynh_app_setting_get --app=$app --key=final_path) 19 | install_type=$(ynh_app_setting_get --app=$app --key=install_type) 20 | 21 | #================================================= 22 | # SPECIFIC CODE 23 | #================================================= 24 | # DECLARE GENERIC FUNCTION 25 | #================================================= 26 | 27 | config_file_main="$final_path/send_process/config.conf" 28 | config_file_fallback="$final_path/deploy_process/auto_check.conf" 29 | 30 | passkey="$final_path/send_process/passkey" 31 | 32 | get_config_value() { 33 | option_name="$1" 34 | config_file=$2 35 | # Get the value of this option in the config file 36 | grep "^$option_name=" "${!config_file}" 2> /dev/null | cut -d= -f2 37 | } 38 | 39 | #================================================= 40 | # LOAD VALUES 41 | #================================================= 42 | 43 | # Load the real value from the app config or elsewhere. 44 | # Then get the value from the form. 45 | # If the form has a value for a variable, take the value from the form, 46 | # Otherwise, keep the value from the app config. 47 | 48 | # Encryption 49 | old_encrypt="$(get_config_value encrypt config_file_main)" 50 | encrypt="${YNH_CONFIG_MAIN_SERVER_ENCRYPTION_ENCRYPT:-$old_encrypt}" 51 | if [ -z "$encrypt" ]; then 52 | encrypt=0 53 | fi 54 | # Encryption password 55 | old_encrypt_password="$(cat $passkey 2> /dev/null)" 56 | encrypt_password="${YNH_CONFIG_MAIN_SERVER_ENCRYPTION_ENCRYPTION_PWD:-$old_encrypt_password}" 57 | 58 | # ssh_host 59 | old_ssh_host="$(get_config_value ssh_host config_file_main)" 60 | ssh_host="${YNH_CONFIG_MAIN_SERVER_SSH_CONFIG_SSH_HOST:-$old_ssh_host}" 61 | # ssh_port 62 | old_ssh_port="$(get_config_value ssh_port config_file_main)" 63 | ssh_port="${YNH_CONFIG_MAIN_SERVER_SSH_CONFIG_SSH_PORT:-$old_ssh_port}" 64 | 65 | # pub_key 66 | old_pub_key="$(ynh_app_setting_get --app=$app --key=pub_key)" 67 | pub_key="${YNH_CONFIG_FALLBACK_SERVER_SSH_CONFIG_PUB_KEY:-$old_pub_key}" 68 | # auto_detect_failure 69 | old_auto_detect_failure=$(ynh_app_setting_get --app=$app --key=auto_detect_failure) 70 | auto_detect_failure="${YNH_CONFIG_FALLBACK_SERVER_AUTO_DEPLOY_AUTO_DETECT_FAILURE:-$old_auto_detect_failure}" 71 | if [ -z "$auto_detect_failure" ]; then 72 | auto_detect_failure=0 73 | fi 74 | # delay_before_incident 75 | old_delay_before_incident="$(get_config_value delay_before_incident config_file_fallback)" 76 | delay_before_incident="${YNH_CONFIG_FALLBACK_SERVER_AUTO_DEPLOY_DELAY_BEFORE_INCIDENT:-$old_delay_before_incident}" 77 | # contact_mail 78 | old_contact_mail="$(get_config_value contact_mail config_file_fallback)" 79 | contact_mail="${YNH_CONFIG_FALLBACK_SERVER_AUTO_DEPLOY_CONTACT_MAIL:-$old_contact_mail}" 80 | # auto_deploy 81 | old_auto_deploy="$(get_config_value auto_deploy config_file_fallback)" 82 | auto_deploy="${YNH_CONFIG_FALLBACK_SERVER_AUTO_DEPLOY_AUTO_DEPLOY:-$old_auto_deploy}" 83 | if [ -z "$auto_deploy" ]; then 84 | auto_deploy=0 85 | fi 86 | 87 | # Type of admin mail configuration 88 | old_admin_mail_html="$(ynh_app_setting_get --app=$app --key=admin_mail_html)" 89 | admin_mail_html="${YNH_CONFIG_GLOBAL_CONFIG_EMAIL_TYPE:-$old_admin_mail_html}" 90 | 91 | #================================================= 92 | # SHOW_CONFIG FUNCTION FOR 'SHOW' COMMAND 93 | #================================================= 94 | 95 | show_config() { 96 | # here you are supposed to read some config file/database/other then print the values 97 | # ynh_return "YNH_CONFIG_${PANEL_ID}_${SECTION_ID}_${OPTION_ID}=value" 98 | 99 | ynh_return "YNH_CONFIG_MAIN_SERVER_ENCRYPTION_ENCRYPT=$encrypt" 100 | ynh_return "YNH_CONFIG_MAIN_SERVER_ENCRYPTION_ENCRYPTION_PWD=" 101 | 102 | ynh_return "YNH_CONFIG_MAIN_SERVER_SSH_CONFIG_SSH_HOST=$ssh_host" 103 | ynh_return "YNH_CONFIG_MAIN_SERVER_SSH_CONFIG_SSH_PORT=$ssh_port" 104 | 105 | ynh_return "YNH_CONFIG_FALLBACK_SERVER_SSH_CONFIG_PUB_KEY=$pub_key" 106 | ynh_return "YNH_CONFIG_FALLBACK_SERVER_AUTO_DEPLOY_AUTO_DETECT_FAILURE=$auto_detect_failure" 107 | ynh_return "YNH_CONFIG_FALLBACK_SERVER_AUTO_DEPLOY_DELAY_BEFORE_INCIDENT=$delay_before_incident" 108 | ynh_return "YNH_CONFIG_FALLBACK_SERVER_AUTO_DEPLOY_CONTACT_MAIL=$contact_mail" 109 | ynh_return "YNH_CONFIG_FALLBACK_SERVER_AUTO_DEPLOY_AUTO_DEPLOY=$auto_deploy" 110 | 111 | ynh_return "YNH_CONFIG_GLOBAL_CONFIG_EMAIL_TYPE=$admin_mail_html" 112 | } 113 | 114 | #================================================= 115 | # MODIFY THE CONFIGURATION 116 | #================================================= 117 | 118 | apply_config() { 119 | if [ "$install_type" = "main" ] 120 | then 121 | # Change the password if needed 122 | if [ "$encrypt" = "1" ] 123 | then 124 | test -n "$encrypt_password" || ynh_die --message="The password for encryption can't be empty if you choose to enable encryption." 125 | # Replace the password by the previous one 126 | passkey="$final_path/send_process/passkey" 127 | echo "$encrypt_password" > "$passkey" 128 | chmod 400 "$passkey" 129 | ynh_replace_string --match_string="^pass_file=.*" --replace_string="pass_file=$passkey" --target_file="$config_file_main" 130 | fi 131 | 132 | # Change encrypt in the config file 133 | ynh_replace_string --match_string="^encrypt=.*" --replace_string="encrypt=$encrypt" --target_file="$config_file_main" 134 | 135 | # Change ssh_host in the config file 136 | ynh_replace_string --match_string="^ssh_host=.*" --replace_string="ssh_host=$ssh_host" --target_file="$config_file_main" 137 | # Change ssh_port in the config file 138 | ynh_replace_string --match_string="^ssh_port=.*" --replace_string="ssh_port=$ssh_port" --target_file="$config_file_main" 139 | 140 | else 141 | # Add the public ssh key to authorized_keys 142 | # Secure the ssh key 143 | pub_key="no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty $pub_key" 144 | # Then add the key 145 | grep --quiet "$pub_key" "/home/$app/.ssh/authorized_keys" || \ 146 | echo "$pub_key" >> "/home/$app/.ssh/authorized_keys" 147 | 148 | # Change auto_detect_failure feature 149 | if [ "$auto_detect_failure" = "1" ] && [ "$old_auto_detect_failure" = "0" ] 150 | then 151 | # Put in place the cron file 152 | ynh_replace_string --match_string="__FINAL_PATH__" --replace_string="$final_path" --target_file="$final_path/deploy_process/auto_deploy_fallback.cron" 153 | cp $final_path/deploy_process/auto_deploy_fallback.cron /etc/cron.d/auto_deploy_fallback 154 | ynh_app_setting_set --app=$app --key=auto_detect_failure --value=1 155 | elif [ "$auto_detect_failure" = "0" ] && [ "$old_auto_detect_failure" = "1" ] 156 | then 157 | rm "/etc/cron.d/auto_deploy_fallback" 158 | ynh_app_setting_set --app=$app --key=auto_detect_failure --value=0 159 | fi 160 | # Change delay_before_incident in the config file 161 | ynh_replace_string --match_string="^delay_before_incident=.*" --replace_string="delay_before_incident=$delay_before_incident" --target_file="$config_file_fallback" 162 | ynh_app_setting_set --app=$app --key=delay_before_incident --value="$delay_before_incident" 163 | 164 | if [ $delay_before_incident -le 5 ] 165 | then 166 | cron_delay="*/1 *" 167 | elif [ $delay_before_incident -le 60 ] 168 | then 169 | cron_delay="*/10 *" 170 | elif [ $delay_before_incident -le 240 ] 171 | then 172 | cron_delay="*/30 *" 173 | else 174 | cron_delay="0 */1" 175 | fi 176 | # Reconfigure the cron file 177 | if [ -e "/etc/cron.d/auto_deploy_fallback" ] 178 | then 179 | ynh_replace_string --match_string="^.* root" --replace_string="$cron_delay * * * root" --target_file="/etc/cron.d/auto_deploy_fallback" 180 | fi 181 | # Change contact_mail in the config file 182 | ynh_replace_string --match_string="^contact_mail=.*" --replace_string="contact_mail=$contact_mail" --target_file="$config_file_fallback" 183 | ynh_app_setting_set --app=$app --key=contact_mail --value="$contact_mail" 184 | # Change auto_deploy in the config file 185 | ynh_replace_string --match_string="^auto_deploy=.*" --replace_string="auto_deploy=$auto_deploy" --target_file="$config_file_fallback" 186 | ynh_app_setting_set --app=$app --key=auto_deploy --value="$auto_deploy" 187 | fi 188 | 189 | # Set admin_mail_html 190 | ynh_app_setting_set --app=$app --key=admin_mail_html --value="$admin_mail_html" 191 | } 192 | 193 | #================================================= 194 | # GENERIC FINALIZATION 195 | #================================================= 196 | ynh_app_config_run $1 197 | -------------------------------------------------------------------------------- /scripts/install: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | #================================================= 4 | # GENERIC START 5 | #================================================= 6 | # IMPORT GENERIC HELPERS 7 | #================================================= 8 | 9 | source _common.sh 10 | source /usr/share/yunohost/helpers 11 | 12 | #================================================= 13 | # MANAGE SCRIPT FAILURE 14 | #================================================= 15 | 16 | # Exit if an error occurs during the execution of the script 17 | ynh_abort_if_errors 18 | 19 | #================================================= 20 | # RETRIEVE ARGUMENTS FROM THE MANIFEST 21 | #================================================= 22 | 23 | install_type=$YNH_APP_ARG_INSTALL_TYPE 24 | encrypt=$YNH_APP_ARG_ENCRYPT 25 | encryption_pwd=$YNH_APP_ARG_ENCRYPTION_PWD 26 | ssh_host=$YNH_APP_ARG_SSH_HOST 27 | ssh_port=$YNH_APP_ARG_SSH_PORT 28 | 29 | pub_key="$YNH_APP_ARG_PUB_KEY" 30 | auto_detect_failure=$YNH_APP_ARG_AUTO_DETECT_FAILURE 31 | delay_before_incident=$YNH_APP_ARG_DELAY_BEFORE_INCIDENT 32 | contact_mail=$YNH_APP_ARG_CONTACT_MAIL 33 | auto_deploy=$YNH_APP_ARG_AUTO_DEPLOY 34 | 35 | app=$YNH_APP_INSTANCE_NAME 36 | 37 | #================================================= 38 | # CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS 39 | #================================================= 40 | ynh_script_progression --message="Validating installation parameters..." 41 | 42 | if [ "$install_type" == "main server" ]; then 43 | install_type=main 44 | elif [ "$install_type" == "fallback server" ]; then 45 | install_type=fallback 46 | else 47 | ynh_die --message="Install type not recognized" 48 | fi 49 | 50 | if [ "$install_type" == "main" ] 51 | then 52 | if [ $encrypt -eq 1 ]; then 53 | test -n "$encryption_pwd" || ynh_die --message="encryption_pwd can't be empty if you choose a 'main server' installation with encryption." 54 | fi 55 | test -n "$ssh_host" || ynh_die --message="ssh_host can't be empty if you choose a 'main server' installation." 56 | test -n "$ssh_port" || ynh_die --message="ssh_port can't be empty if you choose a 'main server' installation." 57 | else 58 | test -n "$pub_key" || ynh_die --message="pub_key can't be empty if you choose a 'fallback server' installation" 59 | fi 60 | 61 | final_path=/opt/yunohost/$app 62 | test ! -e "$final_path" || ynh_die --message="This path already contains a folder" 63 | 64 | #================================================= 65 | # STORE SETTINGS FROM MANIFEST 66 | #================================================= 67 | ynh_script_progression --message="Storing installation settings..." --weight=2 68 | 69 | ynh_app_setting_set --app=$app --key=install_type --value=$install_type 70 | ynh_app_setting_set --app=$app --key=encrypt --value=$encrypt 71 | if [ "$install_type" == "main" ] 72 | then 73 | ynh_app_setting_set --app=$app --key=ssh_host --value=$ssh_host 74 | ynh_app_setting_set --app=$app --key=ssh_port --value=$ssh_port 75 | else 76 | ynh_app_setting_set --app=$app --key=pub_key --value="$pub_key" 77 | ynh_app_setting_set --app=$app --key=auto_detect_failure --value="$auto_detect_failure" 78 | ynh_app_setting_set --app=$app --key=delay_before_incident --value="$delay_before_incident" 79 | ynh_app_setting_set --app=$app --key=contact_mail --value="$contact_mail" 80 | ynh_app_setting_set --app=$app --key=auto_deploy --value="$auto_deploy" 81 | fi 82 | ynh_app_setting_set --app=$app --key=admin_mail_html --value=1 83 | 84 | #================================================= 85 | # STANDARD MODIFICATIONS 86 | #================================================= 87 | # INSTALL DEPENDENCIES 88 | #================================================= 89 | ynh_script_progression --message="Installing dependencies..." --weight=14 90 | 91 | ynh_install_app_dependencies $pkg_dependencies 92 | 93 | #================================================= 94 | # DOWNLOAD, CHECK AND UNPACK SOURCE 95 | #================================================= 96 | ynh_script_progression --message="Setting up source files..." --weight=4 97 | 98 | ynh_app_setting_set --app=$app --key=final_path --value=$final_path 99 | # Download, check integrity, uncompress and patch the source from app.src 100 | ynh_setup_source --dest_dir="$final_path" 101 | 102 | chmod 400 "$final_path/DynHost/cred" 103 | 104 | #================================================= 105 | # SPECIFIC SETUP 106 | #================================================= 107 | # CREATE DATA DIRECTORY 108 | #================================================= 109 | ynh_script_progression --message="Creating a data directory..." 110 | 111 | datadir=/home/yunohost.app/$app 112 | ynh_app_setting_set --app=$app --key=datadir --value=$datadir 113 | 114 | mkdir -p $datadir 115 | 116 | if [ "$install_type" == "main" ] 117 | then 118 | #================================================= 119 | # CONFIG FOR THE MAIN SERVER 120 | #================================================= 121 | # MODIFY THE CONFIG 122 | #================================================= 123 | ynh_script_progression --message="Configuring Fallback..." 124 | 125 | configfile="$final_path/send_process/config.conf" 126 | cp "${configfile}.modele" "$configfile" 127 | ynh_replace_string --match_string="ssh_user=.*" --replace_string="ssh_user=$app" --target_file="$configfile" 128 | ynh_replace_string --match_string="ssh_host=.*" --replace_string="ssh_host=$ssh_host" --target_file="$configfile" 129 | ynh_replace_string --match_string="ssh_port=.*" --replace_string="ssh_port=$ssh_port" --target_file="$configfile" 130 | 131 | ynh_replace_string --match_string="main_storage_dir=.*" --replace_string="main_storage_dir=$datadir/fallback_backup" --target_file="$configfile" 132 | ynh_replace_string --match_string="encrypt=.*" --replace_string="encrypt=$encrypt" --target_file="$configfile" 133 | 134 | #================================================= 135 | # CREATE A SSH KEY 136 | #================================================= 137 | ynh_script_progression --message="Creating a ssh key..." --weight=3 138 | 139 | ssh_key="$final_path/send_process/ssh_key" 140 | ssh-keygen -t rsa -b 4096 -N "" -f "$ssh_key" > /dev/null 141 | chown root: "$ssh_key" 142 | chmod 400 "$ssh_key" 143 | pub_key="${ssh_key}.pub" 144 | ynh_replace_string --match_string="ssh_key=.*" --replace_string="ssh_key=$ssh_key" --target_file="$configfile" 145 | 146 | mkdir -p /root/.ssh 147 | ssh-keyscan -t rsa $ssh_host >> /root/.ssh/known_hosts 148 | 149 | #================================================= 150 | # FILL THE APPS LIST 151 | #================================================= 152 | ynh_script_progression --message="Filling the apps list..." 153 | 154 | echo "#000# For each app you want to backup, precede by [.]" > "$final_path/send_process/app_list" 155 | yunohost app list | grep id: | sed 's/.*id:/[]:/' >> "$final_path/send_process/app_list" 156 | ynh_replace_string --match_string=".*: fallback" --replace_string="[-]: fallback" --target_file="$final_path/send_process/app_list" 157 | 158 | #================================================= 159 | # SET THE CRON 160 | #================================================= 161 | 162 | echo "0 1 * * * root \"$final_path/send_process/send_backup.sh\" > /dev/null" > /etc/cron.d/$app 163 | 164 | #================================================= 165 | # CREATE DIRECTORIES 166 | #================================================= 167 | 168 | mkdir -p "$final_path/send_process/checksum" 169 | mkdir -p "$datadir/fallback_backup/backup" 170 | 171 | #================================================= 172 | # STORE THE PASSWORD FOR ENCRYPTION 173 | #================================================= 174 | 175 | passkey="$final_path/send_process/passkey" 176 | ynh_replace_string --match_string="pass_file=.*" --replace_string="pass_file=$passkey" --target_file="$configfile" 177 | echo "$encryption_pwd" > "$passkey" 178 | chmod 400 "$passkey" 179 | 180 | #================================================= 181 | # STORE A CHECKSUM OF THIS CONFIG FILE 182 | #================================================= 183 | 184 | # Calculate and store the config file checksum into the app settings 185 | ynh_store_file_checksum --file="$configfile" 186 | 187 | else 188 | #================================================= 189 | # CONFIG FOR THE FALLBACK SERVER 190 | #================================================= 191 | # CREATE DEDICATED USER 192 | #================================================= 193 | ynh_script_progression --message="Configuring system user..." --weight=2 194 | 195 | if ! ynh_system_user_exists --username=$app 196 | then 197 | useradd -d "/home/$app" --system --user-group $app --shell /bin/bash 198 | fi 199 | 200 | # Allow the user to use ssh 201 | adduser $app ssh.app 202 | 203 | #================================================= 204 | # ADD THE SSH PUBLIC KEY 205 | #================================================= 206 | 207 | mkdir -p "/home/$app/.ssh" 208 | # Secure the ssh key 209 | echo -n "no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty " >> "/home/$app/.ssh/authorized_keys" 210 | # Then add the key 211 | echo "$pub_key" >> "/home/$app/.ssh/authorized_keys" 212 | 213 | #================================================= 214 | # SET THE USER IN DEPLOY_FALLBACK SCRIPTS 215 | #================================================= 216 | 217 | ynh_replace_string --match_string="\(local_archive_dir=\"/home/\)fallback" --replace_string="\1$app" --target_file="$final_path/deploy_process/deploy_fallback.sh" 218 | ynh_replace_string --match_string="\(local_archive_dir=\"/home/\)fallback" --replace_string="\1$app" --target_file="$final_path/deploy_process/close_fallback.sh" 219 | ynh_replace_string --match_string="\(local_archive_dir=\"/home/\)fallback" --replace_string="\1$app" --target_file="$final_path/deploy_process/auto_deploy_fallback.sh" 220 | 221 | #================================================= 222 | # SET A CHROOT FOR THE SSH USER 223 | #================================================= 224 | ynh_script_progression --message="Setting a chroot for the ssh user..." --weight=2 225 | 226 | chroot_dir="/home/$app" 227 | 228 | # Create directories for the binaries 229 | mkdir -p $chroot_dir/{bin,lib,lib64} 230 | 231 | # Copy the ld-linux file, according to the architecture 232 | copy_ld-linux () { 233 | ! test -e "$1" || cp "$1" "$chroot_dir/$2/" 234 | } 235 | copy_ld-linux /lib/ld-linux.so.2 lib 236 | copy_ld-linux /lib64/ld-linux-x86-64.so.2 lib64 237 | copy_ld-linux /lib/ld-linux-armhf.so.3 lib 238 | 239 | # Copy binary and its libraries into the chroot. 240 | ssh_chroot_copy_binary () { 241 | # Find and copy the binary file 242 | cp `which $1` "$chroot_dir/bin/$(basename $1)" 243 | # Then search for its libraries 244 | while read lib_file 245 | do 246 | # Filter lib without path 247 | if echo "$lib_file" | grep --quiet "=> /" 248 | then 249 | # Keep only the path of this lib 250 | local lib_path=$(echo "$lib_file" | awk '{print $3}') 251 | cp $lib_path "$chroot_dir/lib/" 252 | fi 253 | done <<< "$(ldd `which $1`)" 254 | } 255 | 256 | # Copy bash in the chroot 257 | ssh_chroot_copy_binary bash 258 | # Then rsync 259 | ssh_chroot_copy_binary rsync 260 | 261 | # Create the directory for rsync 262 | mkdir -p $chroot_dir/backup 263 | 264 | # The parent directory shall belong to root 265 | chown $app: -R "$chroot_dir" 266 | chown root: "$chroot_dir" 267 | chmod 755 -R "$chroot_dir" 268 | # Set the permissions for the ssh key 269 | chmod 644 "$chroot_dir/.ssh/authorized_keys" 270 | 271 | # Set the chroot in the ssh config 272 | echo " 273 | Match User $app # Automatically added by $app 274 | ChrootDirectory /home/%u # Automatically added by $app 275 | AllowTcpForwarding no # Automatically added by $app 276 | X11Forwarding no # Automatically added by $app" >> /etc/ssh/sshd_config 277 | 278 | # Reload ssh service 279 | ynh_systemd_action --action=reload --service_name=ssh 280 | 281 | #================================================= 282 | # CONFIGURE AUTO DEPLOYMENT 283 | #================================================= 284 | ynh_script_progression --message="Configuring auto deployment..." 285 | 286 | auto_check_config_file="$final_path/deploy_process/auto_check.conf" 287 | cp "${auto_check_config_file}.modele" "$auto_check_config_file" 288 | 289 | # If failure auto detection is activated 290 | if [ $auto_detect_failure -eq 1 ] 291 | then 292 | # Convert delay_before_incident in minutes 293 | case $delay_before_incident in 294 | 5min ) delay_before_incident=5; cron_delay="*/1 *" ;; 295 | 1h ) delay_before_incident=60; cron_delay="*/10 *" ;; 296 | 4h ) delay_before_incident=240; cron_delay="*/30 *" ;; 297 | 8h ) delay_before_incident=480; cron_delay="0 */1" ;; 298 | 12h ) delay_before_incident=720; cron_delay="0 */1" ;; 299 | 24h ) delay_before_incident=1440; cron_delay="0 */1" ;; 300 | esac 301 | 302 | # Fill the config file 303 | ynh_replace_string --match_string="delay_before_incident=.*" --replace_string="delay_before_incident=$delay_before_incident" --target_file="$auto_check_config_file" 304 | # Send email at root if there no email 305 | if [ -z "$contact_mail" ] 306 | then 307 | ynh_print_info --message="Seems that you forgot to fill in a contact email address. root is going to be used instead." 308 | contact_mail=root 309 | ynh_app_setting_set --app=$app --key=contact_mail --value="$contact_mail" 310 | fi 311 | ynh_replace_string --match_string="contact_mail=.*" --replace_string="contact_mail=$contact_mail" --target_file="$auto_check_config_file" 312 | ynh_replace_string --match_string="auto_deploy=.*" --replace_string="auto_deploy=$auto_deploy" --target_file="$auto_check_config_file" 313 | ynh_replace_string --match_string="__FINAL_PATH__" --replace_string="$final_path" --target_file="$auto_check_config_file" 314 | 315 | # Put in place the cron file 316 | ynh_replace_string --match_string="__FINAL_PATH__" --replace_string="$final_path" --target_file="$final_path/deploy_process/auto_deploy_fallback.cron" 317 | cp $final_path/deploy_process/auto_deploy_fallback.cron /etc/cron.d/auto_deploy_fallback 318 | # And configure it 319 | ynh_replace_string --match_string="? ?" --replace_string="$cron_delay" --target_file="/etc/cron.d/auto_deploy_fallback" 320 | fi 321 | 322 | #================================================= 323 | # STORE A CHECKSUM OF THIS CONFIG FILE 324 | #================================================= 325 | 326 | # Calculate and store the config file checksum into the app settings 327 | ynh_store_file_checksum --file="$auto_check_config_file" 328 | fi 329 | 330 | #================================================= 331 | # SET LINKS FOR THE ADMIN USER 332 | #================================================= 333 | 334 | if [ "$install_type" == "main" ]; then 335 | ln -s "$final_path/send_process/app_list" "$datadir/app_list" 336 | ln -s "$final_path/send_process/config.conf" "$datadir/config.conf" 337 | ln -s "$final_path/update_from_fallback_process/update_from_fallback.sh" "$datadir/update_from_fallback" 338 | else 339 | ln -s "$final_path/deploy_process/deploy_fallback.sh" "$datadir/deploy_fallback" 340 | ln -s "$final_path/deploy_process/close_fallback.sh" "$datadir/close_fallback" 341 | fi 342 | 343 | #================================================= 344 | # CONFIGURE HOOKS 345 | #================================================= 346 | 347 | ynh_replace_string --match_string="__FINALPATH__" --replace_string="$final_path" --target_file="../hooks/post_app_install" 348 | ynh_replace_string --match_string="__FINALPATH__" --replace_string="$final_path" --target_file="../hooks/post_app_remove" 349 | 350 | #================================================= 351 | # GENERIC FINALIZATION 352 | #================================================= 353 | # SECURE FILES AND DIRECTORIES 354 | #================================================= 355 | 356 | # Set permissions to app files 357 | chown -R root: $final_path 358 | 359 | if [ "$install_type" == "main" ] 360 | then 361 | #================================================= 362 | # SETUP LOGROTATE 363 | #================================================= 364 | ynh_script_progression --message="Configuring log rotation..." 365 | 366 | mkdir -p /var/log/$app 367 | ln -s /var/log/$app/$app.log "$final_path/send_process/send_backup.log" 368 | # Use logrotate to manage application logfile(s) 369 | ynh_use_logrotate 370 | 371 | #================================================= 372 | # DISCLAIMER 373 | #================================================= 374 | 375 | ynh_print_info --message=" 376 | By default, no apps will be backuped for your fallback server. To select which apps you want to backup, please have a look at the app_list file in $datadir/app_list. 377 | Please take note of your public ssh key. This will be usefull during the installation on your fallback server." >&2 378 | ynh_print_info --message="$(cat "$pub_key")" >&2 379 | fi 380 | 381 | #================================================= 382 | # SEND A README FOR THE ADMIN 383 | #================================================= 384 | 385 | # Get main domain and buid the url of the admin panel of the app. 386 | admin_panel="https://$(grep portal_domain /etc/ssowat/conf.json | cut -d'"' -f4)/yunohost/admin/#/apps/$app" 387 | 388 | if [ $encrypt -eq 1 ] 389 | then 390 | encrypt_infos="Your password for encryption is '$encryption_pwd' 391 | 392 | " 393 | else 394 | encrypt_infos="" 395 | fi 396 | 397 | if [ "$install_type" == "main" ] 398 | then 399 | Informations="${encrypt_infos}By default, no apps will be backuped for your fallback server. To select which apps you want to backup, please have a look at the app_list file in $datadir/app_list. 400 | 401 | Please take note of your public ssh key. This will be usefull during the installation on your fallback server : 402 | $(cat "$pub_key") 403 | 404 | Fallback is going to backup and send its backup every night. 405 | If you want to change the frequency, have a look to the file /etc/cron.d/$app. 406 | 407 | You can also find a config file at /home/yunohost.app/fallback/config.conf" 408 | else 409 | domain=$(yunohost domain list | grep -m1 "-" | awk '{print $2}') 410 | if [ -n "$domain" ] 411 | then 412 | domain="$domain or " 413 | else 414 | domain="" 415 | fi 416 | Informations="This server is now your fallback, you should keep this server only for this purpose. 417 | 418 | Credentials for the ssh connexion from the other part of this app: 419 | - ssh_host: $domain$(hostname) 420 | - ssh_user: $app 421 | - ssh_port: $(grep "^Port " /etc/ssh/sshd_config | awk '{print $2}')" 422 | 423 | if [ $auto_detect_failure -eq 1 ] 424 | then 425 | Informations="$Informations 426 | 427 | Failure auto detection is activated on this fallback. In case of failure of your main server, you should receive an email on $contact_mail to inform you. 428 | To modify the configuration related to this option, please have a look at $auto_check_config_file" 429 | fi 430 | fi 431 | 432 | echo "$Informations 433 | 434 | For more information about how to use this app, please read the __URL_TAG1__readme__URL_TAG2__https://github.com/YunoHost-Apps/fallback_ynh/blob/master/README.md#how-it-works__URL_TAG3__. 435 | 436 | You can configure this app easily by using the experimental __URL_TAG1__config-panel feature__URL_TAG2__$admin_panel/config-panel__URL_TAG3__. 437 | You can also find some specific actions for this app by using the experimental __URL_TAG1__action feature__URL_TAG2__$admin_panel/actions__URL_TAG3__. 438 | 439 | If you're facing an issue or want to improve this app, please open a new issue in this __URL_TAG1__project__URL_TAG2__https://github.com/YunoHost-Apps/fallback_ynh__URL_TAG3__." > mail_to_send 440 | 441 | ynh_send_readme_to_admin --app_message="mail_to_send" --recipients="root" --type=install 442 | 443 | #================================================= 444 | # END OF SCRIPT 445 | #================================================= 446 | 447 | ynh_script_progression --message="Installation of $app completed" --last 448 | -------------------------------------------------------------------------------- /scripts/remove: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | #================================================= 4 | # GENERIC START 5 | #================================================= 6 | # IMPORT GENERIC HELPERS 7 | #================================================= 8 | 9 | source _common.sh 10 | source /usr/share/yunohost/helpers 11 | 12 | #================================================= 13 | # LOAD SETTINGS 14 | #================================================= 15 | ynh_script_progression --message="Loading installation settings..." --weight=2 16 | 17 | app=$YNH_APP_INSTANCE_NAME 18 | 19 | install_type=$(ynh_app_setting_get --app=$app --key=install_type) 20 | final_path=$(ynh_app_setting_get --app=$app --key=final_path) 21 | datadir=$(ynh_app_setting_get --app=$app --key=datadir) 22 | ssh_host=$(ynh_app_setting_get --app=$app --key=ssh_host) 23 | 24 | #================================================= 25 | # STANDARD REMOVE 26 | #================================================= 27 | # REMOVE LOGROTATE CONFIGURATION 28 | #================================================= 29 | 30 | if [ "$install_type" == "main" ] 31 | then 32 | 33 | ynh_script_progression --message="Removing logrotate configuration..." 34 | 35 | # Remove the app-specific logrotate config 36 | ynh_remove_logrotate 37 | ynh_secure_remove --file="/var/log/$app" 38 | fi 39 | 40 | #================================================= 41 | # REMOVE APP MAIN DIR 42 | #================================================= 43 | ynh_script_progression --message="Removing app main directory..." 44 | 45 | # Remove the app directory securely 46 | ynh_secure_remove --file="$final_path" 47 | 48 | #================================================= 49 | # REMOVE DATA DIR 50 | #================================================= 51 | ynh_script_progression --message="Removing app data directory..." --weight=2 52 | 53 | ynh_secure_remove --file="$datadir" 54 | 55 | #================================================= 56 | # REMOVE DEPENDENCIES 57 | #================================================= 58 | ynh_script_progression --message="Removing dependencies..." --weight=7 59 | 60 | # Remove metapackage and its dependencies 61 | ynh_remove_app_dependencies 62 | 63 | #================================================= 64 | # SPECIFIC REMOVE 65 | #================================================= 66 | 67 | if [ "$install_type" == "main" ] 68 | then 69 | #================================================= 70 | # REMOVE THE CRON FILE 71 | #================================================= 72 | 73 | ynh_secure_remove --file="/etc/cron.d/$app" 74 | 75 | #================================================= 76 | # REMOVE THE HOST FROM THE KNOWN HOST LIST 77 | #================================================= 78 | 79 | sed -i "/$ssh_host/d" /root/.ssh/known_hosts 80 | 81 | #================================================= 82 | # REMOVE DEDICATED USER 83 | #================================================= 84 | ynh_script_progression --message="Removing the dedicated system user..." 85 | 86 | ynh_system_user_delete --username=$app 87 | ynh_secure_remove --file="/home/$app" 88 | 89 | #================================================= 90 | # REMOVE THE CHROOT FROM THE SSH CONFIG 91 | #================================================= 92 | ynh_script_progression --message="Removing the chroot from the ssh config..." 93 | 94 | sed -i "/# Automatically added by $app/d" /etc/ssh/sshd_config 95 | 96 | # Reload ssh service 97 | ynh_systemd_action --action=reload --service_name=ssh 98 | else 99 | #================================================= 100 | # REMOVE THE CRON FILE 101 | #================================================= 102 | 103 | ynh_secure_remove --file="/etc/cron.d/auto_deploy_fallback" 104 | fi 105 | 106 | #================================================= 107 | # END OF SCRIPT 108 | #================================================= 109 | 110 | ynh_script_progression --message="Removal of $app completed" --last 111 | -------------------------------------------------------------------------------- /scripts/restore: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | #================================================= 4 | # GENERIC START 5 | #================================================= 6 | # IMPORT GENERIC HELPERS 7 | #================================================= 8 | 9 | # Keep this path for calling _common.sh inside the execution's context of backup and restore scripts 10 | source ../settings/scripts/_common.sh 11 | source /usr/share/yunohost/helpers 12 | 13 | #================================================= 14 | # MANAGE SCRIPT FAILURE 15 | #================================================= 16 | 17 | # Exit if an error occurs during the execution of the script 18 | ynh_abort_if_errors 19 | 20 | #================================================= 21 | # LOAD SETTINGS 22 | #================================================= 23 | ynh_script_progression --message="Loading installation settings..." --weight=3 24 | 25 | app=$YNH_APP_INSTANCE_NAME 26 | 27 | install_type=$(ynh_app_setting_get --app=$app --key=install_type) 28 | final_path=$(ynh_app_setting_get --app=$app --key=final_path) 29 | ssh_host=$(ynh_app_setting_get --app=$app --key=ssh_host) 30 | ssh_port=$(ynh_app_setting_get --app=$app --key=ssh_port) 31 | pub_key="$(ynh_app_setting_get --app=$app --key=pub_key)" 32 | auto_detect_failure=$(ynh_app_setting_get --app=$app --key=auto_detect_failure) 33 | datadir=$(ynh_app_setting_get --app=$app --key=datadir) 34 | 35 | #================================================= 36 | # CHECK IF THE APP CAN BE RESTORED 37 | #================================================= 38 | ynh_script_progression --message="Validating restoration parameters..." 39 | 40 | test ! -d $final_path \ 41 | || ynh_die --message="There is already a directory: $final_path " 42 | 43 | #================================================= 44 | # STANDARD RESTORATION STEPS 45 | #================================================= 46 | # RESTORE THE APP MAIN DIR 47 | #================================================= 48 | ynh_script_progression --message="Restoring the app main directory..." 49 | 50 | ynh_restore_file --origin_path="$final_path" 51 | 52 | #================================================= 53 | # SPECIFIC RESTORATION 54 | #================================================= 55 | # REINSTALL DEPENDENCIES 56 | #================================================= 57 | ynh_script_progression --message="Reinstalling dependencies..." --weight=9 58 | 59 | # Define and install dependencies 60 | ynh_install_app_dependencies $pkg_dependencies 61 | 62 | if [ "$install_type" == "main" ] 63 | then 64 | 65 | #================================================= 66 | # ADD THE HOST TO THE KNOWN HOST LIST 67 | #================================================= 68 | ynh_script_progression --message="Adding the host to the known host list..." --weight=3 69 | 70 | mkdir -p /root/.ssh 71 | ssh-keyscan -t rsa $ssh_host >> /root/.ssh/known_hosts 2> /dev/null 72 | 73 | #================================================= 74 | # RESTORE THE LOGROTATE CONFIGURATION 75 | #================================================= 76 | 77 | ynh_restore_file --origin_path="/etc/logrotate.d/$app" 78 | 79 | #================================================= 80 | # RESTORE THE CRON FILE 81 | #================================================= 82 | 83 | ynh_restore_file --origin_path="/etc/cron.d/$app" 84 | 85 | else 86 | #================================================= 87 | # RECREATE DEDICATED USER 88 | #================================================= 89 | 90 | if ! ynh_system_user_exists --username=$app 91 | then 92 | ynh_script_progression --message="Recreating the dedicated system user..." 93 | useradd -d "/home/$app" --system --user-group $app --shell /bin/bash 94 | fi 95 | 96 | # Allow the user to use ssh 97 | adduser $app ssh.app 98 | 99 | #================================================= 100 | # RESTORE USER DIRECTORIES 101 | #================================================= 102 | ynh_script_progression --message="Restoring user directories..." --weight=2 103 | 104 | ynh_restore_file --origin_path="/home/$app/bin" 105 | ynh_restore_file --origin_path="/home/$app/lib" 106 | ynh_restore_file --origin_path="/home/$app/lib64" 107 | ynh_restore_file --origin_path="/home/$app/.ssh" 108 | 109 | # Update the ssh config 110 | if ! grep "# Automatically added by $app" /etc/ssh/sshd_config 111 | then 112 | ynh_script_progression --message="Update the ssh config" 113 | 114 | echo " 115 | Match User $app # Automatically added by $app 116 | ChrootDirectory /home/%u # Automatically added by $app 117 | AllowTcpForwarding no # Automatically added by $app 118 | X11Forwarding no # Automatically added by $app" >> /etc/ssh/sshd_config 119 | 120 | # Reload ssh service 121 | ynh_systemd_action --action=reload --service_name=ssh 122 | fi 123 | 124 | #================================================= 125 | # RESTORE THE CRON FILE 126 | #================================================= 127 | 128 | if [ $auto_detect_failure -eq 1 ] 129 | then 130 | ynh_restore_file --origin_path="/etc/cron.d/auto_deploy_fallback" 131 | fi 132 | fi 133 | 134 | #================================================= 135 | # RECREATE DIRECTORIES 136 | #================================================= 137 | 138 | mkdir -p "$datadir/fallback_backup/backup" 139 | if [ "$install_type" == "main" ] 140 | then 141 | mkdir -p /var/log/$app 142 | else 143 | mkdir -p /home/$app/backup 144 | fi 145 | 146 | #================================================= 147 | # SET LINKS FOR THE ADMIN USER 148 | #================================================= 149 | 150 | if [ "$install_type" == "main" ]; then 151 | ln -sf "$final_path/send_process/app_list" "$datadir/app_list" 152 | ln -sf "$final_path/send_process/config.conf" "$datadir/config.conf" 153 | ln -sf "$final_path/update_from_fallback_process/update_from_fallback.sh" "$datadir/update_from_fallback" 154 | else 155 | ln -sf "$final_path/deploy_process/deploy_fallback.sh" "$datadir/deploy_fallback" 156 | ln -sf "$final_path/deploy_process/close_fallback.sh" "$datadir/close_fallback" 157 | fi 158 | 159 | #================================================= 160 | # GENERIC FINALIZATION 161 | #================================================= 162 | # SECURE FILES AND DIRECTORIES 163 | #================================================= 164 | 165 | if [ "$install_type" == "fallback" ] 166 | then 167 | # The parent directory shall belong to root 168 | chown $app: -R "/home/$app" 169 | chown root: "/home/$app" 170 | chmod 755 -R "/home/$app" 171 | # Set the permissions for the ssh key 172 | chmod 644 "$chroot_dir/.ssh/authorized_keys" 173 | fi 174 | 175 | #================================================= 176 | # SEND A README FOR THE ADMIN 177 | #================================================= 178 | 179 | # Get main domain and buid the url of the admin panel of the app. 180 | admin_panel="https://$(grep portal_domain /etc/ssowat/conf.json | cut -d'"' -f4)/yunohost/admin/#/apps/$app" 181 | 182 | if [ "$install_type" == "main" ] 183 | then 184 | Informations="By default, no apps will be backuped for your fallback server. To select which apps you want to backup, please have a look at the app_list file in $datadir/app_list. 185 | 186 | Please take note of your public ssh key. This will be usefull during the installation on your fallback server : 187 | $pub_key 188 | 189 | Fallback is going to backup and send its backup every night. 190 | If you want to change the frequency, have a look to the file /etc/cron.d/$app. 191 | 192 | You can also find a config file at /home/yunohost.app/fallback/config.conf" 193 | else 194 | domain=$(yunohost domain list | grep -m1 "-" | awk '{print $2}') 195 | if [ -n "$domain" ] 196 | then 197 | domain="$domain or " 198 | else 199 | domain="" 200 | fi 201 | Informations="This server is now your fallback, you should keep this server only for this purpose. 202 | 203 | Credentials for the ssh connexion from the other part of this app: 204 | - ssh_host: $domain$(hostname) 205 | - ssh_user: $app 206 | - ssh_port: $(grep "^Port " /etc/ssh/sshd_config | awk '{print $2}')" 207 | 208 | if [ $auto_detect_failure -eq 1 ] 209 | then 210 | Informations="$Informations 211 | 212 | Failure auto detection is activated on this fallback. In case of failure of your main server. 213 | To modify the configuration related to this option, please have a look at $final_path/deploy_process/auto_check.conf" 214 | fi 215 | fi 216 | 217 | echo "$Informations 218 | 219 | For more information about how to use this app, please read the __URL_TAG1__readme__URL_TAG2__https://github.com/YunoHost-Apps/fallback_ynh/blob/master/README.md#how-it-works__URL_TAG3__. 220 | 221 | You can configure this app easily by using the experimental __URL_TAG1__config-panel feature__URL_TAG2__$admin_panel/config-panel__URL_TAG3__. 222 | You can also find some specific actions for this app by using the experimental __URL_TAG1__action feature__URL_TAG2__$admin_panel/actions__URL_TAG3__. 223 | 224 | If you're facing an issue or want to improve this app, please open a new issue in this __URL_TAG1__project__URL_TAG2__https://github.com/YunoHost-Apps/fallback_ynh__URL_TAG3__." > mail_to_send 225 | 226 | ynh_send_readme_to_admin --app_message="mail_to_send" --recipients="root" --type=restore 227 | 228 | #================================================= 229 | # END OF SCRIPT 230 | #================================================= 231 | 232 | ynh_script_progression --message="Restoration completed for $app" --last 233 | -------------------------------------------------------------------------------- /scripts/upgrade: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | #================================================= 4 | # GENERIC START 5 | #================================================= 6 | # IMPORT GENERIC HELPERS 7 | #================================================= 8 | 9 | source _common.sh 10 | source /usr/share/yunohost/helpers 11 | 12 | #================================================= 13 | # LOAD SETTINGS 14 | #================================================= 15 | ynh_script_progression --message="Loading installation settings..." --weight=4 16 | 17 | app=$YNH_APP_INSTANCE_NAME 18 | 19 | install_type=$(ynh_app_setting_get --app=$app --key=install_type) 20 | encrypt=$(ynh_app_setting_get --app=$app --key=encrypt) 21 | final_path=$(ynh_app_setting_get --app=$app --key=final_path) 22 | ssh_host=$(ynh_app_setting_get --app=$app --key=ssh_host) 23 | ssh_port=$(ynh_app_setting_get --app=$app --key=ssh_port) 24 | pub_key="$(ynh_app_setting_get --app=$app --key=pub_key)" 25 | auto_detect_failure=$(ynh_app_setting_get --app=$app --key=auto_detect_failure) 26 | datadir=$(ynh_app_setting_get --app=$app --key=datadir) 27 | 28 | # Optional parameters from config-panel feature 29 | contact_mail=$(ynh_app_setting_get --app=$app --key=contact_mail) 30 | auto_deploy=$(ynh_app_setting_get --app=$app --key=auto_deploy) 31 | delay_before_incident=$(ynh_app_setting_get --app=$app --key=delay_before_incident) 32 | 33 | #================================================= 34 | # CHECK VERSION 35 | #================================================= 36 | ynh_script_progression --message="Checking version..." 37 | 38 | upgrade_type=$(ynh_check_app_version_changed) 39 | 40 | #================================================= 41 | # BACKUP BEFORE UPGRADE THEN ACTIVE TRAP 42 | #================================================= 43 | ynh_script_progression --message="Backing up the app before upgrading (may take a while)..." --weight=5 44 | 45 | # Backup the current version of the app 46 | ynh_backup_before_upgrade 47 | ynh_clean_setup () { 48 | # Restore it if the upgrade fails 49 | ynh_restore_upgradebackup 50 | } 51 | # Exit if an error occurs during the execution of the script 52 | ynh_abort_if_errors 53 | 54 | #================================================= 55 | # STANDARD UPGRADE STEPS 56 | #================================================= 57 | # ENSURE DOWNWARD COMPATIBILITY 58 | #================================================= 59 | ynh_script_progression --message="Ensuring downward compatibility..." 60 | 61 | if [ "$install_type" == "fallback" ] 62 | then 63 | # If auto_detect_failure setting doesn't exist 64 | if [ -z "$auto_detect_failure" ]; then 65 | auto_detect_failure=0 66 | ynh_app_setting_set --app=$app --key=auto_detect_failure --value=$auto_detect_failure 67 | fi 68 | # If delay_before_incident setting doesn't exist 69 | if [ -z "$delay_before_incident" ]; then 70 | delay_before_incident=$(grep "^delay_before_incident=" "$final_path/deploy_process/auto_check.conf" | cut -d= -f2) 71 | ynh_app_setting_set --app=$app --key=delay_before_incident --value=$delay_before_incident 72 | fi 73 | # If contact_mail setting doesn't exist 74 | if [ -z "$contact_mail" ]; then 75 | contact_mail=$(grep "^contact_mail=" "$final_path/deploy_process/auto_check.conf" | cut -d= -f2) 76 | ynh_app_setting_set --app=$app --key=contact_mail --value=$contact_mail 77 | fi 78 | # If auto_deploy setting doesn't exist 79 | if [ -z "$auto_deploy" ]; then 80 | auto_deploy=$(grep "^auto_deploy=" "$final_path/deploy_process/auto_check.conf" | cut -d= -f2) 81 | ynh_app_setting_set --app=$app --key=auto_deploy --value=$auto_deploy 82 | fi 83 | fi 84 | 85 | # If datadir doesn't exist, create it 86 | if [ -z "$datadir" ]; then 87 | datadir=/home/yunohost.app/$app 88 | ynh_app_setting_set --app=$app --key=datadir --value=$datadir 89 | fi 90 | 91 | #================================================= 92 | # DOWNLOAD, CHECK AND UNPACK SOURCE 93 | #================================================= 94 | 95 | if [ "$upgrade_type" == "UPGRADE_APP" ] 96 | then 97 | ynh_script_progression --message="Upgrading source files..." --weight=2 98 | # Do not replace credentials 99 | mv "$final_path/DynHost/cred" "$final_path/DynHost/cred_backup" 100 | # Download, check integrity, uncompress and patch the source from app.src 101 | ynh_setup_source --dest_dir="$final_path" 102 | mv "$final_path/DynHost/cred_backup" "$final_path/DynHost/cred" 103 | fi 104 | 105 | #================================================= 106 | # UPGRADE DEPENDENCIES 107 | #================================================= 108 | ynh_script_progression --message="Upgrading dependencies..." --weight=9 109 | 110 | ynh_install_app_dependencies $pkg_dependencies 111 | 112 | #================================================= 113 | # SPECIFIC UPGRADE 114 | #================================================= 115 | 116 | if [ "$install_type" == "main" ] 117 | then 118 | #================================================= 119 | # CONFIG FOR THE MAIN SERVER 120 | #================================================= 121 | # MODIFY THE CONFIG 122 | #================================================= 123 | 124 | if [ "$upgrade_type" == "UPGRADE_APP" ] 125 | then 126 | ynh_script_progression --message="Reconfiguring Fallback..." 127 | configfile="$final_path/send_process/config.conf" 128 | ynh_replace_string --match_string="ssh_user=.*" --replace_string="ssh_user=$app" --target_file="$configfile" 129 | ynh_replace_string --match_string="ssh_host=.*" --replace_string="ssh_host=$ssh_host" --target_file="$configfile" 130 | ynh_replace_string --match_string="ssh_port=.*" --replace_string="ssh_port=$ssh_port" --target_file="$configfile" 131 | ynh_replace_string --match_string="ssh_key=.*" --replace_string="ssh_key=$final_path/send_process/ssh_key" --target_file="$configfile" 132 | 133 | ynh_replace_string --match_string="main_storage_dir=.*" --replace_string="main_storage_dir=$datadir/fallback_backup" --target_file="$configfile" 134 | ynh_replace_string --match_string="encrypt=.*" --replace_string="encrypt=$encrypt" --target_file="$configfile" 135 | fi 136 | 137 | #================================================= 138 | # UPDATE THE CRON 139 | #================================================= 140 | ynh_script_progression --message="Updating the cron..." 141 | 142 | echo "0 1 * * * root \"$final_path/send_process/send_backup.sh\" > /dev/null" > /etc/cron.d/$app 143 | 144 | else 145 | #================================================= 146 | # CONFIG FOR THE FALLBACK SERVER 147 | #================================================= 148 | # CREATE DEDICATED USER 149 | #================================================= 150 | 151 | if ! ynh_system_user_exists --username=$app 152 | then 153 | ynh_script_progression --message="Making sure dedicated system user exists..." 154 | useradd -d "/home/$app" --system --user-group $app --shell /bin/bash 155 | fi 156 | 157 | # Allow the user to use ssh 158 | adduser $app ssh.app 159 | 160 | #================================================= 161 | # SET THE USER IN DEPLOY_FALLBACK SCRIPTS 162 | #================================================= 163 | 164 | if [ "$upgrade_type" == "UPGRADE_APP" ] 165 | then 166 | ynh_replace_string --match_string="\(local_archive_dir=\"/home/\)USER" --replace_string="\1$app" --target_file="$final_path/deploy_process/deploy_fallback.sh" 167 | ynh_replace_string --match_string="\(local_archive_dir=\"/home/\)USER" --replace_string="\1$app" --target_file="$final_path/deploy_process/close_fallback.sh" 168 | fi 169 | 170 | #================================================= 171 | # UPDATE THE CHROOT FOR THE SSH USER 172 | #================================================= 173 | ynh_script_progression --message="Updating the chroot for the ssh user..." 174 | 175 | chroot_dir="/home/$app" 176 | 177 | # Create directories for the binaries 178 | mkdir -p $chroot_dir/{bin,lib,lib64} 179 | 180 | # Copy the ld-linux file, according to the architecture 181 | copy_ld-linux () { 182 | ! test -e "$1" || cp "$1" "$chroot_dir/$2/" 183 | } 184 | copy_ld-linux /lib/ld-linux.so.2 lib 185 | copy_ld-linux /lib64/ld-linux-x86-64.so.2 lib64 186 | copy_ld-linux /lib/ld-linux-armhf.so.3 lib 187 | 188 | # Copy binary and its libraries into the chroot. 189 | ssh_chroot_copy_binary () { 190 | # Find and copy the binary file 191 | cp `which $1` "$chroot_dir/bin/$(basename $1)" 192 | # Then search for its libraries 193 | while read lib_file 194 | do 195 | # Filter lib without path 196 | if echo "$lib_file" | grep --quiet "=> /" 197 | then 198 | # Keep only the path of this lib 199 | local lib_path=$(echo "$lib_file" | awk '{print $3}') 200 | cp $lib_path "$chroot_dir/lib/" 201 | fi 202 | done <<< "$(ldd `which $1`)" 203 | } 204 | 205 | # Copy bash in the chroot 206 | ssh_chroot_copy_binary bash 207 | # Then rsync 208 | ssh_chroot_copy_binary rsync 209 | 210 | # Create the directory for rsync 211 | mkdir -p $chroot_dir/backup 212 | 213 | # The parent directory shall belong to root 214 | chown $app: -R "$chroot_dir" 215 | chown root: "$chroot_dir" 216 | chmod 755 -R "$chroot_dir" 217 | # Set the permissions for the ssh key 218 | chmod 644 "$chroot_dir/.ssh/authorized_keys" 219 | 220 | # Update the ssh config 221 | if ! grep "# Automatically added by $app" /etc/ssh/sshd_config 222 | then 223 | echo " 224 | Match User $app # Automatically added by $app 225 | ChrootDirectory /home/%u # Automatically added by $app 226 | AllowTcpForwarding no # Automatically added by $app 227 | X11Forwarding no # Automatically added by $app" >> /etc/ssh/sshd_config 228 | 229 | # Reload ssh service 230 | ynh_system_reload --action=reload --service_name=ssh 231 | fi 232 | 233 | #================================================= 234 | # CONFIGURE AUTO DEPLOYMENT 235 | #================================================= 236 | 237 | if [ "$upgrade_type" == "UPGRADE_APP" ] 238 | then 239 | ynh_script_progression --message="Configuring auto deployment..." 240 | auto_check_config_file="$final_path/deploy_process/auto_check.conf" 241 | ynh_replace_string --match_string="__FINAL_PATH__" --replace_string="$final_path" --target_file="$auto_check_config_file" 242 | 243 | # Optional parameters from config-panel feature 244 | if [ -n "$contact_mail" ]; then 245 | ynh_replace_string --match_string="^contact_mail=.*" --replace_string="contact_mail=$contact_mail" --target_file="$auto_check_config_file" 246 | fi 247 | if [ -n "$auto_deploy" ]; then 248 | ynh_replace_string --match_string="^auto_deploy=.*" --replace_string="auto_deploy=$auto_deploy" --target_file="$auto_check_config_file" 249 | fi 250 | if [ -n "$delay_before_incident" ]; then 251 | ynh_replace_string --match_string="^delay_before_incident=.*" --replace_string="delay_before_incident=$delay_before_incident" --target_file="$auto_check_config_file" 252 | fi 253 | 254 | # Configure the cron file 255 | ynh_replace_string --match_string="__FINAL_PATH__" --replace_string="$final_path" --target_file="$final_path/deploy_process/auto_deploy_fallback.cron" 256 | fi 257 | fi 258 | 259 | #================================================= 260 | # UPDATE LINKS FOR THE ADMIN USER 261 | #================================================= 262 | 263 | if [ "$install_type" == "main" ]; then 264 | ln -sf "$final_path/send_process/app_list" "$datadir/app_list" 265 | ln -sf "$final_path/send_process/config.conf" "$datadir/config.conf" 266 | ln -sf "$final_path/update_from_fallback_process/update_from_fallback.sh" "$datadir/update_from_fallback" 267 | else 268 | ln -sf "$final_path/deploy_process/deploy_fallback.sh" "$datadir/deploy_fallback" 269 | ln -sf "$final_path/deploy_process/close_fallback.sh" "$datadir/close_fallback" 270 | fi 271 | 272 | #================================================= 273 | # CONFIGURE HOOKS 274 | #================================================= 275 | 276 | ynh_replace_string --match_string="__FINALPATH__" --replace_string="$final_path" --target_file="../hooks/post_app_install" 277 | ynh_replace_string --match_string="__FINALPATH__" --replace_string="$final_path" --target_file="../hooks/post_app_remove" 278 | 279 | #================================================= 280 | # SECURE FILES AND DIRECTORIES 281 | #================================================= 282 | 283 | # Set permissions to app files 284 | chown -R root: $final_path 285 | 286 | #================================================= 287 | # GENERIC FINALIZATION 288 | #================================================= 289 | # SETUP LOGROTATE 290 | #================================================= 291 | 292 | if [ "$install_type" == "main" ] 293 | then 294 | ynh_script_progression --message="Upgrading logrotate configuration..." --weight=2 295 | 296 | # Use logrotate to manage application logfile(s) 297 | ynh_use_logrotate --non-append 298 | fi 299 | 300 | #================================================= 301 | # SEND A README FOR THE ADMIN 302 | #================================================= 303 | 304 | # Get main domain and buid the url of the admin panel of the app. 305 | admin_panel="https://$(grep portal_domain /etc/ssowat/conf.json | cut -d'"' -f4)/yunohost/admin/#/apps/$app" 306 | 307 | # Build the changelog 308 | ynh_app_changelog || true 309 | 310 | if [ "$install_type" == "main" ] 311 | then 312 | Informations="By default, no apps will be backuped for your fallback server. To select which apps you want to backup, please have a look at the app_list file in $datadir/app_list. 313 | 314 | Please take note of your public ssh key. This will be usefull during the installation on your fallback server : 315 | $pub_key 316 | 317 | Fallback is going to backup and send its backup every night. 318 | If you want to change the frequency, have a look to the file /etc/cron.d/$app. 319 | 320 | You can also find a config file at /home/yunohost.app/fallback/config.conf" 321 | else 322 | domain=$(yunohost domain list | grep -m1 "-" | awk '{print $2}') 323 | if [ -n "$domain" ] 324 | then 325 | domain="$domain or " 326 | else 327 | domain="" 328 | fi 329 | Informations="This server is now your fallback, you should keep this server only for this purpose. 330 | 331 | Credentials for the ssh connexion from the other part of this app: 332 | - ssh_host: $domain$(hostname) 333 | - ssh_user: $app 334 | - ssh_port: $(grep "^Port " /etc/ssh/sshd_config | awk '{print $2}')" 335 | 336 | if [ $auto_detect_failure -eq 1 ] 337 | then 338 | Informations="$Informations 339 | 340 | Failure auto detection is activated on this fallback. In case of failure of your main server. 341 | To modify the configuration related to this option, please have a look at $final_path/deploy_process/auto_check.conf" 342 | fi 343 | fi 344 | 345 | echo "$Informations 346 | 347 | For more information about how to use this app, please read the __URL_TAG1__readme__URL_TAG2__https://github.com/YunoHost-Apps/fallback_ynh/blob/master/README.md#how-it-works__URL_TAG3__. 348 | 349 | You can configure this app easily by using the experimental __URL_TAG1__config-panel feature__URL_TAG2__$admin_panel/config-panel__URL_TAG3__. 350 | You can also find some specific actions for this app by using the experimental __URL_TAG1__action feature__URL_TAG2__$admin_panel/actions__URL_TAG3__. 351 | 352 | If you're facing an issue or want to improve this app, please open a new issue in this __URL_TAG1__project__URL_TAG2__https://github.com/YunoHost-Apps/fallback_ynh__URL_TAG3__. 353 | 354 | --- 355 | 356 | Changelog since your last upgrade: 357 | $(cat changelog)" > mail_to_send 358 | 359 | ynh_send_readme_to_admin --app_message="mail_to_send" --recipients="root" --type=upgrade 360 | 361 | #================================================= 362 | # END OF SCRIPT 363 | #================================================= 364 | 365 | ynh_script_progression --message="Upgrade of $app completed" --last 366 | --------------------------------------------------------------------------------