install 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #!/bin/bash
  2. # PirateBox app for YunoHost
  3. # Copyright (C) 2015 Julien Vaubourg <julien@vaubourg.com>
  4. # Contribute at https://github.com/labriqueinternet/piratebox_ynh
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #=================================================
  19. # GENERIC START
  20. #=================================================
  21. # IMPORT GENERIC HELPERS
  22. #=================================================
  23. source _common.sh
  24. source /usr/share/yunohost/helpers
  25. #=================================================
  26. # MANAGE SCRIPT FAILURE
  27. #=================================================
  28. ynh_clean_setup () {
  29. ### Remove this function if there's nothing to clean before calling the remove script.
  30. true
  31. }
  32. # Exit if an error occurs during the execution of the script
  33. ynh_abort_if_errors
  34. #=================================================
  35. # RETRIEVE ARGUMENTS FROM THE MANIFEST
  36. #=================================================
  37. domain=$YNH_APP_ARG_DOMAIN
  38. path_url=$YNH_APP_ARG_PATH
  39. opt_domain=$YNH_APP_ARG_OPT_DOMAIN
  40. opt_name=$YNH_APP_ARG_OPT_NAME
  41. opt_deleting=$YNH_APP_ARG_OPT_DELETING
  42. opt_renaming=$YNH_APP_ARG_OPT_RENAMING
  43. opt_chat=$YNH_APP_ARG_OPT_CHAT
  44. #admin=$YNH_APP_ARG_ADMIN
  45. app=$YNH_APP_INSTANCE_NAME
  46. #=================================================
  47. # CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
  48. #=================================================
  49. ynh_script_progression --message="Validating installation parameters..." --time --weight=1
  50. final_path=/var/www/$app
  51. final_path_admin=/var/www/"$app"_admin
  52. final_path_tmp=/var/spool/$app
  53. test ! -e "$final_path" || ynh_die --message="This path $final_path already contains a folder"
  54. test ! -e "$final_path_admin" || ynh_die --message="This path $final_path_admin already contains a folder"
  55. test ! -e "$final_path_tmp" || ynh_die --message="This path $final_path_tmp already contains a folder"
  56. if ! yunohost app list --installed | grep -q "hotspot";
  57. then
  58. ynh_die --message="Please install https://github.com/labriqueinternet/hotspot_ynh before"
  59. fi
  60. # Register (book) web path
  61. ynh_webpath_register --app=$app --domain=$domain --path_url=$path_url
  62. #=================================================
  63. # STORE SETTINGS FROM MANIFEST
  64. #=================================================
  65. ynh_script_progression --message="Storing installation settings..." --time --weight=1
  66. ynh_app_setting_set --app=$app --key=domain --value=$domain
  67. ynh_app_setting_set --app=$app --key=path --value=$path_url
  68. ynh_app_setting_set --app=$app --key=service_enabled --value=0
  69. ynh_app_setting_set --app=$app --key=opt_domain --value=$opt_domain
  70. ynh_app_setting_set --app=$app --key=opt_name --value=$opt_name
  71. ynh_app_setting_set --app=$app --key=opt_renaming --value=$opt_renaming
  72. ynh_app_setting_set --app=$app --key=opt_maxspace --value=90
  73. ynh_app_setting_set --app=$app --key=opt_deleting --value=$opt_deleting
  74. ynh_app_setting_set --app=$app --key=opt_chat --value=$opt_chat
  75. # For now, there is a bug with getops /o\
  76. yunohost app setting $app wifi_device_id -v -1
  77. #=================================================
  78. # STANDARD MODIFICATIONS
  79. #=================================================
  80. # FIND AND OPEN A PORT
  81. #=================================================
  82. ynh_script_progression --message="Configuring firewall..." --time --weight=1
  83. # Find a free port
  84. dns_port=$(ynh_find_port --port=4253)
  85. web_port=$(ynh_find_port --port=4280)
  86. # Open this port
  87. ynh_exec_warn_less yunohost firewall allow --no-upnp UDP $dns_port
  88. ynh_app_setting_set --app=$app --key=dns_port --value=$dns_port
  89. ynh_exec_warn_less yunohost firewall allow --no-upnp TCP $web_port
  90. ynh_app_setting_set --app=$app --key=web_port --value=$web_port
  91. #=================================================
  92. # INSTALL DEPENDENCIES
  93. #=================================================
  94. ynh_script_progression --message="Installing dependencies..." --time --weight=1
  95. ynh_install_app_dependencies $pkg_dependencies
  96. #=================================================
  97. # DOWNLOAD, CHECK AND UNPACK SOURCE
  98. #=================================================
  99. ynh_script_progression --message="Setting up source files..." --time --weight=1
  100. ynh_app_setting_set --app=$app --key=final_path --value=$final_path
  101. ynh_app_setting_set --app=$app --key=final_path_admin --value=$final_path_admin
  102. ynh_app_setting_set --app=$app --key=final_path_tmp --value=$final_path_tmp
  103. # Download, check integrity, uncompress and patch the source from app.src
  104. ynh_setup_source --dest_dir="$final_path"
  105. # Copy web sources (admin)
  106. mkdir -pm 0755 $final_path_admin
  107. cp -a ../sources/* $final_path_admin/
  108. mkdir -pm 0755 $final_path_tmp
  109. # Install fakedns
  110. install -o root -g root -m 0755 ../conf/piratebox_fakedns /usr/local/bin/
  111. # Copy init script
  112. install -o root -g root -m 0755 ../conf/ynh-piratebox /usr/local/bin/
  113. #=================================================
  114. # NGINX CONFIGURATION
  115. #=================================================
  116. ynh_script_progression --message="Configuring nginx web server..." --time --weight=1
  117. # Create a dedicated nginx config for the admin portal
  118. ynh_add_nginx_config final_path_admin
  119. # nginx captive portal
  120. install -b -o root -g root -m 0644 ../conf/nginx_captive-piratebox.conf /etc/nginx/captive-piratebox.conf
  121. ynh_replace_string --match_string="__DOMAIN__" --replace_string="$opt_domain" --target_file="/etc/nginx/captive-piratebox.conf"
  122. ynh_replace_string --match_string="__FINAL_PATH__" --replace_string="$final_path" --target_file="/etc/nginx/captive-piratebox.conf"
  123. ynh_replace_string --match_string="__NAME__" --replace_string="$app" --target_file="/etc/nginx/captive-piratebox.conf"
  124. #=================================================
  125. # CREATE DEDICATED USER
  126. #=================================================
  127. ynh_script_progression --message="Configuring system user..." --time --weight=1
  128. # Create a system user
  129. ynh_system_user_create --username=$app
  130. # Ensure the system user has enough sudo permissions
  131. install -b -o root -g root -m 0440 ../conf/sudoers.conf /etc/sudoers.d/${app}_ynh
  132. ynh_replace_string "__PIRATEBOX_SYSUSER__" "${app}" /etc/sudoers.d/${app}_ynh
  133. #=================================================
  134. # PHP-FPM CONFIGURATION
  135. #=================================================
  136. ynh_script_progression --message="Configuring php-fpm..." --time --weight=1
  137. # Create a dedicated php-fpm config for the admin panel
  138. tmp_final_path=$final_path
  139. final_path=$final_path_admin
  140. ynh_add_fpm_config
  141. final_path=$tmp_final_path
  142. install -b -o root -g root -m 0644 ../conf/phpfpm_captive-piratebox.conf /etc/php/7.0/fpm/pool.d/captive-piratebox.conf
  143. ynh_replace_string --match_string="__NAMETOCHANGE__" --replace_string="$app" --target_file="/etc/php/7.0/fpm/pool.d/captive-piratebox.conf"
  144. ynh_replace_string --match_string="__FINALPATH__" --replace_string="$final_path" --target_file="/etc/php/7.0/fpm/pool.d/captive-piratebox.conf"
  145. ynh_replace_string --match_string="__USER__" --replace_string="$app" --target_file="/etc/php/7.0/fpm/pool.d/captive-piratebox.conf"
  146. ynh_replace_string --match_string="__PHPVERSION__" --replace_string="$7.0" --target_file="/etc/php/7.0/fpm/pool.d/captive-piratebox.conf"
  147. #=================================================
  148. # SPECIFIC SETUP
  149. #=================================================
  150. # SETUP SYSTEMD
  151. #=================================================
  152. ynh_script_progression --message="Configuring a systemd service..." --time --weight=1
  153. # Create a dedicated systemd config
  154. ynh_add_systemd_config
  155. #=================================================
  156. # MODIFY A CONFIG FILE
  157. #=================================================
  158. install -b -o root -g root -m 0644 ../conf/piratebox_config.php.tpl $final_path/config.tpl.php
  159. # Fix php-piratebox configuration
  160. ynh_replace_string --match_string="<TPL:NGINX_REALPATH>" --replace_string="$final_path" --target_file="$final_path/config.tpl.php"
  161. # Fix sources
  162. ynh_replace_string --match_string="__PATH__" --replace_string="$path_url" --target_file="$final_path_admin/config.php"
  163. #=================================================
  164. # STORE THE CONFIG FILE CHECKSUM
  165. #=================================================
  166. # Calculate and store the config file checksum into the app settings
  167. ynh_store_file_checksum --file="$final_path/config.tpl.php"
  168. ynh_store_file_checksum --file="$final_path_admin/config.php"
  169. #=================================================
  170. # GENERIC FINALIZATION
  171. #=================================================
  172. # SECURE FILES AND DIRECTORIES
  173. #=================================================
  174. # Set permissions to app files
  175. chown -R root: $final_path
  176. chown www-data: $final_path_tmp
  177. chmod -R 0644 $final_path
  178. find $final_path -type d -exec chmod +x {} \;
  179. sudo chown -R root: $final_path_admin
  180. sudo chmod -R 0644 $final_path_admin/*
  181. sudo find $final_path_admin/ -type d -exec chmod +x {} \;
  182. # Fix permissions
  183. sudo chown -R www-data: $final_path/public/uploads/
  184. sudo chown -R www-data: $final_path/public/chat/
  185. #=================================================
  186. # ADVERTISE SERVICE IN ADMIN PANEL
  187. #=================================================
  188. yunohost service add $app --log "/var/log/$app/$app.log"
  189. #=================================================
  190. # START SYSTEMD SERVICE
  191. #=================================================
  192. ynh_script_progression --message="Starting a systemd service..." --time --weight=1
  193. # Start a systemd service
  194. ynh_systemd_action --service_name=$app --action="start"
  195. ynh_print_warn --message="PirateBox is not started because you need to define an associated wifi hotspot through the web admin"
  196. #=================================================
  197. # RELOAD NGINX
  198. #=================================================
  199. ynh_script_progression --message="Reloading nginx web server and php7.0-fpm..." --time --weight=1
  200. ynh_systemd_action --service_name=nginx --action=reload
  201. ynh_systemd_action --service_name=php7.0-fpm --action=restart
  202. #=================================================
  203. # END OF SCRIPT
  204. #=================================================
  205. ynh_script_progression --message="Installation of $app completed" --time --last