diff --git a/mariadb/hooks/sql_database-relation-joined b/mariadb/hooks/sql_database-relation-joined new file mode 100755 index 0000000..c9d906a --- /dev/null +++ b/mariadb/hooks/sql_database-relation-joined @@ -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 \ No newline at end of file diff --git a/mariadb/metadata.yml b/mariadb/metadata.yml index 676e493..4a8d9b4 100644 --- a/mariadb/metadata.yml +++ b/mariadb/metadata.yml @@ -2,6 +2,7 @@ name: MariaDB maintainer: "Valentin Lab " provides: mysql-database: + sql-database: data-resources: - /var/lib/mysql - /var/backups/mysql diff --git a/nextcloud/hooks/mysql_database-relation-joined b/nextcloud/hooks/mysql_database-relation-joined deleted file mode 100755 index 792d8f4..0000000 --- a/nextcloud/hooks/mysql_database-relation-joined +++ /dev/null @@ -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." diff --git a/nextcloud/hooks/mysql_database-relation-joined b/nextcloud/hooks/mysql_database-relation-joined new file mode 120000 index 0000000..c54b577 --- /dev/null +++ b/nextcloud/hooks/mysql_database-relation-joined @@ -0,0 +1 @@ +postgres_database-relation-joined \ No newline at end of file diff --git a/nextcloud/hooks/postgres_database-relation-joined b/nextcloud/hooks/postgres_database-relation-joined index ff60bb5..2fa2d49 100755 --- a/nextcloud/hooks/postgres_database-relation-joined +++ b/nextcloud/hooks/postgres_database-relation-joined @@ -1,51 +1,11 @@ #!/bin/bash -. lib/common +type="${0##*/}" +type="${type%_database-relation-joined}" -set -e +relation-set 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 diff --git a/nextcloud/hooks/sql_database-relation-joined b/nextcloud/hooks/sql_database-relation-joined new file mode 100755 index 0000000..7ae6ea7 --- /dev/null +++ b/nextcloud/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." diff --git a/nextcloud/metadata.yml b/nextcloud/metadata.yml index 196eb08..215cddf 100644 --- a/nextcloud/metadata.yml +++ b/nextcloud/metadata.yml @@ -7,20 +7,13 @@ config-resources: provides: nextcloud-app: uses: - postgres-database: + sql-database: #constraint: required | recommended | optional #auto: pair | summon | none ## default: pair constraint: required auto: summon solves: database: "main storage" - mysql-database: - #constraint: required | recommended | optional - #auto: pair | summon | none ## default: pair - constraint: optional - auto: pair - solves: - database: "main storage" web-proxy: #constraint: required | recommended | optional #auto: pair | summon | none ## default: pair diff --git a/postgres/hooks/sql_database-relation-joined b/postgres/hooks/sql_database-relation-joined new file mode 100755 index 0000000..75fac30 --- /dev/null +++ b/postgres/hooks/sql_database-relation-joined @@ -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 + diff --git a/postgres/metadata.yml b/postgres/metadata.yml index 4191144..bda14c6 100644 --- a/postgres/metadata.yml +++ b/postgres/metadata.yml @@ -4,6 +4,7 @@ data-resources: - /var/lib/postgresql/data provides: postgres-database: + sql-database: uses: schedule-command: