_common.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. #!/bin/bash
  2. #=================================================
  3. # COMMON VARIABLES
  4. #=================================================
  5. # dependencies used by the app
  6. pkg_dependencies="rsync encfs sshpass ccrypt lzop zstd lzip"
  7. #=================================================
  8. # PERSONAL HELPERS
  9. #=================================================
  10. #=================================================
  11. # EXPERIMENTAL HELPERS
  12. #=================================================
  13. # Send an email to inform the administrator
  14. #
  15. # usage: ynh_send_readme_to_admin --app_message=app_message [--recipients=recipients] [--type=type]
  16. # | arg: -m --app_message= - The file with the content to send to the administrator.
  17. # | arg: -r, --recipients= - The recipients of this email. Use spaces to separate multiples recipients. - default: root
  18. # example: "root admin@domain"
  19. # If you give the name of a YunoHost user, ynh_send_readme_to_admin will find its email adress for you
  20. # example: "root admin@domain user1 user2"
  21. # | arg: -t, --type= - Type of mail, could be 'backup', 'change_url', 'install', 'remove', 'restore', 'upgrade'
  22. ynh_send_readme_to_admin() {
  23. # Declare an array to define the options of this helper.
  24. declare -Ar args_array=( [m]=app_message= [r]=recipients= [t]=type= )
  25. local app_message
  26. local recipients
  27. local type
  28. # Manage arguments with getopts
  29. ynh_handle_getopts_args "$@"
  30. app_message="${app_message:-}"
  31. recipients="${recipients:-root}"
  32. type="${type:-install}"
  33. # Get the value of admin_mail_html
  34. admin_mail_html=$(ynh_app_setting_get $app admin_mail_html)
  35. admin_mail_html="${admin_mail_html:-0}"
  36. # Retrieve the email of users
  37. find_mails () {
  38. local list_mails="$1"
  39. local mail
  40. local recipients=" "
  41. # Read each mail in argument
  42. for mail in $list_mails
  43. do
  44. # Keep root or a real email address as it is
  45. if [ "$mail" = "root" ] || echo "$mail" | grep --quiet "@"
  46. then
  47. recipients="$recipients $mail"
  48. else
  49. # But replace an user name without a domain after by its email
  50. if mail=$(ynh_user_get_info "$mail" "mail" 2> /dev/null)
  51. then
  52. recipients="$recipients $mail"
  53. fi
  54. fi
  55. done
  56. echo "$recipients"
  57. }
  58. recipients=$(find_mails "$recipients")
  59. # Subject base
  60. local mail_subject="☁️🆈🅽🅷☁️: \`$app\`"
  61. # Adapt the subject according to the type of mail required.
  62. if [ "$type" = "backup" ]; then
  63. mail_subject="$mail_subject has just been backup."
  64. elif [ "$type" = "change_url" ]; then
  65. mail_subject="$mail_subject has just been moved to a new URL!"
  66. elif [ "$type" = "remove" ]; then
  67. mail_subject="$mail_subject has just been removed!"
  68. elif [ "$type" = "restore" ]; then
  69. mail_subject="$mail_subject has just been restored!"
  70. elif [ "$type" = "upgrade" ]; then
  71. mail_subject="$mail_subject has just been upgraded!"
  72. else # install
  73. mail_subject="$mail_subject has just been installed!"
  74. fi
  75. local mail_message="This is an automated message from your beloved YunoHost server.
  76. Specific information for the application $app.
  77. $(if [ -n "$app_message" ]
  78. then
  79. cat "$app_message"
  80. else
  81. echo "...No specific information..."
  82. fi)
  83. ---
  84. Automatic diagnosis data from YunoHost
  85. __PRE_TAG1__$(yunohost tools diagnosis | grep -B 100 "services:" | sed '/services:/d')__PRE_TAG2__"
  86. # Store the message into a file for further modifications.
  87. echo "$mail_message" > mail_to_send
  88. # If a html email is required. Apply html tags to the message.
  89. if [ "$admin_mail_html" -eq 1 ]
  90. then
  91. # Insert 'br' tags at each ending of lines.
  92. ynh_replace_string "$" "<br>" mail_to_send
  93. # Insert starting HTML tags
  94. sed --in-place '1s@^@<!DOCTYPE html>\n<html>\n<head></head>\n<body>\n@' mail_to_send
  95. # Keep tabulations
  96. ynh_replace_string " " "\&#160;\&#160;" mail_to_send
  97. ynh_replace_string "\t" "\&#160;\&#160;" mail_to_send
  98. # Insert url links tags
  99. ynh_replace_string "__URL_TAG1__\(.*\)__URL_TAG2__\(.*\)__URL_TAG3__" "<a href=\"\2\">\1</a>" mail_to_send
  100. # Insert pre tags
  101. ynh_replace_string "__PRE_TAG1__" "<pre>" mail_to_send
  102. ynh_replace_string "__PRE_TAG2__" "<\pre>" mail_to_send
  103. # Insert finishing HTML tags
  104. echo -e "\n</body>\n</html>" >> mail_to_send
  105. # Otherwise, remove tags to keep a plain text.
  106. else
  107. # Remove URL tags
  108. ynh_replace_string "__URL_TAG[1,3]__" "" mail_to_send
  109. ynh_replace_string "__URL_TAG2__" ": " mail_to_send
  110. # Remove PRE tags
  111. ynh_replace_string "__PRE_TAG[1-2]__" "" mail_to_send
  112. fi
  113. # Define binary to use for mail command
  114. if [ -e /usr/bin/bsd-mailx ]
  115. then
  116. local mail_bin=/usr/bin/bsd-mailx
  117. else
  118. local mail_bin=/usr/bin/mail.mailutils
  119. fi
  120. if [ "$admin_mail_html" -eq 1 ]
  121. then
  122. content_type="text/html"
  123. else
  124. content_type="text/plain"
  125. fi
  126. # Send the email to the recipients
  127. cat mail_to_send | $mail_bin -a "Content-Type: $content_type; charset=UTF-8" -s "$mail_subject" "$recipients"
  128. }
  129. #=================================================
  130. ynh_maintenance_mode_ON () {
  131. # Load value of $path_url and $domain from the config if their not set
  132. if [ -z $path_url ]; then
  133. path_url=$(ynh_app_setting_get $app path)
  134. fi
  135. if [ -z $domain ]; then
  136. domain=$(ynh_app_setting_get $app domain)
  137. fi
  138. mkdir -p /var/www/html/
  139. # Create an html to serve as maintenance notice
  140. echo "<!DOCTYPE html>
  141. <html>
  142. <head>
  143. <meta http-equiv="refresh" content="3">
  144. <title>Your app $app is currently under maintenance!</title>
  145. <style>
  146. body {
  147. width: 70em;
  148. margin: 0 auto;
  149. }
  150. </style>
  151. </head>
  152. <body>
  153. <h1>Your app $app is currently under maintenance!</h1>
  154. <p>This app has been put under maintenance by your administrator at $(date)</p>
  155. <p>Please wait until the maintenance operation is done. This page will be reloaded as soon as your app will be back.</p>
  156. </body>
  157. </html>" > "/var/www/html/maintenance.$app.html"
  158. # Create a new nginx config file to redirect all access to the app to the maintenance notice instead.
  159. echo "# All request to the app will be redirected to ${path_url}_maintenance and fall on the maintenance notice
  160. rewrite ^${path_url}/(.*)$ ${path_url}_maintenance/? redirect;
  161. # Use another location, to not be in conflict with the original config file
  162. location ${path_url}_maintenance/ {
  163. alias /var/www/html/ ;
  164. try_files maintenance.$app.html =503;
  165. # Include SSOWAT user panel.
  166. include conf.d/yunohost_panel.conf.inc;
  167. }" > "/etc/nginx/conf.d/$domain.d/maintenance.$app.conf"
  168. # The current config file will redirect all requests to the root of the app.
  169. # To keep the full path, we can use the following rewrite rule:
  170. # rewrite ^${path_url}/(.*)$ ${path_url}_maintenance/\$1? redirect;
  171. # The difference will be in the $1 at the end, which keep the following queries.
  172. # But, if it works perfectly for a html request, there's an issue with any php files.
  173. # This files are treated as simple files, and will be downloaded by the browser.
  174. # Would be really be nice to be able to fix that issue. So that, when the page is reloaded after the maintenance, the user will be redirected to the real page he was.
  175. systemctl reload nginx
  176. }
  177. ynh_maintenance_mode_OFF () {
  178. # Load value of $path_url and $domain from the config if their not set
  179. if [ -z $path_url ]; then
  180. path_url=$(ynh_app_setting_get $app path)
  181. fi
  182. if [ -z $domain ]; then
  183. domain=$(ynh_app_setting_get $app domain)
  184. fi
  185. # Rewrite the nginx config file to redirect from ${path_url}_maintenance to the real url of the app.
  186. echo "rewrite ^${path_url}_maintenance/(.*)$ ${path_url}/\$1 redirect;" > "/etc/nginx/conf.d/$domain.d/maintenance.$app.conf"
  187. systemctl reload nginx
  188. # Sleep 4 seconds to let the browser reload the pages and redirect the user to the app.
  189. sleep 4
  190. # Then remove the temporary files used for the maintenance.
  191. rm "/var/www/html/maintenance.$app.html"
  192. rm "/etc/nginx/conf.d/$domain.d/maintenance.$app.conf"
  193. systemctl reload nginx
  194. }
  195. #=================================================
  196. # Create a changelog for an app after an upgrade from the file CHANGELOG.md.
  197. #
  198. # usage: ynh_app_changelog [--format=markdown/html/plain] [--output=changelog_file] --changelog=changelog_source]
  199. # | arg: -f --format= - Format in which the changelog will be printed
  200. # markdown: Default format.
  201. # html: Turn urls into html format.
  202. # plain: Plain text changelog
  203. # | arg: -o --output= - Output file for the changelog file (Default ./changelog)
  204. # | arg: -c --changelog= - CHANGELOG.md source (Default ../CHANGELOG.md)
  205. #
  206. # The changelog is printed into the file ./changelog and ./changelog_lite
  207. ynh_app_changelog () {
  208. # Declare an array to define the options of this helper.
  209. local legacy_args=foc
  210. declare -Ar args_array=( [f]=format= [o]=output= [c]=changelog= )
  211. local format
  212. local output
  213. local changelog
  214. # Manage arguments with getopts
  215. ynh_handle_getopts_args "$@"
  216. format=${format:-markdown}
  217. output=${output:-changelog}
  218. changelog=${changelog:-../CHANGELOG.md}
  219. local original_changelog="$changelog"
  220. local temp_changelog="changelog_temp"
  221. local final_changelog="$output"
  222. if [ ! -n "$original_changelog" ]
  223. then
  224. echo "No changelog available..." > "$final_changelog"
  225. echo "No changelog available..." > "${final_changelog}_lite"
  226. return 0
  227. fi
  228. local current_version=$(ynh_read_manifest --manifest="/etc/yunohost/apps/$YNH_APP_INSTANCE_NAME/manifest.json" --manifest_key="version")
  229. local update_version=$(ynh_read_manifest --manifest="../manifest.json" --manifest_key="version")
  230. # Get the line of the version to update to into the changelog
  231. local update_version_line=$(grep --max-count=1 --line-number "^## \[$update_version" "$original_changelog" | cut -d':' -f1)
  232. # If there's no entry for this version yet into the changelog
  233. # Get the first available version
  234. if [ -z "$update_version_line" ]
  235. then
  236. update_version_line=$(grep --max-count=1 --line-number "^##" "$original_changelog" | cut -d':' -f1)
  237. fi
  238. # Get the length of the complete changelog.
  239. local changelog_length=$(wc --lines "$original_changelog" | awk '{print $1}')
  240. # Cut the file before the version to update to.
  241. tail --lines=$(( $changelog_length - $update_version_line + 1 )) "$original_changelog" > "$temp_changelog"
  242. # Get the length of the troncated changelog.
  243. changelog_length=$(wc --lines "$temp_changelog" | awk '{print $1}')
  244. # Get the line of the current version into the changelog
  245. # Keep only the last line found
  246. local current_version_line=$(grep --line-number "^## \[$current_version" "$temp_changelog" | cut -d':' -f1 | tail --lines=1)
  247. # If there's no entry for this version into the changelog
  248. # Get the last available version
  249. if [ -z "$current_version_line" ]
  250. then
  251. current_version_line=$(grep --line-number "^##" "$original_changelog" | cut -d':' -f1 | tail --lines=1)
  252. fi
  253. # Cut the file before the current version.
  254. # Then grep the previous version into the changelog to get the line number of the previous version
  255. local previous_version_line=$(tail --lines=$(( $changelog_length - $current_version_line )) \
  256. "$temp_changelog" | grep --max-count=1 --line-number "^## " | cut -d':' -f1)
  257. # If there's no previous version into the changelog
  258. # Go until the end of the changelog
  259. if [ -z "$previous_version_line" ]
  260. then
  261. previous_version_line=$changelog_length
  262. fi
  263. # Cut the file after the previous version to keep only the changelog between the current version and the version to update to.
  264. head --lines=$(( $current_version_line + $previous_version_line - 1 )) "$temp_changelog" | tee "$final_changelog"
  265. if [ "$format" = "html" ]
  266. then
  267. # Replace markdown links by html links
  268. ynh_replace_string --match_string="\[\(.*\)\](\(.*\)))" --replace_string="<a href=\"\2\">\1</a>)" --target_file="$final_changelog"
  269. ynh_replace_string --match_string="\[\(.*\)\](\(.*\))" --replace_string="<a href=\"\2\">\1</a>" --target_file="$final_changelog"
  270. elif [ "$format" = "plain" ]
  271. then
  272. # Change title format.
  273. ynh_replace_string --match_string="^##.*\[\(.*\)\](\(.*\)) - \(.*\)$" --replace_string="## \1 (\3) - \2" --target_file="$final_changelog"
  274. # Change modifications lines format.
  275. ynh_replace_string --match_string="^\([-*]\).*\[\(.*\)\]\(.*\)" --replace_string="\1 \2 \3" --target_file="$final_changelog"
  276. fi
  277. # else markdown. As the file is already in markdown, nothing to do.
  278. # Keep only important changes into the changelog
  279. # Remove all minor changes
  280. sed '/^-/d' "$final_changelog" > "${final_changelog}_lite"
  281. # Remove all blank lines (to keep a clear workspace)
  282. sed --in-place '/^$/d' "${final_changelog}_lite"
  283. # Add a blank line at the end
  284. echo "" >> "${final_changelog}_lite"
  285. # Clean titles if there's no significative changes
  286. local line
  287. local previous_line=""
  288. while read line <&3
  289. do
  290. if [ -n "$previous_line" ]
  291. then
  292. # Remove the line if it's a title or a blank line, and the previous one was a title as well.
  293. if ( [ "${line:0:1}" = "#" ] || [ ${#line} -eq 0 ] ) && [ "${previous_line:0:1}" = "#" ]
  294. then
  295. ynh_replace_special_string --match_string="${previous_line//[/.}" --replace_string="" --target_file="${final_changelog}_lite"
  296. fi
  297. fi
  298. previous_line="$line"
  299. done 3< "${final_changelog}_lite"
  300. # Remove all blank lines again
  301. sed --in-place '/^$/d' "${final_changelog}_lite"
  302. # Restore changelog format with blank lines
  303. ynh_replace_string --match_string="^##.*" --replace_string="\n\n&\n" --target_file="${final_changelog}_lite"
  304. # Remove the 2 first blank lines
  305. sed --in-place '1,2d' "${final_changelog}_lite"
  306. # Add a blank line at the end
  307. echo "" >> "${final_changelog}_lite"
  308. # If changelog are empty, add an info
  309. if [ $(wc --words "$final_changelog" | awk '{print $1}') -eq 0 ]
  310. then
  311. echo "No changes from the changelog..." > "$final_changelog"
  312. fi
  313. if [ $(wc --words "${final_changelog}_lite" | awk '{print $1}') -eq 0 ]
  314. then
  315. echo "No significative changes from the changelog..." > "${final_changelog}_lite"
  316. fi
  317. }