2
0

config 6.6 KB

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