From 0b02655080015d9e0cac5629d503999ecf048b5e Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Tue, 8 Sep 2020 12:09:48 +0200 Subject: [PATCH] new: [apache,piwigo] added ``piwigo`` charm. Signed-off-by: Valentin Lab --- apache/build/Dockerfile | 8 + piwigo/hooks/init | 27 +++ piwigo/hooks/mysql_database-relation-joined | 40 +++++ piwigo/hooks/post_deploy | 172 ++++++++++++++++++++ piwigo/metadata.yml | 27 +++ 5 files changed, 274 insertions(+) create mode 100755 piwigo/hooks/init create mode 100755 piwigo/hooks/mysql_database-relation-joined create mode 100755 piwigo/hooks/post_deploy create mode 100644 piwigo/metadata.yml diff --git a/apache/build/Dockerfile b/apache/build/Dockerfile index d14a6612..6a148a9e 100644 --- a/apache/build/Dockerfile +++ b/apache/build/Dockerfile @@ -25,6 +25,14 @@ RUN apt-get update && \ rm -rf /var/lib/apt/lists/* && \ docker-php-ext-install mbstring intl xml pdo_pgsql +## Piwigo +RUN apt-get update && \ + DEBIAN_FRONTEND=noninteractive apt-get install -y libexif-dev libexif12 libfreetype6-dev libjpeg62-turbo-dev libpng-dev && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* && \ + docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ && \ + docker-php-ext-install exif gd pdo_mysql mysqli + ## XXXvlab: could load these in 'entrypoint.sh' to be more dynamic RUN a2enmod headers proxy_http rewrite ssl proxy_wstunnel http2 proxy_connect diff --git a/piwigo/hooks/init b/piwigo/hooks/init new file mode 100755 index 00000000..b9d2c0a6 --- /dev/null +++ b/piwigo/hooks/init @@ -0,0 +1,27 @@ +#!/bin/bash + +## Init is run on host +## For now it is run every time the script is launched, but +## it should be launched only once after build. + +## Accessible variables are: +## - SERVICE_NAME Name of current service +## - DOCKER_BASE_IMAGE Base image from which this service might be built if any +## - SERVICE_DATASTORE Location on host of the DATASTORE of this service +## - SERVICE_CONFIGSTORE Location on host of the CONFIGSTORE of this service + +set -e + +SOURCE_URL=https://piwigo.org/download/dlcounter.php?code=latest +LOCATION="$SERVICE_DATASTORE/opt/apps/piwigo" + +mkdir -p "$LOCATION" +if dir_is_empty "$LOCATION"; then + cd "$LOCATION" + wget -q "$SOURCE_URL" -O file.zip + unzip file.zip + rm file.zip + chown root:root "$LOCATION" -R + mv piwigo/* piwigo/.gitignore . + rmdir piwigo +fi diff --git a/piwigo/hooks/mysql_database-relation-joined b/piwigo/hooks/mysql_database-relation-joined new file mode 100755 index 00000000..854d5fa5 --- /dev/null +++ b/piwigo/hooks/mysql_database-relation-joined @@ -0,0 +1,40 @@ +#!/bin/bash + + +## XXXvlab: should get location of code +#CONFIG=$(echo $COMPOSE_CONFIG | shyaml get-value $SERVICE_NAME.relations.publish-dir...) +# CONFIG="$SERVICE_DATASTORE/opt/apps/piwigo/local/config/database.inc.php" + +set -e + +PASSWORD="$(relation-get password)" +USER="$(relation-get user)" +DBNAME="$(relation-get dbname)" + +# control=$(echo -en "$USER\0$DBNAME\0$PASSWORD" | md5_compat) + +#[ "$control" == "$(relation-get control)" ] && exit 0 + +## creation of config file is done through install.php in post_deploy + +## Do not support \ and ' or \n in DBNAME, PASSWORD. +# file_put "$CONFIG" </dev/null ) || exit 1 +admin_email=$(options-get admin-email 2>/dev/null ) || true +admin_email=${admin_email:-"admin@$domain"} + + + +CONTROL="$SERVICE_DATASTORE/.control" +## Was it already properly propagated to database ? +control=$(H "${admin_password}" "${admin_email}") +if [ -e "$CONTROL" ] && [ "$control" == "$(cat "$CONTROL")" ]; then + exit 0 +fi + +## XXXvlab: can't get real config here +if ! read-0 ts _ _ < <(get_service_relation "$SERVICE_NAME" "mysql-database"); then + err "Couldn't find relation ${DARKCYAN}mysql-database${NORMAL}." + exit 1 +fi + + +## +## We need mysql relation config also +## + +relation_file=$(get_relation_data_file "$SERVICE_NAME" "$ts" "mysql-database") || { + err "Failed to find relation file" + exit 1 +} + +mysql_config=$(cat "$relation_file") || exit 2 + +DBNAME="$(e "$mysql_config" | shyaml get-value dbname)" || { + err "Couldn't retrieve information of ${DARKCYAN}mysql-database${NORMAL}'s relation." + exit 1 +} + +CONFIG="$SERVICE_DATASTORE/opt/apps/piwigo/local/config/database.inc.php" +if [ -f "$CONFIG" ]; then + debug "Database already configured." + + export SERVICE_NAME="$ts" + export SERVICE_DATASTORE="$DATASTORE/$SERVICE_NAME" + DOCKER_BASE_IMAGE=$(service_base_docker_image "$SERVICE_NAME") + export DOCKER_BASE_IMAGE + + target_charm=$(get_service_charm "$ts") || exit 1 + target_charm_path=$(charm.get_dir "$target_charm") || exit 1 + + set +e + + . "$target_charm_path/lib/common" + + set -e + + ensure_db_docker_running + + if ! out=$(ddb "$DBNAME" < <(e "UPDATE users SET password=md5('$admin_password'), mail_address='$admin_email' WHERE id=1")); then + debug "Failed to set password for admin users." + exit 1 + fi + e "$control" > "$CONTROL" + exit 0 +fi + + +## +## We are in post_deploy, so our service is up, we need to get +## the ``network`` and ``container``'s id to communicate with +## him. +## + +container_id=$( + for container_id in $(get_running_containers_for_service "$MASTER_BASE_SERVICE_NAME"); do + e "$container_id" + break + done +) + +read-0 network container_ip < <(get_container_network_ip "$container_id") + + +## +## We'll need password from mysql database config +## + +PASSWORD="$(e "$mysql_config" | shyaml get-value password)" && +USER="$(e "$mysql_config" | shyaml get-value user)" || { + err "Couldn't retrieve information of ${DARKCYAN}mysql-database${NORMAL}'s relation." + exit 1 +} + +default_lang=$(options-get default-lang) || exit 1 + + +## We deliberately are not setting these: (they will be considered off) +# newsletter_subscribe "on" +# send_password_by_mail "on" + + +debug "Running install.php" +out=$(docker run --network "$network" docker.0k.io/curl -s -X POST -H "Host: $domain" -L \ + -F language="$default_lang" \ + -F dbhost="$ts" \ + -F dbuser="$USER" \ + -F dbpasswd="$PASSWORD" \ + -F dbname="$DBNAME" \ + -F prefix="" \ + -F admin_name="admin" \ + -F admin_pass1="$admin_password" \ + -F admin_pass2="$admin_password" \ + -F admin_mail="$admin_email" \ + -F install="Start+installation" \ + "$url/install.php") || { + err "Failed to run install.php script" + exit 1 +} + + +#e "$out" > "$SERVICE_DATASTORE/out.html" +# debug "written $SERVICE_DATASTORE/out.html" +had_error= +while read-0 error_msg; do + had_error=1 + err "Installation failed with these errors:" + echo "- ${error_msg}" >&2 +done < <(e "$out" | xpath "//div[@class='errors']/ul/li/text()") + +if [ "$had_error" ]; then + exit 1 +fi + +debug "No error catched on \`\`install.php\`\` result." +e "$control" > "$CONTROL" + +exit 0 \ No newline at end of file diff --git a/piwigo/metadata.yml b/piwigo/metadata.yml new file mode 100644 index 00000000..df8d40f6 --- /dev/null +++ b/piwigo/metadata.yml @@ -0,0 +1,27 @@ +description: Piwigo +subordinate: true + +default-options: + admin-password: admin + default-lang: en_US + +uses: + mysql-database: + #constraint: required | recommended | optional + #auto: pair | summon | none ## default: pair + constraint: required + auto: summon + solves: + database: "main storage" + publish-dir: + #constraint: required | recommended | optional + #auto: pair | summon | none ## default: pair + scope: container + constraint: required + auto: summon + solves: + container: "main running server" + default-options: + location: !var-expand "$DATASTORE/$BASE_SERVICE_NAME/opt/apps/piwigo" + # data-dirs: ## write permission for web-app + # - .