From a333907d97c1f7ec2c9f3502f9c169a0c56c16d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?E=CC=81ric=20Gaspar?= <46165813+ericgaspar@users.noreply.github.com> Date: Wed, 15 May 2024 10:36:39 +0200 Subject: [PATCH 01/13] Create redis --- helpers/redis | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 helpers/redis diff --git a/helpers/redis b/helpers/redis new file mode 100644 index 000000000..9d6257db3 --- /dev/null +++ b/helpers/redis @@ -0,0 +1,39 @@ +#!/bin/bash + +# get the first available redis database +# +# usage: ynh_redis_get_free_db +# | returns: the database number to use +ynh_redis_get_free_db() { + local result max db + result=$(redis-cli INFO keyspace) + + # get the num + max=$(cat /etc/redis/redis.conf | grep ^databases | grep -Eow "[0-9]+") + + db=0 + # default Debian setting is 15 databases + for i in $(seq 0 "$max") + do + if ! echo "$result" | grep -q "db$i" + then + db=$i + break 1 + fi + db=-1 + done + + test "$db" -eq -1 && ynh_die --message="No available Redis databases..." + + echo "$db" +} + +# Create a master password and set up global settings +# Please always call this script in install and restore scripts +# +# usage: ynh_redis_remove_db database +# | arg: database - the database to erase +ynh_redis_remove_db() { + local db=$1 + redis-cli -n "$db" flushall +} From 12ea021e3af5abdbf4062c0dec055e71d5e5c224 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?E=CC=81ric=20Gaspar?= <46165813+ericgaspar@users.noreply.github.com> Date: Wed, 15 May 2024 12:09:07 +0200 Subject: [PATCH 02/13] Add mongodb --- helpers/mongodb | 360 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 360 insertions(+) create mode 100644 helpers/mongodb diff --git a/helpers/mongodb b/helpers/mongodb new file mode 100644 index 000000000..e0c9277ce --- /dev/null +++ b/helpers/mongodb @@ -0,0 +1,360 @@ +#!/bin/bash + +readonly YNH_DEFAULT_MONGO_VERSION=4.4 +# Declare the actual MongoDB version to use: 4.4 ; 5.0 ; 6.0 +# A packager willing to use another version of MongoDB can override the variable into its _common.sh. +YNH_MONGO_VERSION=${YNH_MONGO_VERSION:-$YNH_DEFAULT_MONGO_VERSION} + +# Execute a mongo command +# +# example: ynh_mongo_exec --command='db.getMongo().getDBNames().indexOf("wekan")' +# example: ynh_mongo_exec --command="db.getMongo().getDBNames().indexOf(\"wekan\")" +# +# usage: ynh_mongo_exec [--user=user] [--password=password] [--authenticationdatabase=authenticationdatabase] [--database=database] [--host=host] [--port=port] --command="command" [--eval] +# | arg: -u, --user= - The user name to connect as +# | arg: -p, --password= - The user password +# | arg: -d, --authenticationdatabase= - The authenticationdatabase to connect to +# | arg: -d, --database= - The database to connect to +# | arg: -h, --host= - The host to connect to +# | arg: -P, --port= - The port to connect to +# | arg: -c, --command= - The command to evaluate +# | arg: -e, --eval - Evaluate instead of execute the command. +# +# +ynh_mongo_exec() { + # Declare an array to define the options of this helper. + local legacy_args=upadhPce + local -A args_array=( [u]=user= [p]=password= [a]=authenticationdatabase= [d]=database= [h]=host= [P]=port= [c]=command= [e]=eval ) + local user + local password + local authenticationdatabase + local database + local host + local port + local command + local eval + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + user="${user:-}" + password="${password:-}" + authenticationdatabase="${authenticationdatabase:-}" + database="${database:-}" + host="${host:-}" + port="${port:-}" + eval=${eval:-0} + + # If user is provided + if [ -n "$user" ] + then + user="--username=$user" + + # If password is provided + if [ -n "$password" ] + then + password="--password=$password" + fi + + # If authenticationdatabase is provided + if [ -n "$authenticationdatabase" ] + then + authenticationdatabase="--authenticationDatabase=$authenticationdatabase" + else + authenticationdatabase="--authenticationDatabase=admin" + fi + else + password="" + authenticationdatabase="" + fi + + # If host is provided + if [ -n "$host" ] + then + host="--host=$host" + fi + + # If port is provided + if [ -n "$port" ] + then + port="--port=$port" + fi + + # If eval is not provided + if [ $eval -eq 0 ] + then + # If database is provided + if [ -n "$database" ] + then + database="use $database" + else + database="" + fi + + mongosh --quiet --username $user --password $password --authenticationDatabase $authenticationdatabase --host $host --port $port < ./dump.bson +# +# usage: ynh_mongo_dump_db --database=database +# | arg: -d, --database= - The database name to dump +# | ret: the mongodump output +# +# +ynh_mongo_dump_db() { + # Declare an array to define the options of this helper. + local legacy_args=d + local -A args_array=( [d]=database= ) + local database + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + mongodump --quiet --db="$database" --archive +} + +# Create a user +# +# [internal] +# +# usage: ynh_mongo_create_user --db_user=user --db_pwd=pwd --db_name=name +# | arg: -u, --db_user= - The user name to create +# | arg: -p, --db_pwd= - The password to identify user by +# | arg: -n, --db_name= - Name of the database to grant privilegies +# +# +ynh_mongo_create_user() { + # Declare an array to define the options of this helper. + local legacy_args=unp + local -A args_array=( [u]=db_user= [n]=db_name= [p]=db_pwd= ) + local db_user + local db_name + local db_pwd + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + # Create the user and set the user as admin of the db + ynh_mongo_exec --database="$db_name" --command='db.createUser( { user: "'${db_user}'", pwd: "'${db_pwd}'", roles: [ { role: "readWrite", db: "'${db_name}'" } ] } );' + + # Add clustermonitoring rights + ynh_mongo_exec --database="$db_name" --command='db.grantRolesToUser("'${db_user}'",[{ role: "clusterMonitor", db: "admin" }]);' +} + +# Check if a mongo database exists +# +# usage: ynh_mongo_database_exists --database=database +# | arg: -d, --database= - The database for which to check existence +# | exit: Return 1 if the database doesn't exist, 0 otherwise +# +# +ynh_mongo_database_exists() { + # Declare an array to define the options of this helper. + local legacy_args=d + local -A args_array=([d]=database=) + local database + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + if [ $(ynh_mongo_exec --command='db.getMongo().getDBNames().indexOf("'${database}'")' --eval) -lt 0 ] + then + return 1 + else + return 0 + fi +} + +# Restore a database +# +# example: ynh_mongo_restore_db --database=wekan < ./dump.bson +# +# usage: ynh_mongo_restore_db --database=database +# | arg: -d, --database= - The database name to restore +# +# +ynh_mongo_restore_db() { + # Declare an array to define the options of this helper. + local legacy_args=d + local -A args_array=( [d]=database= ) + local database + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + mongorestore --quiet --db="$database" --archive +} + +# Drop a user +# +# [internal] +# +# usage: ynh_mongo_drop_user --db_user=user --db_name=name +# | arg: -u, --db_user= - The user to drop +# | arg: -n, --db_name= - Name of the database +# +# +ynh_mongo_drop_user() { + # Declare an array to define the options of this helper. + local legacy_args=un + local -A args_array=( [u]=db_user= [n]=db_name= ) + local db_user + local db_name + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + ynh_mongo_exec --database="$db_name" --command='db.dropUser("'$db_user'", {w: "majority", wtimeout: 5000})' +} + +# Create a database, an user and its password. Then store the password in the app's config +# +# usage: ynh_mongo_setup_db --db_user=user --db_name=name [--db_pwd=pwd] +# | arg: -u, --db_user= - Owner of the database +# | arg: -n, --db_name= - Name of the database +# | arg: -p, --db_pwd= - Password of the database. If not provided, a password will be generated +# +# After executing this helper, the password of the created database will be available in $db_pwd +# It will also be stored as "mongopwd" into the app settings. +# +# +ynh_mongo_setup_db() { + # Declare an array to define the options of this helper. + local legacy_args=unp + local -A args_array=( [u]=db_user= [n]=db_name= [p]=db_pwd= ) + local db_user + local db_name + db_pwd="" + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + local new_db_pwd=$(ynh_string_random) # Generate a random password + # If $db_pwd is not provided, use new_db_pwd instead for db_pwd + db_pwd="${db_pwd:-$new_db_pwd}" + + # Create the user and grant access to the database + ynh_mongo_create_user --db_user="$db_user" --db_pwd="$db_pwd" --db_name="$db_name" + + # Store the password in the app's config + ynh_app_setting_set --app=$app --key=db_pwd --value=$db_pwd +} + +# Remove a database if it exists, and the associated user +# +# usage: ynh_mongo_remove_db --db_user=user --db_name=name +# | arg: -u, --db_user= - Owner of the database +# | arg: -n, --db_name= - Name of the database +# +# +ynh_mongo_remove_db() { + # Declare an array to define the options of this helper. + local legacy_args=un + local -A args_array=( [u]=db_user= [n]=db_name= ) + local db_user + local db_name + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + if ynh_mongo_database_exists --database=$db_name; then # Check if the database exists + ynh_mongo_drop_db --database=$db_name # Remove the database + else + ynh_print_warn --message="Database $db_name not found" + fi + + # Remove mongo user if it exists + ynh_mongo_drop_user --db_user=$db_user --db_name=$db_name +} + +# Install MongoDB and integrate MongoDB service in YunoHost +# +# usage: ynh_install_mongo [--mongo_version=mongo_version] +# | arg: -m, --mongo_version= - Version of MongoDB to install +# +# +ynh_install_mongo() { + # Declare an array to define the options of this helper. + local legacy_args=m + local -A args_array=([m]=mongo_version=) + local mongo_version + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + mongo_version="${mongo_version:-$YNH_MONGO_VERSION}" + + ynh_print_info --message="Installing MongoDB Community Edition ..." + local mongo_debian_release=$(ynh_get_debian_release) + + if [[ $(cat /proc/cpuinfo) != *"avx"* && "$mongo_version" != "4.4" ]]; then + ynh_print_warn --message="Installing Mongo 4.4 as $mongo_version is not compatible with your cpu (see https://docs.mongodb.com/manual/administration/production-notes/#x86_64)." + mongo_version="4.4" + fi + if [[ "$mongo_version" == "4.4" && "$mongo_debian_release" != "buster" ]]; then + ynh_print_warn --message="Switched to buster install as Mongo 4.4 is not compatible with $mongo_debian_release." + mongo_debian_release=buster + fi + + ynh_install_extra_app_dependencies --repo="deb http://repo.mongodb.org/apt/debian $mongo_debian_release/mongodb-org/$mongo_version main" --package="mongodb-org mongodb-org-server mongodb-org-tools mongodb-mongosh" --key="https://www.mongodb.org/static/pgp/server-$mongo_version.asc" + mongodb_servicename=mongod + + # Make sure MongoDB is started and enabled + systemctl enable $mongodb_servicename --quiet + systemctl daemon-reload --quiet + ynh_systemd_action --service_name=$mongodb_servicename --action=restart --line_match="aiting for connections" --log_path="/var/log/mongodb/$mongodb_servicename.log" + + # Integrate MongoDB service in YunoHost + yunohost service add $mongodb_servicename --description="MongoDB daemon" --log="/var/log/mongodb/$mongodb_servicename.log" + + # Store mongo_version into the config of this app + ynh_app_setting_set --app=$app --key=mongo_version --value=$mongo_version +} + +# Remove MongoDB +# Only remove the MongoDB service integration in YunoHost for now +# if MongoDB package as been removed +# +# usage: ynh_remove_mongo +# +# +ynh_remove_mongo() { + # Only remove the mongodb service if it is not installed. + if ! ynh_package_is_installed --package="mongodb*" + then + ynh_print_info --message="Removing MongoDB service..." + mongodb_servicename=mongod + # Remove the mongodb service + yunohost service remove $mongodb_servicename + ynh_secure_remove --file="/var/lib/mongodb" + ynh_secure_remove --file="/var/log/mongodb" + fi +} From 6e1980279c6072345a0dd55d05f9862f6e2c1f4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?E=CC=81ric=20Gaspar?= <46165813+ericgaspar@users.noreply.github.com> Date: Wed, 15 May 2024 12:09:19 +0200 Subject: [PATCH 03/13] Add ruby --- helpers/ruby | 310 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 310 insertions(+) create mode 100644 helpers/ruby diff --git a/helpers/ruby b/helpers/ruby new file mode 100644 index 000000000..982d0b266 --- /dev/null +++ b/helpers/ruby @@ -0,0 +1,310 @@ +#!/bin/bash + +ynh_ruby_try_bash_extension() { + if [ -x src/configure ]; then + src/configure && make -C src || { + ynh_print_info --message="Optional bash extension failed to build, but things will still work normally." + } + fi +} + +rbenv_install_dir="/opt/rbenv" +ruby_version_path="$rbenv_install_dir/versions" +# RBENV_ROOT is the directory of rbenv, it needs to be loaded as a environment variable. +export RBENV_ROOT="$rbenv_install_dir" +export rbenv_root="$rbenv_install_dir" + +ruby_dependencies="" +build_ruby_dependencies="libjemalloc-dev curl build-essential libreadline-dev zlib1g-dev libsqlite3-dev libssl-dev libxml2-dev libxslt-dev autoconf automake bison libtool" +pkg_dependencies="$pkg_dependencies $ruby_dependencies" +build_pkg_dependencies="$build_pkg_dependencies $build_ruby_dependencies" + +# Load the version of Ruby for an app, and set variables. +# +# ynh_use_ruby has to be used in any app scripts before using Ruby for the first time. +# This helper will provide alias and variables to use in your scripts. +# +# To use gem or Ruby, use the alias `ynh_gem` and `ynh_ruby` +# Those alias will use the correct version installed for the app +# For example: use `ynh_gem install` instead of `gem install` +# +# With `sudo` or `ynh_exec_as`, use instead the fallback variables `$ynh_gem` and `$ynh_ruby` +# And propagate $PATH to sudo with $ynh_ruby_load_path +# Exemple: `ynh_exec_as $app $ynh_ruby_load_path $ynh_gem install` +# +# $PATH contains the path of the requested version of Ruby. +# However, $PATH is duplicated into $ruby_path to outlast any manipulation of $PATH +# You can use the variable `$ynh_ruby_load_path` to quickly load your Ruby version +# in $PATH for an usage into a separate script. +# Exemple: $ynh_ruby_load_path $final_path/script_that_use_gem.sh` +# +# +# Finally, to start a Ruby service with the correct version, 2 solutions +# Either the app is dependent of Ruby or gem, but does not called it directly. +# In such situation, you need to load PATH +# `Environment="__YNH_RUBY_LOAD_PATH__"` +# `ExecStart=__FINALPATH__/my_app` +# You will replace __YNH_RUBY_LOAD_PATH__ with $ynh_ruby_load_path +# +# Or Ruby start the app directly, then you don't need to load the PATH variable +# `ExecStart=__YNH_RUBY__ my_app run` +# You will replace __YNH_RUBY__ with $ynh_ruby +# +# +# one other variable is also available +# - $ruby_path: The absolute path to Ruby binaries for the chosen version. +# +# usage: ynh_use_ruby +# +# Requires YunoHost version 3.2.2 or higher. +ynh_use_ruby () { + ruby_version=$(ynh_app_setting_get --app=$app --key=ruby_version) + + # Get the absolute path of this version of Ruby + ruby_path="$ruby_version_path/$YNH_APP_INSTANCE_NAME/bin" + + # Allow alias to be used into bash script + shopt -s expand_aliases + + # Create an alias for the specific version of Ruby and a variable as fallback + ynh_ruby="$ruby_path/ruby" + alias ynh_ruby="$ynh_ruby" + # And gem + ynh_gem="$ruby_path/gem" + alias ynh_gem="$ynh_gem" + + # Load the path of this version of Ruby in $PATH + if [[ :$PATH: != *":$ruby_path"* ]]; then + PATH="$ruby_path:$PATH" + fi + # Create an alias to easily load the PATH + ynh_ruby_load_path="PATH=$PATH" + + # Sets the local application-specific Ruby version + pushd $final_path + $rbenv_install_dir/bin/rbenv local $ruby_version + popd +} + +# Install a specific version of Ruby +# +# ynh_install_ruby will install the version of Ruby provided as argument by using rbenv. +# +# This helper creates a /etc/profile.d/rbenv.sh that configures PATH environment for rbenv +# for every LOGIN user, hence your user must have a defined shell (as opposed to /usr/sbin/nologin) +# +# Don't forget to execute ruby-dependent command in a login environment +# (e.g. sudo --login option) +# When not possible (e.g. in systemd service definition), please use direct path +# to rbenv shims (e.g. $RBENV_ROOT/shims/bundle) +# +# usage: ynh_install_ruby --ruby_version=ruby_version +# | arg: -v, --ruby_version= - Version of ruby to install. +# +# Requires YunoHost version 3.2.2 or higher. +ynh_install_ruby () { + # Declare an array to define the options of this helper. + local legacy_args=v + local -A args_array=( [v]=ruby_version= ) + local ruby_version + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + # Load rbenv path in PATH + local CLEAR_PATH="$rbenv_install_dir/bin:$PATH" + + # Remove /usr/local/bin in PATH in case of Ruby prior installation + PATH=$(echo $CLEAR_PATH | sed 's@/usr/local/bin:@@') + + # Move an existing Ruby binary, to avoid to block rbenv + test -x /usr/bin/ruby && mv /usr/bin/ruby /usr/bin/ruby_rbenv + + # Install or update rbenv + rbenv="$(command -v rbenv $rbenv_install_dir/bin/rbenv | grep "$rbenv_install_dir/bin/rbenv" | head -1)" + if [ -n "$rbenv" ]; then + ynh_print_info --message="rbenv already seems installed in \`$rbenv'." + pushd "${rbenv%/*/*}" + if git remote -v 2>/dev/null | grep "https://github.com/rbenv/rbenv.git"; then + ynh_print_info --message="Trying to update with git..." + git pull -q --tags origin master + ynh_ruby_try_bash_extension + else + ynh_print_info --message="Reinstalling rbenv with git..." + cd .. + ynh_secure_remove --file=$rbenv_install_dir + mkdir -p $rbenv_install_dir + cd $rbenv_install_dir + git init -q + git remote add -f -t master origin https://github.com/rbenv/rbenv.git > /dev/null 2>&1 + git checkout -q -b master origin/master + ynh_ruby_try_bash_extension + rbenv=$rbenv_install_dir/bin/rbenv + fi + popd + else + ynh_print_info --message="Installing rbenv with git..." + mkdir -p $rbenv_install_dir + pushd $rbenv_install_dir + git init -q + git remote add -f -t master origin https://github.com/rbenv/rbenv.git > /dev/null 2>&1 + git checkout -q -b master origin/master + ynh_ruby_try_bash_extension + rbenv=$rbenv_install_dir/bin/rbenv + popd + fi + + ruby_build="$(command -v "$rbenv_install_dir"/plugins/*/bin/rbenv-install rbenv-install | head -1)" + if [ -n "$ruby_build" ]; then + ynh_print_info --message="\`rbenv install' command already available in \`$ruby_build'." + pushd "${ruby_build%/*/*}" + if git remote -v 2>/dev/null | grep "https://github.com/rbenv/ruby-build.git"; then + ynh_print_info --message="Trying to update rbenv with git..." + git pull -q origin master + fi + popd + else + ynh_print_info --message="Installing ruby-build with git..." + mkdir -p "${rbenv_install_dir}/plugins" + git clone -q https://github.com/rbenv/ruby-build.git "${rbenv_install_dir}/plugins/ruby-build" + fi + + rbenv_alias="$(command -v "$rbenv_install_dir"/plugins/*/bin/rbenv-alias rbenv-alias | head -1)" + if [ -n "$rbenv_alias" ]; then + ynh_print_info --message="\`rbenv alias' command already available in \`$rbenv_alias'." + pushd "${rbenv_alias%/*/*}" + if git remote -v 2>/dev/null | grep "https://github.com/tpope/rbenv-aliases.git"; then + ynh_print_info --message="Trying to update rbenv-aliases with git..." + git pull -q origin master + fi + popd + else + ynh_print_info --message="Installing rbenv-aliases with git..." + mkdir -p "${rbenv_install_dir}/plugins" + git clone -q https://github.com/tpope/rbenv-aliases.git "${rbenv_install_dir}/plugins/rbenv-aliase" + fi + + rbenv_latest="$(command -v "$rbenv_install_dir"/plugins/*/bin/rbenv-latest rbenv-latest | head -1)" + if [ -n "$rbenv_latest" ]; then + ynh_print_info --message="\`rbenv latest' command already available in \`$rbenv_latest'." + pushd "${rbenv_latest%/*/*}" + if git remote -v 2>/dev/null | grep "https://github.com/momo-lab/xxenv-latest.git"; then + ynh_print_info --message="Trying to update xxenv-latest with git..." + git pull -q origin master + fi + popd + else + ynh_print_info --message="Installing xxenv-latest with git..." + mkdir -p "${rbenv_install_dir}/plugins" + git clone -q https://github.com/momo-lab/xxenv-latest.git "${rbenv_install_dir}/plugins/xxenv-latest" + fi + + # Enable caching + mkdir -p "${rbenv_install_dir}/cache" + + # Create shims directory if needed + mkdir -p "${rbenv_install_dir}/shims" + + # Restore /usr/local/bin in PATH + PATH=$CLEAR_PATH + + # And replace the old Ruby binary + test -x /usr/bin/ruby_rbenv && mv /usr/bin/ruby_rbenv /usr/bin/ruby + + # Install the requested version of Ruby + local final_ruby_version=$(rbenv latest --print $ruby_version) + if ! [ -n "$final_ruby_version" ]; then + final_ruby_version=$ruby_version + fi + ynh_print_info --message="Installing Ruby-$final_ruby_version" + CONFIGURE_OPTS="--disable-install-doc --with-jemalloc" MAKE_OPTS="-j2" rbenv install --skip-existing $final_ruby_version > /dev/null 2>&1 + + # Store ruby_version into the config of this app + ynh_app_setting_set --app=$YNH_APP_INSTANCE_NAME --key=ruby_version --value=$final_ruby_version + + # Remove app virtualenv + if `rbenv alias --list | grep --quiet "$YNH_APP_INSTANCE_NAME " 1>/dev/null 2>&1` + then + rbenv alias $YNH_APP_INSTANCE_NAME --remove + fi + + # Create app virtualenv + rbenv alias $YNH_APP_INSTANCE_NAME $final_ruby_version + + # Cleanup Ruby versions + ynh_cleanup_ruby + + # Set environment for Ruby users + echo "#rbenv +export RBENV_ROOT=$rbenv_install_dir +export PATH=\"$rbenv_install_dir/bin:$PATH\" +eval \"\$(rbenv init -)\" +#rbenv" > /etc/profile.d/rbenv.sh + + # Load the environment + eval "$(rbenv init -)" +} + +# Remove the version of Ruby used by the app. +# +# This helper will also cleanup Ruby versions +# +# usage: ynh_remove_ruby +ynh_remove_ruby () { + local ruby_version=$(ynh_app_setting_get --app=$YNH_APP_INSTANCE_NAME --key=ruby_version) + + # Load rbenv path in PATH + local CLEAR_PATH="$rbenv_install_dir/bin:$PATH" + + # Remove /usr/local/bin in PATH in case of Ruby prior installation + PATH=$(echo $CLEAR_PATH | sed 's@/usr/local/bin:@@') + + rbenv alias $YNH_APP_INSTANCE_NAME --remove + + # Remove the line for this app + ynh_app_setting_delete --app=$YNH_APP_INSTANCE_NAME --key=ruby_version + + # Cleanup Ruby versions + ynh_cleanup_ruby +} + +# Remove no more needed versions of Ruby used by the app. +# +# This helper will check what Ruby version are no more required, +# and uninstall them +# If no app uses Ruby, rbenv will be also removed. +# +# usage: ynh_cleanup_ruby +ynh_cleanup_ruby () { + + # List required Ruby versions + local installed_apps=$(yunohost app list | grep -oP 'id: \K.*$') + local required_ruby_versions="" + for installed_app in $installed_apps + do + local installed_app_ruby_version=$(ynh_app_setting_get --app=$installed_app --key="ruby_version") + if [[ $installed_app_ruby_version ]] + then + required_ruby_versions="${installed_app_ruby_version}\n${required_ruby_versions}" + fi + done + + # Remove no more needed Ruby versions + local installed_ruby_versions=$(rbenv versions --bare --skip-aliases | grep -Ev '/') + for installed_ruby_version in $installed_ruby_versions + do + if ! `echo ${required_ruby_versions} | grep "${installed_ruby_version}" 1>/dev/null 2>&1` + then + ynh_print_info --message="Removing of Ruby-$installed_ruby_version" + $rbenv_install_dir/bin/rbenv uninstall --force $installed_ruby_version + fi + done + + # If none Ruby version is required + if [[ ! $required_ruby_versions ]] + then + # Remove rbenv environment configuration + ynh_print_info --message="Removing of rbenv-$rbenv_version" + ynh_secure_remove --file="$rbenv_install_dir" + ynh_secure_remove --file="/etc/profile.d/rbenv.sh" + fi +} From 1a24d32e98bcbc0a76e035c41b66edea8fa4a76d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?E=CC=81ric=20Gaspar?= <46165813+ericgaspar@users.noreply.github.com> Date: Wed, 15 May 2024 12:15:39 +0200 Subject: [PATCH 04/13] Update mongodb --- helpers/mongodb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helpers/mongodb b/helpers/mongodb index e0c9277ce..f374a9e01 100644 --- a/helpers/mongodb +++ b/helpers/mongodb @@ -1,6 +1,6 @@ #!/bin/bash -readonly YNH_DEFAULT_MONGO_VERSION=4.4 +readonly YNH_DEFAULT_MONGO_VERSION=5.0 # Declare the actual MongoDB version to use: 4.4 ; 5.0 ; 6.0 # A packager willing to use another version of MongoDB can override the variable into its _common.sh. YNH_MONGO_VERSION=${YNH_MONGO_VERSION:-$YNH_DEFAULT_MONGO_VERSION} From cc3c5fc4f02fcdf5e466fec5078c60f14bbc28d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?E=CC=81ric=20Gaspar?= <46165813+ericgaspar@users.noreply.github.com> Date: Wed, 15 May 2024 12:18:13 +0200 Subject: [PATCH 05/13] Update go --- helpers/go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helpers/go b/helpers/go index fd7117414..0e18301f7 100644 --- a/helpers/go +++ b/helpers/go @@ -30,7 +30,7 @@ export GOENV_ROOT="$goenv_install_dir" # However, $PATH is duplicated into $go_path to outlast any manipulation of $PATH # You can use the variable `$ynh_go_load_path` to quickly load your Go version # in $PATH for an usage into a separate script. -# Exemple: $ynh_go_load_path $install_dir/script_that_use_gem.sh` +# Exemple: `$ynh_go_load_path $install_dir/script_that_use_gem.sh` # # # Finally, to start a Go service with the correct version, 2 solutions From 76c54ebdea8069297eaa024968daaa2005d0b4c6 Mon Sep 17 00:00:00 2001 From: eric_G <46165813+ericgaspar@users.noreply.github.com> Date: Sat, 18 May 2024 16:33:30 +0200 Subject: [PATCH 06/13] Update helpers/ruby Co-authored-by: Alexandre Aubin <4533074+alexAubin@users.noreply.github.com> --- helpers/ruby | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helpers/ruby b/helpers/ruby index 982d0b266..e0e388e22 100644 --- a/helpers/ruby +++ b/helpers/ruby @@ -216,7 +216,7 @@ ynh_install_ruby () { final_ruby_version=$ruby_version fi ynh_print_info --message="Installing Ruby-$final_ruby_version" - CONFIGURE_OPTS="--disable-install-doc --with-jemalloc" MAKE_OPTS="-j2" rbenv install --skip-existing $final_ruby_version > /dev/null 2>&1 + RUBY_CONFIGURE_OPTS="--disable-install-doc --with-jemalloc" MAKE_OPTS="-j2" rbenv install --skip-existing $final_ruby_version > /dev/null 2>&1 # Store ruby_version into the config of this app ynh_app_setting_set --app=$YNH_APP_INSTANCE_NAME --key=ruby_version --value=$final_ruby_version From 89e24eb258b04c89bdc8c3e256572ac26e9103ac Mon Sep 17 00:00:00 2001 From: eric_G <46165813+ericgaspar@users.noreply.github.com> Date: Sat, 18 May 2024 16:33:41 +0200 Subject: [PATCH 07/13] Update helpers/ruby Co-authored-by: Alexandre Aubin <4533074+alexAubin@users.noreply.github.com> --- helpers/ruby | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helpers/ruby b/helpers/ruby index e0e388e22..625ded52e 100644 --- a/helpers/ruby +++ b/helpers/ruby @@ -81,7 +81,7 @@ ynh_use_ruby () { ynh_ruby_load_path="PATH=$PATH" # Sets the local application-specific Ruby version - pushd $final_path + pushd ${install_dir:-$final_path} $rbenv_install_dir/bin/rbenv local $ruby_version popd } From 7f52988671ac41b9f51aad66e724f0410504ff95 Mon Sep 17 00:00:00 2001 From: eric_G <46165813+ericgaspar@users.noreply.github.com> Date: Sat, 18 May 2024 16:35:37 +0200 Subject: [PATCH 08/13] Update helpers/mongodb Co-authored-by: Alexandre Aubin <4533074+alexAubin@users.noreply.github.com> --- helpers/mongodb | 5 ----- 1 file changed, 5 deletions(-) diff --git a/helpers/mongodb b/helpers/mongodb index f374a9e01..f441c7e16 100644 --- a/helpers/mongodb +++ b/helpers/mongodb @@ -1,10 +1,5 @@ #!/bin/bash -readonly YNH_DEFAULT_MONGO_VERSION=5.0 -# Declare the actual MongoDB version to use: 4.4 ; 5.0 ; 6.0 -# A packager willing to use another version of MongoDB can override the variable into its _common.sh. -YNH_MONGO_VERSION=${YNH_MONGO_VERSION:-$YNH_DEFAULT_MONGO_VERSION} - # Execute a mongo command # # example: ynh_mongo_exec --command='db.getMongo().getDBNames().indexOf("wekan")' From 44840603b5005414809b423f961fcf375aa9eec6 Mon Sep 17 00:00:00 2001 From: eric_G <46165813+ericgaspar@users.noreply.github.com> Date: Sat, 18 May 2024 16:36:05 +0200 Subject: [PATCH 09/13] Update helpers/mongodb Co-authored-by: Alexandre Aubin <4533074+alexAubin@users.noreply.github.com> --- helpers/mongodb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helpers/mongodb b/helpers/mongodb index f441c7e16..d40d11bfe 100644 --- a/helpers/mongodb +++ b/helpers/mongodb @@ -314,7 +314,7 @@ ynh_install_mongo() { ynh_print_warn --message="Installing Mongo 4.4 as $mongo_version is not compatible with your cpu (see https://docs.mongodb.com/manual/administration/production-notes/#x86_64)." mongo_version="4.4" fi - if [[ "$mongo_version" == "4.4" && "$mongo_debian_release" != "buster" ]]; then + if [[ "$mongo_version" == "4.4" ]]; then ynh_print_warn --message="Switched to buster install as Mongo 4.4 is not compatible with $mongo_debian_release." mongo_debian_release=buster fi From f798236a3a80585d7b1003d29dad4812d86d1e9e Mon Sep 17 00:00:00 2001 From: eric_G <46165813+ericgaspar@users.noreply.github.com> Date: Sat, 18 May 2024 17:17:01 +0200 Subject: [PATCH 10/13] Update helpers/redis Co-authored-by: Alexandre Aubin <4533074+alexAubin@users.noreply.github.com> --- helpers/redis | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helpers/redis b/helpers/redis index 9d6257db3..545bb8705 100644 --- a/helpers/redis +++ b/helpers/redis @@ -35,5 +35,5 @@ ynh_redis_get_free_db() { # | arg: database - the database to erase ynh_redis_remove_db() { local db=$1 - redis-cli -n "$db" flushall + redis-cli -n "$db" flushdb } From 30046784a0b89aab3ccfc9730cbec1e07379bd02 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin <4533074+alexAubin@users.noreply.github.com> Date: Sun, 19 May 2024 15:20:33 +0200 Subject: [PATCH 11/13] Update ruby: simplify messages, fix bash mess --- helpers/ruby | 60 ++++++++++++++++++++++++---------------------------- 1 file changed, 28 insertions(+), 32 deletions(-) diff --git a/helpers/ruby b/helpers/ruby index 625ded52e..4bfa3f063 100644 --- a/helpers/ruby +++ b/helpers/ruby @@ -1,15 +1,8 @@ #!/bin/bash -ynh_ruby_try_bash_extension() { - if [ -x src/configure ]; then - src/configure && make -C src || { - ynh_print_info --message="Optional bash extension failed to build, but things will still work normally." - } - fi -} - rbenv_install_dir="/opt/rbenv" ruby_version_path="$rbenv_install_dir/versions" + # RBENV_ROOT is the directory of rbenv, it needs to be loaded as a environment variable. export RBENV_ROOT="$rbenv_install_dir" export rbenv_root="$rbenv_install_dir" @@ -120,16 +113,16 @@ ynh_install_ruby () { test -x /usr/bin/ruby && mv /usr/bin/ruby /usr/bin/ruby_rbenv # Install or update rbenv + mkdir -p $rbenv_install_dir rbenv="$(command -v rbenv $rbenv_install_dir/bin/rbenv | grep "$rbenv_install_dir/bin/rbenv" | head -1)" if [ -n "$rbenv" ]; then - ynh_print_info --message="rbenv already seems installed in \`$rbenv'." pushd "${rbenv%/*/*}" if git remote -v 2>/dev/null | grep "https://github.com/rbenv/rbenv.git"; then - ynh_print_info --message="Trying to update with git..." + ynh_print_info --message="Updating rbenv..." git pull -q --tags origin master ynh_ruby_try_bash_extension else - ynh_print_info --message="Reinstalling rbenv with git..." + ynh_print_info --message="Reinstalling rbenv..." cd .. ynh_secure_remove --file=$rbenv_install_dir mkdir -p $rbenv_install_dir @@ -142,8 +135,7 @@ ynh_install_ruby () { fi popd else - ynh_print_info --message="Installing rbenv with git..." - mkdir -p $rbenv_install_dir + ynh_print_info --message="Installing rbenv..." pushd $rbenv_install_dir git init -q git remote add -f -t master origin https://github.com/rbenv/rbenv.git > /dev/null 2>&1 @@ -153,48 +145,44 @@ ynh_install_ruby () { popd fi + mkdir -p "${rbenv_install_dir}/plugins" + ruby_build="$(command -v "$rbenv_install_dir"/plugins/*/bin/rbenv-install rbenv-install | head -1)" if [ -n "$ruby_build" ]; then - ynh_print_info --message="\`rbenv install' command already available in \`$ruby_build'." pushd "${ruby_build%/*/*}" if git remote -v 2>/dev/null | grep "https://github.com/rbenv/ruby-build.git"; then - ynh_print_info --message="Trying to update rbenv with git..." + ynh_print_info --message="Updating ruby-build..." git pull -q origin master fi popd else - ynh_print_info --message="Installing ruby-build with git..." - mkdir -p "${rbenv_install_dir}/plugins" + ynh_print_info --message="Installing ruby-build..." git clone -q https://github.com/rbenv/ruby-build.git "${rbenv_install_dir}/plugins/ruby-build" fi rbenv_alias="$(command -v "$rbenv_install_dir"/plugins/*/bin/rbenv-alias rbenv-alias | head -1)" if [ -n "$rbenv_alias" ]; then - ynh_print_info --message="\`rbenv alias' command already available in \`$rbenv_alias'." pushd "${rbenv_alias%/*/*}" if git remote -v 2>/dev/null | grep "https://github.com/tpope/rbenv-aliases.git"; then - ynh_print_info --message="Trying to update rbenv-aliases with git..." + ynh_print_info --message="Updating rbenv-aliases..." git pull -q origin master fi popd else - ynh_print_info --message="Installing rbenv-aliases with git..." - mkdir -p "${rbenv_install_dir}/plugins" + ynh_print_info --message="Installing rbenv-aliases..." git clone -q https://github.com/tpope/rbenv-aliases.git "${rbenv_install_dir}/plugins/rbenv-aliase" fi rbenv_latest="$(command -v "$rbenv_install_dir"/plugins/*/bin/rbenv-latest rbenv-latest | head -1)" if [ -n "$rbenv_latest" ]; then - ynh_print_info --message="\`rbenv latest' command already available in \`$rbenv_latest'." pushd "${rbenv_latest%/*/*}" if git remote -v 2>/dev/null | grep "https://github.com/momo-lab/xxenv-latest.git"; then - ynh_print_info --message="Trying to update xxenv-latest with git..." + ynh_print_info --message="Updating xxenv-latest..." git pull -q origin master fi popd else - ynh_print_info --message="Installing xxenv-latest with git..." - mkdir -p "${rbenv_install_dir}/plugins" + ynh_print_info --message="Installing xxenv-latest..." git clone -q https://github.com/momo-lab/xxenv-latest.git "${rbenv_install_dir}/plugins/xxenv-latest" fi @@ -215,14 +203,14 @@ ynh_install_ruby () { if ! [ -n "$final_ruby_version" ]; then final_ruby_version=$ruby_version fi - ynh_print_info --message="Installing Ruby-$final_ruby_version" + ynh_print_info --message="Installing Ruby $final_ruby_version" RUBY_CONFIGURE_OPTS="--disable-install-doc --with-jemalloc" MAKE_OPTS="-j2" rbenv install --skip-existing $final_ruby_version > /dev/null 2>&1 # Store ruby_version into the config of this app ynh_app_setting_set --app=$YNH_APP_INSTANCE_NAME --key=ruby_version --value=$final_ruby_version # Remove app virtualenv - if `rbenv alias --list | grep --quiet "$YNH_APP_INSTANCE_NAME " 1>/dev/null 2>&1` + if rbenv alias --list | grep --quiet "$YNH_APP_INSTANCE_NAME " then rbenv alias $YNH_APP_INSTANCE_NAME --remove fi @@ -282,7 +270,7 @@ ynh_cleanup_ruby () { for installed_app in $installed_apps do local installed_app_ruby_version=$(ynh_app_setting_get --app=$installed_app --key="ruby_version") - if [[ $installed_app_ruby_version ]] + if [[ -n "$installed_app_ruby_version" ]] then required_ruby_versions="${installed_app_ruby_version}\n${required_ruby_versions}" fi @@ -292,19 +280,27 @@ ynh_cleanup_ruby () { local installed_ruby_versions=$(rbenv versions --bare --skip-aliases | grep -Ev '/') for installed_ruby_version in $installed_ruby_versions do - if ! `echo ${required_ruby_versions} | grep "${installed_ruby_version}" 1>/dev/null 2>&1` + if ! echo ${required_ruby_versions} | grep -q "${installed_ruby_version}" then - ynh_print_info --message="Removing of Ruby-$installed_ruby_version" + ynh_print_info --message="Removing Ruby-$installed_ruby_version" $rbenv_install_dir/bin/rbenv uninstall --force $installed_ruby_version fi done # If none Ruby version is required - if [[ ! $required_ruby_versions ]] + if [[ -z "$required_ruby_versions" ]] then # Remove rbenv environment configuration - ynh_print_info --message="Removing of rbenv-$rbenv_version" + ynh_print_info --message="Removing rbenv" ynh_secure_remove --file="$rbenv_install_dir" ynh_secure_remove --file="/etc/profile.d/rbenv.sh" fi } + +ynh_ruby_try_bash_extension() { + if [ -x src/configure ]; then + src/configure && make -C src || { + ynh_print_info --message="Optional bash extension failed to build, but things will still work normally." + } + fi +} From ad6c75652c75d908e6af3edad8094c5c48495657 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin <4533074+alexAubin@users.noreply.github.com> Date: Sun, 19 May 2024 15:21:05 +0200 Subject: [PATCH 12/13] Update helpers/ruby --- helpers/ruby | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/helpers/ruby b/helpers/ruby index 4bfa3f063..cc7c73f90 100644 --- a/helpers/ruby +++ b/helpers/ruby @@ -7,10 +7,10 @@ ruby_version_path="$rbenv_install_dir/versions" export RBENV_ROOT="$rbenv_install_dir" export rbenv_root="$rbenv_install_dir" -ruby_dependencies="" -build_ruby_dependencies="libjemalloc-dev curl build-essential libreadline-dev zlib1g-dev libsqlite3-dev libssl-dev libxml2-dev libxslt-dev autoconf automake bison libtool" -pkg_dependencies="$pkg_dependencies $ruby_dependencies" -build_pkg_dependencies="$build_pkg_dependencies $build_ruby_dependencies" +if dpkg --compare-versions ${YNH_APP_PACKAGING_FORMAT:-0} lt 2; then + build_ruby_dependencies="libjemalloc-dev curl build-essential libreadline-dev zlib1g-dev libsqlite3-dev libssl-dev libxml2-dev libxslt-dev autoconf automake bison libtool" + build_pkg_dependencies="$build_pkg_dependencies $build_ruby_dependencies" +fi # Load the version of Ruby for an app, and set variables. # From 9b7b265cbf4dd4e8e11a90b64f828dc1a4d09af4 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin <4533074+alexAubin@users.noreply.github.com> Date: Sun, 19 May 2024 15:50:18 +0200 Subject: [PATCH 13/13] Delete helpers/mongodb, to be readded in another PR --- helpers/mongodb | 355 ------------------------------------------------ 1 file changed, 355 deletions(-) delete mode 100644 helpers/mongodb diff --git a/helpers/mongodb b/helpers/mongodb deleted file mode 100644 index d40d11bfe..000000000 --- a/helpers/mongodb +++ /dev/null @@ -1,355 +0,0 @@ -#!/bin/bash - -# Execute a mongo command -# -# example: ynh_mongo_exec --command='db.getMongo().getDBNames().indexOf("wekan")' -# example: ynh_mongo_exec --command="db.getMongo().getDBNames().indexOf(\"wekan\")" -# -# usage: ynh_mongo_exec [--user=user] [--password=password] [--authenticationdatabase=authenticationdatabase] [--database=database] [--host=host] [--port=port] --command="command" [--eval] -# | arg: -u, --user= - The user name to connect as -# | arg: -p, --password= - The user password -# | arg: -d, --authenticationdatabase= - The authenticationdatabase to connect to -# | arg: -d, --database= - The database to connect to -# | arg: -h, --host= - The host to connect to -# | arg: -P, --port= - The port to connect to -# | arg: -c, --command= - The command to evaluate -# | arg: -e, --eval - Evaluate instead of execute the command. -# -# -ynh_mongo_exec() { - # Declare an array to define the options of this helper. - local legacy_args=upadhPce - local -A args_array=( [u]=user= [p]=password= [a]=authenticationdatabase= [d]=database= [h]=host= [P]=port= [c]=command= [e]=eval ) - local user - local password - local authenticationdatabase - local database - local host - local port - local command - local eval - # Manage arguments with getopts - ynh_handle_getopts_args "$@" - user="${user:-}" - password="${password:-}" - authenticationdatabase="${authenticationdatabase:-}" - database="${database:-}" - host="${host:-}" - port="${port:-}" - eval=${eval:-0} - - # If user is provided - if [ -n "$user" ] - then - user="--username=$user" - - # If password is provided - if [ -n "$password" ] - then - password="--password=$password" - fi - - # If authenticationdatabase is provided - if [ -n "$authenticationdatabase" ] - then - authenticationdatabase="--authenticationDatabase=$authenticationdatabase" - else - authenticationdatabase="--authenticationDatabase=admin" - fi - else - password="" - authenticationdatabase="" - fi - - # If host is provided - if [ -n "$host" ] - then - host="--host=$host" - fi - - # If port is provided - if [ -n "$port" ] - then - port="--port=$port" - fi - - # If eval is not provided - if [ $eval -eq 0 ] - then - # If database is provided - if [ -n "$database" ] - then - database="use $database" - else - database="" - fi - - mongosh --quiet --username $user --password $password --authenticationDatabase $authenticationdatabase --host $host --port $port < ./dump.bson -# -# usage: ynh_mongo_dump_db --database=database -# | arg: -d, --database= - The database name to dump -# | ret: the mongodump output -# -# -ynh_mongo_dump_db() { - # Declare an array to define the options of this helper. - local legacy_args=d - local -A args_array=( [d]=database= ) - local database - # Manage arguments with getopts - ynh_handle_getopts_args "$@" - - mongodump --quiet --db="$database" --archive -} - -# Create a user -# -# [internal] -# -# usage: ynh_mongo_create_user --db_user=user --db_pwd=pwd --db_name=name -# | arg: -u, --db_user= - The user name to create -# | arg: -p, --db_pwd= - The password to identify user by -# | arg: -n, --db_name= - Name of the database to grant privilegies -# -# -ynh_mongo_create_user() { - # Declare an array to define the options of this helper. - local legacy_args=unp - local -A args_array=( [u]=db_user= [n]=db_name= [p]=db_pwd= ) - local db_user - local db_name - local db_pwd - # Manage arguments with getopts - ynh_handle_getopts_args "$@" - - # Create the user and set the user as admin of the db - ynh_mongo_exec --database="$db_name" --command='db.createUser( { user: "'${db_user}'", pwd: "'${db_pwd}'", roles: [ { role: "readWrite", db: "'${db_name}'" } ] } );' - - # Add clustermonitoring rights - ynh_mongo_exec --database="$db_name" --command='db.grantRolesToUser("'${db_user}'",[{ role: "clusterMonitor", db: "admin" }]);' -} - -# Check if a mongo database exists -# -# usage: ynh_mongo_database_exists --database=database -# | arg: -d, --database= - The database for which to check existence -# | exit: Return 1 if the database doesn't exist, 0 otherwise -# -# -ynh_mongo_database_exists() { - # Declare an array to define the options of this helper. - local legacy_args=d - local -A args_array=([d]=database=) - local database - # Manage arguments with getopts - ynh_handle_getopts_args "$@" - - if [ $(ynh_mongo_exec --command='db.getMongo().getDBNames().indexOf("'${database}'")' --eval) -lt 0 ] - then - return 1 - else - return 0 - fi -} - -# Restore a database -# -# example: ynh_mongo_restore_db --database=wekan < ./dump.bson -# -# usage: ynh_mongo_restore_db --database=database -# | arg: -d, --database= - The database name to restore -# -# -ynh_mongo_restore_db() { - # Declare an array to define the options of this helper. - local legacy_args=d - local -A args_array=( [d]=database= ) - local database - # Manage arguments with getopts - ynh_handle_getopts_args "$@" - - mongorestore --quiet --db="$database" --archive -} - -# Drop a user -# -# [internal] -# -# usage: ynh_mongo_drop_user --db_user=user --db_name=name -# | arg: -u, --db_user= - The user to drop -# | arg: -n, --db_name= - Name of the database -# -# -ynh_mongo_drop_user() { - # Declare an array to define the options of this helper. - local legacy_args=un - local -A args_array=( [u]=db_user= [n]=db_name= ) - local db_user - local db_name - # Manage arguments with getopts - ynh_handle_getopts_args "$@" - - ynh_mongo_exec --database="$db_name" --command='db.dropUser("'$db_user'", {w: "majority", wtimeout: 5000})' -} - -# Create a database, an user and its password. Then store the password in the app's config -# -# usage: ynh_mongo_setup_db --db_user=user --db_name=name [--db_pwd=pwd] -# | arg: -u, --db_user= - Owner of the database -# | arg: -n, --db_name= - Name of the database -# | arg: -p, --db_pwd= - Password of the database. If not provided, a password will be generated -# -# After executing this helper, the password of the created database will be available in $db_pwd -# It will also be stored as "mongopwd" into the app settings. -# -# -ynh_mongo_setup_db() { - # Declare an array to define the options of this helper. - local legacy_args=unp - local -A args_array=( [u]=db_user= [n]=db_name= [p]=db_pwd= ) - local db_user - local db_name - db_pwd="" - # Manage arguments with getopts - ynh_handle_getopts_args "$@" - - local new_db_pwd=$(ynh_string_random) # Generate a random password - # If $db_pwd is not provided, use new_db_pwd instead for db_pwd - db_pwd="${db_pwd:-$new_db_pwd}" - - # Create the user and grant access to the database - ynh_mongo_create_user --db_user="$db_user" --db_pwd="$db_pwd" --db_name="$db_name" - - # Store the password in the app's config - ynh_app_setting_set --app=$app --key=db_pwd --value=$db_pwd -} - -# Remove a database if it exists, and the associated user -# -# usage: ynh_mongo_remove_db --db_user=user --db_name=name -# | arg: -u, --db_user= - Owner of the database -# | arg: -n, --db_name= - Name of the database -# -# -ynh_mongo_remove_db() { - # Declare an array to define the options of this helper. - local legacy_args=un - local -A args_array=( [u]=db_user= [n]=db_name= ) - local db_user - local db_name - # Manage arguments with getopts - ynh_handle_getopts_args "$@" - - if ynh_mongo_database_exists --database=$db_name; then # Check if the database exists - ynh_mongo_drop_db --database=$db_name # Remove the database - else - ynh_print_warn --message="Database $db_name not found" - fi - - # Remove mongo user if it exists - ynh_mongo_drop_user --db_user=$db_user --db_name=$db_name -} - -# Install MongoDB and integrate MongoDB service in YunoHost -# -# usage: ynh_install_mongo [--mongo_version=mongo_version] -# | arg: -m, --mongo_version= - Version of MongoDB to install -# -# -ynh_install_mongo() { - # Declare an array to define the options of this helper. - local legacy_args=m - local -A args_array=([m]=mongo_version=) - local mongo_version - # Manage arguments with getopts - ynh_handle_getopts_args "$@" - mongo_version="${mongo_version:-$YNH_MONGO_VERSION}" - - ynh_print_info --message="Installing MongoDB Community Edition ..." - local mongo_debian_release=$(ynh_get_debian_release) - - if [[ $(cat /proc/cpuinfo) != *"avx"* && "$mongo_version" != "4.4" ]]; then - ynh_print_warn --message="Installing Mongo 4.4 as $mongo_version is not compatible with your cpu (see https://docs.mongodb.com/manual/administration/production-notes/#x86_64)." - mongo_version="4.4" - fi - if [[ "$mongo_version" == "4.4" ]]; then - ynh_print_warn --message="Switched to buster install as Mongo 4.4 is not compatible with $mongo_debian_release." - mongo_debian_release=buster - fi - - ynh_install_extra_app_dependencies --repo="deb http://repo.mongodb.org/apt/debian $mongo_debian_release/mongodb-org/$mongo_version main" --package="mongodb-org mongodb-org-server mongodb-org-tools mongodb-mongosh" --key="https://www.mongodb.org/static/pgp/server-$mongo_version.asc" - mongodb_servicename=mongod - - # Make sure MongoDB is started and enabled - systemctl enable $mongodb_servicename --quiet - systemctl daemon-reload --quiet - ynh_systemd_action --service_name=$mongodb_servicename --action=restart --line_match="aiting for connections" --log_path="/var/log/mongodb/$mongodb_servicename.log" - - # Integrate MongoDB service in YunoHost - yunohost service add $mongodb_servicename --description="MongoDB daemon" --log="/var/log/mongodb/$mongodb_servicename.log" - - # Store mongo_version into the config of this app - ynh_app_setting_set --app=$app --key=mongo_version --value=$mongo_version -} - -# Remove MongoDB -# Only remove the MongoDB service integration in YunoHost for now -# if MongoDB package as been removed -# -# usage: ynh_remove_mongo -# -# -ynh_remove_mongo() { - # Only remove the mongodb service if it is not installed. - if ! ynh_package_is_installed --package="mongodb*" - then - ynh_print_info --message="Removing MongoDB service..." - mongodb_servicename=mongod - # Remove the mongodb service - yunohost service remove $mongodb_servicename - ynh_secure_remove --file="/var/lib/mongodb" - ynh_secure_remove --file="/var/log/mongodb" - fi -}