WIP packaging

This commit is contained in:
oiseauroch
2022-07-30 14:02:57 +02:00
parent 7d197c8959
commit bbcc67e597
16 changed files with 1031 additions and 686 deletions

View File

@@ -24,7 +24,16 @@ ynh_abort_if_errors
# RETRIEVE ARGUMENTS FROM THE MANIFEST
#=================================================
#port mandatory for garage
port=3901
ip=$(get_ip)
domain=$YNH_APP_ARG_DOMAIN
rpc_secret=$YNH_APP_ARG_RPC_SECRET
bootstrap_peers=$YNH_APP_ARG_BOOTSTRAP_PEERS
datadir=$YNH_APP_ARG_DATADIR
weight=$YNH_APP_ARG_WEIGHT
### If it's a multi-instance app, meaning it can be installed several times independently
### The id of the app as stated in the manifest is available as $YNH_APP_ID
@@ -55,9 +64,25 @@ ynh_script_progression --message="Validating installation parameters..." --time
### If the app provides an internal web server (or uses another application server such as uWSGI), the final path should be "/opt/yunohost/$app"
final_path=/opt/yunohost/$app
test ! -e "$final_path" || ynh_die --message="This path already contains a folder"
ynh_port_available --port=3900|| ynh_die --message="Port 3900 is needs to be available for this app"
if [[ -n "$rpc_secret" ]]
then
echo "$rpc_secret" | grep -E ^[0-9a-f]{64}$ || ynh_die --message="rpc_secret have to be a 32-byte hex-encoded random string. See https://garagehq.deuxfleurs.fr/documentation/reference-manual/configuration/ for more information"
else
rpc_secret=$(ynh_string_random 32| od -A n -t x -w64 | sed 's/ //g')
fi
if [ -n "$bootstrap_peers" ]
then
echo "$bootstrap_peers" | grep -E '[0-9a-f]{64}@(\b25[0-5]|\b2[0-4][0-9]|\b[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}:3901' || ynh_die --message="friend server id must have id with the following form : 1799bccfd7411eddcf9ebd316bc1f5287ad12a68094e1c6ac6abde7e6feae1ec@192.168.1.1:3901"
fi
if [ "$datadir" = "/home/yunohost.app/__APP_NAME__/data" ]
then
datadir="/home/yunohost.app/$app/data"
fi
# Register (book) web path
ynh_webpath_register --app=$app --domain=$domain --path_url=$path_url
ynh_webpath_register --app=$app --domain=$domain --path_url="/"
#=================================================
# STORE SETTINGS FROM MANIFEST
@@ -65,14 +90,17 @@ ynh_webpath_register --app=$app --domain=$domain --path_url=$path_url
ynh_script_progression --message="Storing installation settings..." --time --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=rpc_secret --value=$rpc_secret
ynh_app_setting_set --app=$app --key=datadir --value=$datadir
ynh_app_setting_set --app=$app --key=bootstrap_peers --value=$bootstrap_peers
#=================================================
# STANDARD MODIFICATIONS
#=================================================
# FIND AND OPEN A PORT
#=================================================
ynh_script_progression --message="Finding an available port..." --time --weight=1
ynh_script_progression --message="Finding available ports..." --time --weight=1
### Use these lines if you have to open a port for the application
### `ynh_find_port` will find the first available port starting from the given port.
@@ -80,22 +108,36 @@ ynh_script_progression --message="Finding an available port..." --time --weight=
### - Remove the section "CLOSE A PORT" in the remove script
# Find an available port
port=$(ynh_find_port --port=3900)
ynh_app_setting_set --app=$app --key=port --value=$port
port_api=$(ynh_find_port --port=3900)
port_web=$(ynh_find_port --port=3902)
port_admin=$(ynh_find_port --port=3903)
ynh_app_setting_set --app=$app --key=port_api --value=$port_api
ynh_app_setting_set --app=$app --key=port_web --value=$port_web
ynh_app_setting_set --app=$app --key=port_admin --value=$port_admin
# Optional: Expose this port publicly
# (N.B.: you only need to do this if the app actually needs to expose the port publicly.
# If you do this and the app doesn't actually need you are CREATING SECURITY HOLES IN THE SERVER !)
# Open the port
# ynh_script_progression --message="Configuring firewall..." --time --weight=1
# ynh_exec_warn_less yunohost firewall allow --no-upnp TCP $port
ynh_script_progression --message="Configuring firewall..." --time --weight=1
ynh_exec_warn_less yunohost firewall allow --no-upnp TCP $port
#=================================================
# INSTALL DEPENDENCIES
#=================================================
ynh_script_progression --message="Installing dependencies..." --time --weight=1
### `ynh_install_app_dependencies` allows you to add any "apt" dependencies to the package.
### Those deb packages will be installed as dependencies of this package.
### If you're not using this helper:
### - Remove the section "REMOVE DEPENDENCIES" in the remove script
### - Remove the variable "pkg_dependencies" in _common.sh
### - As well as the section "REINSTALL DEPENDENCIES" in the restore script
### - And the section "UPGRADE DEPENDENCIES" in the upgrade script
ynh_install_app_dependencies $pkg_dependencies
#=================================================
# CREATE DEDICATED USER
#=================================================
@@ -115,7 +157,9 @@ ynh_script_progression --message="Setting up source files..." --time --weight=1
ynh_app_setting_set --app=$app --key=final_path --value=$final_path
# Download, check integrity, uncompress and patch the source from app.src
ynh_setup_source --dest_dir="$final_path"
pushd $final_path
install_garage
popd
# FIXME: this should be managed by the core in the future
# Here, as a packager, you may have to tweak the ownerhsip/permissions
@@ -125,7 +169,7 @@ ynh_setup_source --dest_dir="$final_path"
# this will be treated as a security issue.
chmod 750 "$final_path"
chmod -R o-rwx "$final_path"
chown -R $app:www-data "$final_path"
chown -R $app:$app "$final_path"
#=================================================
@@ -138,6 +182,9 @@ ynh_script_progression --message="Configuring NGINX web server..." --time --weig
# Create a dedicated NGINX config
ynh_add_nginx_config
#add wildcard subdomain
ynh_replace_string --match_string="server_name $domain" --replace_string="server_name $domain *.$domain" --target_file="/etc/nginx/conf.d/$domain.conf"
ynh_store_file_checksum --file="/etc/nginx/conf.d/$domain.conf"
#=================================================
# SPECIFIC SETUP
#=================================================
@@ -157,10 +204,16 @@ ynh_script_progression --message="Creating a data directory..." --time --weight=
### - Remove the section "RESTORE THE DATA DIRECTORY" in the restore script
### - As well as the section "REMOVE DATA DIR" in the remove script
datadir=/home/yunohost.app/$app
ynh_app_setting_set --app=$app --key=datadir --value=$datadir
mkdir -p $datadir
mkdir -p $datadir/data
#=================================================
# create data partition
#=================================================
# to be sure to not exceed size limit, i use a virtual disk with a fix size to have a max limit size.
qemu-img create -f qcow2 $datadir/garage_data.qcow2 "$weight"G
nbd_index=$($final_path/mount_disk.sh "$datadir" true)
# FIXME: this should be managed by the core in the future
# Here, as a packager, you may have to tweak the ownerhsip/permissions
@@ -170,8 +223,10 @@ mkdir -p $datadir
# this will be treated as a security issue.
chmod 750 "$datadir"
chmod -R o-rwx "$datadir"
chown -R $app:www-data "$datadir"
chown -R $app:$app "$datadir"
umount /dev/nbd$nbd_index
qemu-img --disconnect /dev/nbd$nbd_index
#=================================================
# ADD A CONFIGURATION
#=================================================
@@ -189,13 +244,13 @@ ynh_script_progression --message="Adding a configuration file..." --time --weigh
###
### Check the documentation of `ynh_add_config` for more info.
ynh_add_config --template="some_config_file" --destination="$final_path/some_config_file"
ynh_add_config --template="garage.toml" --destination="$final_path/garage.toml"
# FIXME: this should be handled by the core in the future
# You may need to use chmod 600 instead of 400,
# for example if the app is expected to be able to modify its own config
chmod 400 "$final_path/some_config_file"
chown $app:$app "$final_path/some_config_file"
chmod 400 "$final_path/garage.toml"
chown $app:$app "$final_path/garag.toml"
### For more complex cases where you want to replace stuff using regexes,
### you shoud rely on ynh_replace_string (which is basically a wrapper for sed)
@@ -223,27 +278,6 @@ ynh_script_progression --message="Configuring a systemd service..." --time --wei
# Create a dedicated systemd config
ynh_add_systemd_config
#=================================================
# SETUP APPLICATION WITH CURL
#=================================================
### Use these lines only if the app installation needs to be finalized through
### web forms. We generally don't want to ask the final user,
### so we're going to use curl to automatically fill the fields and submit the
### forms.
# Set the app as temporarily public for curl call
ynh_script_progression --message="Configuring SSOwat..." --time --weight=1
# Making the app public for curl
ynh_permission_update --permission="main" --add="visitors"
# Installation with curl
ynh_script_progression --message="Finalizing installation..." --time --weight=1
ynh_local_curl "/INSTALL_PATH" "key1=value1" "key2=value2" "key3=value3"
# Remove the public access
ynh_permission_update --permission="main" --remove="visitors"
#=================================================
# GENERIC FINALIZATION
#=================================================
@@ -276,7 +310,7 @@ ynh_script_progression --message="Integrating service in YunoHost..." --time --w
### - As well as the section "INTEGRATE SERVICE IN YUNOHOST" in the restore script
### - And the section "INTEGRATE SERVICE IN YUNOHOST" in the upgrade script
yunohost service add $app --description="A short description of the app" --log="/var/log/$app/$app.log"
yunohost service add --needs_exposed_ports $app --description="s3 storage" --log="/var/log/$app/$app.log"
### Additional options starting with 3.8:
###
@@ -294,6 +328,7 @@ yunohost service add $app --description="A short description of the app" --log="
### weren't enabled on old installs (be careful it'll override the existing
### service though so you should re-provide all relevant flags when doing so)
#=================================================
# START SYSTEMD SERVICE
#=================================================
@@ -310,37 +345,14 @@ ynh_script_progression --message="Starting a systemd service..." --time --weight
# Start a systemd service
ynh_systemd_action --service_name=$app --action="start" --log_path="/var/log/$app/$app.log"
#=================================================
# SETUP FAIL2BAN
#=================================================
ynh_script_progression --message="Configuring Fail2Ban..." --time --weight=1
# Create a dedicated Fail2Ban config
ynh_add_fail2ban_config --logpath="/var/log/nginx/${domain}-error.log" --failregex="Regex to match into the log for a failed login"
#=================================================
# SETUP SSOWAT
#=================================================
ynh_script_progression --message="Configuring permissions..." --time --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
### N.B. : the following extra permissions only make sense if your app
### does have for example an admin interface or an API.
# Only the admin can access the admin panel of the app (if the app has an admin panel)
ynh_permission_create --permission="admin" --url="/admin" --allowed=$admin
# Everyone can access the API part
# We don't want to display the tile in the SSO so we put --show_tile="false"
# And we don't want the YunoHost admin to be able to remove visitors group to this permission, so we put --protected="true"
ynh_permission_create --permission="api" --url="/api" --allowed="visitors" --show_tile="false" --protected="true"
#=================================================
# RELOAD NGINX
@@ -349,6 +361,24 @@ ynh_script_progression --message="Reloading NGINX web server..." --time --weight
ynh_systemd_action --service_name=nginx --action=reload
#=================================================
# CONFIGURE GARAGE
#=================================================
ynh_script_progression --message="Configuring garage..." --time --weight=1
garage_command="$final_path/garage -c $final_path/garage.toml"
gagare_id=$($garage_command node id -q | cut -d '@' -f1)
$garage_command assign $garage_id -z $domain -c
#=================================================
# Send email to admin
#=================================================
ynh_send_readme_to_admin --app_message=app_message
#=================================================
# END OF SCRIPT
#=================================================