From 7428ad8313c2fd54b8f811aa11168494dedc7ba7 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Fri, 8 Feb 2019 23:10:29 +0100 Subject: [PATCH] Moving environment variable from systemd to .env file --- conf/.env | 14 ++++++++++++++ conf/systemd.service | 6 +----- scripts/install | 14 ++++++++------ scripts/upgrade | 35 ++++++++++++++++++++++------------- 4 files changed, 45 insertions(+), 24 deletions(-) create mode 100644 conf/.env diff --git a/conf/.env b/conf/.env new file mode 100644 index 0000000..6df3fe4 --- /dev/null +++ b/conf/.env @@ -0,0 +1,14 @@ +# The path to NODEJS +PATH=__NODEJS_PATH__ + +# The port used by the application +PORT=__PORT__ + +# The directory where is store the database +DB_DIR=__FINALPATH__/db + +# The external URL +EXTERNAL_URL=https://__DOMAIN_URI__ + +# The Node Environnement +NODE_ENV=production \ No newline at end of file diff --git a/conf/systemd.service b/conf/systemd.service index 92ec11a..1dd8209 100644 --- a/conf/systemd.service +++ b/conf/systemd.service @@ -6,11 +6,7 @@ After=network.target Type=simple User=__APP__ Group=__APP__ -Environment="PATH=__NODEJS_PATH__" -Environment="PORT=__PORT__" -Environment="DB_DIR=__FINALPATH__/db" -Environment="EXTERNAL_URL=https://__DOMAIN_URI__" -Environment="NODE_ENV=production" +EnvironmentFile=__FINALPATH__/.env WorkingDirectory=__FINALPATH__/ ExecStart=/bin/sh -c '__FINALPATH__/node_modules/.bin/ts-node --project __FINALPATH__/tsconfig.json __FINALPATH__/bin/server.ts >> /var/log/__APP__/__APP__.log 2>&1' diff --git a/scripts/install b/scripts/install index 9d4400f..f6ba20e 100644 --- a/scripts/install +++ b/scripts/install @@ -207,9 +207,6 @@ popd ### - And the section "SETUP SYSTEMD" in the upgrade script # Create a dedicated systemd config -ynh_replace_string "__NODEJS_PATH__" "$nodejs_path" "../conf/systemd.service" -ynh_replace_string "__PORT__" "$port" "../conf/systemd.service" -ynh_replace_string "__DOMAIN_URI__" "$domain$path_url" "../conf/systemd.service" ynh_add_systemd_config #================================================= @@ -248,7 +245,12 @@ ynh_add_systemd_config ### `ynh_replace_string` is used to replace a string in a file. ### (It's compatible with sed regular expressions syntax) -#ynh_replace_string "match_string" "replace_string" "$final_path/CONFIG_FILE" +cp "../conf/.env" "$final_path/.env" + +ynh_replace_string "__NODEJS_PATH__" "$nodejs_path" "$final_path/.env" +ynh_replace_string "__PORT__" "$port" "$final_path/.env" +ynh_replace_string "__DOMAIN_URI__" "$domain$path_url" "$final_path/.env" +ynh_replace_string "__FINALPATH__" "$final_path" "$final_path/.env" #================================================= # STORE THE CONFIG FILE CHECKSUM @@ -259,7 +261,7 @@ ynh_add_systemd_config ### you can make a backup of this file before modifying it again if the admin had modified it. # Calculate and store the config file checksum into the app settings -#ynh_store_file_checksum "$final_path/package.json" +ynh_store_file_checksum "$final_path/.env" #================================================= # GENERIC FINALIZATION @@ -272,7 +274,7 @@ ynh_add_systemd_config ### that really need such authorization. # Set permissions to app files -#chown -R root: $final_path +chown -R "$app":"$app" "$final_path" #================================================= # SETUP LOGROTATE diff --git a/scripts/upgrade b/scripts/upgrade index f20e274..578ddde 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -64,12 +64,6 @@ ynh_clean_setup () { # Exit if an error occurs during the execution of the script ynh_abort_if_errors -#================================================= -# STOP SERVICE -#================================================= - -ynh_systemd_action --action=stop --service_name=$app - #================================================= # CHECK THE PATH #================================================= @@ -77,6 +71,12 @@ ynh_systemd_action --action=stop --service_name=$app # Normalize the URL path syntax path_url=$(ynh_normalize_url_path $path_url) +#================================================= +# STOP SERVICE +#================================================= + +ynh_systemd_action --action=stop --service_name=$app + #================================================= # STANDARD UPGRADE STEPS #================================================= @@ -120,8 +120,6 @@ ynh_system_user_create "$app" "$final_path" #================================================= # SPECIFIC UPGRADE -#================================================= - #================================================= # CREATE DB FOLDER #================================================= @@ -140,11 +138,25 @@ pushd $final_path sudo -u $app env PATH=$PATH:$nodejs_path npm audit fix popd +#================================================= +# MODIFY A CONFIG FILE +#================================================= + +### `ynh_replace_string` is used to replace a string in a file. +### (It's compatible with sed regular expressions syntax) + +cp "../conf/.env" "$final_path/.env" + +ynh_replace_string "__NODEJS_PATH__" "$nodejs_path" "$final_path/.env" +ynh_replace_string "__PORT__" "$port" "$final_path/.env" +ynh_replace_string "__DOMAIN_URI__" "$domain$path_url" "$final_path/.env" +ynh_replace_string "__FINALPATH__" "$final_path" "$final_path/.env" + ### Verify the checksum of a file, stored by `ynh_store_file_checksum` in the install script. ### And create a backup of this file if the checksum is different. So the file will be backed up if the admin had modified it. -#ynh_backup_if_checksum_is_different "$final_path/CONFIG_FILE" +ynh_backup_if_checksum_is_different "$final_path/.env" # Recalculate and store the checksum of the file for the next upgrade. -#ynh_store_file_checksum "$final_path/CONFIG_FILE" +ynh_store_file_checksum "$final_path/.env" #================================================= # SETUP LOGROTATE @@ -158,9 +170,6 @@ ynh_use_logrotate --non-append #================================================= # Create a dedicated systemd config -ynh_replace_string "__NODEJS_PATH__" "$nodejs_path" "../conf/systemd.service" -ynh_replace_string "__PORT__" "$port" "../conf/systemd.service" -ynh_replace_string "__DOMAIN_URI__" "$domain$path_url" "../conf/systemd.service" ynh_add_systemd_config #=================================================