2017-03-03 21:26:30 +01:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
# Exit on command errors and treat unset variables as an error
|
|
|
|
set -eu
|
|
|
|
|
|
|
|
source /usr/share/yunohost/helpers
|
|
|
|
source functions.sh
|
|
|
|
|
|
|
|
nginx_config_file() {
|
|
|
|
local app=$1
|
|
|
|
local domain=$2
|
|
|
|
|
|
|
|
sudo mkdir --parents "/etc/nginx/conf.d/${domain}.d"
|
|
|
|
|
|
|
|
echo "/etc/nginx/conf.d/${domain}.d/${app}.conf"
|
|
|
|
}
|
|
|
|
|
|
|
|
update_nginx_configuration() {
|
|
|
|
local app=$1
|
|
|
|
local config_template=$2
|
|
|
|
local config_file=$3
|
|
|
|
local path=$4
|
|
|
|
local port=$5
|
|
|
|
|
|
|
|
sudo cp $config_template $config_file
|
|
|
|
|
|
|
|
sudo sed --in-place "s@YNH_WWW_PATH@${path}@g" ${config_file}
|
|
|
|
sudo sed --in-place "s@YNH_LOCAL_PORT@${port}/@g" ${config_file}
|
|
|
|
|
|
|
|
sudo service nginx reload
|
|
|
|
}
|
|
|
|
|
|
|
|
systemd_service_name() {
|
|
|
|
local app=$1
|
|
|
|
local user=$2
|
|
|
|
|
|
|
|
echo "ynh-${app}-${user}.service"
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Creates service which runs zeronet on unique port for the user
|
|
|
|
# ZeroNet data is stored in the users home directory in ~/.zeronet
|
|
|
|
#
|
|
|
|
update_systemd_configuration() {
|
|
|
|
local app=$1
|
|
|
|
local service_template=$2
|
|
|
|
local service_name=$3
|
|
|
|
local service_file=$4
|
|
|
|
local user=$5
|
2017-03-04 13:35:40 +01:00
|
|
|
local port=$6
|
|
|
|
local symlink_to_deploy_path=$7
|
|
|
|
local zeronet_dir=$8
|
2017-07-27 15:38:06 +02:00
|
|
|
local ui_host=$9
|
2017-03-03 21:26:30 +01:00
|
|
|
|
|
|
|
# create zeronet user data location
|
|
|
|
local data_dir=$zeronet_dir/data
|
|
|
|
local log_dir=$zeronet_dir/log
|
|
|
|
|
|
|
|
sudo mkdir -p $data_dir
|
|
|
|
sudo mkdir -p $log_dir
|
2017-03-04 13:35:40 +01:00
|
|
|
sudo chown -R $user: $zeronet_dir
|
2017-03-03 21:26:30 +01:00
|
|
|
|
|
|
|
# configure systemd service
|
|
|
|
sudo cp $service_template $service_file
|
|
|
|
|
2017-03-04 13:35:40 +01:00
|
|
|
sudo sed --in-place "s@__USER__@$user@g" $service_file
|
|
|
|
sudo sed --in-place "s@__UI_PORT__@$port@g" $service_file
|
2017-07-27 15:38:06 +02:00
|
|
|
sudo sed --in-place "s@__UI_HOST__@$ui_host@g" $service_file
|
2017-03-04 13:35:40 +01:00
|
|
|
sudo sed --in-place "s@__APP_NAME__@$app@g" $service_file
|
|
|
|
sudo sed --in-place "s@__WORKING_DIRECTORY__@$symlink_to_deploy_path@g" $service_file
|
|
|
|
sudo sed --in-place "s@__DATA_DIR__@$data_dir@g" $service_file
|
|
|
|
sudo sed --in-place "s@__LOG_DIR__@$log_dir@g" $service_file
|
2017-03-03 21:26:30 +01:00
|
|
|
|
|
|
|
sudo systemctl daemon-reload
|
|
|
|
sudo systemctl enable $service_name
|
|
|
|
sudo systemctl start $service_name
|
|
|
|
}
|
|
|
|
|
|
|
|
# Source: https://github.com/YunoHost/yunohost/blob/901e3df9b604f542f2c460aad05bcc8efc9fd054/data/helpers.d/network
|
|
|
|
#
|
|
|
|
# Find a free port and return it
|
|
|
|
#
|
|
|
|
# example: port=$(ynh_find_port 8080)
|
|
|
|
#
|
|
|
|
# usage: ynh_find_port begin_port
|
|
|
|
# | arg: begin_port - port to start to search
|
|
|
|
ynh_find_port () {
|
|
|
|
port=$1
|
|
|
|
|
|
|
|
while netcat -z 127.0.0.1 $port # Check if the port is free
|
|
|
|
do
|
|
|
|
port=$((port+1)) # Else, pass to next port
|
|
|
|
done
|
|
|
|
echo $port
|
|
|
|
}
|
|
|
|
|
|
|
|
main() {
|
|
|
|
local app=$YNH_APP_INSTANCE_NAME
|
|
|
|
local number=$YNH_APP_INSTANCE_NUMBER
|
|
|
|
local domain=$YNH_APP_ARG_DOMAIN
|
|
|
|
local user=$YNH_APP_ARG_ADMIN
|
|
|
|
|
|
|
|
local port=$( ynh_find_port 43110 ) # the port zeronet listens on
|
|
|
|
|
|
|
|
local path="/"
|
|
|
|
local is_public=0 # by defaut access to zeronet is private
|
|
|
|
local nginx_config_template=../conf/nginx.conf.template
|
|
|
|
local systemd_service_template=../conf/systemd.service.template
|
|
|
|
local app_config=../conf/app.src
|
|
|
|
local systemd_service_dir=/etc/systemd/system
|
|
|
|
|
|
|
|
local url=$domain$path
|
|
|
|
local source_version=$( app_config_get $app_config "SOURCE_VERSION" )
|
|
|
|
local systemd_service_name=$( systemd_service_name $app $user )
|
|
|
|
local systemd_service_file="${systemd_service_dir}/${systemd_service_name}"
|
|
|
|
local nginx_config_file=$( nginx_config_file $app $domain )
|
|
|
|
local user_zeronet_dir="/home/${user}/.zeronet"
|
|
|
|
local deploy_path=$( make_deploy_path $app $source_version )
|
|
|
|
local symlink_to_deploy_path="/var/www/${app}"
|
|
|
|
|
|
|
|
sudo yunohost app checkurl $url -a $app
|
|
|
|
|
|
|
|
ynh_user_exists $user
|
|
|
|
|
|
|
|
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 url $url
|
|
|
|
ynh_app_setting_set $app admin $user
|
2017-03-05 21:36:56 +01:00
|
|
|
ynh_app_setting_set $app allowed_users $user
|
2017-03-03 21:26:30 +01:00
|
|
|
ynh_app_setting_set $app deploy_path $deploy_path
|
|
|
|
ynh_app_setting_set $app symlink_to_deploy_path $symlink_to_deploy_path
|
|
|
|
ynh_app_setting_set $app port $port
|
|
|
|
ynh_app_setting_set $app installed_version $source_version
|
|
|
|
ynh_app_setting_set $app nginx_config_file $nginx_config_file
|
2017-03-04 13:35:40 +01:00
|
|
|
ynh_app_setting_set $app user $user
|
2017-03-03 21:26:30 +01:00
|
|
|
ynh_app_setting_set $app user_zeronet_dir $user_zeronet_dir
|
|
|
|
ynh_app_setting_set $app systemd_service_name $systemd_service_name
|
|
|
|
ynh_app_setting_set $app systemd_service_file $systemd_service_file
|
|
|
|
|
|
|
|
install_dependencies
|
2017-03-04 13:35:40 +01:00
|
|
|
|
|
|
|
obtain_and_deploy_source $app_config $deploy_path $symlink_to_deploy_path $user
|
2017-03-03 21:26:30 +01:00
|
|
|
|
|
|
|
update_nginx_configuration $app $nginx_config_template $nginx_config_file $path $port
|
|
|
|
update_systemd_configuration $app $systemd_service_template $systemd_service_name $systemd_service_file \
|
2017-07-27 15:38:06 +02:00
|
|
|
$user $port $symlink_to_deploy_path $user_zeronet_dir $domain
|
2017-03-03 21:26:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
main
|