_common.sh 22 KB

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