upgrade 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #!/bin/bash
  2. #=================================================
  3. # GENERIC START
  4. #=================================================
  5. # IMPORT GENERIC HELPERS
  6. #=================================================
  7. source _common.sh
  8. source /usr/share/yunohost/helpers
  9. #=================================================
  10. # LOAD SETTINGS
  11. #=================================================
  12. ynh_script_progression --message="Loading installation settings..."
  13. app=$YNH_APP_INSTANCE_NAME
  14. firmware_nonfree=$(ynh_app_setting_get --app=$app --key=firmware_nonfree)
  15. service_name=$(ynh_app_setting_get --app=$app --key=service_name)
  16. #=================================================
  17. # CHECK VERSION
  18. #=================================================
  19. ynh_script_progression --message="Checking version..."
  20. upgrade_type=$(ynh_check_app_version_changed)
  21. #=================================================
  22. # BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
  23. #=================================================
  24. ynh_script_progression --message="Backing up the app before upgrading (may take a while)..."
  25. # Backup the current version of the app
  26. ynh_backup_before_upgrade
  27. ynh_clean_setup () {
  28. # Restore it if the upgrade fails
  29. ynh_restore_upgradebackup
  30. }
  31. # Exit if an error occurs during the execution of the script
  32. ynh_abort_if_errors
  33. #=================================================
  34. # STANDARD UPGRADE STEPS
  35. #=================================================
  36. # STOP SYSTEMD SERVICE
  37. #=================================================
  38. ynh_script_progression --message="Stopping the hotspot service ... (this may take some time)"
  39. ynh_systemd_action --service_name=$service_name --action="stop" --log_path=systemd #--line_match="Started YunoHost Wifi Hotspot"
  40. #=================================================
  41. # ENSURE DOWNWARD COMPATIBILITY
  42. #=================================================
  43. ynh_script_progression --message="Ensuring downward compatibility..."
  44. if [ -d /var/www/wifiadmin/ ]; then
  45. ynh_secure_remove /var/www/wifiadmin/
  46. fi
  47. if [ $firmware_nonfree = "yes" ]; then
  48. firmware_nonfree=1
  49. ynh_app_setting_set --app=$app --key=firmware_nonfree --value=$firmware_nonfree
  50. elif [ $firmware_nonfree = "no" ]; then
  51. firmware_nonfree=0
  52. ynh_app_setting_set --app=$app --key=firmware_nonfree --value=$firmware_nonfree
  53. fi
  54. if [ -z $service_name ]; then
  55. service_name="ynh-hotspot"
  56. ynh_app_setting_set --app=$app --key=service_name --value=$service_name
  57. fi
  58. multissid=$(ynh_app_setting_get --app=$app --key=multissid)
  59. if [[ -n ${multissid} ]] && [[ ${multissid} -gt 1 ]]; then
  60. wifi_ssid=$(ynh_app_setting_get --app=$app --key=wifi_ssid | cut -d'|' -f 1)
  61. wifi_secure=$(ynh_app_setting_get --app=$app --key=wifi_secure | cut -d'|' -f 1)
  62. wifi_passphrase=$(ynh_app_setting_get --app=$app --key=wifi_passphrase | cut -d'|' -f 1)
  63. ip4_nat_prefix=$(ynh_app_setting_get --app=$app --key=ip4_nat_prefix | cut -d'|' -f 1)
  64. ip6_net=$(ynh_app_setting_get --app=$app --key=ip6_net | cut -d'|' -f 1)
  65. ip6_firewall=$(ynh_app_setting_get --app=$app --key=ip6_firewall | cut -d'|' -f 1)
  66. dns=$(ynh_app_setting_get --app=$app --key=dns | cut -d'|' -f 1)
  67. ynh_app_setting_set --app=$app --key=wifi_ssid --value="${wifi_ssid}"
  68. ynh_app_setting_set --app=$app --key=wifi_secure --value="${wifi_secure}"
  69. ynh_app_setting_set --app=$app --key=wifi_passphrase --value="${wifi_passphrase}"
  70. ynh_app_setting_set --app=$app --key=ip4_nat_prefix --value="${ip4_nat_prefix}"
  71. ynh_app_setting_set --app=$app --key=ip6_net --value="${ip6_net}"
  72. ynh_app_setting_set --app=$app --key=ip6_firewall --value="${ip6_firewall}"
  73. else
  74. dns=$(ynh_app_setting_get --app=$app --key=dns)
  75. fi
  76. if [[ -n "${dns}" ]]; then
  77. ip6_dns=""
  78. ip4_dns=""
  79. for ip in $(echo "${dns}" | tr ',' ' '); do
  80. if [[ "$ip" == *":"* ]]; then
  81. ip6_dns+="[$ip],"
  82. else
  83. ip4_dns+="$ip,"
  84. fi
  85. done
  86. # Remove trailing ,
  87. ip6_dns="${ip6_dns%%,}"
  88. ip4_dns="${ip4_dns%%,}"
  89. if [[ -z "$(ynh_app_setting_get --app=$app --key=ip6_dns)" ]]; then
  90. ynh_app_setting_set --app=$app --key=ip6_dns --value="${ip6_dns}"
  91. if
  92. if [[ -z "$(ynh_app_setting_get --app=$app --key=ip4_dns)" ]]; then
  93. ynh_app_setting_set --app=$app --key=ip4_dns --value="${ip4_dns}"
  94. if
  95. ynh_app_setting_delete $app dns
  96. fi
  97. if [[ -n ${multissid} ]]; then
  98. ynh_app_setting_delete --app=$app --key=multissid
  99. fi
  100. # Old stuff prior to 2.x
  101. ip6_net=$(ynh_app_setting_get --app=$app --key=ip6_net)
  102. if [ "$ip6_net" == "none" ]
  103. then
  104. ip6_net=""
  105. ynh_app_setting_set --app=$app --key=ip6_net --value="$ip6_net"
  106. fi
  107. if [ -f /etc/nginx/conf.d/*.d/$app.conf ]; then
  108. ynh_secure_remove /etc/nginx/conf.d/*.d/$app.conf
  109. ynh_systemd_action --service_name=nginx --action=reload
  110. fi
  111. for php_path in $(ls /etc/php/*/fpm/pool.d/$app.conf 2> /dev/null); do
  112. ynh_secure_remove $php_path
  113. done
  114. if [ -d /var/www/$app ]; then
  115. ynh_secure_remove /var/www/$app
  116. fi
  117. [ -z "$(ynh_app_setting_get $app domain)" ] || ynh_app_setting_delete $app domain
  118. [ -z "$(ynh_app_setting_get $app path)" ] || ynh_app_setting_delete $app path
  119. [ -z "$(ynh_app_setting_get $app final_path)" ] || ynh_app_setting_delete $app final_path
  120. if [ -e "/etc/sudoers.d/${app}_ynh" ]; then
  121. ynh_secure_remove "/etc/sudoers.d/${app}_ynh"
  122. fi
  123. #=================================================
  124. # CREATE DEDICATED USER
  125. #=================================================
  126. ynh_script_progression --message="Making sure dedicated system user exists..."
  127. # Create a dedicated user (if not existing)
  128. ynh_system_user_create --username=$app
  129. #=================================================
  130. # UPGRADE DEPENDENCIES
  131. #=================================================
  132. ynh_script_progression --message="Upgrading dependencies..."
  133. if [[ $firmware_nonfree -eq 1 ]]; then
  134. check_armbian_nonfree_conflict
  135. ynh_install_extra_app_dependencies --repo="deb http://deb.debian.org/debian $(ynh_get_debian_release) non-free" --package="$nonfree_firmware_packages"
  136. else
  137. pkg_dependencies="$pkg_dependencies $free_firmware_packages"
  138. fi
  139. ynh_install_app_dependencies $pkg_dependencies
  140. #=================================================
  141. # SPECIFIC UPGRADE
  142. #=================================================
  143. # COPY CONFIGS
  144. #=================================================
  145. ynh_script_progression --message="Copying configuration..."
  146. mkdir -pm 0755 /etc/dnsmasq.dhcpd/
  147. chown root: /etc/dnsmasq.dhcpd/
  148. install -b -o root -g root -m 0644 ../conf/hostapd.*.conf /etc/hostapd/
  149. install -b -o root -g root -m 0644 ../conf/dnsmasq_dhcpdv6.conf.tpl /etc/dnsmasq.dhcpd/dhcpdv6.conf.tpl
  150. install -b -o root -g root -m 0644 ../conf/dnsmasq_dhcpdv4.conf.tpl /etc/dnsmasq.dhcpd/dhcpdv4.conf.tpl
  151. # Copy init script
  152. install -o root -g root -m 0755 ../conf/$service_name /usr/local/bin/
  153. # Copy openvpn scripts
  154. mkdir -pm 0755 /etc/openvpn/scripts
  155. mkdir -pm 0755 /etc/openvpn/scripts/route-up.d
  156. mkdir -pm 0755 /etc/openvpn/scripts/route-down.d
  157. install -o root -g root -m 0755 ../conf/openvpn_90-hotspot /etc/openvpn/scripts/route-up.d/90-hotspot
  158. install -o root -g root -m 0755 ../conf/openvpn_90-hotspot /etc/openvpn/scripts/route-down.d/90-hotspot
  159. #=================================================
  160. # SETUP SYSTEMD
  161. #=================================================
  162. ynh_script_progression --message="Upgrading systemd configuration..."
  163. # Create a dedicated systemd config
  164. ynh_add_systemd_config --service=$service_name
  165. #=================================================
  166. # GENERIC FINALIZATION
  167. #=================================================
  168. # INTEGRATE SERVICE IN YUNOHOST
  169. #=================================================
  170. ynh_script_progression --message="Integrating service in YunoHost..."
  171. yunohost service add $service_name --description "Creates a Wi-Fi access point" --test_status "systemctl is-active hostapd"
  172. #=================================================
  173. # START SYSTEMD SERVICE
  174. #=================================================
  175. ynh_script_progression --message="Starting the hotspot service..."
  176. hot_reload_usb_wifi_cards
  177. wifi_device=$(iw_devices | awk -F\| '{ print $1 }')
  178. if [[ -z $wifi_device ]]; then
  179. ynh_app_setting_set --app=$app --key=service_enabled --value=0
  180. wifi_device=""
  181. else
  182. ynh_app_setting_set --app=$app --key=service_enabled --value=1
  183. fi
  184. # Start a systemd service if device is present
  185. if [[ $wifi_device == "" ]]; then
  186. echo "WARNING: Wifi Hotspot is not started because no wifi device was found (please, check the web admin)" >&2
  187. else
  188. ynh_systemd_action --service_name=$service_name --action="start" --log_path=systemd #--line_match="Started YunoHost Wifi Hotspot"
  189. fi
  190. #=================================================
  191. # END OF SCRIPT
  192. #=================================================
  193. ynh_script_progression --message="Upgrade of $app completed"