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

157 lines
4.5 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
source future.sh
source common.sh
readonly app=$YNH_APP_INSTANCE_NAME
# Retrieve arguments
readonly domain=$YNH_APP_ARG_DOMAIN
#readonly path=$YNH_APP_ARG_PATH
readonly path=/
readonly admin=$YNH_APP_ARG_ADMIN
readonly email=$YNH_APP_ARG_EMAIL
readonly isp_name=$YNH_APP_ARG_ISP_NAME
readonly isp_site=$YNH_APP_ARG_ISP_SITE
readonly secret=$(ynh_string_random 24) # A bug don't allow to do random string bigger than 24
# Check if admin exists
ynh_user_exists $admin \
|| ynh_die "Wrong admin"
function configure_app()
{
# Check domain/path availability
sudo yunohost app checkurl "${domain}${path}" -a "$app" \
|| ynh_die "Path not available: ${domain}${path}"
ynh_app_setting_set "$app" admin "$admin"
ynh_app_setting_set "$app" email "$email"
ynh_app_setting_set "$app" isp_name "$isp_name"
ynh_app_setting_set "$app" isp_site "$isp_site"
ynh_app_setting_set "$app" secret "$secret"
}
function init_db()
{
# Generate random password
db_name=$app
db_user=$app
db_pwd=$(ynh_string_random)
# Initialize database and store mysql password for upgrade
ynh_mysql_create_db $db_name $db_user $db_pwd
ynh_app_setting_set "$app" mysqlpassword "$db_pwd"
}
function install_from_sources()
{
$final_path/venv/bin/pip install "pip>=1.5.6"
$final_path/venv/bin/pip install gunicorn
$final_path/venv/bin/pip install -r $final_path/requirements.txt
$final_path/venv/bin/pip install django-auth-ldap
#$final_path/venv/bin/pip install mysqlclient
prefix="${path#"/"}/"
prefix=${prefix%"/"}
# Configuration Django
sed -i "s@YNH_APP_ARG_ADMIN@$admin@g" ../conf/local.py
sed -i "s@YNH_APP_ARG_DOMAIN@$domain@g" ../conf/local.py
sed -i "s@YNH_APP_ARG_PATH@$path@g" ../conf/local.py
sed -i "s@YNH_APP_PREFIX@$prefix@g" ../conf/local.py
sed -i "s#YNH_APP_ARG_EMAIL#$email#g" ../conf/local.py
sed -i "s#YNH_APP_SECRET_KEY#$secret#g" ../conf/local.py
sed -i "s#YNH_APP_ARG_ISP_NAME#$isp_name#g" ../conf/local.py
sed -i "s#YNH_APP_ARG_ISP_SITE#$isp_site#g" ../conf/local.py
sed -i "s#YNH_APP_STATIC_ROOT#$final_path/static#g" ../conf/local.py
# sed -i "s#YNH_DB_NAME#$db_name#g" ../conf/local.py
# sed -i "s#YNH_DB_USER#$db_user#g" ../conf/local.py
# sed -i "s#YNH_DB_PASSWORD#$db_pwd#g" ../conf/local.py
sudo cp ../conf/local.py $final_path/$app/settings_local.py
# Set production
sudo ln -s $final_path/$app/static $final_path/static
# Set permissions
sudo useradd $app -d $final_path || echo "User already exists"
sudo chown -R $app:www-data $final_path
pushd $final_path
$final_path/venv/bin/python manage.py migrate --noinput
$final_path/venv/bin/python manage.py collectstatic --noinput
popd
# Set permissions to directory
sudo chown $app:www-data -R $final_path
}
function configure_log()
{
# Log folder
sudo mkdir -p /var/log/$app
sudo chown -R $app /var/log/$app
sudo chgrp -R www-data /var/log/$app
}
function configure_gunicorn()
{
# Service gunicorn
sudo sed -i "s@YNH_APP_INSTANCE_NAME@$app@g" ../conf/gunicorn_config.py
sudo sed -i "s@YNH_APP_INSTANCE_NAME@$app@g" ../conf/gunicorn.service
sudo cp ../conf/gunicorn.service /etc/systemd/system/$app.service
sudo systemctl daemon-reload
sudo cp ../conf/gunicorn_config.py /opt/$app/
}
function configure_nginx_and_ssowat()
{
# Reload Nginx and regenerate SSOwat conf
sudo yunohost app addaccess $app -u $admin
# 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
sed -i "s@YNH_APP_INSTANCE_NAME@$app@g" ../conf/nginx.conf
sudo cp ../conf/nginx.conf /etc/nginx/conf.d/$domain.d/$app.conf
## Reload Nginx and regenerate SSOwat conf
sudo service nginx reload
sudo systemctl start $app
sudo systemctl enable $app
sudo yunohost service add $app -l /var/log/gunicorn/$app.log
ynh_app_setting_set "$app" skipped_uris "/"
sudo yunohost app ssowatconf
}
configure_app
#init_db
install_dependencies
# Copy files to the right place
final_path=/opt/$app
ynh_setup_source $final_path
# Install venv
pip install virtualenv
virtualenv $final_path/venv
set +o nounset
source $final_path/venv/bin/activate
set -o nounset
install_from_sources
configure_log
configure_gunicorn
configure_nginx_and_ssowat