Skip to content

Commit

Permalink
Move ENV_FILE usage to config file
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoColomb committed Feb 22, 2021
1 parent 4d53a3e commit 3b766d4
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 148 deletions.
25 changes: 18 additions & 7 deletions apache/config-docker.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,44 @@
* Edit this file with your own settings and save it as "config.php"
*/

// a helper function to lookup "env_FILE", "env", then fallback
function getenv_docker(string $name, ?string $default = null): ?string {
if ($fileEnv = getenv($name . '_FILE')) {
return trim(file_get_contents($fileEnv));
}
if ($value = getenv($name)) {
return $value;
}
return $default;
}

/*
** MySQL settings - You can get this info from your web host
*/

/** MySQL database username */
define( 'YOURLS_DB_USER', getenv('YOURLS_DB_USER') ?: 'root' );
define( 'YOURLS_DB_USER', getenv_docker('YOURLS_DB_USER', 'root') );

/** MySQL database password */
define( 'YOURLS_DB_PASS', getenv('YOURLS_DB_PASS') );
define( 'YOURLS_DB_PASS', getenv_docker('YOURLS_DB_PASS') );

/** The name of the database for YOURLS */
define( 'YOURLS_DB_NAME', getenv('YOURLS_DB_NAME') ?: 'yourls' );
define( 'YOURLS_DB_NAME', getenv_docker('YOURLS_DB_NAME', 'yourls') );

/** MySQL hostname.
** If using a non standard port, specify it like 'hostname:port', eg. 'localhost:9999' or '127.0.0.1:666' */
define( 'YOURLS_DB_HOST', getenv('YOURLS_DB_HOST') ?: 'mysql' );
define( 'YOURLS_DB_HOST', getenv_docker('YOURLS_DB_HOST', 'mysql') );

/** MySQL tables prefix */
define( 'YOURLS_DB_PREFIX', getenv('YOURLS_DB_PREFIX') ?: 'yourls_' );
define( 'YOURLS_DB_PREFIX', getenv_docker('YOURLS_DB_PREFIX', 'yourls_') );

/*
** Site options
*/

/** YOURLS installation URL -- all lowercase, no trailing slash at the end.
** If you define it to "http://sho.rt", don't use "http://www.sho.rt" in your browser (and vice-versa) */
define( 'YOURLS_SITE', getenv('YOURLS_SITE') ?: 'http://your-own-domain-here.com' );
define( 'YOURLS_SITE', getenv_docker('YOURLS_SITE', 'http://your-own-domain-here.com') );

/** Server timezone GMT offset */
define( 'YOURLS_HOURS_OFFSET', filter_var(getenv('YOURLS_HOURS_OFFSET'), FILTER_VALIDATE_INT) ?: 0 );
Expand Down Expand Up @@ -57,7 +68,7 @@
** YOURLS will auto encrypt plain text passwords in this file
** Read http://yourls.org/userpassword for more information */
$yourls_user_passwords = [
getenv('YOURLS_USER') => getenv('YOURLS_PASS'),
getenv_docker('YOURLS_USER') => getenv_docker('YOURLS_PASS'),
];

/** Debug mode to output some internal information
Expand Down
30 changes: 0 additions & 30 deletions apache/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,36 +1,6 @@
#!/bin/bash
set -euo pipefail

# usage: file_env VAR [DEFAULT]
# ie: file_env 'XYZ_DB_PASSWORD' 'example'
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
file_env() {
local var="$1"
local fileVar="${var}_FILE"
local def="${2:-}"
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
printf 'Both %s and %s are set (but are exclusive)' "$var" "$fileVar"
fi
local val="$def"
if [ "${!var:-}" ]; then
val="${!var}"
elif [ "${!fileVar:-}" ]; then
val="$(< "${!fileVar}")"
fi
export "$var"="$val"
unset "$fileVar"
}

file_env 'YOURLS_DB_HOST'
file_env 'YOURLS_DB_USER'
file_env 'YOURLS_DB_PASS'
file_env 'YOURLS_DB_NAME'
file_env 'YOURLS_DB_PREFIX'
file_env 'YOURLS_SITE'
file_env 'YOURLS_USER'
file_env 'YOURLS_PASS'

if [ ! -e /var/www/html/yourls-loader.php ]; then
tar cf - --one-file-system -C /usr/src/yourls . | tar xf -
chown -R www-data:www-data /var/www/html
Expand Down
25 changes: 18 additions & 7 deletions config-docker.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,44 @@
* Edit this file with your own settings and save it as "config.php"
*/

// a helper function to lookup "env_FILE", "env", then fallback
function getenv_docker(string $name, ?string $default = null): ?string {
if ($fileEnv = getenv($name . '_FILE')) {
return trim(file_get_contents($fileEnv));
}
if ($value = getenv($name)) {
return $value;
}
return $default;
}

/*
** MySQL settings - You can get this info from your web host
*/

/** MySQL database username */
define( 'YOURLS_DB_USER', getenv('YOURLS_DB_USER') ?: 'root' );
define( 'YOURLS_DB_USER', getenv_docker('YOURLS_DB_USER', 'root') );

/** MySQL database password */
define( 'YOURLS_DB_PASS', getenv('YOURLS_DB_PASS') );
define( 'YOURLS_DB_PASS', getenv_docker('YOURLS_DB_PASS') );

/** The name of the database for YOURLS */
define( 'YOURLS_DB_NAME', getenv('YOURLS_DB_NAME') ?: 'yourls' );
define( 'YOURLS_DB_NAME', getenv_docker('YOURLS_DB_NAME', 'yourls') );

/** MySQL hostname.
** If using a non standard port, specify it like 'hostname:port', eg. 'localhost:9999' or '127.0.0.1:666' */
define( 'YOURLS_DB_HOST', getenv('YOURLS_DB_HOST') ?: 'mysql' );
define( 'YOURLS_DB_HOST', getenv_docker('YOURLS_DB_HOST', 'mysql') );

/** MySQL tables prefix */
define( 'YOURLS_DB_PREFIX', getenv('YOURLS_DB_PREFIX') ?: 'yourls_' );
define( 'YOURLS_DB_PREFIX', getenv_docker('YOURLS_DB_PREFIX', 'yourls_') );

/*
** Site options
*/

/** YOURLS installation URL -- all lowercase, no trailing slash at the end.
** If you define it to "http://sho.rt", don't use "http://www.sho.rt" in your browser (and vice-versa) */
define( 'YOURLS_SITE', getenv('YOURLS_SITE') ?: 'http://your-own-domain-here.com' );
define( 'YOURLS_SITE', getenv_docker('YOURLS_SITE', 'http://your-own-domain-here.com') );

/** Server timezone GMT offset */
define( 'YOURLS_HOURS_OFFSET', filter_var(getenv('YOURLS_HOURS_OFFSET'), FILTER_VALIDATE_INT) ?: 0 );
Expand Down Expand Up @@ -57,7 +68,7 @@
** YOURLS will auto encrypt plain text passwords in this file
** Read http://yourls.org/userpassword for more information */
$yourls_user_passwords = [
getenv('YOURLS_USER') => getenv('YOURLS_PASS'),
getenv_docker('YOURLS_USER') => getenv_docker('YOURLS_PASS'),
];

/** Debug mode to output some internal information
Expand Down
30 changes: 0 additions & 30 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,36 +1,6 @@
#!/bin/bash
set -euo pipefail

# usage: file_env VAR [DEFAULT]
# ie: file_env 'XYZ_DB_PASSWORD' 'example'
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
file_env() {
local var="$1"
local fileVar="${var}_FILE"
local def="${2:-}"
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
printf 'Both %s and %s are set (but are exclusive)' "$var" "$fileVar"
fi
local val="$def"
if [ "${!var:-}" ]; then
val="${!var}"
elif [ "${!fileVar:-}" ]; then
val="$(< "${!fileVar}")"
fi
export "$var"="$val"
unset "$fileVar"
}

file_env 'YOURLS_DB_HOST'
file_env 'YOURLS_DB_USER'
file_env 'YOURLS_DB_PASS'
file_env 'YOURLS_DB_NAME'
file_env 'YOURLS_DB_PREFIX'
file_env 'YOURLS_SITE'
file_env 'YOURLS_USER'
file_env 'YOURLS_PASS'

if [ ! -e /var/www/html/yourls-loader.php ]; then
tar cf - --one-file-system -C /usr/src/yourls . | tar xf -
chown -R www-data:www-data /var/www/html
Expand Down
25 changes: 18 additions & 7 deletions fpm-alpine/config-docker.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,44 @@
* Edit this file with your own settings and save it as "config.php"
*/

// a helper function to lookup "env_FILE", "env", then fallback
function getenv_docker(string $name, ?string $default = null): ?string {
if ($fileEnv = getenv($name . '_FILE')) {
return trim(file_get_contents($fileEnv));
}
if ($value = getenv($name)) {
return $value;
}
return $default;
}

/*
** MySQL settings - You can get this info from your web host
*/

/** MySQL database username */
define( 'YOURLS_DB_USER', getenv('YOURLS_DB_USER') ?: 'root' );
define( 'YOURLS_DB_USER', getenv_docker('YOURLS_DB_USER', 'root') );

/** MySQL database password */
define( 'YOURLS_DB_PASS', getenv('YOURLS_DB_PASS') );
define( 'YOURLS_DB_PASS', getenv_docker('YOURLS_DB_PASS') );

/** The name of the database for YOURLS */
define( 'YOURLS_DB_NAME', getenv('YOURLS_DB_NAME') ?: 'yourls' );
define( 'YOURLS_DB_NAME', getenv_docker('YOURLS_DB_NAME', 'yourls') );

/** MySQL hostname.
** If using a non standard port, specify it like 'hostname:port', eg. 'localhost:9999' or '127.0.0.1:666' */
define( 'YOURLS_DB_HOST', getenv('YOURLS_DB_HOST') ?: 'mysql' );
define( 'YOURLS_DB_HOST', getenv_docker('YOURLS_DB_HOST', 'mysql') );

/** MySQL tables prefix */
define( 'YOURLS_DB_PREFIX', getenv('YOURLS_DB_PREFIX') ?: 'yourls_' );
define( 'YOURLS_DB_PREFIX', getenv_docker('YOURLS_DB_PREFIX', 'yourls_') );

/*
** Site options
*/

/** YOURLS installation URL -- all lowercase, no trailing slash at the end.
** If you define it to "http://sho.rt", don't use "http://www.sho.rt" in your browser (and vice-versa) */
define( 'YOURLS_SITE', getenv('YOURLS_SITE') ?: 'http://your-own-domain-here.com' );
define( 'YOURLS_SITE', getenv_docker('YOURLS_SITE', 'http://your-own-domain-here.com') );

/** Server timezone GMT offset */
define( 'YOURLS_HOURS_OFFSET', filter_var(getenv('YOURLS_HOURS_OFFSET'), FILTER_VALIDATE_INT) ?: 0 );
Expand Down Expand Up @@ -57,7 +68,7 @@
** YOURLS will auto encrypt plain text passwords in this file
** Read http://yourls.org/userpassword for more information */
$yourls_user_passwords = [
getenv('YOURLS_USER') => getenv('YOURLS_PASS'),
getenv_docker('YOURLS_USER') => getenv_docker('YOURLS_PASS'),
];

/** Debug mode to output some internal information
Expand Down
30 changes: 0 additions & 30 deletions fpm-alpine/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,36 +1,6 @@
#!/bin/bash
set -euo pipefail

# usage: file_env VAR [DEFAULT]
# ie: file_env 'XYZ_DB_PASSWORD' 'example'
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
file_env() {
local var="$1"
local fileVar="${var}_FILE"
local def="${2:-}"
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
printf 'Both %s and %s are set (but are exclusive)' "$var" "$fileVar"
fi
local val="$def"
if [ "${!var:-}" ]; then
val="${!var}"
elif [ "${!fileVar:-}" ]; then
val="$(< "${!fileVar}")"
fi
export "$var"="$val"
unset "$fileVar"
}

file_env 'YOURLS_DB_HOST'
file_env 'YOURLS_DB_USER'
file_env 'YOURLS_DB_PASS'
file_env 'YOURLS_DB_NAME'
file_env 'YOURLS_DB_PREFIX'
file_env 'YOURLS_SITE'
file_env 'YOURLS_USER'
file_env 'YOURLS_PASS'

if [ ! -e /var/www/html/yourls-loader.php ]; then
tar cf - --one-file-system -C /usr/src/yourls . | tar xf -
chown -R www-data:www-data /var/www/html
Expand Down
25 changes: 18 additions & 7 deletions fpm/config-docker.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,44 @@
* Edit this file with your own settings and save it as "config.php"
*/

// a helper function to lookup "env_FILE", "env", then fallback
function getenv_docker(string $name, ?string $default = null): ?string {
if ($fileEnv = getenv($name . '_FILE')) {
return trim(file_get_contents($fileEnv));
}
if ($value = getenv($name)) {
return $value;
}
return $default;
}

/*
** MySQL settings - You can get this info from your web host
*/

/** MySQL database username */
define( 'YOURLS_DB_USER', getenv('YOURLS_DB_USER') ?: 'root' );
define( 'YOURLS_DB_USER', getenv_docker('YOURLS_DB_USER', 'root') );

/** MySQL database password */
define( 'YOURLS_DB_PASS', getenv('YOURLS_DB_PASS') );
define( 'YOURLS_DB_PASS', getenv_docker('YOURLS_DB_PASS') );

/** The name of the database for YOURLS */
define( 'YOURLS_DB_NAME', getenv('YOURLS_DB_NAME') ?: 'yourls' );
define( 'YOURLS_DB_NAME', getenv_docker('YOURLS_DB_NAME', 'yourls') );

/** MySQL hostname.
** If using a non standard port, specify it like 'hostname:port', eg. 'localhost:9999' or '127.0.0.1:666' */
define( 'YOURLS_DB_HOST', getenv('YOURLS_DB_HOST') ?: 'mysql' );
define( 'YOURLS_DB_HOST', getenv_docker('YOURLS_DB_HOST', 'mysql') );

/** MySQL tables prefix */
define( 'YOURLS_DB_PREFIX', getenv('YOURLS_DB_PREFIX') ?: 'yourls_' );
define( 'YOURLS_DB_PREFIX', getenv_docker('YOURLS_DB_PREFIX', 'yourls_') );

/*
** Site options
*/

/** YOURLS installation URL -- all lowercase, no trailing slash at the end.
** If you define it to "http://sho.rt", don't use "http://www.sho.rt" in your browser (and vice-versa) */
define( 'YOURLS_SITE', getenv('YOURLS_SITE') ?: 'http://your-own-domain-here.com' );
define( 'YOURLS_SITE', getenv_docker('YOURLS_SITE', 'http://your-own-domain-here.com') );

/** Server timezone GMT offset */
define( 'YOURLS_HOURS_OFFSET', filter_var(getenv('YOURLS_HOURS_OFFSET'), FILTER_VALIDATE_INT) ?: 0 );
Expand Down Expand Up @@ -57,7 +68,7 @@
** YOURLS will auto encrypt plain text passwords in this file
** Read http://yourls.org/userpassword for more information */
$yourls_user_passwords = [
getenv('YOURLS_USER') => getenv('YOURLS_PASS'),
getenv_docker('YOURLS_USER') => getenv_docker('YOURLS_PASS'),
];

/** Debug mode to output some internal information
Expand Down
30 changes: 0 additions & 30 deletions fpm/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,36 +1,6 @@
#!/bin/bash
set -euo pipefail

# usage: file_env VAR [DEFAULT]
# ie: file_env 'XYZ_DB_PASSWORD' 'example'
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
file_env() {
local var="$1"
local fileVar="${var}_FILE"
local def="${2:-}"
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
printf 'Both %s and %s are set (but are exclusive)' "$var" "$fileVar"
fi
local val="$def"
if [ "${!var:-}" ]; then
val="${!var}"
elif [ "${!fileVar:-}" ]; then
val="$(< "${!fileVar}")"
fi
export "$var"="$val"
unset "$fileVar"
}

file_env 'YOURLS_DB_HOST'
file_env 'YOURLS_DB_USER'
file_env 'YOURLS_DB_PASS'
file_env 'YOURLS_DB_NAME'
file_env 'YOURLS_DB_PREFIX'
file_env 'YOURLS_SITE'
file_env 'YOURLS_USER'
file_env 'YOURLS_PASS'

if [ ! -e /var/www/html/yourls-loader.php ]; then
tar cf - --one-file-system -C /usr/src/yourls . | tar xf -
chown -R www-data:www-data /var/www/html
Expand Down

0 comments on commit 3b766d4

Please sign in to comment.