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

269 lines
8.9 KiB
Text
Raw Normal View History

2022-07-20 12:04:29 +02:00
#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
2022-07-21 08:24:29 +02:00
source ynh_install_go
2022-07-20 12:04:29 +02:00
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
#=================================================
# RETRIEVE ARGUMENTS FROM THE MANIFEST
#=================================================
domain=$YNH_APP_ARG_DOMAIN
2022-07-21 08:24:29 +02:00
path_url="/"
2022-07-20 12:04:29 +02:00
is_public=$YNH_APP_ARG_IS_PUBLIC
2022-07-21 08:24:29 +02:00
node_type=$YNH_APP_ARG_NODE_TYPE
2022-07-23 04:31:09 +02:00
bootstrap_expect=$YNH_APP_ARG_BOOTSTRAP_EXPECT
2022-07-21 08:24:29 +02:00
server_ip=$YNH_APP_ARG_SERVER_IP
2022-07-20 12:04:29 +02:00
app=$YNH_APP_INSTANCE_NAME
2022-07-21 08:24:29 +02:00
lxc_bridge="lxcbr0"
plage_ip="10.1.44"
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
2022-07-20 12:04:29 +02:00
#=================================================
# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
#=================================================
2022-07-20 12:07:54 +02:00
ynh_script_progression --message="Validating installation parameters..."
2022-07-20 12:04:29 +02:00
# Register (book) web path
ynh_webpath_register --app=$app --domain=$domain --path_url=$path_url
#=================================================
# STORE SETTINGS FROM MANIFEST
#=================================================
2022-07-20 12:07:54 +02:00
ynh_script_progression --message="Storing installation settings..."
2022-07-20 12:04:29 +02:00
ynh_app_setting_set --app=$app --key=domain --value=$domain
ynh_app_setting_set --app=$app --key=path --value=$path_url
2022-07-21 08:24:29 +02:00
ynh_app_setting_set --app=$app --key=node_type --value=$node_type
2022-07-23 04:31:09 +02:00
ynh_app_setting_set --app=$app --key=bootstrap_expect --value=$bootstrap_expect
2022-07-21 08:24:29 +02:00
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
2022-07-20 12:04:29 +02:00
#=================================================
# STANDARD MODIFICATIONS
#=================================================
# FIND AND OPEN A PORT
#=================================================
2022-07-20 12:07:54 +02:00
ynh_script_progression --message="Finding an available port..."
2022-07-20 12:04:29 +02:00
# Find an available port
2022-07-21 08:24:29 +02:00
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
2022-07-20 12:04:29 +02:00
2022-07-21 08:24:29 +02:00
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
2022-07-20 12:04:29 +02:00
# Open the port
2022-07-21 08:24:29 +02:00
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
2022-07-20 12:04:29 +02:00
#=================================================
# INSTALL DEPENDENCIES
#=================================================
2022-07-20 12:07:54 +02:00
ynh_script_progression --message="Installing 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
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"
2022-07-20 12:04:29 +02:00
#=================================================
# CREATE DEDICATED USER
#=================================================
2022-07-20 12:07:54 +02:00
ynh_script_progression --message="Configuring system user..."
2022-07-20 12:04:29 +02:00
# Create a system user
2022-07-21 08:24:29 +02:00
ynh_system_user_create --username=$app
2022-07-20 12:04:29 +02:00
#=================================================
# NGINX CONFIGURATION
#=================================================
2022-07-20 12:07:54 +02:00
ynh_script_progression --message="Configuring NGINX web server..."
2022-07-20 12:04:29 +02:00
# Create a dedicated NGINX config
ynh_add_nginx_config
#=================================================
# SPECIFIC SETUP
#=================================================
# CREATE DATA DIRECTORY
#=================================================
2022-07-20 12:07:54 +02:00
ynh_script_progression --message="Creating a data directory..."
2022-07-20 12:04:29 +02:00
datadir=/home/yunohost.app/$app
ynh_app_setting_set --app=$app --key=datadir --value=$datadir
mkdir -p $datadir
2022-07-21 08:24:29 +02:00
mkdir -p $datadir/plugins
2022-07-20 12:04:29 +02:00
chmod 750 "$datadir"
chmod -R o-rwx "$datadir"
2022-07-21 08:24:29 +02:00
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"
2022-07-23 03:46:50 +02:00
ynh_exec_warn_less $ynh_go build
2022-07-21 08:24:29 +02:00
popd
mv -f $tempdir/nomad-driver-lxc $datadir/plugins/nomad-driver-lxc
ynh_secure_remove --file="$tempdir"
fi
2022-07-20 12:04:29 +02:00
#=================================================
# ADD A CONFIGURATION
#=================================================
2022-07-20 12:07:54 +02:00
ynh_script_progression --message="Adding a configuration file..."
2022-07-20 12:04:29 +02:00
2022-07-21 08:24:29 +02:00
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"
2022-07-20 12:04:29 +02:00
2022-07-21 08:24:29 +02:00
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"
2022-07-23 03:49:39 +02:00
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"
2022-07-22 03:13:37 +02:00
ynh_add_config --template="../conf/dnsmasq-lxd" --destination="/etc/dnsmasq.d/lxd"
2022-07-22 02:31:36 +02:00
systemctl restart dnsmasq
2022-07-23 03:41:02 +02:00
if [ ! ${PACKAGE_CHECK_EXEC:-0} -eq 1 ]; then
ynh_add_config --template="../conf/lxc-net" --destination="/etc/default/lxc-net"
fi
2022-07-22 02:31:36 +02:00
ynh_add_config --template="../conf/default.conf" --destination="/etc/lxc/default.conf"
systemctl enable lxc-net --quiet
2022-07-22 03:42:13 +02:00
ynh_systemd_action --service_name=lxc-net --action="restart" --line_match="Started LXC network bridge" --log_path="systemd"
2022-07-21 08:24:29 +02:00
fi
2022-07-20 12:04:29 +02:00
#=================================================
# SETUP SYSTEMD
#=================================================
2022-07-20 12:07:54 +02:00
ynh_script_progression --message="Configuring a systemd service..."
2022-07-20 12:04:29 +02:00
2022-07-21 08:24:29 +02:00
systemd_user=$app
if [ "$node_type" == "client" ]
then
systemd_user="root"
fi
2022-07-20 12:04:29 +02:00
# Create a dedicated systemd config
ynh_add_systemd_config
#=================================================
# GENERIC FINALIZATION
#=================================================
# SETUP LOGROTATE
#=================================================
2022-07-20 12:07:54 +02:00
ynh_script_progression --message="Configuring log rotation..."
2022-07-20 12:04:29 +02:00
# Use logrotate to manage application logfile(s)
ynh_use_logrotate
#=================================================
# INTEGRATE SERVICE IN YUNOHOST
#=================================================
2022-07-20 12:07:54 +02:00
ynh_script_progression --message="Integrating service in YunoHost..."
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
# Start a systemd service
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
#=================================================
# SETUP SSOWAT
#=================================================
2022-07-20 12:07:54 +02:00
ynh_script_progression --message="Configuring permissions..."
2022-07-20 12:04:29 +02:00
# 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
#=================================================
2022-07-20 12:07:54 +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="Installation of $app completed"