config 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. final_path=$(ynh_app_setting_get --app=$app --key=final_path)
  13. #=================================================
  14. # SPECIFIC CODE
  15. #=================================================
  16. # DECLARE GENERIC FUNCTION
  17. #=================================================
  18. config_file="$final_path/Backup_list.conf"
  19. passkey="$final_path/passkey"
  20. get_config_value() {
  21. option_name="$1"
  22. # Get the value of this option in the config file
  23. grep "^$option_name=" "$config_file" | cut -d= -f2
  24. }
  25. #=================================================
  26. # LOAD VALUES
  27. #=================================================
  28. # Load the real value from the app config or elsewhere.
  29. # Then get the value from the form.
  30. # If the form has a value for a variable, take the value from the form,
  31. # Otherwise, keep the value from the app config.
  32. # Encryption
  33. old_encrypt="$(get_config_value encrypt)"
  34. encrypt="${YNH_CONFIG_MAIN_ENCRYPTION_ENCRYPT:-$old_encrypt}"
  35. # Encryption password
  36. ynh_print_OFF
  37. old_encrypt_password="$(cat $passkey)"
  38. encrypt_password="${YNH_CONFIG_MAIN_ENCRYPTION_ENCRYPTION_PWD:-$old_encrypt_password}"
  39. ynh_print_ON
  40. # Compression algorithms
  41. old_ynh_compression="$(get_config_value ynh_compression_mode)"
  42. old_ynh_compression=${old_ynh_compression:-gzip}
  43. ynh_compression="${YNH_CONFIG_MAIN_COMPRESSION_YNH:-$old_ynh_compression}"
  44. old_files_compression="$(get_config_value files_compression_mode)"
  45. old_files_compression=${old_files_compression:-gzip}
  46. files_compression="${YNH_CONFIG_MAIN_COMPRESSION_FILES:-$old_files_compression}"
  47. # ynh_core_backup
  48. old_ynh_core_backup="$(get_config_value ynh_core_backup)"
  49. ynh_core_backup="${YNH_CONFIG_MAIN_BACKUP_TYPES_CORE_BACKUP:-$old_ynh_core_backup}"
  50. # ynh_app_backup
  51. if [ -n "$(get_config_value ynh_app_backup)" ]
  52. then
  53. old_ynh_app_backup=true
  54. else
  55. old_ynh_app_backup=false
  56. fi
  57. ynh_app_backup="${YNH_CONFIG_MAIN_BACKUP_TYPES_APPS_BACKUP:-$old_ynh_app_backup}"
  58. # Frequency
  59. old_frequency="$(grep "^frequency: " "/etc/yunohost/apps/$app/settings.yml" | cut -d' ' -f2)"
  60. frequency="${YNH_CONFIG_MAIN_BACKUP_OPTIONS_FREQUENCY:-$old_frequency}"
  61. # Max size
  62. old_max_size="$(get_config_value max_size)"
  63. max_size="${YNH_CONFIG_MAIN_BACKUP_OPTIONS_MAX_SIZE:-$old_max_size}"
  64. # Overwrite cron file
  65. old_overwrite_cron="$(ynh_app_setting_get $app overwrite_cron)"
  66. overwrite_cron="${YNH_CONFIG_MAIN_OVERWRITE_FILES_OVERWRITE_CRON:-$old_overwrite_cron}"
  67. # Type of admin mail configuration
  68. old_admin_mail_html="$(ynh_app_setting_get $app admin_mail_html)"
  69. admin_mail_html="${YNH_CONFIG_MAIN_GLOBAL_CONFIG_EMAIL_TYPE:-$old_admin_mail_html}"
  70. #=================================================
  71. # SHOW_CONFIG FUNCTION FOR 'SHOW' COMMAND
  72. #=================================================
  73. show_config() {
  74. # here you are supposed to read some config file/database/other then print the values
  75. # ynh_return "YNH_CONFIG_${PANEL_ID}_${SECTION_ID}_${OPTION_ID}=value"
  76. ynh_return "YNH_CONFIG_MAIN_ENCRYPTION_ENCRYPT=$encrypt"
  77. ynh_return "YNH_CONFIG_MAIN_ENCRYPTION_ENCRYPTION_PWD="
  78. ynh_return "YNH_CONFIG_MAIN_COMPRESSION_YNH=$ynh_compression"
  79. ynh_return "YNH_CONFIG_MAIN_COMPRESSION_FILES=$files_compression"
  80. ynh_return "YNH_CONFIG_MAIN_BACKUP_TYPES_CORE_BACKUP=$ynh_core_backup"
  81. ynh_return "YNH_CONFIG_MAIN_BACKUP_TYPES_APPS_BACKUP=$ynh_app_backup"
  82. ynh_return "YNH_CONFIG_MAIN_BACKUP_OPTIONS_FREQUENCY=$frequency"
  83. ynh_return "YNH_CONFIG_MAIN_BACKUP_OPTIONS_MAX_SIZE=$max_size"
  84. ynh_return "YNH_CONFIG_MAIN_OVERWRITE_FILES_OVERWRITE_CRON=$overwrite_cron"
  85. ynh_return "YNH_CONFIG_MAIN_GLOBAL_CONFIG_EMAIL_TYPE=$admin_mail_html"
  86. }
  87. #=================================================
  88. # MODIFY THE CONFIGURATION
  89. #=================================================
  90. apply_config() {
  91. #=================================================
  92. # MODIFY THE PASSWORD
  93. #=================================================
  94. # Change the password if needed
  95. if [ "$encrypt" ]
  96. then
  97. ynh_print_OFF
  98. test -n "$encrypt_password" || ynh_die --message="The password for encryption can't be empty if you choose to enable encryption."
  99. ynh_print_ON
  100. # Replace the password by the previous one
  101. passkey="$final_path/passkey"
  102. ynh_print_OFF; echo "$encrypt_password" > "$passkey"; ynh_print_ON
  103. chmod 400 "$passkey"
  104. ynh_replace_string --match_string="^cryptpass=.*" --replace_string="cryptpass=$passkey" --target_file="$config_file"
  105. fi
  106. #=================================================
  107. # MODIFY ENCRYPT SETTING
  108. #=================================================
  109. # Change encrypt in the config file
  110. ynh_replace_string --match_string="^encrypt=.*" --replace_string="encrypt=$encrypt" --target_file="$config_file"
  111. #=================================================
  112. # MODIFY SETTINGS
  113. #=================================================
  114. # Change the compression algorithms
  115. # Replace "No compression" by "none" for the config file
  116. if [ "$ynh_compression" == "No compression" ]; then
  117. ynh_compression=none
  118. fi
  119. if [ "$ynh_compression" != "$old_ynh_compression" ]
  120. then
  121. # Update the config, or add the config if not yet existing
  122. if grep "^ynh_compression_mode=" "$config_file"
  123. then
  124. ynh_replace_string --match_string="^ynh_compression_mode=.*" --replace_string="ynh_compression_mode=$ynh_compression" --target_file="$config_file"
  125. else
  126. echo "ynh_compression_mode=$ynh_compression" >> "$config_file"
  127. fi
  128. fi
  129. # Replace "No compression" by "none" for the config file
  130. if [ "$files_compression" == "No compression" ]; then
  131. files_compression=none
  132. fi
  133. if [ "$files_compression" != "$old_files_compression" ]
  134. then
  135. # Update the config, or add the config if not yet existing
  136. if grep "^files_compression_mode=" "$config_file"
  137. then
  138. ynh_replace_string --match_string="^files_compression_mode=.*" --replace_string="files_compression_mode=$files_compression" --target_file="$config_file"
  139. else
  140. echo "files_compression_mode=$files_compression" >> "$config_file"
  141. fi
  142. fi
  143. # Change ynh_core_backup in the config file
  144. ynh_replace_string --match_string="^ynh_core_backup=.*" --replace_string="ynh_core_backup=$ynh_core_backup" --target_file="$config_file"
  145. # Change ynh_app_backup in the config file
  146. if [ "$ynh_app_backup" = true ] && [ "$old_ynh_app_backup" = false ]
  147. then
  148. # If ynh_app_backup changed from false to true.
  149. # Add all current applications to the backup
  150. while read backup_app
  151. do
  152. ynh_print_info --message="Add a backup for the app $backup_app."
  153. ynh_replace_string --match_string="^ynh_app_backup=$" --replace_string="ynh_app_backup=$backup_app\n&" --target_file="$config_file"
  154. done <<< "$(yunohost app list -i | grep id: | sed 's/.*id: //')"
  155. elif [ "$ynh_app_backup" = false ] && [ "$old_ynh_app_backup" = true ]
  156. then
  157. # Remove all app currently backup
  158. # By deleting all line starting by 'ynh_app_backup=' and having something after '='
  159. sed -i "/^ynh_app_backup=.\+$/d" "$config_file"
  160. fi
  161. # Change frequency in the cron file and store the value into the settings
  162. ynh_app_setting_set --app=$app --key=frequency --value="$frequency"
  163. if [ "$frequency" = "Daily" ]; then
  164. cron_freq="0 2 * * *"
  165. run_freq="every day"
  166. elif [ "$frequency" = "Each 3 days" ]; then
  167. cron_freq="0 2 */3 * *"
  168. run_freq="each 3 days"
  169. elif [ "$frequency" = "Weekly" ]; then
  170. cron_freq="0 2 * * 0"
  171. run_freq="once a week on sunday"
  172. elif [ "$frequency" = "Biweekly" ]; then
  173. cron_freq="0 2 * * 0/2"
  174. run_freq="one sunday out of two"
  175. else # Monthly
  176. cron_freq="0 2 1 * *"
  177. run_freq="once a month on the first sunday"
  178. fi
  179. ynh_replace_string --match_string=".* root" --replace_string="$cron_freq root" --target_file=/etc/cron.d/$app
  180. # Change max_size in the config file
  181. ynh_replace_string --match_string="^max_size=.*" --replace_string="max_size=$max_size" --target_file="$config_file"
  182. #=================================================
  183. # MODIFY OVERWRITTING SETTINGS
  184. #=================================================
  185. # Set overwrite_cron
  186. ynh_app_setting_set --app=$app --key=overwrite_cron --value="$overwrite_cron"
  187. #=================================================
  188. # MODIFY EMAIL SETTING
  189. #=================================================
  190. # Set admin_mail_html
  191. ynh_app_setting_set --app=$app --key=admin_mail_html --value="$admin_mail_html"
  192. }
  193. #=================================================
  194. # GENERIC FINALIZATION
  195. #=================================================
  196. # SELECT THE ACTION FOLLOWING THE GIVEN ARGUMENT
  197. #=================================================
  198. case $1 in
  199. show) show_config;;
  200. apply) apply_config;;
  201. esac