_common.sh 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. #!/bin/bash
  2. #=================================================
  3. # PERSONAL HELPERS
  4. #=================================================
  5. #=================================================
  6. # BACKUP
  7. #=================================================
  8. HUMAN_SIZE () { # Transforme une taille en Ko en une taille lisible pour un humain
  9. human=$(numfmt --to=iec --from-unit=1K $1)
  10. echo $human
  11. }
  12. CHECK_SIZE () { # Vérifie avant chaque backup que l'espace est suffisant
  13. file_to_analyse=$1
  14. backup_size=$(du --summarize "$file_to_analyse" | cut -f1)
  15. free_space=$(df --output=avail "/home/yunohost.backup" | sed 1d)
  16. if [ $free_space -le $backup_size ]
  17. then
  18. ynh_print_err "Espace insuffisant pour sauvegarder $file_to_analyse."
  19. ynh_print_err "Espace disponible: $(HUMAN_SIZE $free_space)"
  20. ynh_die "Espace nécessaire: $(HUMAN_SIZE $backup_size)"
  21. fi
  22. }
  23. #=================================================
  24. # PACKAGE CHECK BYPASSING...
  25. #=================================================
  26. IS_PACKAGE_CHECK () {
  27. if [ ${PACKAGE_CHECK_EXEC:-0} -eq 1 ]
  28. then
  29. return 0
  30. else
  31. return 1
  32. fi
  33. }
  34. #=================================================
  35. # BOOLEAN CONVERTER
  36. #=================================================
  37. bool_to_01 () {
  38. local var="$1"
  39. [ "$var" = "true" ] && var=1
  40. [ "$var" = "false" ] && var=0
  41. echo "$var"
  42. }
  43. bool_to_true_false () {
  44. local var="$1"
  45. [ "$var" = "1" ] && var=true
  46. [ "$var" = "0" ] && var=false
  47. echo "$var"
  48. }
  49. #=================================================
  50. # FUTUR OFFICIAL HELPERS
  51. #=================================================
  52. # Install or update the main directory yunohost.multimedia
  53. #
  54. # usage: ynh_multimedia_build_main_dir
  55. ynh_multimedia_build_main_dir () {
  56. local ynh_media_release="v1.2"
  57. local checksum="806a827ba1902d6911095602a9221181"
  58. # Download yunohost.multimedia scripts
  59. wget -nv https://github.com/YunoHost-Apps/yunohost.multimedia/archive/${ynh_media_release}.tar.gz
  60. # Check the control sum
  61. echo "${checksum} ${ynh_media_release}.tar.gz" | md5sum -c --status \
  62. || ynh_die "Corrupt source"
  63. # Check if the package acl is installed. Or install it.
  64. ynh_package_is_installed 'acl' \
  65. || ynh_package_install acl
  66. # Extract
  67. mkdir yunohost.multimedia-master
  68. tar -xf ${ynh_media_release}.tar.gz -C yunohost.multimedia-master --strip-components 1
  69. ./yunohost.multimedia-master/script/ynh_media_build.sh
  70. }
  71. # Add a directory in yunohost.multimedia
  72. # This "directory" will be a symbolic link to a existing directory.
  73. #
  74. # usage: ynh_multimedia_addfolder "Source directory" "Destination directory"
  75. #
  76. # | arg: -s, --source_dir= - Source directory - The real directory which contains your medias.
  77. # | arg: -d, --dest_dir= - Destination directory - The name and the place of the symbolic link, relative to "/home/yunohost.multimedia"
  78. ynh_multimedia_addfolder () {
  79. # Declare an array to define the options of this helper.
  80. declare -Ar args_array=( [s]=source_dir= [d]=dest_dir= )
  81. local source_dir
  82. local dest_dir
  83. # Manage arguments with getopts
  84. ynh_handle_getopts_args "$@"
  85. ./yunohost.multimedia-master/script/ynh_media_addfolder.sh --source="$source_dir" --dest="$dest_dir"
  86. }
  87. # Move a directory in yunohost.multimedia, and replace by a symbolic link
  88. #
  89. # usage: ynh_multimedia_movefolder "Source directory" "Destination directory"
  90. #
  91. # | arg: -s, --source_dir= - Source directory - The real directory which contains your medias.
  92. # It will be moved to "Destination directory"
  93. # A symbolic link will replace it.
  94. # | arg: -d, --dest_dir= - Destination directory - The new name and place of the directory, relative to "/home/yunohost.multimedia"
  95. ynh_multimedia_movefolder () {
  96. # Declare an array to define the options of this helper.
  97. declare -Ar args_array=( [s]=source_dir= [d]=dest_dir= )
  98. local source_dir
  99. local dest_dir
  100. # Manage arguments with getopts
  101. ynh_handle_getopts_args "$@"
  102. ./yunohost.multimedia-master/script/ynh_media_addfolder.sh --inv --source="$source_dir" --dest="$dest_dir"
  103. }
  104. # Allow an user to have an write authorisation in multimedia directories
  105. #
  106. # usage: ynh_multimedia_addaccess user_name
  107. #
  108. # | arg: -u, --user_name= - The name of the user which gain this access.
  109. ynh_multimedia_addaccess () {
  110. # Declare an array to define the options of this helper.
  111. declare -Ar args_array=( [u]=user_name=)
  112. local user_name
  113. # Manage arguments with getopts
  114. ynh_handle_getopts_args "$@"
  115. groupadd -f multimedia
  116. usermod -a -G multimedia $user_name
  117. }
  118. #=================================================
  119. # EXPERIMENTAL HELPERS
  120. #=================================================
  121. # Send an email to inform the administrator
  122. #
  123. # usage: ynh_send_readme_to_admin --app_message=app_message [--recipients=recipients] [--type=type]
  124. # | arg: -m --app_message= - The file with the content to send to the administrator.
  125. # | arg: -r, --recipients= - The recipients of this email. Use spaces to separate multiples recipients. - default: root
  126. # example: "root admin@domain"
  127. # If you give the name of a YunoHost user, ynh_send_readme_to_admin will find its email adress for you
  128. # example: "root admin@domain user1 user2"
  129. # | arg: -t, --type= - Type of mail, could be 'backup', 'change_url', 'install', 'remove', 'restore', 'upgrade'
  130. ynh_send_readme_to_admin() {
  131. # Declare an array to define the options of this helper.
  132. declare -Ar args_array=( [m]=app_message= [r]=recipients= [t]=type= )
  133. local app_message
  134. local recipients
  135. local type
  136. # Manage arguments with getopts
  137. ynh_handle_getopts_args "$@"
  138. app_message="${app_message:-}"
  139. recipients="${recipients:-root}"
  140. type="${type:-install}"
  141. # Get the value of admin_mail_html
  142. admin_mail_html=$(ynh_app_setting_get $app admin_mail_html)
  143. admin_mail_html="${admin_mail_html:-0}"
  144. # Retrieve the email of users
  145. find_mails () {
  146. local list_mails="$1"
  147. local mail
  148. local recipients=" "
  149. # Read each mail in argument
  150. for mail in $list_mails
  151. do
  152. # Keep root or a real email address as it is
  153. if [ "$mail" = "root" ] || echo "$mail" | grep --quiet "@"
  154. then
  155. recipients="$recipients $mail"
  156. else
  157. # But replace an user name without a domain after by its email
  158. if mail=$(ynh_user_get_info "$mail" "mail" 2> /dev/null)
  159. then
  160. recipients="$recipients $mail"
  161. fi
  162. fi
  163. done
  164. echo "$recipients"
  165. }
  166. recipients=$(find_mails "$recipients")
  167. # Subject base
  168. local mail_subject="☁️🆈🅽🅷☁️: \`$app\`"
  169. # Adapt the subject according to the type of mail required.
  170. if [ "$type" = "backup" ]; then
  171. mail_subject="$mail_subject has just been backup."
  172. elif [ "$type" = "change_url" ]; then
  173. mail_subject="$mail_subject has just been moved to a new URL!"
  174. elif [ "$type" = "remove" ]; then
  175. mail_subject="$mail_subject has just been removed!"
  176. elif [ "$type" = "restore" ]; then
  177. mail_subject="$mail_subject has just been restored!"
  178. elif [ "$type" = "upgrade" ]; then
  179. mail_subject="$mail_subject has just been upgraded!"
  180. else # install
  181. mail_subject="$mail_subject has just been installed!"
  182. fi
  183. local mail_message="This is an automated message from your beloved YunoHost server.
  184. Specific information for the application $app.
  185. $(if [ -n "$app_message" ]
  186. then
  187. cat "$app_message"
  188. else
  189. echo "...No specific information..."
  190. fi)
  191. ---
  192. Automatic diagnosis data from YunoHost
  193. __PRE_TAG1__$(yunohost tools diagnosis | grep -B 100 "services:" | sed '/services:/d')__PRE_TAG2__"
  194. # Store the message into a file for further modifications.
  195. echo "$mail_message" > mail_to_send
  196. # If a html email is required. Apply html tags to the message.
  197. if [ "$admin_mail_html" -eq 1 ]
  198. then
  199. # Insert 'br' tags at each ending of lines.
  200. ynh_replace_string "$" "<br>" mail_to_send
  201. # Insert starting HTML tags
  202. sed --in-place '1s@^@<!DOCTYPE html>\n<html>\n<head></head>\n<body>\n@' mail_to_send
  203. # Keep tabulations
  204. ynh_replace_string " " "\&#160;\&#160;" mail_to_send
  205. ynh_replace_string "\t" "\&#160;\&#160;" mail_to_send
  206. # Insert url links tags
  207. ynh_replace_string "__URL_TAG1__\(.*\)__URL_TAG2__\(.*\)__URL_TAG3__" "<a href=\"\2\">\1</a>" mail_to_send
  208. # Insert pre tags
  209. ynh_replace_string "__PRE_TAG1__" "<pre>" mail_to_send
  210. ynh_replace_string "__PRE_TAG2__" "<\pre>" mail_to_send
  211. # Insert finishing HTML tags
  212. echo -e "\n</body>\n</html>" >> mail_to_send
  213. # Otherwise, remove tags to keep a plain text.
  214. else
  215. # Remove URL tags
  216. ynh_replace_string "__URL_TAG[1,3]__" "" mail_to_send
  217. ynh_replace_string "__URL_TAG2__" ": " mail_to_send
  218. # Remove PRE tags
  219. ynh_replace_string "__PRE_TAG[1-2]__" "" mail_to_send
  220. fi
  221. # Define binary to use for mail command
  222. if [ -e /usr/bin/bsd-mailx ]
  223. then
  224. local mail_bin=/usr/bin/bsd-mailx
  225. else
  226. local mail_bin=/usr/bin/mail.mailutils
  227. fi
  228. if [ "$admin_mail_html" -eq 1 ]
  229. then
  230. content_type="text/html"
  231. else
  232. content_type="text/plain"
  233. fi
  234. # Send the email to the recipients
  235. cat mail_to_send | $mail_bin -a "Content-Type: $content_type; charset=UTF-8" -s "$mail_subject" "$recipients"
  236. }
  237. #=================================================
  238. ynh_debian_release () {
  239. lsb_release --codename --short
  240. }
  241. is_stretch () {
  242. if [ "$(ynh_debian_release)" == "stretch" ]
  243. then
  244. return 0
  245. else
  246. return 1
  247. fi
  248. }
  249. is_jessie () {
  250. if [ "$(ynh_debian_release)" == "jessie" ]
  251. then
  252. return 0
  253. else
  254. return 1
  255. fi
  256. }
  257. #=================================================
  258. ynh_maintenance_mode_ON () {
  259. # Load value of $path_url and $domain from the config if their not set
  260. if [ -z $path_url ]; then
  261. path_url=$(ynh_app_setting_get $app path)
  262. fi
  263. if [ -z $domain ]; then
  264. domain=$(ynh_app_setting_get $app domain)
  265. fi
  266. mkdir -p /var/www/html/
  267. # Create an html to serve as maintenance notice
  268. echo "<!DOCTYPE html>
  269. <html>
  270. <head>
  271. <meta http-equiv="refresh" content="3">
  272. <title>Your app $app is currently under maintenance!</title>
  273. <style>
  274. body {
  275. width: 70em;
  276. margin: 0 auto;
  277. }
  278. </style>
  279. </head>
  280. <body>
  281. <h1>Your app $app is currently under maintenance!</h1>
  282. <p>This app has been put under maintenance by your administrator at $(date)</p>
  283. <p>Please wait until the maintenance operation is done. This page will be reloaded as soon as your app will be back.</p>
  284. </body>
  285. </html>" > "/var/www/html/maintenance.$app.html"
  286. # Create a new nginx config file to redirect all access to the app to the maintenance notice instead.
  287. echo "# All request to the app will be redirected to ${path_url}_maintenance and fall on the maintenance notice
  288. rewrite ^${path_url}/(.*)$ ${path_url}_maintenance/? redirect;
  289. # Use another location, to not be in conflict with the original config file
  290. location ${path_url}_maintenance/ {
  291. alias /var/www/html/ ;
  292. try_files maintenance.$app.html =503;
  293. # Include SSOWAT user panel.
  294. include conf.d/yunohost_panel.conf.inc;
  295. }" > "/etc/nginx/conf.d/$domain.d/maintenance.$app.conf"
  296. # The current config file will redirect all requests to the root of the app.
  297. # To keep the full path, we can use the following rewrite rule:
  298. # rewrite ^${path_url}/(.*)$ ${path_url}_maintenance/\$1? redirect;
  299. # The difference will be in the $1 at the end, which keep the following queries.
  300. # But, if it works perfectly for a html request, there's an issue with any php files.
  301. # This files are treated as simple files, and will be downloaded by the browser.
  302. # 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.
  303. systemctl reload nginx
  304. }
  305. ynh_maintenance_mode_OFF () {
  306. # Load value of $path_url and $domain from the config if their not set
  307. if [ -z $path_url ]; then
  308. path_url=$(ynh_app_setting_get $app path)
  309. fi
  310. if [ -z $domain ]; then
  311. domain=$(ynh_app_setting_get $app domain)
  312. fi
  313. # Rewrite the nginx config file to redirect from ${path_url}_maintenance to the real url of the app.
  314. echo "rewrite ^${path_url}_maintenance/(.*)$ ${path_url}/\$1 redirect;" > "/etc/nginx/conf.d/$domain.d/maintenance.$app.conf"
  315. systemctl reload nginx
  316. # Sleep 4 seconds to let the browser reload the pages and redirect the user to the app.
  317. sleep 4
  318. # Then remove the temporary files used for the maintenance.
  319. rm "/var/www/html/maintenance.$app.html"
  320. rm "/etc/nginx/conf.d/$domain.d/maintenance.$app.conf"
  321. systemctl reload nginx
  322. }
  323. #=================================================
  324. # Create a changelog for an app after an upgrade from the file CHANGELOG.md.
  325. #
  326. # usage: ynh_send_readme_to_admin [--format=markdown/html/plain] [--output=changelog_file] --changelog=changelog_source]
  327. # | arg: -f --format= - Format in which the changelog will be printed
  328. # markdown: Default format.
  329. # html: Turn urls into html format.
  330. # plain: Plain text changelog
  331. # | arg: -o --output= - Output file for the changelog file (Default ./changelog)
  332. # | arg: -c --changelog= - CHANGELOG.md source (Default ../CHANGELOG.md)
  333. #
  334. # The changelog is printed into the file ./changelog and ./changelog_lite
  335. ynh_app_changelog () {
  336. # Declare an array to define the options of this helper.
  337. local legacy_args=foc
  338. declare -Ar args_array=( [f]=format= [o]=output= [c]=changelog= )
  339. local format
  340. local output
  341. local changelog
  342. # Manage arguments with getopts
  343. ynh_handle_getopts_args "$@"
  344. format=${format:-markdown}
  345. output=${output:-changelog}
  346. changelog=${changelog:-../CHANGELOG.md}
  347. local original_changelog="$changelog"
  348. local temp_changelog="changelog_temp"
  349. local final_changelog="$output"
  350. if [ ! -n "$original_changelog" ]
  351. then
  352. echo "No changelog available..." > "$final_changelog"
  353. echo "No changelog available..." > "${final_changelog}_lite"
  354. return 0
  355. fi
  356. local current_version=$(ynh_read_manifest --manifest="/etc/yunohost/apps/$YNH_APP_INSTANCE_NAME/manifest.json" --manifest_key="version")
  357. local update_version=$(ynh_read_manifest --manifest="../manifest.json" --manifest_key="version")
  358. # Get the line of the version to update to into the changelog
  359. local update_version_line=$(grep --max-count=1 --line-number "^## $update_version" "$original_changelog" | cut -d':' -f1)
  360. # If there's no entry for this version yet into the changelog
  361. # Get the first available version
  362. if [ -z "$update_version_line" ]
  363. then
  364. update_version_line=$(grep --max-count=1 --line-number "^##" "$original_changelog" | cut -d':' -f1)
  365. fi
  366. # Get the length of the complete changelog.
  367. local changelog_length=$(wc --lines "$original_changelog" | awk '{print $1}')
  368. # Cut the file before the version to update to.
  369. tail --lines=$(( $changelog_length - $update_version_line + 1 )) "$original_changelog" > "$temp_changelog"
  370. # Get the length of the troncated changelog.
  371. changelog_length=$(wc --lines "$temp_changelog" | awk '{print $1}')
  372. # Get the line of the current version into the changelog
  373. # Keep only the last line found
  374. local current_version_line=$(grep --line-number "^## $current_version" "$temp_changelog" | cut -d':' -f1 | tail --lines=1)
  375. # If there's no entry for this version into the changelog
  376. # Get the last available version
  377. if [ -z "$current_version_line" ]
  378. then
  379. current_version_line=$(grep --line-number "^##" "$original_changelog" | cut -d':' -f1 | tail --lines=1)
  380. fi
  381. # Cut the file before the current version.
  382. # Then grep the previous version into the changelog to get the line number of the previous version
  383. local previous_version_line=$(tail --lines=$(( $changelog_length - $current_version_line )) \
  384. "$temp_changelog" | grep --max-count=1 --line-number "^##" | cut -d':' -f1)
  385. # If there's no previous version into the changelog
  386. # Go until the end of the changelog
  387. if [ -z "$previous_version_line" ]
  388. then
  389. previous_version_line=$changelog_length
  390. fi
  391. # Cut the file after the previous version to keep only the changelog between the current version and the version to update to.
  392. head --lines=$(( $current_version_line + $previous_version_line - 1 )) "$temp_changelog" | tee "$final_changelog"
  393. if [ "$format" = "html" ]
  394. then
  395. # Replace markdown links by html links
  396. ynh_replace_string --match_string="\[\(.*\)\](\(.*\)))" --replace_string="<a href=\"\2\">\1</a>)" --target_file="$final_changelog"
  397. ynh_replace_string --match_string="\[\(.*\)\](\(.*\))" --replace_string="<a href=\"\2\">\1</a>" --target_file="$final_changelog"
  398. elif [ "$format" = "plain" ]
  399. then
  400. # Change title format.
  401. ynh_replace_string --match_string="^\(##.* \)(\[\(.*\)\](\(.*\)))" --replace_string="\1- \2 \3" --target_file="$final_changelog"
  402. # Change modifications lines format.
  403. ynh_replace_string --match_string="^\([-*]\).*\[\(.*\)\]\(.*\)" --replace_string="\1 \2 \3" --target_file="$final_changelog"
  404. fi
  405. # else markdown. As the file is already in markdown, nothing to do.
  406. # Keep only important changes into the changelog
  407. # Remove all minor changes
  408. sed '/^-/d' "$final_changelog" > "${final_changelog}_lite"
  409. # Remove all blank lines (to keep a clear workspace)
  410. sed --in-place '/^$/d' "${final_changelog}_lite"
  411. # Add a blank line at the end
  412. echo "" >> "${final_changelog}_lite"
  413. # Clean titles if there's no significative changes
  414. local line
  415. local previous_line=""
  416. while read line <&3
  417. do
  418. if [ -n "$previous_line" ]
  419. then
  420. # Remove the line if it's a title or a blank line, and the previous one was a title as well.
  421. if ( [ "${line:0:1}" = "#" ] || [ ${#line} -eq 0 ] ) && [ "${previous_line:0:1}" = "#" ]
  422. then
  423. ynh_replace_special_string --match_string="${previous_line//[/.}" --replace_string="" --target_file="${final_changelog}_lite"
  424. fi
  425. fi
  426. previous_line="$line"
  427. done 3< "${final_changelog}_lite"
  428. # Remove all blank lines again
  429. sed --in-place '/^$/d' "${final_changelog}_lite"
  430. # Restore changelog format with blank lines
  431. ynh_replace_string --match_string="^##.*" --replace_string="\n\n&\n" --target_file="${final_changelog}_lite"
  432. # Remove the 2 first blank lines
  433. sed --in-place '1,2d' "${final_changelog}_lite"
  434. # Add a blank line at the end
  435. echo "" >> "${final_changelog}_lite"
  436. # If changelog are empty, add an info
  437. if [ $(wc --words "$final_changelog" | awk '{print $1}') -eq 0 ]
  438. then
  439. echo "No changes from the changelog..." > "$final_changelog"
  440. fi
  441. if [ $(wc --words "${final_changelog}_lite" | awk '{print $1}') -eq 0 ]
  442. then
  443. echo "No significative changes from the changelog..." > "${final_changelog}_lite"
  444. fi
  445. }
  446. #=================================================
  447. # Check the amount of available RAM
  448. #
  449. # usage: ynh_check_ram [--required=RAM required in Mb] [--no_swap|--only_swap] [--free_ram]
  450. # | arg: -r, --required= - Amount of RAM required in Mb. The helper will return 0 is there's enough RAM, or 1 otherwise.
  451. # If --required isn't set, the helper will print the amount of RAM, in Mb.
  452. # | arg: -s, --no_swap - Ignore swap
  453. # | arg: -o, --only_swap - Ignore real RAM, consider only swap.
  454. # | arg: -f, --free_ram - Count only free RAM, not the total amount of RAM available.
  455. ynh_check_ram () {
  456. # Declare an array to define the options of this helper.
  457. declare -Ar args_array=( [r]=required= [s]=no_swap [o]=only_swap [f]=free_ram )
  458. local required
  459. local no_swap
  460. local only_swap
  461. # Manage arguments with getopts
  462. ynh_handle_getopts_args "$@"
  463. required=${required:-}
  464. no_swap=${no_swap:-0}
  465. only_swap=${only_swap:-0}
  466. local total_ram=$(vmstat --stats --unit M | grep "total memory" | awk '{print $1}')
  467. local total_swap=$(vmstat --stats --unit M | grep "total swap" | awk '{print $1}')
  468. local total_ram_swap=$(( total_ram + total_swap ))
  469. local free_ram=$(vmstat --stats --unit M | grep "free memory" | awk '{print $1}')
  470. local free_swap=$(vmstat --stats --unit M | grep "free swap" | awk '{print $1}')
  471. local free_ram_swap=$(( free_ram + free_swap ))
  472. # Use the total amount of ram
  473. local ram=$total_ram_swap
  474. if [ $free_ram -eq 1 ]
  475. then
  476. # Use the total amount of free ram
  477. ram=$free_ram_swap
  478. if [ $no_swap -eq 1 ]
  479. then
  480. # Use only the amount of free ram
  481. ram=$free_ram
  482. elif [ $only_swap -eq 1 ]
  483. then
  484. # Use only the amount of free swap
  485. ram=$free_swap
  486. fi
  487. else
  488. if [ $no_swap -eq 1 ]
  489. then
  490. # Use only the amount of free ram
  491. ram=$total_ram
  492. elif [ $only_swap -eq 1 ]
  493. then
  494. # Use only the amount of free swap
  495. ram=$total_swap
  496. fi
  497. fi
  498. if [ -n "$required" ]
  499. then
  500. # Return 1 if the amount of ram isn't enough.
  501. if [ $ram -lt $required ]
  502. then
  503. return 1
  504. else
  505. return 0
  506. fi
  507. # If no RAM is required, return the amount of available ram.
  508. else
  509. echo $ram
  510. fi
  511. }
  512. #=================================================
  513. # Define the values to configure php-fpm
  514. #
  515. # usage: ynh_get_scalable_phpfpm --usage=usage --footprint=footprint [--print]
  516. # | arg: -f, --footprint - Memory footprint of the service (low/medium/high).
  517. # low - Less than 20Mb of ram by pool.
  518. # medium - Between 20Mb and 40Mb of ram by pool.
  519. # high - More than 40Mb of ram by pool.
  520. # Or specify exactly the footprint, the load of the service as Mb by pool instead of having a standard value.
  521. # To have this value, use the following command and stress the service.
  522. # watch -n0.5 ps -o user,cmd,%cpu,rss -u APP
  523. #
  524. # | arg: -u, --usage - Expected usage of the service (low/medium/high).
  525. # low - Personal usage, behind the sso.
  526. # medium - Low usage, few people or/and publicly accessible.
  527. # high - High usage, frequently visited website.
  528. #
  529. # | arg: -p, --print - Print the result
  530. #
  531. #
  532. #
  533. # The footprint of the service will be used to defined the maximum footprint we can allow, which is half the maximum RAM.
  534. # So it will be used to defined 'pm.max_children'
  535. # A lower value for the footprint will allow more children for 'pm.max_children'. And so for
  536. # 'pm.start_servers', 'pm.min_spare_servers' and 'pm.max_spare_servers' which are defined from the
  537. # value of 'pm.max_children'
  538. # NOTE: 'pm.max_children' can't exceed 4 times the number of processor's cores.
  539. #
  540. # The usage value will defined the way php will handle the children for the pool.
  541. # A value set as 'low' will set the process manager to 'ondemand'. Children will start only if the
  542. # service is used, otherwise no child will stay alive. This config gives the lower footprint when the
  543. # service is idle. But will use more proc since it has to start a child as soon it's used.
  544. # Set as 'medium', the process manager will be at dynamic. If the service is idle, a number of children
  545. # equal to pm.min_spare_servers will stay alive. So the service can be quick to answer to any request.
  546. # The number of children can grow if needed. The footprint can stay low if the service is idle, but
  547. # not null. The impact on the proc is a little bit less than 'ondemand' as there's always a few
  548. # children already available.
  549. # Set as 'high', the process manager will be set at 'static'. There will be always as many children as
  550. # 'pm.max_children', the footprint is important (but will be set as maximum a quarter of the maximum
  551. # RAM) but the impact on the proc is lower. The service will be quick to answer as there's always many
  552. # children ready to answer.
  553. ynh_get_scalable_phpfpm () {
  554. local legacy_args=ufp
  555. # Declare an array to define the options of this helper.
  556. declare -Ar args_array=( [u]=usage= [f]=footprint= [p]=print )
  557. local usage
  558. local footprint
  559. local print
  560. # Manage arguments with getopts
  561. ynh_handle_getopts_args "$@"
  562. # Set all characters as lowercase
  563. footprint=${footprint,,}
  564. usage=${usage,,}
  565. print=${print:-0}
  566. if [ "$footprint" = "low" ]
  567. then
  568. footprint=20
  569. elif [ "$footprint" = "medium" ]
  570. then
  571. footprint=35
  572. elif [ "$footprint" = "high" ]
  573. then
  574. footprint=50
  575. fi
  576. # Define the way the process manager handle child processes.
  577. if [ "$usage" = "low" ]
  578. then
  579. php_pm=ondemand
  580. elif [ "$usage" = "medium" ]
  581. then
  582. php_pm=dynamic
  583. elif [ "$usage" = "high" ]
  584. then
  585. php_pm=static
  586. else
  587. ynh_die --message="Does not recognize '$usage' as an usage value."
  588. fi
  589. # Get the total of RAM available, except swap.
  590. local max_ram=$(ynh_check_ram --no_swap)
  591. less0() {
  592. # Do not allow value below 1
  593. if [ $1 -le 0 ]
  594. then
  595. echo 1
  596. else
  597. echo $1
  598. fi
  599. }
  600. # Define pm.max_children
  601. # The value of pm.max_children is the total amount of ram divide by 2 and divide again by the footprint of a pool for this app.
  602. # So if php-fpm start the maximum of children, it won't exceed half of the ram.
  603. php_max_children=$(( $max_ram / 2 / $footprint ))
  604. # If process manager is set as static, use half less children.
  605. # Used as static, there's always as many children as the value of pm.max_children
  606. if [ "$php_pm" = "static" ]
  607. then
  608. php_max_children=$(( $php_max_children / 2 ))
  609. fi
  610. php_max_children=$(less0 $php_max_children)
  611. # To not overload the proc, limit the number of children to 4 times the number of cores.
  612. local core_number=$(nproc)
  613. local max_proc=$(( $core_number * 4 ))
  614. if [ $php_max_children -gt $max_proc ]
  615. then
  616. php_max_children=$max_proc
  617. fi
  618. if [ "$php_pm" = "dynamic" ]
  619. then
  620. # Define pm.start_servers, pm.min_spare_servers and pm.max_spare_servers for a dynamic process manager
  621. php_min_spare_servers=$(( $php_max_children / 8 ))
  622. php_min_spare_servers=$(less0 $php_min_spare_servers)
  623. php_max_spare_servers=$(( $php_max_children / 2 ))
  624. php_max_spare_servers=$(less0 $php_max_spare_servers)
  625. php_start_servers=$(( $php_min_spare_servers + ( $php_max_spare_servers - $php_min_spare_servers ) /2 ))
  626. php_start_servers=$(less0 $php_start_servers)
  627. else
  628. php_min_spare_servers=0
  629. php_max_spare_servers=0
  630. php_start_servers=0
  631. fi
  632. if [ $print -eq 1 ]
  633. then
  634. ynh_debug --message="Footprint=${footprint}Mb by pool."
  635. ynh_debug --message="Process manager=$php_pm"
  636. ynh_debug --message="Max RAM=${max_ram}Mb"
  637. if [ "$php_pm" != "static" ]; then
  638. ynh_debug --message="\nMax estimated footprint=$(( $php_max_children * $footprint ))"
  639. ynh_debug --message="Min estimated footprint=$(( $php_min_spare_servers * $footprint ))"
  640. fi
  641. if [ "$php_pm" = "dynamic" ]; then
  642. ynh_debug --message="Estimated average footprint=$(( $php_max_spare_servers * $footprint ))"
  643. elif [ "$php_pm" = "static" ]; then
  644. ynh_debug --message="Estimated footprint=$(( $php_max_children * $footprint ))"
  645. fi
  646. ynh_debug --message="\nRaw php-fpm values:"
  647. ynh_debug --message="pm.max_children = $php_max_children"
  648. if [ "$php_pm" = "dynamic" ]; then
  649. ynh_debug --message="pm.start_servers = $php_start_servers"
  650. ynh_debug --message="pm.min_spare_servers = $php_min_spare_servers"
  651. ynh_debug --message="pm.max_spare_servers = $php_max_spare_servers"
  652. fi
  653. fi
  654. }