#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source ynh_install_go
source /usr/share/yunohost/helpers
#=================================================
# LOAD SETTINGS
#=================================================
password=$(ynh_app_setting_get --app=$app --key=password)
#=================================================
# CHECK VERSION
#=================================================
upgrade_type=$(ynh_check_app_version_changed)
#=================================================
# STANDARD UPGRADE STEPS
#=================================================
# STOP SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Stopping a systemd service..." --weight=3
ynh_systemd_action --service_name=$app --action="stop" --log_path="systemd"
ynh_systemd_action --service_name=${app}_ldap --action="stop" --log_path="systemd"
#=================================================
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
ynh_script_progression --message="Ensuring downward compatibility..." --weight=1
# For version 0.6~ynh1 and before
if [[ ! -d "$install_dir/live" ]]
then
tempdir="$(mktemp -d)"
mv $install_dir $tempdir
mkdir -p "$install_dir/live"
mv $tempdir/$app/galene "$install_dir/live/"
mv $tempdir/$app/data/ "$install_dir/live/"
mv $tempdir/$app/static/ "$install_dir/live/"
ynh_secure_remove --file="$tempdir"
fi
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
ynh_script_progression --message="Upgrading source files..." --weight=2
ynh_setup_source --dest_dir="$install_dir/build"
ynh_setup_source --dest_dir="$install_dir/build_ldap" --source_id="ldap"
mkdir -p "$install_dir/live/data"
mkdir -p "$install_dir/live_ldap/data"
ynh_secure_remove --file="$install_dir/live/static/"
cp -r "$install_dir/build/static/" "$install_dir/live/"
ynh_replace_string --match_string="
" --replace_string="" --target_file="$install_dir/live/static/galene.html"
chmod -R o-rwx "$install_dir"
chown -R $app:www-data "$install_dir"
#=================================================
# NGINX CONFIGURATION
#=================================================
ynh_script_progression --message="Upgrading NGINX web server configuration..." --weight=3
# Create a dedicated NGINX config
ynh_add_nginx_config
#=================================================
# SPECIFIC UPGRADE
#=================================================
# BUILD APP
#=================================================
ynh_script_progression --message="Building app..." --weight=1
if [ "$upgrade_type" == "UPGRADE_APP" ]
then
ynh_exec_warn_less ynh_install_go --go_version=$go_version
ynh_use_go
pushd $install_dir/build/galene-password-generator/
ynh_exec_warn_less ynh_exec_as $app CGO_ENABLED=0 $ynh_go build -o $install_dir/
popd
pushd $install_dir/build/
ynh_exec_warn_less ynh_exec_as $app CGO_ENABLED=0 $ynh_go build -ldflags='-s -w' -o $install_dir/live/
popd
pushd $install_dir/build_ldap/
ynh_exec_warn_less ynh_exec_as $app CGO_ENABLED=0 $ynh_go build -ldflags='-s -w' -o $install_dir/live_ldap/
popd
ynh_remove_go
ynh_secure_remove --file="$install_dir/build/"
ynh_secure_remove --file="$install_dir/build_ldap/"
ynh_secure_remove --file="$install_dir/.cache/"
ynh_secure_remove --file="$install_dir/go/"
ynh_secure_remove --file="$install_dir/.go-version"
chmod +x "$install_dir/galene-password-generator"
fi
#=================================================
# UPDATE A CONFIG FILE
#=================================================
ynh_script_progression --message="Updating a configuration file..." --weight=2
# If password_hash doesn't exist, create it
if [ -z "$password_hash" ]; then
password_hash=$(echo $($install_dir/galene-password-generator $password))
ynh_app_setting_set --app=$app --key=password_hash --value="$password_hash"
fi
# Configure Galène
ynh_add_config --template="../conf/config.json" --destination="$install_dir/live/data/config.json"
chmod 400 "$install_dir/live/data/config.json"
chown $app:$app "$install_dir/live/data/config.json"
# Configure Galène LDAP
# If key doesn't exist, create it
if [ -z "$key" ]; then
key=$(jose jwk gen -i '{"kty":"oct","alg":"HS256"}')
ynh_app_setting_set --app=$app --key=key --value=$key
fi
ynh_add_config --template="../conf/galene-ldap.json" --destination="$install_dir/live_ldap/data/galene-ldap.json"
chmod 400 "$install_dir/live_ldap/data/galene-ldap.json"
chown $app:$app "$install_dir/live_ldap/data/galene-ldap.json"
# Create a group name config
ynh_add_config --template="../conf/groupname.json" --destination="$data_dir/groups/$group_name.json"
chmod 400 "$data_dir/groups/$group_name.json"
chown $app:$app "$data_dir/groups/$group_name.json"
# Create a group name authenticated on LDAP
ynh_add_config --template="../conf/groupname-ldap.json" --destination="$data_dir/groups/YunoHost_Users.json"
chmod 400 "$data_dir/groups/YunoHost_Users.json"
chown $app:$app "$data_dir/groups/YunoHost_Users.json"
#=================================================
# SETUP SYSTEMD
#=================================================
ynh_script_progression --message="Upgrading systemd configuration..." --weight=1
public_ip4="$(curl -s ip.yunohost.org)" || true
# Create a dedicated systemd config
ynh_add_systemd_config
ynh_add_systemd_config --service=${app}_ldap --template="ldap.service"
#=================================================
# GENERIC FINALIZATION
#=================================================
# INTEGRATE SERVICE IN YUNOHOST
#=================================================
ynh_script_progression --message="Integrating service in YunoHost..." --weight=1
yunohost service add $app --description="Videoconferencing server" --needs_exposed_ports="$port_turn"
yunohost service add ${app}_ldap --description="LDAP integration for the videoconferencing server"
#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Starting a systemd service..." --weight=1
ynh_systemd_action --service_name=$app --action="start" --log_path="systemd"
ynh_systemd_action --service_name=${app}_ldap --action="start" --log_path="systemd"
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Upgrade of $app completed" --last