From af05a9d9706ffb603c4869f6dddb379c7e3ac704 Mon Sep 17 00:00:00 2001 From: Thomas Parisot Date: Sat, 16 Jan 2021 21:28:32 +0100 Subject: [PATCH] Alter scripts to meet with new requirements --- scripts/change_url | 15 +++---- scripts/install | 102 ++++++++++++++++++--------------------------- scripts/restore | 7 ---- scripts/upgrade | 65 ++++++++++------------------- 4 files changed, 67 insertions(+), 122 deletions(-) diff --git a/scripts/change_url b/scripts/change_url index bb7b90a..b588349 100644 --- a/scripts/change_url +++ b/scripts/change_url @@ -103,23 +103,18 @@ then mv $nginx_conf_path /etc/nginx/conf.d/$new_domain.d/$app.conf # Store file checksum for the new config file location ynh_store_file_checksum --file="/etc/nginx/conf.d/$new_domain.d/$app.conf" - yunohost user update "${app}_notifs" --mail ${app}_notifs@{$new_domain} --remove-mailalias ${app}@${old_domain} --add-mailalias ${app}@${new_domain} + yunohost user update "${app}_notifs" --remove-mailalias ${app}@${old_domain} --add-mailalias ${app}@${new_domain} fi #================================================= # SPECIFIC MODIFICATIONS #================================================= -# MODIFY A CONFIG FILE +# MODIFY SYSTEMD SERVICE #================================================= -ynh_script_progression --message="Modifying a config file..." +ynh_script_progression --message="Modifying a systemd service file..." -config="$final_path/$app/config/prod.secret.exs" -ynh_backup_if_checksum_is_different --file="$config" - -ynh_replace_string --match_string="$old_domain" --replace_string="$new_domain" --target_file="$config" - -# Recalculate and store the checksum of the file for the next upgrade. -ynh_store_file_checksum --file="$config" +finalsystemdconf="/etc/systemd/system/$app.service" +ynh_replace_string --match_string="$old_domain" --replace_string="$new_domain" --target_file="$finalsystemdconf" #================================================= # GENERIC FINALISATION diff --git a/scripts/install b/scripts/install index 5458eda..37eaab8 100644 --- a/scripts/install +++ b/scripts/install @@ -31,6 +31,9 @@ language=$YNH_APP_ARG_LANGUAGE admin_email=$(ynh_user_get_info $admin 'mail') ynh_user_password=$(ynh_string_random --length=30) app=$YNH_APP_INSTANCE_NAME +ynh_user="${app}_notifs" +secret=$(ynh_string_random --length=64) +secret_key=$(ynh_string_random --length=64) #================================================= # CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS @@ -57,6 +60,8 @@ ynh_app_setting_set --app=$app --key=is_public --value=$is_public ynh_app_setting_set --app=$app --key=language --value=$language ynh_app_setting_set --app=$app --key=admin_email --value=$admin_email ynh_app_setting_set --app=$app --key=ynh_user_password --value=$ynh_user_password +ynh_app_setting_set --app=$app --key=secret --value=$secret +ynh_app_setting_set --app=$app --key=secret_key --value=$secret_key #================================================= # STANDARD MODIFICATIONS @@ -66,28 +71,16 @@ ynh_app_setting_set --app=$app --key=ynh_user_password --value=$ynh_user_passwor ynh_script_progression --message="Finding an available port..." # Find an available port -port=$(ynh_find_port --port=8095) +port=$(ynh_find_port --port=4000) ynh_app_setting_set --app=$app --key=port --value=$port #================================================= # INSTALL DEPENDENCIES #================================================= -ynh_script_progression --message="Installing dependencies..." +ynh_script_progression --message="Installing dependencies..." --weight=2 ynh_install_app_dependencies $pkg_dependencies -ynh_install_nodejs --nodejs_version=$NODEJS_VERSION - -ynh_install_extra_app_dependencies --repo="deb https://dl.yarnpkg.com/debian/ stable main" --package="yarn" --key='https://dl.yarnpkg.com/debian/pubkey.gpg' - -#================================================= -# INSTALL EXTRA DEPENDENCIES -#================================================= -ynh_script_progression --message="Installing extra dependencies ..." - -lsb_name="$(lsb_release --codename --short)" -ynh_install_extra_app_dependencies --repo="deb http://packages.erlang-solutions.com/debian $lsb_name contrib" --package="$extra_pkg_dependencies" --key='https://packages.erlang-solutions.com/debian/erlang_solutions.asc' - #================================================= # CREATE A POSTGRESQL DATABASE #================================================= @@ -108,7 +101,7 @@ ynh_psql_execute_as_root --sql="ALTER USER $db_user PASSWORD '$db_pwd';" --datab #================================================= # DOWNLOAD, CHECK AND UNPACK SOURCE #================================================= -ynh_script_progression --message="Setting up source files..." +ynh_script_progression --message="Setting up source files..." --weight=2 ynh_app_setting_set --app=$app --key=final_path --value=$final_path # Download, check integrity, uncompress and patch the source from app.src @@ -119,8 +112,10 @@ ynh_setup_source --dest_dir="$final_path/$app" #================================================= ynh_script_progression --message="Configuring nginx web server..." +app_version=$($final_path/$app/bin/mobilizon version | cut -d " " -f2) + # Create a dedicated nginx config -ynh_add_nginx_config +ynh_add_nginx_config "app_version" #================================================= # CREATE DEDICATED USER @@ -130,7 +125,7 @@ ynh_script_progression --message="Configuring system user..." # Create a system user ynh_system_user_create --username=$app --home_dir=$final_path -yunohost user create ${app}_notifs --firstname "Mobilizon" --lastname "Notifications" --mail ${app}_notifs@$domain --password "$ynh_user_password" -q 0 +yunohost user create ${app}_notifs --firstname "Mobilizon" --lastname "Notifications" --domain $domain --password "$ynh_user_password" -q 0 yunohost user update ${app}_notifs --add-mailalias $app@$domain --add-mailforward $admin_email #================================================= @@ -138,46 +133,12 @@ yunohost user update ${app}_notifs --add-mailalias $app@$domain --add-mailforwar #================================================= # MAKE SETUP #================================================= -ynh_script_progression --message="Installing dependencies and building app..." --weight=5 +ynh_script_progression --message="Installing dependencies and building app..." --weight=1 # Give permission to the final_path chown -R "$app":"$app" "$final_path" -config="$final_path/$app/config/prod.secret.exs" - -pushd $final_path/$app/js - ynh_use_nodejs - ynh_script_progression --message="Installing NodeJS dependencies (this is going to take a while...)" - sudo -u $app env PATH=$PATH yarn install - ynh_script_progression --message="Building NodeJS application (this is going to take a while...)" - sudo -u $app env PATH=$PATH NODE_BUILD_MEMORY=1024 yarn run build -popd - -ynh_script_progression --message="Building Elixir application (this is going to take a while...)" -pushd $final_path/$app - sudo -u "$app" MIX_ENV=prod mix local.hex --force - sudo -u "$app" MIX_ENV=prod mix local.rebar --force - sudo -u "$app" MIX_ENV=prod mix deps.get - sudo -u "$app" MIX_ENV=prod mix compile - sudo -u "$app" MIX_ENV=prod mix mobilizon.instance gen --force --output $config --output-psql /tmp/setup_db.psql --domain $domain --instance-name "Mobilizon" --admin-email "$app@$domain" --dbhost localhost --dbname $db_name --dbuser $db_user --dbpass $db_pwd --listen-port $port -popd - -cat "../conf/ldap.exs" >> "$config" -cat "../conf/mail.exs" >> "$config" - -pushd $final_path/$app - chmod o-rwx $config - ynh_replace_string --match_string="__YNH_USER__" --replace_string="${app}_notifs" --target_file="$config" - ynh_replace_string --match_string="__YNH_USER_PASSWORD__" --replace_string="${ynh_user_password}" --target_file="$config" - # Compile *again* because we added ldap conf in between... dunno if the first is relevant - sudo -u "$app" MIX_ENV=prod mix compile - ynh_secure_remove --file="/tmp/setup_db.psql" - sudo -u "$app" MIX_ENV=prod mix ecto.migrate - - # We generate a dummy password ... this will actually *not* be used because the admin is supposed to connect via the ldap - password=$(ynh_string_random --length=30) - sudo -u "$app" MIX_ENV=prod mix mobilizon.users.new "$admin_email" --admin --password "$password" -popd +cat "../conf/ldap.exs" >> "$final_path/$app/releases/$app_version/releases.exs" #================================================= # SETUP SYSTEMD @@ -185,15 +146,7 @@ popd ynh_script_progression --message="Configuring a systemd service..." # Create a dedicated systemd config -ynh_add_systemd_config - -#================================================= -# STORE THE CONFIG FILE CHECKSUM -#================================================= -ynh_script_progression --message="Storing the config file checksum..." - -# Calculate and store the config file checksum into the app settings -ynh_store_file_checksum --file="$config" +ynh_add_systemd_config --others_var="secret secret_key port domain ynh_user ynh_user_password db_user db_name db_pwd" #================================================= # GENERIC FINALIZATION @@ -220,6 +173,31 @@ ynh_script_progression --message="Starting a systemd service..." # Start a systemd service ynh_systemd_action --service_name=$app --action="start" --log_path=systemd --line_match="Access Mobilizon.Web.Endpoint at" +#================================================= +# SPECIFIC SETUP +#================================================= +# CONFIGURE LDAP CONNECTION +#================================================= +ynh_script_progression --message="App: setup ldap connection" + +sudo -u "$app" MIX_ENV=prod $final_path/$app/bin/mobilizon_ctl "ecto.migrate" + +#================================================= +# MIGRATE DATABASE +#================================================= +ynh_script_progression --message="App: migrate database" + +sudo -u "$app" MIX_ENV=prod $final_path/$app/bin/mobilizon_ctl "ecto.migrate" + +#================================================= +# AND CREATE FIRST USER +#================================================= +ynh_script_progression --message="App: create initial user" + +# We generate a dummy password ... this will actually *not* be used because the admin is supposed to connect via the ldap +password=$(ynh_string_random --length=30) +sudo -u "$app" MIX_ENV=prod $final_path/$app/bin/mobilizon_ctl "mobilizon.users.new" "$admin_email" --admin --password "$password" + #================================================= # SETUP SSOWAT #================================================= diff --git a/scripts/restore b/scripts/restore index 5358caa..dbb3971 100644 --- a/scripts/restore +++ b/scripts/restore @@ -92,13 +92,6 @@ ynh_script_progression --message="Reinstalling dependencies..." # Define and install dependencies ynh_install_app_dependencies $pkg_dependencies -ynh_install_nodejs --nodejs_version=$NODEJS_VERSION - -ynh_install_extra_app_dependencies --repo="deb https://dl.yarnpkg.com/debian/ stable main" --package="yarn" --key='https://dl.yarnpkg.com/debian/pubkey.gpg' - -lsb_name="$(lsb_release --codename --short)" -ynh_install_extra_app_dependencies --repo="deb http://packages.erlang-solutions.com/debian $lsb_name contrib" --package="$extra_pkg_dependencies" --key='https://packages.erlang-solutions.com/debian/erlang_solutions.asc' - #================================================= # RESTORE THE POSTGRESQL DATABASE #================================================= diff --git a/scripts/upgrade b/scripts/upgrade index 14fcdfe..cc74b17 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -28,6 +28,7 @@ db_user=$db_name db_pwd=$(ynh_app_setting_get --app=$app --key=db_pwd) port=$(ynh_app_setting_get --app=$app --key=port) secret=$(ynh_app_setting_get --app=$app --key=secret) +secret_key=$(ynh_app_setting_get --app=$app --key=secret_key) admin_email=$(ynh_app_setting_get --app=$app --key=admin_email) #================================================= @@ -128,7 +129,7 @@ if ynh_version_gt "1.0.0~ynh1" "${previous_version}" ; then # Implement ldap and mail cat "../conf/ldap.exs" >> "$config" cat "../conf/mail.exs" >> "$config" - + ynh_replace_string --match_string="__YNH_USER__" --replace_string="${app}_notifs" --target_file="$config" ynh_replace_string --match_string="__YNH_USER_PASSWORD__" --replace_string="${ynh_user_password}" --target_file="$config" @@ -143,24 +144,18 @@ fi if [ "$upgrade_type" == "UPGRADE_APP" ] then ynh_script_progression --message="Upgrading source files..." - + # Create a temporary directory tmpdir="$(mktemp -d)" - + # Backup the config file in the temp dir cp -a "$final_path/$app/config/prod.secret.exs" "$tmpdir/prod.secret.exs" - + # Remove the app directory securely ynh_secure_remove --file="$final_path/$app" # Download, check integrity, uncompress and patch the source from app.src ynh_setup_source --dest_dir="$final_path/$app" - - #Copy the admin saved settings from tmp directory to final path - cp -a "$tmpdir/prod.secret.exs" "$final_path/$app/config/prod.secret.exs" - - # Remove the tmp directory securely - ynh_secure_remove --file="$tmpdir" fi #================================================= @@ -169,7 +164,9 @@ fi ynh_script_progression --message="Upgrading nginx web server configuration..." # Create a dedicated nginx config -ynh_add_nginx_config +app_version=$($final_path/$app/bin/mobilizon version | cut -d " " -f2) + +ynh_add_nginx_config "app_version" #================================================= # UPGRADE DEPENDENCIES @@ -178,11 +175,6 @@ ynh_script_progression --message="Upgrading dependencies..." ynh_install_app_dependencies $pkg_dependencies -ynh_install_nodejs --nodejs_version=$NODEJS_VERSION - -lsb_name="$(lsb_release --codename --short)" -ynh_install_extra_app_dependencies --repo="deb http://packages.erlang-solutions.com/debian $lsb_name contrib" --package="$extra_pkg_dependencies" --key='https://packages.erlang-solutions.com/debian/erlang_solutions.asc' - #================================================= # CREATE DEDICATED USER #================================================= @@ -198,8 +190,8 @@ ynh_system_user_create --username=$app --home_dir=$final_path #================================================= ynh_script_progression --message="Modifying a config file..." -config="$final_path/$app/config/prod.secret.exs" -ynh_backup_if_checksum_is_different --file="$config" +cat "../conf/ldap.exs" >> "$final_path/$app/releases/$app_version/releases.exs" + chmod o-rwx $config #================================================= @@ -209,30 +201,6 @@ chmod o-rwx $config # Give permission to the final_path chown -R "$app":"$app" "$final_path" -pushd $final_path/$app/js - ynh_use_nodejs - ynh_script_progression --message="Installing NodeJS dependencies (this is going to take a while...)" - sudo -u $app env PATH=$PATH yarn install - ynh_script_progression --message="Building NodeJS application (this is going to take a while...)" - sudo -u $app env PATH=$PATH NODE_BUILD_MEMORY=1024 yarn run build -popd - -ynh_script_progression --message="Building Elixir application (this is going to take a while...)" -pushd $final_path/$app - sudo -u "$app" MIX_ENV=prod mix local.hex --force - sudo -u "$app" MIX_ENV=prod mix local.rebar --force - sudo -u "$app" MIX_ENV=prod mix deps.get - sudo -u "$app" MIX_ENV=prod mix compile - sudo -u "$app" MIX_ENV=prod mix ecto.migrate -popd - -#================================================= -# STORE THE CONFIG FILE CHECKSUM -#================================================= -ynh_script_progression --message="Storing the config file checksum..." - -# Recalculate and store the checksum of the file for the next upgrade. -ynh_store_file_checksum --file="$config" #================================================= # SETUP SYSTEMD @@ -240,7 +208,7 @@ ynh_store_file_checksum --file="$config" ynh_script_progression --message="Upgrading systemd configuration..." # Create a dedicated systemd config -ynh_add_systemd_config +ynh_add_systemd_config --others_var="secret secret_key port domain ynh_user ynh_user_password db_user db_name db_pwd" #================================================= # GENERIC FINALIZATION @@ -266,6 +234,17 @@ ynh_script_progression --message="Starting a systemd service..." ynh_systemd_action --service_name=$app --action="start" --log_path=systemd --line_match="Access Mobilizon.Web.Endpoint at" +#================================================= +# GENERIC FINALIZATION +#================================================= +# SECURE FILES AND DIRECTORIES +#================================================= + +ynh_script_progression --message="App: running database migrations" +pushd $final_path/$app + sudo -u "$app" MIX_ENV=prod $final_path/$app/bin/mobilizon_ctl "ecto.migrate" +popd + #================================================= # SETUP SSOWAT #=================================================