2
0

config 5.8 KB

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