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/install
2022-09-03 18:58:58 +02:00

288 lines
9.7 KiB
Bash

#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source ynh_install_go
source /usr/share/yunohost/helpers
#=================================================
# MANAGE SCRIPT FAILURE
#=================================================
ynh_clean_setup () {
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="/"
is_public=$YNH_APP_ARG_IS_PUBLIC
node_type=$YNH_APP_ARG_NODE_TYPE
bootstrap_expect=$YNH_APP_ARG_BOOTSTRAP_EXPECT
retry_join=$YNH_APP_ARG_RETRY_JOIN
server_ip=$YNH_APP_ARG_SERVER_IP
driver_lxc=$YNH_APP_ARG_DRIVER_LXC
app=$YNH_APP_INSTANCE_NAME
client_lxc_bridge="lxcbr0"
client_lxc_plage_ip="10.1.44"
client_lxc_main_iface=$(ip route | grep default | awk '{print $5;}')
#=================================================
# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
#=================================================
ynh_script_progression --message="Validating installation parameters..." --weight=1
# 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
ynh_app_setting_set --app=$app --key=domain --value=$domain
ynh_app_setting_set --app=$app --key=path --value=$path_url
ynh_app_setting_set --app=$app --key=node_type --value=$node_type
ynh_app_setting_set --app=$app --key=bootstrap_expect --value=$bootstrap_expect
ynh_app_setting_set --app=$app --key=retry_join --value=$retry_join
ynh_app_setting_set --app=$app --key=server_ip --value=$server_ip
ynh_app_setting_set --app=$app --key=driver_lxc --value=$driver_lxc
ynh_app_setting_set --app=$app --key=client_lxc_bridge --value=$client_lxc_bridge
ynh_app_setting_set --app=$app --key=client_lxc_plage_ip --value=$client_lxc_plage_ip
ynh_app_setting_set --app=$app --key=client_lxc_main_iface --value=$client_lxc_main_iface
#=================================================
# STANDARD MODIFICATIONS
#=================================================
# FIND AND OPEN A PORT
#=================================================
ynh_script_progression --message="Finding an available port..." --weight=1
# Find an available port
http_port=4646
ynh_port_available --port=$http_port || ynh_die --message="Port $http_port is needs to be available for this app"
ynh_app_setting_set --app=$app --key=http_port --value=$http_port
rpc_port=4647
ynh_port_available --port=$rpc_port || ynh_die --message="Port $rpc_port is needs to be available for this app"
ynh_app_setting_set --app=$app --key=rpc_port --value=$rpc_port
serf_port=4648
ynh_port_available --port=$serf_port || ynh_die --message="Port $serf_port is needs to be available for this app"
ynh_app_setting_set --app=$app --key=serf_port --value=$serf_port
# Open the port
ynh_script_progression --message="Configuring firewall..." --weight=1
ynh_exec_warn_less yunohost firewall allow --no-upnp TCP $rpc_port
needs_exposed_ports="$rpc_port"
if [ "$node_type" == "server" ]
then
ynh_exec_warn_less yunohost firewall allow --no-upnp TCP $serf_port
needs_exposed_ports="$serf_port $needs_exposed_ports"
fi
#=================================================
# INSTALL DEPENDENCIES
#=================================================
ynh_script_progression --message="Installing dependencies..." --weight=1
if [ "$node_type" == "server" ]
then
pkg_dependencies="$pkg_dependencies $server_pkg_dependencies"
fi
if [ "$node_type" == "client" ]
then
if [ $driver_lxc -eq 1 ]
then
client_pkg_dependencies="$client_pkg_dependencies $client_lxc_pkg_dependencies"
ynh_exec_warn_less ynh_install_go --go_version=$go_version
fi
pkg_dependencies="$pkg_dependencies $client_pkg_dependencies"
fi
ynh_install_app_dependencies $pkg_dependencies
ynh_install_extra_app_dependencies --repo="deb https://apt.releases.hashicorp.com $(lsb_release -cs) main" --package="$extra_pkg_dependencies" --key="https://apt.releases.hashicorp.com/gpg"
#=================================================
# CREATE DEDICATED USER
#=================================================
ynh_script_progression --message="Configuring system user..." --weight=1
# Create a system user
ynh_system_user_create --username=$app
#=================================================
# NGINX CONFIGURATION
#=================================================
ynh_script_progression --message="Configuring NGINX web server..." --weight=1
# Create a dedicated NGINX config
ynh_add_nginx_config
#=================================================
# SPECIFIC SETUP
#=================================================
# CREATE DATA DIRECTORY
#=================================================
ynh_script_progression --message="Creating a data directory..." --weight=1
datadir=/home/yunohost.app/$app
ynh_app_setting_set --app=$app --key=datadir --value=$datadir
mkdir -p $datadir
mkdir -p $datadir/plugins
chmod 750 "$datadir"
chmod -R o-rwx "$datadir"
chown -R $app:$app "$datadir"
#=================================================
# BUILD DRIVERS
#=================================================
if [ "$node_type" == "client" ]
then
if [ $driver_lxc -eq 1 ]
then
ynh_script_progression --message="Building LXC driver..."
tempdir="$(mktemp -d)"
ynh_setup_source --dest_dir="$tempdir" --source_id="driver-lxc"
pushd $tempdir
final_path=$tempdir
ynh_use_go
export GOPATH="$tempdir/go"
export GOCACHE="$tempdir/.cache"
ynh_exec_warn_less $ynh_go build
popd
mv -f $tempdir/nomad-driver-lxc $datadir/plugins/nomad-driver-lxc
ynh_secure_remove --file="$tempdir"
fi
fi
#=================================================
# ADD A CONFIGURATION
#=================================================
ynh_script_progression --message="Adding a configuration file..." --weight=1
config_path=/etc/$app.d
ynh_app_setting_set --app=$app --key=config_path --value=$config_path
mkdir -p $config_path
chmod 750 "$config_path"
chmod -R o-rwx "$config_path"
chown -R $app:$app "$config_path"
ynh_add_config --template="../conf/nomad.hcl" --destination="$config_path/nomad.hcl"
chmod 400 "$config_path/nomad.hcl"
chown $app:$app "$config_path/nomad.hcl"
if [ "$node_type" == "server" ]
then
ynh_add_config --template="../conf/server.hcl" --destination="$config_path/server.hcl"
chmod 400 "$config_path/server.hcl"
chown $app:$app "$config_path/server.hcl"
fi
if [ "$node_type" == "client" ]
then
ynh_add_config --template="../conf/client.hcl" --destination="$config_path/client.hcl"
chmod 400 "$config_path/client.hcl"
chown $app:$app "$config_path/client.hcl"
if [ $driver_lxc -eq 1 ]
then
ynh_add_config --template="../conf/driver-lxc.hcl" --destination="$config_path/driver-lxc.hcl"
chmod 400 "$config_path/driver-lxc.hcl"
chown $app:$app "$config_path/driver-lxc.hcl"
ynh_add_config --template="../conf/dnsmasq-lxd" --destination="/etc/dnsmasq.d/lxd"
systemctl restart dnsmasq
if [ ! ${PACKAGE_CHECK_EXEC:-0} -eq 1 ]; then
ynh_add_config --template="../conf/lxc-net" --destination="/etc/default/lxc-net"
fi
ynh_add_config --template="../conf/default.conf" --destination="/etc/lxc/default.conf"
systemctl enable lxc-net --quiet
ynh_systemd_action --service_name=lxc-net --action="restart" --line_match="Started LXC network bridge" --log_path="systemd"
fi
fi
#=================================================
# SETUP SYSTEMD
#=================================================
ynh_script_progression --message="Configuring a systemd service..." --weight=1
systemd_user=$app
if [ "$node_type" == "client" ]
then
systemd_user="root"
fi
# Create a dedicated systemd config
ynh_add_systemd_config
#=================================================
# GENERIC FINALIZATION
#=================================================
# SETUP LOGROTATE
#=================================================
ynh_script_progression --message="Configuring log rotation..." --weight=1
# Use logrotate to manage application logfile(s)
ynh_use_logrotate
#=================================================
# INTEGRATE SERVICE IN YUNOHOST
#=================================================
ynh_script_progression --message="Integrating service in YunoHost..." --weight=1
yunohost service add $app --log="/var/log/$app/$app.log" --needs_exposed_ports $needs_exposed_ports
#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Starting a systemd service..." --weight=1
# Start a systemd service
ynh_systemd_action --service_name=$app --action="start" --log_path="/var/log/$app/$app.log" --line_match="Nomad agent started"
#=================================================
# SETUP SSOWAT
#=================================================
ynh_script_progression --message="Configuring permissions..." --weight=1
# Make app public if necessary
if [ $is_public -eq 1 ]
then
# Everyone can access the app.
# The "main" permission is automatically created before the install script.
ynh_permission_update --permission="main" --add="visitors"
fi
#=================================================
# RELOAD NGINX
#=================================================
ynh_script_progression --message="Reloading NGINX web server..." --weight=1
ynh_systemd_action --service_name=nginx --action=reload
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Installation of $app completed" --last