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-07-21 19:51:27 +02:00

259 lines
8.5 KiB
Bash
Executable file

#!/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 () {
ynh_clean_check_starting
}
# 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
server_ip=$YNH_APP_ARG_SERVER_IP
app=$YNH_APP_INSTANCE_NAME
lxc_bridge="lxcbr0"
plage_ip="10.1.44"
main_iface=$(sudo ip route | grep default | awk '{print $5;}')
#=================================================
# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
#=================================================
ynh_script_progression --message="Validating installation parameters..."
# 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..."
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=server_ip --value=$server_ip
ynh_app_setting_set --app=$app --key=lxc_bridge --value=$lxc_bridge
ynh_app_setting_set --app=$app --key=plage_ip --value=$plage_ip
ynh_app_setting_set --app=$app --key=main_iface --value=$main_iface
#=================================================
# STANDARD MODIFICATIONS
#=================================================
# FIND AND OPEN A PORT
#=================================================
ynh_script_progression --message="Finding an available port..."
# 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..."
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..."
if [ "$node_type" == "client" ]
then
ynh_install_app_dependencies $client_pkg_dependencies
ynh_install_go --go_version=$go_version
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"
#=================================================
# CREATE DEDICATED USER
#=================================================
ynh_script_progression --message="Configuring system user..."
# Create a system user
ynh_system_user_create --username=$app
#=================================================
# NGINX CONFIGURATION
#=================================================
ynh_script_progression --message="Configuring NGINX web server..."
# Create a dedicated NGINX config
ynh_add_nginx_config
#=================================================
# SPECIFIC SETUP
#=================================================
# CREATE DATA DIRECTORY
#=================================================
ynh_script_progression --message="Creating a data directory..."
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 LXC DRIVER
#=================================================
if [ "$node_type" == "client" ]
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_go build
popd
mv -f $tempdir/nomad-driver-lxc $datadir/plugins/nomad-driver-lxc
ynh_secure_remove --file="$tempdir"
ynh_remove_go
fi
#=================================================
# ADD A CONFIGURATION
#=================================================
ynh_script_progression --message="Adding a configuration file..."
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"
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
#=================================================
# SETUP SYSTEMD
#=================================================
ynh_script_progression --message="Configuring a systemd service..."
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..."
# Use logrotate to manage application logfile(s)
ynh_use_logrotate
#=================================================
# INTEGRATE SERVICE IN YUNOHOST
#=================================================
ynh_script_progression --message="Integrating service in YunoHost..."
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..."
# 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..."
# 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..."
ynh_systemd_action --service_name=nginx --action=reload
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Installation of $app completed"