1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/nomad_ynh.git synced 2024-09-03 19:55:53 +02:00
nomad_ynh/scripts/restore

167 lines
6 KiB
Text
Raw Normal View History

2022-07-20 12:04:29 +02:00
#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
# Keep this path for calling _common.sh inside the execution's context of backup and restore scripts
source ../settings/scripts/_common.sh
source /usr/share/yunohost/helpers
#=================================================
# MANAGE SCRIPT FAILURE
#=================================================
ynh_clean_setup () {
2022-07-20 12:07:54 +02:00
ynh_clean_check_starting
2022-07-20 12:04:29 +02:00
}
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
#=================================================
# LOAD SETTINGS
#=================================================
2022-07-20 12:07:54 +02:00
ynh_script_progression --message="Loading installation settings..."
2022-07-20 12:04:29 +02:00
app=$YNH_APP_INSTANCE_NAME
domain=$(ynh_app_setting_get --app=$app --key=domain)
path_url=$(ynh_app_setting_get --app=$app --key=path)
2022-07-21 08:24:29 +02:00
config_path=$(ynh_app_setting_get --app=$app --key=config_path)
2022-07-20 12:04:29 +02:00
datadir=$(ynh_app_setting_get --app=$app --key=datadir)
2022-07-21 08:24:29 +02:00
node_type=$(ynh_app_setting_get --app=$app --key=node_type)
http_port=$(ynh_app_setting_get --app=$app --key=http_port)
rpc_port=$(ynh_app_setting_get --app=$app --key=rpc_port)
serf_port=$(ynh_app_setting_get --app=$app --key=serf_port)
2022-07-20 12:04:29 +02:00
#=================================================
# CHECK IF THE APP CAN BE RESTORED
#=================================================
2022-07-20 12:07:54 +02:00
ynh_script_progression --message="Validating restoration parameters..."
2022-07-20 12:04:29 +02:00
#=================================================
# STANDARD RESTORATION STEPS
#=================================================
# RECREATE THE DEDICATED USER
#=================================================
2022-07-20 12:07:54 +02:00
ynh_script_progression --message="Recreating the dedicated system user..."
2022-07-20 12:04:29 +02:00
# Create the dedicated user (if not existing)
2022-07-21 08:24:29 +02:00
ynh_system_user_create --username=$app
2022-07-20 12:04:29 +02:00
#=================================================
# RESTORE THE DATA DIRECTORY
#=================================================
2022-07-20 12:07:54 +02:00
ynh_script_progression --message="Restoring the data directory..."
2022-07-20 12:04:29 +02:00
ynh_restore_file --origin_path="$datadir" --not_mandatory
mkdir -p $datadir
chmod 750 "$datadir"
chmod -R o-rwx "$datadir"
2022-07-21 08:24:29 +02:00
chown -R $app:$app "$datadir"
2022-07-20 12:04:29 +02:00
#=================================================
# SPECIFIC RESTORATION
#=================================================
# REINSTALL DEPENDENCIES
#=================================================
2022-07-20 12:07:54 +02:00
ynh_script_progression --message="Reinstalling dependencies..."
2022-07-20 12:04:29 +02:00
2022-07-21 08:24:29 +02:00
if [ "$node_type" == "client" ]
then
ynh_install_app_dependencies $client_pkg_dependencies
fi
ynh_install_extra_app_dependencies --repo="deb https://apt.releases.hashicorp.com $(lsb_release -cs) main" --package="$pkg_dependencies" --key="https://apt.releases.hashicorp.com/gpg"
2022-07-20 12:04:29 +02:00
#=================================================
# RESTORE THE NGINX CONFIGURATION
#=================================================
2022-07-20 12:07:54 +02:00
ynh_script_progression --message="Restoring the NGINX web server configuration..."
2022-07-20 12:04:29 +02:00
ynh_restore_file --origin_path="/etc/nginx/conf.d/$domain.d/$app.conf"
#=================================================
# RESTORE VARIOUS FILES
#=================================================
2022-07-20 12:07:54 +02:00
ynh_script_progression --message="Restoring various files..."
2022-07-20 12:04:29 +02:00
2022-07-21 08:24:29 +02:00
ynh_restore_file --origin_path="$config_path"
chmod 750 "$config_path"
chmod -R o-rwx "$config_path"
chown -R $app:$app "$config_path"
# Open the port
ynh_script_progression --message="Configuring firewall..."
ynh_exec_warn_less yunohost firewall allow --no-upnp TCP $rpc_port
2022-07-21 19:51:27 +02:00
needs_exposed_ports="$rpc_port"
2022-07-21 08:24:29 +02:00
if [ "$node_type" == "server" ]
then
ynh_exec_warn_less yunohost firewall allow --no-upnp TCP $serf_port
2022-07-21 19:51:27 +02:00
needs_exposed_ports="$serf_port $needs_exposed_ports"
2022-07-21 08:24:29 +02:00
fi
if [ "$node_type" == "client" ]
then
2022-07-20 12:04:29 +02:00
2022-07-21 08:24:29 +02:00
lxc_bridge=$(ynh_app_setting_get --app=$app --key=lxc_bridge)
plage_ip=$(ynh_app_setting_get --app=$app --key=plage_ip)
2022-07-21 19:52:20 +02:00
main_iface=$(ip route | grep default | awk '{print $5;}')
2022-07-21 08:24:29 +02:00
ynh_app_setting_set --app=$app --key=main_iface --value=$main_iface
ynh_add_config --template="../conf/lxc_bridge" --destination="/etc/network/interfaces.d/$lxc_bridge"
ifup $lxc_bridge --interfaces=/etc/network/interfaces.d/$lxc_bridge
iptables -A FORWARD -i $lxc_bridge -o $main_iface -j ACCEPT
iptables -A FORWARD -i $main_iface -o $lxc_bridge -j ACCEPT
iptables -t nat -A POSTROUTING -s $plage_ip.0/24 -j MASQUERADE
fi
2022-07-20 12:04:29 +02:00
#=================================================
# RESTORE SYSTEMD
#=================================================
2022-07-20 12:07:54 +02:00
ynh_script_progression --message="Restoring the systemd configuration..."
2022-07-20 12:04:29 +02:00
ynh_restore_file --origin_path="/etc/systemd/system/$app.service"
systemctl enable $app.service --quiet
#=================================================
# RESTORE THE LOGROTATE CONFIGURATION
#=================================================
2022-07-20 12:07:54 +02:00
ynh_script_progression --message="Restoring the logrotate configuration..."
2022-07-20 12:04:29 +02:00
2022-07-21 19:56:12 +02:00
mkdir -p /var/log/$app
chown -R $app:$app "/var/log/$app"
2022-07-20 12:04:29 +02:00
ynh_restore_file --origin_path="/etc/logrotate.d/$app"
#=================================================
# INTEGRATE SERVICE IN YUNOHOST
#=================================================
2022-07-20 12:07:54 +02:00
ynh_script_progression --message="Integrating service in YunoHost..."
2022-07-20 12:04:29 +02:00
2022-07-21 23:14:02 +02:00
yunohost service add $app --log="/var/log/$app/$app.log" --needs_exposed_ports $needs_exposed_ports
2022-07-20 12:04:29 +02:00
#=================================================
# START SYSTEMD SERVICE
#=================================================
2022-07-20 12:07:54 +02:00
ynh_script_progression --message="Starting a systemd service..."
2022-07-20 12:04:29 +02:00
2022-07-21 08:24:29 +02:00
ynh_systemd_action --service_name=$app --action="start" --log_path="/var/log/$app/$app.log" --line_match="Nomad agent started"
2022-07-20 12:04:29 +02:00
#=================================================
# GENERIC FINALIZATION
#=================================================
2022-07-21 08:24:29 +02:00
# RELOAD NGINX
2022-07-20 12:04:29 +02:00
#=================================================
2022-07-21 08:24:29 +02:00
ynh_script_progression --message="Reloading NGINX web server..."
2022-07-20 12:04:29 +02:00
ynh_systemd_action --service_name=nginx --action=reload
#=================================================
# END OF SCRIPT
#=================================================
2022-07-20 12:07:54 +02:00
ynh_script_progression --message="Restoration completed for $app"