config 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. # 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="1"
  48. else
  49. old_ynh_app_backup="0"
  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. # Overwrite cron file
  59. old_overwrite_cron="$(ynh_app_setting_get $app overwrite_cron)"
  60. overwrite_cron="${YNH_CONFIG_MAIN_OVERWRITE_FILES_OVERWRITE_CRON:-$old_overwrite_cron}"
  61. # Type of admin mail configuration
  62. old_admin_mail_html="$(ynh_app_setting_get $app admin_mail_html)"
  63. admin_mail_html="${YNH_CONFIG_MAIN_GLOBAL_CONFIG_EMAIL_TYPE:-$old_admin_mail_html}"
  64. #=================================================
  65. # SHOW_CONFIG FUNCTION FOR 'SHOW' COMMAND
  66. #=================================================
  67. show_config() {
  68. # here you are supposed to read some config file/database/other then print the values
  69. # ynh_return "YNH_CONFIG_${PANEL_ID}_${SECTION_ID}_${OPTION_ID}=value"
  70. ynh_return "YNH_CONFIG_MAIN_ENCRYPTION_ENCRYPT=$encrypt"
  71. ynh_return "YNH_CONFIG_MAIN_ENCRYPTION_ENCRYPTION_PWD="
  72. ynh_return "YNH_CONFIG_MAIN_BACKUP_TYPES_CORE_BACKUP=$ynh_core_backup"
  73. ynh_return "YNH_CONFIG_MAIN_BACKUP_TYPES_APPS_BACKUP=$ynh_app_backup"
  74. ynh_return "YNH_CONFIG_MAIN_BACKUP_OPTIONS_FREQUENCY=$frequency"
  75. ynh_return "YNH_CONFIG_MAIN_BACKUP_OPTIONS_MAX_SIZE=$max_size"
  76. ynh_return "YNH_CONFIG_MAIN_OVERWRITE_FILES_OVERWRITE_CRON=$overwrite_cron"
  77. ynh_return "YNH_CONFIG_MAIN_GLOBAL_CONFIG_EMAIL_TYPE=$admin_mail_html"
  78. }
  79. #=================================================
  80. # MODIFY THE CONFIGURATION
  81. #=================================================
  82. apply_config() {
  83. #=================================================
  84. # MODIFY THE PASSWORD
  85. #=================================================
  86. # Change the password if needed
  87. if [ "$encrypt" = "1" ]
  88. then
  89. ynh_print_OFF
  90. test -n "$encrypt_password" || ynh_die --message="The password for encryption can't be empty if you choose to enable encryption."
  91. ynh_print_ON
  92. # Replace the password by the previous one
  93. passkey="$final_path/passkey"
  94. ynh_print_OFF; echo "$encrypt_password" > "$passkey"; ynh_print_ON
  95. chmod 400 "$passkey"
  96. ynh_replace_string --match_string="^cryptpass=.*" --replace_string="cryptpass=$passkey" --target_file="$config_file"
  97. fi
  98. #=================================================
  99. # MODIFY ENCRYPT SETTING
  100. #=================================================
  101. # Change encrypt in the config file
  102. ynh_replace_string --match_string="^encrypt=.*" --replace_string="encrypt=$encrypt" --target_file="$config_file"
  103. #=================================================
  104. # MODIFY SETTINGS
  105. #=================================================
  106. # Change ynh_core_backup in the config file
  107. ynh_replace_string --match_string="^ynh_core_backup=.*" --replace_string="ynh_core_backup=$ynh_core_backup" --target_file="$config_file"
  108. # Change ynh_app_backup in the config file
  109. if [ "$ynh_app_backup" = "1" ] && [ "$old_ynh_app_backup" = "0" ]
  110. then
  111. # If ynh_app_backup changed from false to true.
  112. # Add all current applications to the backup
  113. while read backup_app
  114. do
  115. ynh_print_info --message="Add a backup for the app $backup_app."
  116. ynh_replace_string --match_string="^ynh_app_backup=$" --replace_string="ynh_app_backup=$backup_app\n&" --target_file="$config_file"
  117. done <<< "$(yunohost app list -i | grep id: | sed 's/.*id: //')"
  118. elif [ "$ynh_app_backup" = "0" ] && [ "$old_ynh_app_backup" = "1" ]
  119. then
  120. # Remove all app currently backup
  121. # By deleting all line starting by 'ynh_app_backup=' and having something after '='
  122. sed -i "/^ynh_app_backup=.\+$/d" "$config_file"
  123. fi
  124. # Change frequency in the cron file and store the value into the settings
  125. ynh_app_setting_set --app=$app --key=frequency --value="$frequency"
  126. if [ "$frequency" = "Daily" ]; then
  127. cron_freq="0 2 * * *"
  128. run_freq="every day"
  129. elif [ "$frequency" = "Each 3 days" ]; then
  130. cron_freq="0 2 */3 * *"
  131. run_freq="each 3 days"
  132. elif [ "$frequency" = "Weekly" ]; then
  133. cron_freq="0 2 * * 0"
  134. run_freq="once a week on sunday"
  135. elif [ "$frequency" = "Biweekly" ]; then
  136. cron_freq="0 2 * * 0/2"
  137. run_freq="one sunday out of two"
  138. else # Monthly
  139. cron_freq="0 2 1 * *"
  140. run_freq="once a month on the first sunday"
  141. fi
  142. ynh_replace_string --match_string=".* root" --replace_string="$cron_freq root" --target_file=/etc/cron.d/$app
  143. # Change max_size in the config file
  144. ynh_replace_string --match_string="^max_size=.*" --replace_string="max_size=$max_size" --target_file="$config_file"
  145. #=================================================
  146. # MODIFY OVERWRITTING SETTINGS
  147. #=================================================
  148. # Set overwrite_cron
  149. ynh_app_setting_set --app=$app --key=overwrite_cron --value="$overwrite_cron"
  150. #=================================================
  151. # MODIFY EMAIL SETTING
  152. #=================================================
  153. # Set admin_mail_html
  154. ynh_app_setting_set --app=$app --key=admin_mail_html --value="$admin_mail_html"
  155. }
  156. #=================================================
  157. # GENERIC FINALIZATION
  158. #=================================================
  159. # SELECT THE ACTION FOLLOWING THE GIVEN ARGUMENT
  160. #=================================================
  161. case $1 in
  162. show) show_config;;
  163. apply) apply_config;;
  164. esac