use helpers & uwsgi. Enable multi instance

This commit is contained in:
Jean-Baptiste Holcroft
2017-10-30 17:52:57 +01:00
parent 8b28531285
commit 745f4243ad
14 changed files with 651 additions and 281 deletions

View File

@@ -1,105 +1,192 @@
#!/bin/bash
# Source app helpers
. /usr/share/yunohost/helpers
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers
#=================================================
# MANAGE SCRIPT FAILURE
#=================================================
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
#=================================================
# RETRIEVE ARGUMENTS FROM THE MANIFEST
#=================================================
# Retrieve arguments
domain=$1
path=$2
domain=$YNH_APP_ARG_DOMAIN
path_url=$YNH_APP_ARG_PATH
# Check domain/path availability
sudo yunohost app checkurl $domain$path -a ffsync
if [[ ! $? -eq 0 ]]; then
ynh_die
fi
app=$YNH_APP_INSTANCE_NAME
# Generate random password
db_pwd=$(head -c 8 /dev/urandom | sha1sum | cut -d " " -f1)
#=================================================
# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
#=================================================
# Use 'FSyncMS' as database name and user
db_user=ffsync
# Check destination directory
final_path="/var/www/$app"
test ! -e "$final_path" || ynh_die "This path already contains a folder"
# Initialize database and store mysql password for upgrade
sudo yunohost app initdb $db_user -p $db_pwd
ynh_app_setting_set ffsync mysqlpwd $db_pwd
# Normalize the url path syntax
path_url=$(ynh_normalize_url_path "$path_url")
# Generate random password and save
secret=$(head -c 20 /dev/urandom | sha1sum | cut -d " " -f1)
ynh_app_setting_set ffsync secret $secret
# Check web path availability
ynh_webpath_available "$domain" "$path_url"
# Register (book) web path
ynh_webpath_register "$app" "$domain" "$path_url"
#=================================================
# STORE SETTINGS FROM MANIFEST
#=================================================
ynh_app_setting_set "$app" domain "$domain"
ynh_app_setting_set "$app" path_url "$path_url"
ynh_app_setting_set "$app" final_path "$final_path"
#=================================================
# STANDARD MODIFICATIONS
#=================================================
#=================================================
# INSTALL DEPENDENCIES
#=================================================
# Check depends installation
sudo apt-get install make python-dev python-virtualenv -y
ynh_install_app_dependencies make python-dev python-virtualenv \
uwsgi uwsgi-plugin-python
# Check Swap
if [ $(sudo swapon -s | wc -l) = 1 ];
then
# It is NOT possible to setup a swap file on a tmpfs filesystem
mount | grep /tmp | grep tmpfs > /dev/null 2>&1
if [ $? = 1 ];
then
tmp_swap_file=/tmp/ffsync_swapfile
else
tmp_swap_file=/var/cache/ffsync_swapfile
fi
sudo dd if=/dev/zero of=$tmp_swap_file bs=1M count=256
sudo chmod 600 $tmp_swap_file
sudo mkswap $tmp_swap_file
sudo swapon $tmp_swap_file
fi
#=================================================
# CREATE A MYSQL DATABASE
#=================================================
# Use 'FSyncMS' as database name and user
db_user=$app
db_name=$(ynh_sanitize_dbid $app)
db_pwd=$(ynh_string_random)
ynh_app_setting_set "$app" db_name "$db_name"
ynh_app_setting_set "$app" db_pwd "$db_name"
ynh_mysql_setup_db "$db_user" "$db_name"
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
# Download, check integrity, uncompress and patch the source from app.src
ynh_setup_source "$final_path"
# Modify assets to take path into account
sudo find ../sources/syncserver/page/sync_files/ -type f -exec sed -i -e "s@media\/img@$path\/media\/img@g" {} \;
# TODO: try to include this as a patch if still needed
# find ../sources/syncserver/page/sync_files/ -type f -exec sed -i -e "s@media\/img@$path_url\/media\/img@g" {} \;
# Copy files to the right place
final_path=/opt/yunohost/ffsync
sudo mkdir -p $final_path
sudo cp -a ../sources/* $final_path
sudo cp ../conf/ffsync /etc/init.d/
sudo cp ../conf/ffsync.logrotate /etc/logrotate.d/ffsync
sudo touch /var/log/ffsync.log
# Set permissions to ffsync directory
sudo useradd ffsync -d $final_path
sudo chown ffsync:ffsync -R $final_path
sudo chown ffsync /var/log/ffsync.log
#=================================================
# NGINX CONFIGURATION
#=================================================
# Modify Nginx configuration file and copy it to Nginx conf directory
sed -i "s@PATHTOCHANGE@$path@g" ../conf/nginx.conf
sed -i "s@ALIASTOCHANGE@$final_path/@g" ../conf/nginx.conf
sudo cp ../conf/nginx.conf /etc/nginx/conf.d/$domain.d/ffsync.conf
sudo cp ../conf/syncserver.ini $final_path/syncserver.ini
sudo sed -i -e "s@ynhbaseurl@$domain$path@g" $final_path/syncserver.ini
sudo sed -i -e "s@changesecret@$secret@g" $final_path/syncserver.ini
sudo sed -i "s/yunouser/$db_user/g" $final_path/syncserver.ini
sudo sed -i "s/yunopass/$db_pwd/g" $final_path/syncserver.ini
sudo sed -i "s/yunobase/$db_user/g" $final_path/syncserver.ini
sudo sed -i -e "s@media\/img@$path\/media\/img@g" $final_path/syncserver/page/sync_files/firefox_sync-bundle.css
sudo sed -i -e "s@media\/img@$path\/media\/img@g" $final_path/syncserver/page/sync_files/responsive-bundle.css
ynh_add_nginx_config
# Init virtualenv
cd $final_path && sudo make build && sudo ./local/bin/easy_install gunicorn
# Disable swapfile
if [ -z ${tmp_swap_file+x} ];
if [ "$path_url" == "/" ]
then
sudo swapoff $tmp_swap_file
sudo rm -f $tmp_swap_file
# $finalnginxconf comes from ynh_add_nginx_config
# uwsgi_param is only needed for non-root installation
ynh_replace_string "uwsgi_param " "#uwsgi_param " "$finalnginxconf"
ynh_replace_string "uwsgi_modifier1 " "#uwsgi_modifier1 " "$finalnginxconf"
ynh_store_file_checksum "$finalnginxconf"
fi
# Fix permission
sudo find $final_path/ -type d -exec chmod 2755 {} \;
sudo find $final_path/ -type f -exec chmod g+r,o+r {} \;
#=================================================
# CREATE DEDICATED USER
#=================================================
#enable services
sudo chmod +x /etc/init.d/ffsync
sudo update-rc.d ffsync defaults
sudo service ffsync restart
sudo service ffsync restart
sudo service ffsync restart
ynh_system_user_create "$app" "$final_path"
# Reload Nginx and regenerate SSOwat conf
sudo yunohost app ssowatconf
sudo service nginx restart
sudo yunohost service add ffsync -l /var/log/ffsync.log
ynh_app_setting_set ffsync skipped_uris "/"
#=================================================
# SPECIFIC SETUP
#=================================================
# SETUP SYSTEMD
#=================================================
ynh_add_systemd_config
#=================================================
# create config file syncserver.ini
#=================================================
# Generate random password and save
secret=$(ynh_string_random)
ynh_app_setting_set "$app" secret "$secret"
# Copy Files
cp ../conf/syncserver.ini "$final_path/syncserver.ini"
ynh_replace_string "__DOMAIN__" "$domain" "$final_path/syncserver.ini"
ynh_replace_string "__PATH__" "$path_url" "$final_path/syncserver.ini"
ynh_replace_string "__NAME__" "$app" "$final_path/syncserver.ini"
ynh_replace_string "__FINALPATH__" "$final_path" "$final_path/syncserver.ini"
ynh_replace_string "__SECRET__" "$secret" "$final_path/syncserver.ini"
ynh_replace_string "__DB_USER__" "$db_user" "$final_path/syncserver.ini"
ynh_replace_string "__DB_PWD__" "$db_pwd" "$final_path/syncserver.ini"
ynh_replace_string "__DB_NAME__" "$db_name" "$final_path/syncserver.ini"
#=================================================
# MODIFY A CONFIG FILE
#=================================================
# TODO: fix this css patch
# ynh_replace_string "media\/img@$path_url\/media\/img@g" $final_path/syncserver/page/sync_files/firefox_sync-bundle.css
# ynh_replace_string "media\/img@$path_url\/media\/img@g" $final_path/syncserver/page/sync_files/responsive-bundle.css
#=================================================
# make
#=================================================
# Init virtualenv
(
cd "$final_path"
make build
)
#=================================================
# GENERIC FINALIZATION
#=================================================
# SECURE FILES AND DIRECTORIES
#=================================================
chown "$app":"$app" -R "$final_path"
find "$final_path"/ -type d -exec chmod 2755 {} \;
find "$final_path"/ -type f -exec chmod g+r,o+r {} \;
#=================================================
# SETUP LOGROTATE
#=================================================
ynh_use_logrotate
#=================================================
# ADVERTISE SERVICE IN ADMIN PANEL
#=================================================
yunohost service add "$app" -l /var/log/$app/$app.log
#=================================================
# SETUP SSOWAT
#=================================================
ynh_app_setting_set "$app" skipped_uris "/"
#=================================================
# RELOAD NGINX
#=================================================
systemctl start "$app.service"
systemctl reload nginx