forked from 0k/0k-charms
Valentin Lab
11 months ago
8 changed files with 102 additions and 106 deletions
-
15mariadb/hooks/sql_database-relation-joined
-
1mariadb/metadata.yml
-
51nextcloud/hooks/mysql_database-relation-joined
-
1nextcloud/hooks/mysql_database-relation-joined
-
54nextcloud/hooks/postgres_database-relation-joined
-
60nextcloud/hooks/sql_database-relation-joined
-
9nextcloud/metadata.yml
-
16postgres/hooks/sql_database-relation-joined
-
1postgres/metadata.yml
@ -0,0 +1,15 @@ |
|||||
|
#!/bin/bash |
||||
|
|
||||
|
## When writing relation script, remember: |
||||
|
## - they should be idempotents |
||||
|
## - they can be launched while the dockers is already up |
||||
|
## - they are launched from the host |
||||
|
## - the target of the link is launched first, and get a chance to ``relation-set`` |
||||
|
## - both side of the scripts get to use ``relation-get``. |
||||
|
|
||||
|
relation-set type mysql || { |
||||
|
err "Could not set relation ${WHITE}type${NORMAL} to 'mysql'." |
||||
|
exit 1 |
||||
|
} |
||||
|
|
||||
|
. hooks/mysql_database-relation-joined |
@ -1,51 +0,0 @@ |
|||||
#!/bin/bash |
|
||||
|
|
||||
. lib/common |
|
||||
|
|
||||
set -e |
|
||||
|
|
||||
PASSWORD="$(relation-get password)" |
|
||||
USER="$(relation-get user)" |
|
||||
DBNAME="$(relation-get dbname)" |
|
||||
|
|
||||
|
|
||||
## This check adds purely arbitrary limits to what could be a password |
|
||||
## if we need to open that more, just consider the next script where we'll |
|
||||
## need to write in a PHP structure, or in YAML structure. |
|
||||
|
|
||||
## Note that here, "[]" chars are not accepted just because it doesn't seem evident |
|
||||
## to test for those in bash. |
|
||||
if ! [[ "$PASSWORD" =~ ^[a-zA-Z0-9~\`\&+=@\#^\*/\\_%\$:\;\!?.,\<\>{}()\"\'|-]*$ ]]; then |
|
||||
err "Invalid password chosen for mysql database." |
|
||||
exit 1 |
|
||||
fi |
|
||||
|
|
||||
## if config is not existent |
|
||||
if [ -e "$CONFIGFILE" ] && grep "^ 'dbuser' => '" "$CONFIGFILE" >/dev/null; then |
|
||||
|
|
||||
## 'occ' can't be used as it will try to connect to mysql before running and |
|
||||
## will fail if user/password is not correct |
|
||||
|
|
||||
## We need to get through bash, and sed interpretation, then PHP single quoted strings. |
|
||||
quoted_user="${USER//\\/\\\\\\\\\\}" |
|
||||
quoted_user="${quoted_user//\'/\\\\\'}" |
|
||||
quoted_password="${PASSWORD//\\/\\\\\\\\\\}" |
|
||||
quoted_password="${quoted_password//\'/\\\\\'}" |
|
||||
sed -ri "s/^( 'dbuser' => ')(.*)(',)$/\1${quoted_user}\3/g;\ |
|
||||
s/^( 'dbpassword' => ')(.*)(',)$/\1${quoted_password}\3/g;" "$CONFIGFILE" |
|
||||
else |
|
||||
|
|
||||
## These variable are not used by current docker image after first install |
|
||||
|
|
||||
config-add "\ |
|
||||
services: |
|
||||
$MASTER_BASE_SERVICE_NAME: |
|
||||
environment: |
|
||||
MYSQL_HOST: $MASTER_TARGET_SERVICE_NAME |
|
||||
MYSQL_DATABASE: $DBNAME |
|
||||
MYSQL_PASSWORD: $PASSWORD |
|
||||
MYSQL_USER: $USER |
|
||||
" |
|
||||
fi |
|
||||
|
|
||||
info "Configured $SERVICE_NAME code for $TARGET_SERVICE_NAME access." |
|
@ -0,0 +1 @@ |
|||||
|
postgres_database-relation-joined |
@ -1,51 +1,11 @@ |
|||||
#!/bin/bash |
#!/bin/bash |
||||
|
|
||||
. lib/common |
|
||||
|
type="${0##*/}" |
||||
|
type="${type%_database-relation-joined}" |
||||
|
|
||||
set -e |
|
||||
|
set-relation type "$type" || { |
||||
|
err "Could not set relation ${WHITE}type${NORMAL} to '$type'." |
||||
|
exit 1 |
||||
|
} |
||||
|
|
||||
PASSWORD="$(relation-get password)" |
|
||||
USER="$(relation-get user)" |
|
||||
DBNAME="$(relation-get dbname)" |
|
||||
|
|
||||
|
|
||||
## This check adds purely arbitrary limits to what could be a password |
|
||||
## if we need to open that more, just consider the next script where we'll |
|
||||
## need to write in a PHP structure, or in YAML structure. |
|
||||
|
|
||||
## Note that here, "[]" chars are not accepted just because it doesn't seem evident |
|
||||
## to test for those in bash. |
|
||||
if ! [[ "$PASSWORD" =~ ^[a-zA-Z0-9~\`\&+=@\#^\*/\\_%\$:\;\!?.,\<\>{}()\"\'|-]*$ ]]; then |
|
||||
err "Invalid password chosen for postgres database." |
|
||||
exit 1 |
|
||||
fi |
|
||||
|
|
||||
## if config is not existent |
|
||||
if [ -e "$CONFIGFILE" ] && grep "^ 'dbuser' => '" "$CONFIGFILE" >/dev/null; then |
|
||||
|
|
||||
## 'occ' can't be used as it will try to connect to postgres before running and |
|
||||
## will fail if user/password is not correct |
|
||||
|
|
||||
## We need to get through bash, and sed interpretation, then PHP single quoted strings. |
|
||||
quoted_user="${USER//\\/\\\\\\\\\\}" |
|
||||
quoted_user="${quoted_user//\'/\\\\\'}" |
|
||||
quoted_password="${PASSWORD//\\/\\\\\\\\\\}" |
|
||||
quoted_password="${quoted_password//\'/\\\\\'}" |
|
||||
sed -ri "s/^( 'dbuser' => ')(.*)(',)$/\1${quoted_user}\3/g;\ |
|
||||
s/^( 'dbpassword' => ')(.*)(',)$/\1${quoted_password}\3/g;" "$CONFIGFILE" |
|
||||
else |
|
||||
|
|
||||
## These variable are not used by current docker image after first install |
|
||||
|
|
||||
config-add "\ |
|
||||
services: |
|
||||
$MASTER_BASE_SERVICE_NAME: |
|
||||
environment: |
|
||||
POSTGRES_HOST: $MASTER_TARGET_SERVICE_NAME |
|
||||
POSTGRES_DB: $DBNAME |
|
||||
POSTGRES_PASSWORD: $PASSWORD |
|
||||
POSTGRES_USER: $USER |
|
||||
" |
|
||||
fi |
|
||||
|
|
||||
info "Configured $SERVICE_NAME code for $TARGET_SERVICE_NAME access." |
|
||||
|
. ./hooks/sql_database-relation-joined |
@ -0,0 +1,60 @@ |
|||||
|
#!/bin/bash |
||||
|
|
||||
|
. lib/common |
||||
|
|
||||
|
set -e |
||||
|
TYPE="$(relation-get type)" || { |
||||
|
err "No ${WHITE}type${NORMAL} set in relation." |
||||
|
exit 1 |
||||
|
} |
||||
|
PASSWORD="$(relation-get password)" |
||||
|
USER="$(relation-get user)" |
||||
|
DBNAME="$(relation-get dbname)" |
||||
|
|
||||
|
|
||||
|
## This check adds purely arbitrary limits to what could be a password |
||||
|
## if we need to open that more, just consider the next script where we'll |
||||
|
## need to write in a PHP structure, or in YAML structure. |
||||
|
|
||||
|
## Note that here, "[]" chars are not accepted just because it doesn't seem evident |
||||
|
## to test for those in bash. |
||||
|
if ! [[ "$PASSWORD" =~ ^[a-zA-Z0-9~\`\&+=@\#^\*/\\_%\$:\;\!?.,\<\>{}()\"\'|-]*$ ]]; then |
||||
|
err "Invalid password chosen for $type database." |
||||
|
exit 1 |
||||
|
fi |
||||
|
|
||||
|
## if config is not existent |
||||
|
if [ -e "$CONFIGFILE" ] && grep "^ 'dbuser' => '" "$CONFIGFILE" >/dev/null; then |
||||
|
|
||||
|
## 'occ' can't be used as it will try to connect to db before running and |
||||
|
## will fail if user/password is not correct |
||||
|
|
||||
|
## We need to get through bash, and sed interpretation, then PHP single quoted strings. |
||||
|
quoted_user="${USER//\\/\\\\\\\\\\}" |
||||
|
quoted_user="${quoted_user//\'/\\\\\'}" |
||||
|
quoted_password="${PASSWORD//\\/\\\\\\\\\\}" |
||||
|
quoted_password="${quoted_password//\'/\\\\\'}" |
||||
|
sed -ri "s/^( 'dbuser' => ')(.*)(',)$/\1${quoted_user}\3/g;\ |
||||
|
s/^( 'dbpassword' => ')(.*)(',)$/\1${quoted_password}\3/g;" "$CONFIGFILE" |
||||
|
else |
||||
|
|
||||
|
## These variable are not used by current docker image after first install |
||||
|
|
||||
|
if [ "$TYPE" == "mysql" ]; then |
||||
|
database_env_label="DATABASE" |
||||
|
else |
||||
|
database_env_label="DB" |
||||
|
fi |
||||
|
|
||||
|
config-add "\ |
||||
|
services: |
||||
|
$MASTER_BASE_SERVICE_NAME: |
||||
|
environment: |
||||
|
${TYPE^^}_HOST: $MASTER_TARGET_SERVICE_NAME |
||||
|
${TYPE^^}_${database_env_label}: $DBNAME |
||||
|
${TYPE^^}_PASSWORD: $PASSWORD |
||||
|
${TYPE^^}_USER: $USER |
||||
|
" |
||||
|
fi |
||||
|
|
||||
|
info "Configured $SERVICE_NAME code for $TARGET_SERVICE_NAME access." |
@ -0,0 +1,16 @@ |
|||||
|
#!/bin/bash |
||||
|
|
||||
|
## When writing relation script, remember: |
||||
|
## - they should be idempotents |
||||
|
## - they can be launched while the dockers is already up |
||||
|
## - they are launched from the host |
||||
|
## - the target of the link is launched first, and get a chance to ``relation-set`` |
||||
|
## - both side of the scripts get to use ``relation-get``. |
||||
|
|
||||
|
relation-set type postgres || { |
||||
|
err "Could not set relation ${WHITE}type${NORMAL} to 'postgres'." |
||||
|
exit 1 |
||||
|
} |
||||
|
|
||||
|
. hooks/postgres_database-relation-joined |
||||
|
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue