#!/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 install_dependencies() { sudo apt-get install --yes python-msgpack python-gevent } 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 local group_id=$6 local port=$7 local symlink_to_deploy_path=$8 local zeronet_dir=$9 # 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 sudo chown -R $user:$group_id $zeronet_dir # configure systemd service sudo cp $service_template $service_file sudo sed --in-place "s@USER@$user@g" $service_file sudo sed --in-place "s@UI_PORT@$port@g" $service_file 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 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 group_id=`id -g $user` 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 ynh_app_setting_set $app group_id $group_id 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 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 obtain_and_deploy_source $app_config $deploy_path $symlink_to_deploy_path #mock_obtain_and_deploy_source $deploy_path $symlink_to_deploy_path 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 \ $user $group_id $port $symlink_to_deploy_path $user_zeronet_dir } main