From 22fd814193ac30d761b78129c637c349872af8ec Mon Sep 17 00:00:00 2001 From: Patrick Gansterer Date: Fri, 27 May 2022 18:35:00 +0200 Subject: [PATCH] Add a script to read all databases with a prefix from the database server When passing a value via the FOODSOFT_DB_PREFIX environment variable the FOODSOFT_FOODCOOPS environment variable will be overwritten with a list of databases starting with the given prefix. This can be used to dynamically generate the app_config.yml in a multicoop installation and avoid listing all instances manually. --- docker-entrypoint.sh | 4 ++++ script/list_databases | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100755 script/list_databases diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index cb35ace0..f6181785 100755 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -6,6 +6,10 @@ if [ -f tmp/pids/server.pid ]; then rm tmp/pids/server.pid fi +if [ ! -z "${FOODSOFT_DB_PREFIX}" ] || [ ! -z "${FOODSOFT_DB_PREFIX_FILE}" ]; then + FOODSOFT_FOODCOOPS=`BUNDLE_CONFIG=/dev/null bundle exec ruby script/list_databases` +fi + if [ -e app_config.defaults.yml ] ; then cat app_config.defaults.yml > config/app_config.yml diff --git a/script/list_databases b/script/list_databases new file mode 100755 index 00000000..86d07ef1 --- /dev/null +++ b/script/list_databases @@ -0,0 +1,22 @@ +require 'mysql2' + +def get_env_or_file_content(name) + ENV.fetch('FOODSOFT_DB_' + name) do |full_name| + file_name = ENV.fetch(full_name + '_FILE', nil) + file_name ? File.read(file_name).strip : '' + end +end + +prefix = get_env_or_file_content('PREFIX') +host = get_env_or_file_content('HOST') +database = get_env_or_file_content('NAME') +username = get_env_or_file_content('USER') +password = get_env_or_file_content('PASSWORD') + +client = Mysql2::Client.new(host: host, database: database, username: username, password: password) +escaped_prefix = client.escape(prefix) +results = client.query("SELECT schema_name FROM information_schema.schemata WHERE schema_name LIKE '#{escaped_prefix}%'") +results.each do |row| + puts row["schema_name"][prefix.length..] +end +client.close