config 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #!/bin/bash
  2. #=================================================
  3. # GENERIC STARTING
  4. #=================================================
  5. # IMPORT GENERIC HELPERS
  6. #=================================================
  7. source _common.sh
  8. source /usr/share/yunohost/helpers
  9. #=================================================
  10. # RETRIEVE ARGUMENTS
  11. #=================================================
  12. app=$YNH_APP_ID
  13. final_path=$(ynh_app_setting_get $app final_path)
  14. #=================================================
  15. # SPECIFIC CODE
  16. #=================================================
  17. # DECLARE GENERIC FUNCTION
  18. #=================================================
  19. config_file="$final_path/Backup_list.conf"
  20. passkey="$final_path/passkey"
  21. get_config_value() {
  22. option_name="$1"
  23. # Get the value of this option in the config file
  24. grep "^$option_name=" "$config_file" | cut -d= -f2
  25. }
  26. #=================================================
  27. # LOAD VALUES
  28. #=================================================
  29. # Load the real value from the app config or elsewhere.
  30. # Then get the value from the form.
  31. # If the form has a value for a variable, take the value from the form,
  32. # Otherwise, keep the value from the app config.
  33. # Encryption
  34. old_encrypt="$(get_config_value encrypt)"
  35. encrypt="${YNH_CONFIG_MAIN_ENCRYPTION_ENCRYPT:-$old_encrypt}"
  36. # Encryption password
  37. ynh_print_OFF
  38. old_encrypt_password="$(cat $passkey)"
  39. encrypt_password="${YNH_CONFIG_MAIN_ENCRYPTION_ENCRYPTION_PWD:-$old_encrypt_password}"
  40. ynh_print_ON
  41. # ynh_core_backup
  42. old_ynh_core_backup="$(get_config_value ynh_core_backup)"
  43. ynh_core_backup="${YNH_CONFIG_MAIN_BACKUP_TYPES_CORE_BACKUP:-$old_ynh_core_backup}"
  44. # ynh_app_backup
  45. if [ -n "$(get_config_value ynh_app_backup)" ]
  46. then
  47. old_ynh_app_backup="true"
  48. else
  49. old_ynh_app_backup="false"
  50. fi
  51. ynh_app_backup="${YNH_CONFIG_MAIN_BACKUP_TYPES_APPS_BACKUP:-$old_ynh_app_backup}"
  52. # Frequency
  53. old_frequency="$(grep "^frequency: " "/etc/yunohost/apps/$app/settings.yml" | cut -d' ' -f2)"
  54. frequency="${YNH_CONFIG_MAIN_BACKUP_OPTIONS_FREQUENCY:-$old_frequency}"
  55. # Max size
  56. old_max_size="$(get_config_value max_size)"
  57. max_size="${YNH_CONFIG_MAIN_BACKUP_OPTIONS_MAX_SIZE:-$old_max_size}"
  58. #=================================================
  59. # SHOW_CONFIG FUNCTION FOR 'SHOW' COMMAND
  60. #=================================================
  61. show_config() {
  62. # here you are supposed to read some config file/database/other then print the values
  63. # echo "YNH_CONFIG_${PANEL_ID}_${SECTION_ID}_${OPTION_ID}=value"
  64. echo "YNH_CONFIG_MAIN_ENCRYPTION_ENCRYPT=$encrypt"
  65. echo "YNH_CONFIG_MAIN_ENCRYPTION_ENCRYPTION_PWD="
  66. echo "YNH_CONFIG_MAIN_BACKUP_TYPES_CORE_BACKUP=$ynh_core_backup"
  67. echo "YNH_CONFIG_MAIN_BACKUP_TYPES_APPS_BACKUP=$ynh_app_backup"
  68. echo "YNH_CONFIG_MAIN_BACKUP_OPTIONS_FREQUENCY=$frequency"
  69. echo "YNH_CONFIG_MAIN_BACKUP_OPTIONS_MAX_SIZE=$max_size"
  70. }
  71. #=================================================
  72. # MODIFY THE CONFIGURATION
  73. #=================================================
  74. apply_config() {
  75. # Change the password if needed
  76. if [ "$encrypt" = "true" ]; then
  77. ynh_print_OFF
  78. test -n "$encrypt_password" || ynh_die "The password for encryption can't be empty if you choose to enable encryption."
  79. ynh_print_ON
  80. # Replace the password by the previous one
  81. passkey="$final_path/passkey"
  82. ynh_print_OFF; echo "$encrypt_password" > "$passkey"; ynh_print_ON
  83. chmod 400 "$passkey"
  84. ynh_replace_string "^cryptpass=.*" "cryptpass=$passkey" "$config_file"
  85. fi
  86. # Change encrypt in the config file
  87. ynh_replace_string "^encrypt=.*" "encrypt=$encrypt" "$config_file"
  88. # Change ynh_core_backup in the config file
  89. ynh_replace_string "^ynh_core_backup=.*" "ynh_core_backup=$ynh_core_backup" "$config_file"
  90. # Change ynh_app_backup in the config file
  91. if [ "$ynh_app_backup" = "true" ] && [ "$old_ynh_app_backup" = "false" ]
  92. then
  93. # If ynh_app_backup changed from false to true.
  94. # Add all current applications to the backup
  95. while read backup_app
  96. do
  97. ynh_print_info "Add a backup for the app $backup_app." >&2
  98. ynh_replace_string "^ynh_app_backup=$" "ynh_app_backup=$backup_app\n&" "$config_file"
  99. done <<< "$(yunohost app list -i | grep id: | sed 's/.*id: //')"
  100. else
  101. # Remove all app currently backup
  102. # By deleting all line starting by 'ynh_app_backup=' and having something after '='
  103. sed -i "/^ynh_app_backup=.\+$/d" "$config_file"
  104. fi
  105. # Change frequency in the cron file and store the value into the settings
  106. ynh_app_setting_set $app frequency "$frequency"
  107. if [ "$frequency" = "Daily" ]; then
  108. cron_freq="0 2 * * *"
  109. run_freq="every day"
  110. elif [ "$frequency" = "Each 3 days" ]; then
  111. cron_freq="0 2 */3 * *"
  112. run_freq="each 3 days"
  113. elif [ "$frequency" = "Weekly" ]; then
  114. cron_freq="0 2 * * 0"
  115. run_freq="once a week on sunday"
  116. elif [ "$frequency" = "Biweekly" ]; then
  117. cron_freq="0 2 * * 0/2"
  118. run_freq="one sunday out of two"
  119. else # Monthly
  120. cron_freq="0 2 1 * *"
  121. run_freq="once a month on the first sunday"
  122. fi
  123. ynh_replace_string ".* root" "$cron_freq root" /etc/cron.d/$app
  124. # Change max_size in the config file
  125. ynh_replace_string "^max_size=.*" "max_size=$max_size" "$config_file"
  126. }
  127. #=================================================
  128. # GENERIC FINALIZATION
  129. #=================================================
  130. # SELECT THE ACTION FOLLOWING THE GIVEN ARGUMENT
  131. #=================================================
  132. case $1 in
  133. show) show_config;;
  134. apply) apply_config;;
  135. esac