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

215 lines
7.3 KiB
Text
Raw Normal View History

2019-12-15 16:09:28 +01:00
#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers
#=================================================
# MANAGE SCRIPT FAILURE
#=================================================
ynh_clean_setup () {
### Remove this function if there's nothing to clean before calling the remove script.
true
}
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
#=================================================
# RETRIEVE ARGUMENTS FROM THE MANIFEST
#=================================================
domain=$YNH_APP_ARG_DOMAIN
path_url="/" #$YNH_APP_ARG_PATH
is_public=$YNH_APP_ARG_IS_PUBLIC
2020-04-18 14:53:52 +02:00
admin=$YNH_APP_ARG_ADMIN
password=$YNH_APP_ARG_PASSWORD
2019-12-15 16:09:28 +01:00
app=$YNH_APP_INSTANCE_NAME
#=================================================
# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
#=================================================
ynh_script_progression --message="Validating installation parameters..." --weight=1
2019-12-15 16:09:28 +01:00
2020-03-20 21:16:20 +01:00
# Testing if ZeroTier is installed
yunohost app list | grep -q "id: zerotier" || ynh_die "ZeroTier is needed, but it is not installed. There is a package for that!"
2020-03-20 21:16:20 +01:00
2020-04-16 16:08:39 +02:00
final_path=/opt/yunohost/$app
2019-12-15 16:09:28 +01:00
test ! -e "$final_path" || ynh_die --message="This path already contains a folder"
# Register (book) web path
ynh_webpath_register --app=$app --domain=$domain --path_url=$path_url
#=================================================
# STORE SETTINGS FROM MANIFEST
#=================================================
ynh_script_progression --message="Storing installation settings..." --weight=1
2019-12-15 16:09:28 +01:00
ynh_app_setting_set --app=$app --key=domain --value=$domain
ynh_app_setting_set --app=$app --key=final_path --value=$final_path
#=================================================
# STANDARD MODIFICATIONS
#=================================================
# FIND AND OPEN A PORT
#=================================================
2021-01-09 22:40:37 +01:00
ynh_script_progression --message="Finding an available port..." --weight=1
2019-12-15 16:09:28 +01:00
# Find an available port
port=$(ynh_find_port --port=8095)
ynh_app_setting_set --app=$app --key=port --value=$port
#=================================================
# INSTALL DEPENDENCIES
#=================================================
ynh_script_progression --message="Installing dependencies..." --weight=2
2019-12-15 16:09:28 +01:00
2020-04-16 16:08:39 +02:00
ynh_install_app_dependencies $pkg_dependencies
ynh_install_nodejs --nodejs_version=$nodejs_version
ynh_use_nodejs
2019-12-15 16:09:28 +01:00
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
ynh_script_progression --message="Setting up source files..." --weight=2
2019-12-15 16:09:28 +01:00
ynh_app_setting_set --app=$app --key=final_path --value=$final_path
# Download, check integrity, uncompress and patch the source from app.src
ynh_setup_source --dest_dir="$final_path"
#=================================================
# NGINX CONFIGURATION
#=================================================
ynh_script_progression --message="Configuring nginx web server..." --weight=1
2019-12-15 16:09:28 +01:00
# Create a dedicated nginx config
ynh_add_nginx_config
#=================================================
# CREATE DEDICATED USER
#=================================================
ynh_script_progression --message="Configuring system user..." --weight=1
2019-12-15 16:09:28 +01:00
# Create a system user
2020-04-17 20:16:53 +02:00
ynh_system_user_create $app $final_path
2019-12-15 16:09:28 +01:00
#=================================================
# SPECIFIC SETUP
#=================================================
# NPM INSTALL
#=================================================
ynh_script_progression --message="Performing Node app installation..." --weight=3
2019-12-15 16:09:28 +01:00
2020-04-17 20:16:53 +02:00
chown -R $app: $final_path
2019-12-15 16:09:28 +01:00
pushd $final_path/src
ynh_exec_as $app $ynh_node_load_PATH $ynh_npm --loglevel=error install node-gyp
ynh_exec_as $app $ynh_node_load_PATH $ynh_npm --loglevel=error install
ynh_exec_as $app $ynh_node_load_PATH $ynh_npm --loglevel=error install argon2-cli
ynh_exec_as $app $ynh_node_load_PATH $ynh_npm --loglevel=error audit fix
2019-12-15 16:09:28 +01:00
popd
#=================================================
# MODIFY A CONFIG FILE
#=================================================
# Setup env file
mkdir -p $final_path
env_file=$final_path/src/.env
touch $env_file
chmod 600 $env_file
2019-12-15 16:09:28 +01:00
echo "ZT_TOKEN=$(</var/lib/zerotier-one/authtoken.secret)" >> $env_file
2020-03-21 00:11:35 +01:00
echo "ZT_ADDR=localhost:$(</var/lib/zerotier-one/zerotier-one.port)" >> $env_file
2019-12-15 16:09:28 +01:00
echo "HTTP_PORT=$port" >> $env_file
2020-04-18 14:53:52 +02:00
# Setup user credentials file
2020-04-18 15:33:31 +02:00
hashedpassword=$(echo -n "$password" | $final_path/src/node_modules/.bin/argon2-cli -e)
2020-04-18 14:53:52 +02:00
echo "{\"$admin\":{\"name\":\"$admin\",\"pass_set\":true,\"hash\":\"$hashedpassword\"}}" >> "$final_path/src/etc/passwd"
# Store user settings
ynh_app_setting_set --app=$app --key=admin --value=$admin
ynh_app_setting_set --app=$app --key=hashedpassword --value=$hashedpassword
2019-12-15 16:09:28 +01:00
#=================================================
# LINK CERTIFICATES
#=================================================
# Even though one can stay in HTTP mode, the ztncui requires SSL certificates
# let's use the ones of the domain
pushd $final_path/src/etc/tls
cp /etc/yunohost/certs/$domain/key.pem privkey.pem
cp /etc/yunohost/certs/$domain/crt.pem fullchain.pem
2019-12-15 16:09:28 +01:00
popd
#=================================================
# SETUP SYSTEMD
#=================================================
ynh_script_progression --message="Configuring a systemd service..." --weight=1
2019-12-15 16:09:28 +01:00
# Create a dedicated systemd config
ynh_add_systemd_config
2019-12-15 16:09:28 +01:00
#=================================================
# GENERIC FINALIZATION
#=================================================
# SECURE FILES AND DIRECTORIES
#=================================================
# Set permissions to app files
chown -R $app: $final_path
#=================================================
# SETUP LOGROTATE
#=================================================
ynh_script_progression --message="Configuring log rotation..." --weight=1
2019-12-15 16:09:28 +01:00
# Use logrotate to manage application logfile(s)
ynh_use_logrotate
#=================================================
# INTEGRATE SERVICE IN YUNOHOST
#=================================================
ynh_script_progression --message="Integrating service in YunoHost..." --weight=1
2019-12-15 16:09:28 +01:00
yunohost service add $app --description "ZeroTier network controller user interface" --log "/var/log/$app/$app.log"
#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Starting the systemd service..." --weight=1
2019-12-15 16:09:28 +01:00
# Start a systemd service
ynh_systemd_action --service_name=$app --action="start" --log_path="/var/log/$app/$app.log"
#=================================================
# SETUP SSOWAT
#=================================================
2020-04-18 13:26:22 +02:00
ynh_script_progression --message="Configuring SSOwat..."
2019-12-15 16:09:28 +01:00
# Make app public if necessary
if [ $is_public -eq 1 ]
then
2020-04-18 13:26:22 +02:00
ynh_permission_update --permission "main" --add visitors
2019-12-15 16:09:28 +01:00
fi
#=================================================
# RELOAD NGINX
#=================================================
ynh_script_progression --message="Reloading nginx web server..." --weight=1
2019-12-15 16:09:28 +01:00
ynh_systemd_action --service_name=nginx --action=reload
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Installation of $app completed" --last