_common.sh 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. #!/bin/bash
  2. # =============================================================================
  3. # YUNOHOST 2.6 FORTHCOMING HELPERS
  4. # (will be part of YunoHost 2.6, so won't be necessary any more after
  5. # YunoHost 2.6 gets widespread)
  6. # =============================================================================
  7. # Normalize the url path syntax
  8. # Handle the slash at the beginning of path and its absence at ending
  9. # Return a normalized url path
  10. #
  11. # example: url_path=$(ynh_normalize_url_path $url_path)
  12. # ynh_normalize_url_path example -> /example
  13. # ynh_normalize_url_path /example -> /example
  14. # ynh_normalize_url_path /example/ -> /example
  15. # ynh_normalize_url_path / -> /
  16. #
  17. # usage: ynh_normalize_url_path path_to_normalize
  18. # | arg: url_path_to_normalize - URL path to normalize before using it
  19. ynh_normalize_url_path () {
  20. path_url=$1
  21. test -n "$path_url" || ynh_die "ynh_normalize_url_path expect a URL path as first argument and received nothing."
  22. if [ "${path_url:0:1}" != "/" ]; then # If the first character is not a /
  23. path_url="/$path_url" # Add / at begin of path variable
  24. fi
  25. if [ "${path_url:${#path_url}-1}" == "/" ] && [ ${#path_url} -gt 1 ]; then # If the last character is a / and that not the only character.
  26. path_url="${path_url:0:${#path_url}-1}" # Delete the last character
  27. fi
  28. echo $path_url
  29. }
  30. # Check if a mysql user exists
  31. #
  32. # usage: ynh_mysql_user_exists user
  33. # | arg: user - the user for which to check existence
  34. function ynh_mysql_user_exists()
  35. {
  36. local user=$1
  37. if [[ -z $(ynh_mysql_execute_as_root "SELECT User from mysql.user WHERE User = '$user';") ]]
  38. then
  39. return 1
  40. else
  41. return 0
  42. fi
  43. }
  44. # Create a database, an user and its password. Then store the password in the app's config
  45. #
  46. # After executing this helper, the password of the created database will be available in $db_pwd
  47. # It will also be stored as "mysqlpwd" into the app settings.
  48. #
  49. # usage: ynh_mysql_setup_db user name [pwd]
  50. # | arg: user - Owner of the database
  51. # | arg: name - Name of the database
  52. # | arg: pwd - Password of the database. If not given, a password will be generated
  53. ynh_mysql_setup_db () {
  54. local db_user="$1"
  55. local db_name="$2"
  56. db_pwd=$(ynh_string_random) # Generate a random password
  57. ynh_mysql_create_db "$db_name" "$db_user" "$db_pwd" # Create the database
  58. ynh_app_setting_set $app mysqlpwd $db_pwd # Store the password in the app's config
  59. }
  60. # Remove a database if it exists, and the associated user
  61. #
  62. # usage: ynh_mysql_remove_db user name
  63. # | arg: user - Owner of the database
  64. # | arg: name - Name of the database
  65. ynh_mysql_remove_db () {
  66. local db_user="$1"
  67. local db_name="$2"
  68. local mysql_root_password=$(sudo cat $MYSQL_ROOT_PWD_FILE)
  69. if mysqlshow -u root -p$mysql_root_password | grep -q "^| $db_name"; then # Check if the database exists
  70. echo "Removing database $db_name" >&2
  71. ynh_mysql_drop_db $db_name # Remove the database
  72. else
  73. echo "Database $db_name not found" >&2
  74. fi
  75. # Remove mysql user if it exists
  76. if $(ynh_mysql_user_exists $db_user); then
  77. ynh_mysql_drop_user $db_user
  78. fi
  79. }
  80. # Correct the name given in argument for mariadb
  81. #
  82. # Avoid invalid name for your database
  83. #
  84. # Exemple: dbname=$(ynh_make_valid_dbid $app)
  85. #
  86. # usage: ynh_make_valid_dbid name
  87. # | arg: name - name to correct
  88. # | ret: the corrected name
  89. ynh_sanitize_dbid () {
  90. dbid=${1//[-.]/_} # We should avoid having - and . in the name of databases. They are replaced by _
  91. echo $dbid
  92. }
  93. # Manage a fail of the script
  94. #
  95. # Print a warning to inform that the script was failed
  96. # Execute the ynh_clean_setup function if used in the app script
  97. #
  98. # usage of ynh_clean_setup function
  99. # This function provide a way to clean some residual of installation that not managed by remove script.
  100. # To use it, simply add in your script:
  101. # ynh_clean_setup () {
  102. # instructions...
  103. # }
  104. # This function is optionnal.
  105. #
  106. # Usage: ynh_exit_properly is used only by the helper ynh_abort_if_errors.
  107. # You must not use it directly.
  108. ynh_exit_properly () {
  109. exit_code=$?
  110. if [ "$exit_code" -eq 0 ]; then
  111. exit 0 # Exit without error if the script ended correctly
  112. fi
  113. trap '' EXIT # Ignore new exit signals
  114. set +eu # Do not exit anymore if a command fail or if a variable is empty
  115. echo -e "!!\n $app's script has encountered an error. Its execution was cancelled.\n!!" >&2
  116. if type -t ynh_clean_setup > /dev/null; then # Check if the function exist in the app script.
  117. ynh_clean_setup # Call the function to do specific cleaning for the app.
  118. fi
  119. ynh_die # Exit with error status
  120. }
  121. # Exit if an error occurs during the execution of the script.
  122. #
  123. # Stop immediatly the execution if an error occured or if a empty variable is used.
  124. # The execution of the script is derivate to ynh_exit_properly function before exit.
  125. #
  126. # Usage: ynh_abort_if_errors
  127. ynh_abort_if_errors () {
  128. set -eu # Exit if a command fail, and if a variable is used unset.
  129. trap ynh_exit_properly EXIT # Capturing exit signals on shell script
  130. }
  131. # Define and install dependencies with a equivs control file
  132. # This helper can/should only be called once per app
  133. #
  134. # usage: ynh_install_app_dependencies dep [dep [...]]
  135. # | arg: dep - the package name to install in dependence
  136. ynh_install_app_dependencies () {
  137. dependencies=$@
  138. manifest_path="../manifest.json"
  139. if [ ! -e "$manifest_path" ]; then
  140. manifest_path="../settings/manifest.json" # Into the restore script, the manifest is not at the same place
  141. fi
  142. version=$(sudo grep '\"version\": ' "$manifest_path" | cut -d '"' -f 4) # Retrieve the version number in the manifest file.
  143. dep_app=${app//_/-} # Replace all '_' by '-'
  144. if ynh_package_is_installed "${dep_app}-ynh-deps"; then
  145. echo "A package named ${dep_app}-ynh-deps is already installed" >&2
  146. else
  147. cat > ./${dep_app}-ynh-deps.control << EOF # Make a control file for equivs-build
  148. Section: misc
  149. Priority: optional
  150. Package: ${dep_app}-ynh-deps
  151. Version: ${version}
  152. Depends: ${dependencies// /, }
  153. Architecture: all
  154. Description: Fake package for ${app} (YunoHost app) dependencies
  155. This meta-package is only responsible of installing its dependencies.
  156. EOF
  157. ynh_package_install_from_equivs ./${dep_app}-ynh-deps.control \
  158. || ynh_die "Unable to install dependencies" # Install the fake package and its dependencies
  159. ynh_app_setting_set $app apt_dependencies $dependencies
  160. fi
  161. }
  162. # Remove fake package and its dependencies
  163. #
  164. # Dependencies will removed only if no other package need them.
  165. #
  166. # usage: ynh_remove_app_dependencies
  167. ynh_remove_app_dependencies () {
  168. dep_app=${app//_/-} # Replace all '_' by '-'
  169. ynh_package_autoremove ${dep_app}-ynh-deps # Remove the fake package and its dependencies if they not still used.
  170. }
  171. # Use logrotate to manage the logfile
  172. #
  173. # usage: ynh_use_logrotate [logfile]
  174. # | arg: logfile - absolute path of logfile
  175. #
  176. # If no argument provided, a standard directory will be use. /var/log/${app}
  177. # You can provide a path with the directory only or with the logfile.
  178. # /parentdir/logdir/
  179. # /parentdir/logdir/logfile.log
  180. #
  181. # It's possible to use this helper several times, each config will added to same logrotate config file.
  182. ynh_use_logrotate () {
  183. if [ "$#" -gt 0 ]; then
  184. if [ "$(echo ${1##*.})" == "log" ]; then # Keep only the extension to check if it's a logfile
  185. logfile=$1 # In this case, focus logrotate on the logfile
  186. else
  187. logfile=$1/.log # Else, uses the directory and all logfile into it.
  188. fi
  189. else
  190. logfile="/var/log/${app}/.log" # Without argument, use a defaut directory in /var/log
  191. fi
  192. cat > ./${app}-logrotate << EOF # Build a config file for logrotate
  193. $logfile {
  194. # Rotate if the logfile exceeds 100Mo
  195. size 100M
  196. # Keep 12 old log maximum
  197. rotate 12
  198. # Compress the logs with gzip
  199. compress
  200. # Compress the log at the next cycle. So keep always 2 non compressed logs
  201. delaycompress
  202. # Copy and truncate the log to allow to continue write on it. Instead of move the log.
  203. copytruncate
  204. # Do not do an error if the log is missing
  205. missingok
  206. # Not rotate if the log is empty
  207. notifempty
  208. # Keep old logs in the same dir
  209. noolddir
  210. }
  211. EOF
  212. sudo mkdir -p $(dirname "$logfile") # Create the log directory, if not exist
  213. cat ${app}-logrotate | sudo tee -a /etc/logrotate.d/$app > /dev/null # Append this config to the others for this app. If a config file already exist
  214. }
  215. # Remove the app's logrotate config.
  216. #
  217. # usage: ynh_remove_logrotate
  218. ynh_remove_logrotate () {
  219. if [ -e "/etc/logrotate.d/$app" ]; then
  220. sudo rm "/etc/logrotate.d/$app"
  221. fi
  222. }
  223. # Find a free port and return it
  224. #
  225. # example: port=$(ynh_find_port 8080)
  226. #
  227. # usage: ynh_find_port begin_port
  228. # | arg: begin_port - port to start to search
  229. ynh_find_port () {
  230. port=$1
  231. test -n "$port" || ynh_die "The argument of ynh_find_port must be a valid port."
  232. while netcat -z 127.0.0.1 $port # Check if the port is free
  233. do
  234. port=$((port+1)) # Else, pass to next port
  235. done
  236. echo $port
  237. }
  238. # Create a system user
  239. #
  240. # usage: ynh_system_user_create user_name [home_dir]
  241. # | arg: user_name - Name of the system user that will be create
  242. # | arg: home_dir - Path of the home dir for the user. Usually the final path of the app. If this argument is omitted, the user will be created without home
  243. ynh_system_user_create () {
  244. if ! ynh_system_user_exists "$1" # Check if the user exists on the system
  245. then # If the user doesn't exist
  246. if [ $# -ge 2 ]; then # If a home dir is mentioned
  247. user_home_dir="-d $2"
  248. else
  249. user_home_dir="--no-create-home"
  250. fi
  251. sudo useradd $user_home_dir --system --user-group $1 --shell /usr/sbin/nologin || ynh_die "Unable to create $1 system account"
  252. fi
  253. }
  254. # Delete a system user
  255. #
  256. # usage: ynh_system_user_delete user_name
  257. # | arg: user_name - Name of the system user that will be create
  258. ynh_system_user_delete () {
  259. if ynh_system_user_exists "$1" # Check if the user exists on the system
  260. then
  261. echo "Remove the user $1" >&2
  262. sudo userdel $1
  263. else
  264. echo "The user $1 was not found" >&2
  265. fi
  266. }
  267. # Curl abstraction to help with POST requests to local pages (such as installation forms)
  268. #
  269. # $domain and $path_url should be defined externally (and correspond to the domain.tld and the /path (of the app?))
  270. #
  271. # example: ynh_local_curl "/install.php?installButton" "foo=$var1" "bar=$var2"
  272. #
  273. # usage: ynh_local_curl "page_uri" "key1=value1" "key2=value2" ...
  274. # | arg: page_uri - Path (relative to $path_url) of the page where POST data will be sent
  275. # | arg: key1=value1 - (Optionnal) POST key and corresponding value
  276. # | arg: key2=value2 - (Optionnal) Another POST key and corresponding value
  277. # | arg: ... - (Optionnal) More POST keys and values
  278. ynh_local_curl () {
  279. # Define url of page to curl
  280. full_page_url=https://localhost$path_url$1
  281. # Concatenate all other arguments with '&' to prepare POST data
  282. POST_data=""
  283. for arg in "${@:2}"
  284. do
  285. POST_data="${POST_data}${arg}&"
  286. done
  287. if [ -n "$POST_data" ]
  288. then
  289. # Add --data arg and remove the last character, which is an unecessary '&'
  290. POST_data="--data \"${POST_data::-1}\""
  291. fi
  292. # Curl the URL
  293. curl --silent --show-error -kL -H "Host: $domain" --resolve $domain:443:127.0.0.1 $POST_data "$full_page_url"
  294. }
  295. # Substitute/replace a string by another in a file
  296. #
  297. # usage: ynh_replace_string match_string replace_string target_file
  298. # | arg: match_string - String to be searched and replaced in the file
  299. # | arg: replace_string - String that will replace matches
  300. # | arg: target_file - File in which the string will be replaced.
  301. ynh_replace_string () {
  302. delimit=@
  303. match_string=${1//${delimit}/"\\${delimit}"} # Escape the delimiter if it's in the string.
  304. replace_string=${2//${delimit}/"\\${delimit}"}
  305. workfile=$3
  306. sudo sed --in-place "s${delimit}${match_string}${delimit}${replace_string}${delimit}g" "$workfile"
  307. }
  308. # Remove a file or a directory securely
  309. #
  310. # usage: ynh_secure_remove path_to_remove
  311. # | arg: path_to_remove - File or directory to remove
  312. ynh_secure_remove () {
  313. path_to_remove=$1
  314. forbidden_path=" \
  315. /var/www \
  316. /home/yunohost.app"
  317. if [[ "$forbidden_path" =~ "$path_to_remove" \
  318. # Match all paths or subpaths in $forbidden_path
  319. || "$path_to_remove" =~ ^/[[:alnum:]]+$ \
  320. # Match all first level paths from / (Like /var, /root, etc...)
  321. || "${path_to_remove:${#path_to_remove}-1}" = "/" ]]
  322. # Match if the path finishes by /. Because it seems there is an empty variable
  323. then
  324. echo "Avoid deleting $path_to_remove." >&2
  325. else
  326. if [ -e "$path_to_remove" ]
  327. then
  328. sudo rm -R "$path_to_remove"
  329. else
  330. echo "$path_to_remove wasn't deleted because it doesn't exist." >&2
  331. fi
  332. fi
  333. }
  334. # Download, check integrity, uncompress and patch the source from app.src
  335. #
  336. # The file conf/app.src need to contains:
  337. #
  338. # SOURCE_URL=Address to download the app archive
  339. # SOURCE_SUM=Control sum
  340. # # (Optional) Programm to check the integrity (sha256sum, md5sum$YNH_EXECUTION_DIR/...)
  341. # # default: sha256
  342. # SOURCE_SUM_PRG=sha256
  343. # # (Optional) Archive format
  344. # # default: tar.gz
  345. # SOURCE_FORMAT=tar.gz
  346. # # (Optional) Put false if source are directly in the archive root
  347. # # default: true
  348. # SOURCE_IN_SUBDIR=false
  349. # # (Optionnal) Name of the local archive (offline setup support)
  350. # # default: ${src_id}.${src_format}
  351. # SOURCE_FILENAME=example.tar.gz
  352. #
  353. # Details:
  354. # This helper download sources from SOURCE_URL if there is no local source
  355. # archive in /opt/yunohost-apps-src/APP_ID/SOURCE_FILENAME
  356. #
  357. # Next, it check the integrity with "SOURCE_SUM_PRG -c --status" command.
  358. #
  359. # If it's ok, the source archive will be uncompress in $dest_dir. If the
  360. # SOURCE_IN_SUBDIR is true, the first level directory of the archive will be
  361. # removed.
  362. #
  363. # Finally, patches named sources/patches/${src_id}-*.patch and extra files in
  364. # sources/extra_files/$src_id will be applyed to dest_dir
  365. #
  366. #
  367. # usage: ynh_setup_source dest_dir [source_id]
  368. # | arg: dest_dir - Directory where to setup sources
  369. # | arg: source_id - Name of the app, if the package contains more than one app
  370. ynh_setup_source () {
  371. local dest_dir=$1
  372. local src_id=${2:-app} # If the argument is not given, source_id equal "app"
  373. # Load value from configuration file (see above for a small doc about this file
  374. # format)
  375. local src_url=$(grep 'SOURCE_URL=' "../conf/${src_id}.src" | cut -d= -f2-)
  376. local src_sum=$(grep 'SOURCE_SUM=' "../conf/${src_id}.src" | cut -d= -f2-)
  377. local src_sumprg=$(grep 'SOURCE_SUM_PRG=' "../conf/${src_id}.src" | cut -d= -f2-)
  378. local src_format=$(grep 'SOURCE_FORMAT=' "../conf/${src_id}.src" | cut -d= -f2-)
  379. local src_in_subdir=$(grep 'SOURCE_IN_SUBDIR=' "../conf/${src_id}.src" | cut -d= -f2-)
  380. local src_filename=$(grep 'SOURCE_FILENAME=' "../conf/${src_id}.src" | cut -d= -f2-)
  381. # Default value
  382. src_sumprg=${src_sumprg:-sha256sum}
  383. src_in_subdir=${src_in_subdir:-true}
  384. src_format=${src_format:-tar.gz}
  385. src_format=$(echo "$src_format" | tr '[:upper:]' '[:lower:]')
  386. if [ "$src_filename" = "" ] ; then
  387. src_filename="${src_id}.${src_format}"
  388. fi
  389. local local_src="/opt/yunohost-apps-src/${YNH_APP_ID}/${src_filename}"
  390. if test -e "$local_src"
  391. then # Use the local source file if it is present
  392. cp $local_src $src_filename
  393. else # If not, download the source
  394. wget -nv -O $src_filename $src_url
  395. fi
  396. # Check the control sum
  397. echo "${src_sum} ${src_filename}" | ${src_sumprg} -c --status \
  398. || ynh_die "Corrupt source"
  399. # Extract source into the app dir
  400. sudo mkdir -p "$dest_dir"
  401. if [ "$src_format" = "zip" ]
  402. then
  403. # Zip format
  404. # Using of a temp directory, because unzip doesn't manage --strip-components
  405. if $src_in_subdir ; then
  406. local tmp_dir=$(mktemp -d)
  407. sudo unzip -quo $src_filename -d "$tmp_dir"
  408. sudo cp -a $tmp_dir/*/. "$dest_dir"
  409. ynh_secure_remove "$tmp_dir"
  410. else
  411. sudo unzip -quo $src_filename -d "$dest_dir"
  412. fi
  413. else
  414. local strip=""
  415. if $src_in_subdir ; then
  416. strip="--strip-components 1"
  417. fi
  418. if [[ "$src_format" =~ ^tar.gz|tar.bz2|tar.xz$ ]] ; then
  419. sudo tar -xf $src_filename -C "$dest_dir" $strip
  420. else
  421. ynh_die "Archive format unrecognized."
  422. fi
  423. fi
  424. # Apply patches
  425. if (( $(find ../sources/patches/ -type f -name "${src_id}-*.patch" 2> /dev/null | wc -l) > "0" )); then
  426. local old_dir=$(pwd)
  427. (cd "$dest_dir" \
  428. && for p in ../sources/patches/${src_id}-*.patch; do \
  429. sudo patch -p1 < $p; done) \
  430. || ynh_die "Unable to apply patches"
  431. cd $old_dir
  432. fi
  433. # Add supplementary files
  434. if test -e "../sources/extra_files/${src_id}"; then
  435. sudo cp -a ../sources/extra_files/$src_id/. "$dest_dir"
  436. fi
  437. }
  438. # Check availability of a web path
  439. #
  440. # example: ynh_webpath_available some.domain.tld /coffee
  441. #
  442. # usage: ynh_webpath_available domain path
  443. # | arg: domain - the domain/host of the url
  444. # | arg: path - the web path to check the availability of
  445. ynh_webpath_available () {
  446. local domain=$1
  447. local path=$2
  448. sudo yunohost domain url-available $domain $path
  449. }
  450. # Register/book a web path for an app
  451. #
  452. # example: ynh_webpath_register wordpress some.domain.tld /coffee
  453. #
  454. # usage: ynh_webpath_register app domain path
  455. # | arg: app - the app for which the domain should be registered
  456. # | arg: domain - the domain/host of the web path
  457. # | arg: path - the web path to be registered
  458. ynh_webpath_register () {
  459. local app=$1
  460. local domain=$2
  461. local path=$3
  462. sudo yunohost app register-url $app $domain $path
  463. }
  464. # Calculate and store a file checksum into the app settings
  465. #
  466. # $app should be defined when calling this helper
  467. #
  468. # usage: ynh_store_file_checksum file
  469. # | arg: file - The file on which the checksum will performed, then stored.
  470. ynh_store_file_checksum () {
  471. local checksum_setting_name=checksum_${1//[\/ ]/_} # Replace all '/' and ' ' by '_'
  472. ynh_app_setting_set $app $checksum_setting_name $(sudo md5sum "$1" | cut -d' ' -f1)
  473. }
  474. # Verify the checksum and backup the file if it's different
  475. # This helper is primarily meant to allow to easily backup personalised/manually
  476. # modified config files.
  477. #
  478. # $app should be defined when calling this helper
  479. #
  480. # usage: ynh_backup_if_checksum_is_different file
  481. # | arg: file - The file on which the checksum test will be perfomed.
  482. #
  483. # | ret: Return the name a the backup file, or nothing
  484. ynh_backup_if_checksum_is_different () {
  485. local file=$1
  486. local checksum_setting_name=checksum_${file//[\/ ]/_} # Replace all '/' and ' ' by '_'
  487. local checksum_value=$(ynh_app_setting_get $app $checksum_setting_name)
  488. if [ -n "$checksum_value" ]
  489. then # Proceed only if a value was stored into the app settings
  490. if ! echo "$checksum_value $file" | sudo md5sum -c --status
  491. then # If the checksum is now different
  492. backup_file="/home/yunohost.conf/backup/$file.backup.$(date '+%Y%m%d.%H%M%S')"
  493. sudo mkdir -p "$(dirname "$backup_file")"
  494. sudo cp -a "$file" "$backup_file" # Backup the current file
  495. echo "File $file has been manually modified since the installation or last upgrade. So it has been duplicated in $backup_file" >&2
  496. echo "$backup_file" # Return the name of the backup file
  497. fi
  498. fi
  499. }
  500. #####################################
  501. # This is not an official helper, just an abstract helper to prepare to the new one.
  502. ynh_restore_file () {
  503. sudo cp -a "${backup_dir}$1" "$1"
  504. }
  505. # =============================================================================
  506. # YUNOHOST 2.6 FORTHCOMING HELPERS
  507. # (will be part of YunoHost 2.6, so won't be necessary any more after
  508. # YunoHost 2.6 gets widespread)
  509. # =============================================================================
  510. # Create a dedicated nginx config
  511. #
  512. # usage: ynh_add_nginx_config
  513. ynh_add_nginx_config () {
  514. finalnginxconf="/etc/nginx/conf.d/$domain.d/$app.conf"
  515. ynh_backup_if_checksum_is_different "$finalnginxconf" 1
  516. sudo cp ../conf/nginx.conf "$finalnginxconf"
  517. # To avoid a break by set -u, use a void substitution ${var:-}. If the variable is not set, it's simply set with an empty variable.
  518. # Substitute in a nginx config file only if the variable is not empty
  519. if test -n "${path_url:-}"; then
  520. ynh_replace_string "__PATH__" "$path_url" "$finalnginxconf"
  521. fi
  522. if test -n "${domain:-}"; then
  523. ynh_replace_string "__DOMAIN__" "$domain" "$finalnginxconf"
  524. fi
  525. if test -n "${port:-}"; then
  526. ynh_replace_string "__PORT__" "$port" "$finalnginxconf"
  527. fi
  528. if test -n "${app:-}"; then
  529. ynh_replace_string "__NAME__" "$app" "$finalnginxconf"
  530. fi
  531. if test -n "${final_path:-}"; then
  532. ynh_replace_string "__FINALPATH__" "$final_path" "$finalnginxconf"
  533. fi
  534. ynh_store_checksum_config "$finalnginxconf"
  535. sudo systemctl reload nginx
  536. }
  537. # Remove the dedicated nginx config
  538. #
  539. # usage: ynh_remove_nginx_config
  540. ynh_remove_nginx_config () {
  541. ynh_secure_remove "/etc/nginx/conf.d/$domain.d/$app.conf"
  542. sudo systemctl reload nginx
  543. }
  544. # Create a dedicated php-fpm config
  545. #
  546. # usage: ynh_add_fpm_config
  547. ynh_add_fpm_config () {
  548. finalphpconf="/etc/php5/fpm/pool.d/$app.conf"
  549. ynh_backup_if_checksum_is_different "$finalphpconf" 1
  550. sudo cp ../conf/php-fpm.conf "$finalphpconf"
  551. ynh_replace_string "__NAMETOCHANGE__" "$app" "$finalphpconf"
  552. ynh_replace_string "__FINALPATH__" "$final_path" "$finalphpconf"
  553. ynh_replace_string "__USER__" "$app" "$finalphpconf"
  554. sudo chown root: "$finalphpconf"
  555. ynh_store_file_checksum "$finalphpconf"
  556. if [ -e "../conf/php-fpm.ini" ]
  557. then
  558. finalphpini="/etc/php5/fpm/conf.d/20-$app.ini"
  559. ynh_compare_checksum_config "$finalphpini" 1
  560. sudo cp ../conf/php-fpm.ini "$finalphpini"
  561. sudo chown root: "$finalphpini"
  562. ynh_store_checksum_config "$finalphpini"
  563. fi
  564. sudo systemctl reload php5-fpm
  565. }
  566. # Remove the dedicated php-fpm config
  567. #
  568. # usage: ynh_remove_fpm_config
  569. ynh_remove_fpm_config () {
  570. ynh_secure_remove "/etc/php5/fpm/pool.d/$app.conf"
  571. ynh_secure_remove "/etc/php5/fpm/conf.d/20-$app.ini" 2>&1
  572. sudo systemctl reload php5-fpm
  573. }
  574. # Create a dedicated systemd config
  575. #
  576. # usage: ynh_add_systemd_config
  577. ynh_add_systemd_config () {
  578. finalsystemdconf="/etc/systemd/system/$app.service"
  579. ynh_compare_checksum_config "$finalsystemdconf" 1
  580. sudo cp ../conf/systemd.service "$finalsystemdconf"
  581. # To avoid a break by set -u, use a void substitution ${var:-}. If the variable is not set, it's simply set with an empty variable.
  582. # Substitute in a nginx config file only if the variable is not empty
  583. if test -n "${final_path:-}"; then
  584. ynh_replace_string "__FINALPATH__" "$final_path" "$finalsystemdconf"
  585. fi
  586. if test -n "${app:-}"; then
  587. ynh_replace_string "__APP__" "$app" "$finalsystemdconf"
  588. fi
  589. ynh_store_checksum_config "$finalsystemdconf"
  590. sudo chown root: "$finalsystemdconf"
  591. sudo systemctl enable $app
  592. sudo systemctl daemon-reload
  593. }
  594. # Remove the dedicated systemd config
  595. #
  596. # usage: ynh_remove_systemd_config
  597. ynh_remove_systemd_config () {
  598. finalsystemdconf="/etc/systemd/system/$app.service"
  599. if [ -e "$finalsystemdconf" ]; then
  600. sudo systemctl stop $app
  601. sudo systemctl disable $app
  602. ynh_secure_remove "$finalsystemdconf"
  603. fi
  604. }