config 9.2 KB

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