2
0

_common.sh 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #!/bin/bash
  2. #=================================================
  3. # DISPLAYING
  4. #=================================================
  5. NO_PRINT () { # Supprime l'affichage dans stdout pour la commande en argument.
  6. set +x
  7. $@
  8. set -x
  9. }
  10. WARNING () { # Écrit sur le canal d'erreur pour passer en warning.
  11. $@ >&2
  12. }
  13. SUPPRESS_WARNING () { # Force l'écriture sur la sortie standard
  14. $@ 2>&1
  15. }
  16. QUIET () { # Redirige la sortie standard dans /dev/null
  17. $@ > /dev/null
  18. }
  19. ALL_QUIET () { # Redirige la sortie standard et d'erreur dans /dev/null
  20. $@ > /dev/null 2>&1
  21. }
  22. #=================================================
  23. # BACKUP
  24. #=================================================
  25. HUMAN_SIZE () { # Transforme une taille en Ko en une taille lisible pour un humain
  26. human=$(numfmt --to=iec --from-unit=1K $1)
  27. echo $human
  28. }
  29. CHECK_SIZE () { # Vérifie avant chaque backup que l'espace est suffisant
  30. file_to_analyse=$1
  31. backup_size=$(du --summarize "$file_to_analyse" | cut -f1)
  32. free_space=$(df --output=avail "/home/yunohost.backup" | sed 1d)
  33. if [ $free_space -le $backup_size ]
  34. then
  35. WARNING echo "Espace insuffisant pour sauvegarder $file_to_analyse."
  36. WARNING echo "Espace disponible: $(HUMAN_SIZE $free_space)"
  37. ynh_die "Espace nécessaire: $(HUMAN_SIZE $backup_size)"
  38. fi
  39. }
  40. #=================================================
  41. # PACKAGE CHECK BYPASSING...
  42. #=================================================
  43. IS_PACKAGE_CHECK () { # Détermine une exécution en conteneur (Non testé)
  44. return $(uname -n | grep -c 'pchecker_lxc')
  45. }
  46. #=================================================
  47. # NODEJS
  48. #=================================================
  49. # INFOS
  50. # n (Node version management) utilise la variable PATH pour stocker le path de la version de node à utiliser.
  51. # C'est ainsi qu'il change de version
  52. # ynh_install_nodejs installe la version de nodejs demandée en argument, avec n
  53. # ynh_use_nodejs active une version de nodejs dans le script courant
  54. # 3 variables sont mises à disposition, et 2 sont stockées dans la config de l'app
  55. # - nodejs_path: Le chemin absolu de cette version de node
  56. # Utilisé pour des appels directs à node.
  57. # - nodejs_version: Simplement le numéro de version de nodejs pour cette application
  58. # - nodejs_use_version: Un alias pour charger une version de node dans le shell courant.
  59. # Utilisé pour démarrer un service ou un script qui utilise node ou npm
  60. # Dans ce cas, c'est $PATH qui contient le chemin de la version de node. Il doit être propagé sur les autres shell si nécessaire.
  61. n_install_dir="/opt/node_n"
  62. node_version_path="/opt/node_n/n/versions/node"
  63. # N_PREFIX est le dossier de n, il doit être chargé dans les variables d'environnement pour n.
  64. export N_PREFIX="$n_install_dir"
  65. ynh_install_n () {
  66. echo "Installation of N - Node.js version management" >&2
  67. # Build an app.src for n
  68. mkdir -p "../conf"
  69. echo "SOURCE_URL=https://github.com/tj/n/archive/v2.1.7.tar.gz
  70. SOURCE_SUM=2ba3c9d4dd3c7e38885b37e02337906a1ee91febe6d5c9159d89a9050f2eea8f" > "../conf/n.src"
  71. # Download and extract n
  72. ynh_setup_source "$n_install_dir/git" n
  73. # Install n
  74. (cd "$n_install_dir/git"
  75. PREFIX=$N_PREFIX make install 2>&1)
  76. }
  77. ynh_use_nodejs () {
  78. nodejs_version=$(ynh_app_setting_get $app nodejs_version)
  79. load_n_path="[[ :$PATH: == *\":$n_install_dir/bin:\"* ]] || PATH=\"$n_install_dir/bin:$PATH\"; N_PREFIX="$n_install_dir""
  80. nodejs_use_version="$n_install_dir/bin/n -q $nodejs_version"
  81. # "Load" a version of node
  82. eval $load_n_path; $nodejs_use_version
  83. # Get the absolute path of this version of node
  84. nodejs_path="$(n bin $nodejs_version)"
  85. # Make an alias for node use
  86. ynh_node_exec="eval $load_n_path; n use $nodejs_version"
  87. }
  88. ynh_install_nodejs () {
  89. # Use n, https://github.com/tj/n to manage the nodejs versions
  90. nodejs_version="$1"
  91. local n_install_script="https://git.io/n-install"
  92. # Create $n_install_dir
  93. mkdir -p "$n_install_dir"
  94. # Load n path in PATH
  95. CLEAR_PATH="$n_install_dir/bin:$PATH"
  96. # Remove /usr/local/bin in PATH in case of node has already setup.
  97. PATH=$(echo $CLEAR_PATH | sed 's@/usr/local/bin:@@')
  98. # Move an existing node binary, to avoid to block n.
  99. test -x /usr/bin/node && mv /usr/bin/node /usr/bin/node_n
  100. test -x /usr/bin/npm && mv /usr/bin/npm /usr/bin/npm_n
  101. # If n is not previously setup, install it
  102. if ! test n --version > /dev/null 2>&1
  103. then
  104. ynh_install_n
  105. fi
  106. # Modify the default N_PREFIX in n script
  107. ynh_replace_string "^N_PREFIX=\${N_PREFIX-.*}$" "N_PREFIX=\${N_PREFIX-$N_PREFIX}" "$n_install_dir/bin/n"
  108. # Restore /usr/local/bin in PATH
  109. PATH=$CLEAR_PATH
  110. # And replace the old node binary.
  111. test -x /usr/bin/node_n && mv /usr/bin/node_n /usr/bin/node
  112. test -x /usr/bin/npm_n && mv /usr/bin/npm_n /usr/bin/npm
  113. # Install the requested version of nodejs
  114. n $nodejs_version
  115. # Find the last "real" version for this major version of node.
  116. real_nodejs_version=$(find $node_version_path/$nodejs_version* -maxdepth 0 | sort --version-sort | tail --lines=1)
  117. real_nodejs_version=$(basename $real_nodejs_version)
  118. # Create a symbolic link for this major version. If the file doesn't already exist
  119. if [ ! -e "$node_version_path/$nodejs_version" ]
  120. then
  121. ln --symbolic --force --no-target-directory $node_version_path/$real_nodejs_version $node_version_path/$nodejs_version
  122. fi
  123. # Store the ID of this app and the version of node requested for it
  124. echo "$YNH_APP_ID:$nodejs_version" | tee --append "$n_install_dir/ynh_app_version"
  125. # Store nodejs_version into the config of this app
  126. ynh_app_setting_set $app nodejs_version $nodejs_version
  127. # Build the update script and set the cronjob
  128. ynh_cron_upgrade_node
  129. ynh_use_nodejs
  130. }
  131. ynh_remove_nodejs () {
  132. ynh_use_nodejs
  133. # Remove the line for this app
  134. sed --in-place "/$YNH_APP_ID:$nodejs_version/d" "$n_install_dir/ynh_app_version"
  135. # If none another app uses this version of nodejs, remove it.
  136. if ! grep --quiet "$nodejs_version" "$n_install_dir/ynh_app_version"
  137. then
  138. n rm $nodejs_version
  139. fi
  140. # If none another app uses n, remove n
  141. if [ ! -s "$n_install_dir/ynh_app_version" ]
  142. then
  143. ynh_secure_remove "$n_install_dir"
  144. ynh_secure_remove "/usr/local/n"
  145. sed --in-place "/N_PREFIX/d" /root/.bashrc
  146. fi
  147. }
  148. ynh_cron_upgrade_node () {
  149. # Build the update script
  150. cat > "$n_install_dir/node_update.sh" << EOF
  151. #!/bin/bash
  152. version_path="$node_version_path"
  153. n_install_dir="$n_install_dir"
  154. # Log the date
  155. date
  156. # List all real installed version of node
  157. all_real_version="\$(find \$version_path/* -maxdepth 0 -type d | sed "s@\$version_path/@@g")"
  158. # Keep only the major version number of each line
  159. all_real_version=\$(echo "\$all_real_version" | sed 's/\..*\$//')
  160. # Remove double entries
  161. all_real_version=\$(echo "\$all_real_version" | sort --unique)
  162. # Read each major version
  163. while read version
  164. do
  165. echo "Update of the version \$version"
  166. sudo \$n_install_dir/bin/n \$version
  167. # Find the last "real" version for this major version of node.
  168. real_nodejs_version=\$(find \$version_path/\$version* -maxdepth 0 | sort --version-sort | tail --lines=1)
  169. real_nodejs_version=\$(basename \$real_nodejs_version)
  170. # Update the symbolic link for this version
  171. sudo ln --symbolic --force --no-target-directory \$version_path/\$real_nodejs_version \$version_path/\$version
  172. done <<< "\$(echo "\$all_real_version")"
  173. EOF
  174. chmod +x "$n_install_dir/node_update.sh"
  175. # Build the cronjob
  176. cat > "/etc/cron.daily/node_update" << EOF
  177. #!/bin/bash
  178. $n_install_dir/node_update.sh >> $n_install_dir/node_update.log
  179. EOF
  180. chmod +x "/etc/cron.daily/node_update"
  181. }
  182. #=================================================
  183. #============= FUTURE YUNOHOST HELPER ============
  184. #=================================================
  185. # Delete a file checksum from the app settings
  186. #
  187. # $app should be defined when calling this helper
  188. #
  189. # usage: ynh_remove_file_checksum file
  190. # | arg: file - The file for which the checksum will be deleted
  191. ynh_delete_file_checksum () {
  192. local checksum_setting_name=checksum_${1//[\/ ]/_} # Replace all '/' and ' ' by '_'
  193. ynh_app_setting_delete $app $checksum_setting_name
  194. }