1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/bozon_ynh.git synced 2024-09-03 18:16:09 +02:00
bozon_ynh/scripts/install

132 lines
4.8 KiB
Bash

#!/bin/bash
# Exit on command errors and treat unset variables as an error
set -eu
# Source YunoHost helpers
source /usr/share/yunohost/helpers
# retrieve arguments
app=$YNH_APP_INSTANCE_NAME
domain=$YNH_APP_ARG_DOMAIN
path=$YNH_APP_ARG_PATH
is_public=$YNH_APP_ARG_IS_PUBLIC
language=$YNH_APP_ARG_LANGUAGE
filesize=$YNH_APP_ARG_FILESIZE
admin=$YNH_APP_ARG_ADMIN
password=$YNH_APP_ARG_PASSWORD
# chech if / is present at start and not at end
if [ "${path:0:1}" != "/" ] && [ ${#path} -gt 0 ]; then
# first char is not / && path not empty => add / at start
path="/$path"
fi
if [ "${path:${#path}-1}" == "/" ] && [ ${#path} -gt 1 ]; then
# last char is / && path is gretter than /
path="${path:0:${#path}-1}"
fi
# definie useful vars
parent_path=/var/www
final_path="$parent_path"/"$app"
data_path=/home/yunohost.app/"$app"
# check domain/path availability
sudo yunohost app checkurl "${domain}${path}" -a "$app" || ynh_die "The path ${domain}${path} is not available for app installation."
# check that admin user is an existing account
ynh_user_exists "$admin" || ynh_die "The chosen admin user does not exist."
# add required packages
ynh_package_is_installed "php5-curl" || ynh_package_install "php5-curl"
ynh_package_is_installed "php5-gd" || ynh_package_install "php5-gd"
# retrieve upstream_version version of bozon
upstream_version=$(cat ../conf/upstream_version)
# save app settings
ynh_app_setting_set "$app" admin_user "$admin"
ynh_app_setting_set "$app" is_public "$is_public"
ynh_app_setting_set "$app" domain "$domain"
ynh_app_setting_set "$app" path "$path"
ynh_app_setting_set "$app" filesize "$filesize"
# download upstream_version version of bozon
wget -nc --quiet https://github.com/broncowdd/BoZoN/archive/"$upstream_version".zip -P /tmp
sudo unzip -oq /tmp/"$upstream_version".zip -d /tmp
sudo mv /tmp/BoZoN-"$upstream_version" "$parent_path"/"$app"
# copy files to final folder and set permissions
sudo find "$final_path" -type f -name ".htaccess" | xargs sudo rm
sudo chown -R root: "$final_path"
sudo find "$final_path" -type f | xargs sudo chmod 644
sudo find "$final_path" -type d | xargs sudo chmod 755
# configure config file
sudo sed -i "s@languageuage='en';@languageuage='${language}';@g" "$final_path"/config.php
# create data folders
sudo mkdir -p "$final_path"/private
sudo mkdir -p "$data_path"/uploads
sudo ln -s "$data_path"/uploads "$final_path"/uploads
sudo mkdir -p "$data_path"/thumbs
sudo ln -s "$data_path"/thumbs "$final_path"/thumbs
sudo chown -R www-data: "$final_path"/private
sudo chown -R www-data: "$data_path"/uploads
sudo chown -R www-data: "$data_path"/thumbs
# configure nginx settings
nginx_conf="/etc/nginx/conf.d/${domain}.d/${app}.conf"
sed -i "s@YNH_EXAMPLE_PATH@${path}@g" ../conf/nginx.conf
sed -i "s@YNH_EXAMPLE_ALIAS@${final_path}@g" ../conf/nginx.conf
sed -i "s@YNH_EXAMPLE_APP@${app}@g" ../conf/nginx.conf
sed -i "s@YNH_FILE_SIZE@${filesize}@g" ../conf/nginx.conf
sudo cp ../conf/nginx.conf "$nginx_conf"
# copy and set php-fpm configuration
postsize=${filesize%?}.1${filesize: -1}
phpfpm_conf="/etc/php5/fpm/pool.d/${app}.conf"
sed -i "s@YNH_EXAMPLE_APP@${app}@g" ../conf/php-fpm.conf
sed -i "s@YNH_EXAMPLE_ALIAS@${final_path}@g" ../conf/php-fpm.conf
sed -i "s@YNH_FILE_SIZE@${filesize}@g" ../conf/php-fpm.conf
sed -i "s@YNH_POST_SIZE@${postsize}@g" ../conf/php-fpm.conf
## copy final and set permissions
sudo cp ../conf/php-fpm.conf "$phpfpm_conf"
sudo chown root: "$phpfpm_conf"
sudo chmod 644 "$phpfpm_conf"
# restart services
sudo service php5-fpm restart || true
sudo service nginx reload || true
# set temporary public access
ynh_app_setting_set "$app" unprotected_uris "/"
sudo yunohost app ssowatconf
# add alias line in hosts file
echo "127.0.0.1 $domain # $app" | sudo tee -a /etc/hosts
# fill the superadmin creation form
curl_path=$([ "$path" == "/" ] || echo "$path")
curl -k https://"$domain""$curl_path"/ > /dev/null 2>&1
sleep 1 && curl -kX POST \
--data-urlencode creation="1" \
--data-urlencode login="$admin" \
--data-urlencode pass="$password" \
--data-urlencode confirm="$password" \
https://"$domain""$curl_path"/index.php?p=login > /dev/null 2>&1
# remove alias line from hosts file
sudo sed -i "/# $app/d" /etc/hosts
# if app is private, remove url to SSOWat conf from skipped_uris
if [ "$is_public" = "No" ];
then
# escape magic chars in vars (lua magic chars are ().%+-*?[^$ according to https://www.lua.org/pil/20.2.html)
domainluaregex=$(echo "$domain" | sed -e 's/[]().%+*?[^$[]/\%&/g' | sed -e 's/\-/\%&/g')
pathluaregex=$([ "$path" == "/" ] || echo "$path" | sed -e 's/[]().%+*?[^$[]/\%&/g' | sed -e 's/\-/\%&/g')
regexList="${domainluaregex}${pathluaregex}/index%.php$","${domainluaregex}${pathluaregex}/index%.php%?p=.*$"
ynh_app_setting_set "$app" protected_regex "$regexList"
sudo yunohost app ssowatconf
fi