_common.sh 14 KB

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