Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add phpMyAdmin service configuration #801

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
12 changes: 12 additions & 0 deletions commands/env.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ fi
## disconnect peered service containers from environment network
if [[ "${WARDEN_PARAMS[0]}" == "down" ]]; then
disconnectPeeredServices "$(renderEnvNetworkName)"

## regenerate PMA config on each env changing
regeneratePMAConfig
fi

## connect peered service containers to environment network
Expand All @@ -168,6 +171,9 @@ if [[ "${WARDEN_PARAMS[0]}" == "up" ]]; then
WARDEN_PARAMS=("${WARDEN_PARAMS[@]:1}")
WARDEN_PARAMS=(up -d "${WARDEN_PARAMS[@]}")
fi

## regenerate PMA config on each env changing
regeneratePMAConfig
fi

## lookup address of traefik container on environment network
Expand Down Expand Up @@ -207,6 +213,12 @@ ${DOCKER_COMPOSE_COMMAND} \
--project-directory "${WARDEN_ENV_PATH}" -p "${WARDEN_ENV_NAME}" \
"${DOCKER_COMPOSE_ARGS[@]}" "${WARDEN_PARAMS[@]}" "$@"


if [[ "${WARDEN_PARAMS[0]}" == "stop" || "${WARDEN_PARAMS[0]}" == "down" || \
"${WARDEN_PARAMS[0]}" == "up" || "${WARDEN_PARAMS[0]}" == "start" ]]; then
regeneratePMAConfig
fi

## resume mutagen sync if available and php-fpm container id hasn't changed
if { [[ "${WARDEN_PARAMS[0]}" == "up" ]] || [[ "${WARDEN_PARAMS[0]}" == "start" ]]; } \
&& [[ $OSTYPE =~ ^darwin ]] && [[ -f "${MUTAGEN_SYNC_FILE}" ]] \
Expand Down
2 changes: 2 additions & 0 deletions commands/install.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,7 @@ if [[ ! -f "${WARDEN_HOME_DIR}/.env" ]]; then
WARDEN_PORTAINER_ENABLE=0
# Set to "0" to disable DNSMasq
WARDEN_DNSMASQ_ENABLE=1
# Set to "0" to disable phpMyAdmin
WARDEN_PHPMYADMIN_ENABLE=1
EOT
fi
15 changes: 15 additions & 0 deletions commands/svc.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ if [[ -f "${WARDEN_HOME_DIR}/.env" ]]; then
eval "$(grep "^WARDEN_DNSMASQ_ENABLE" "${WARDEN_HOME_DIR}/.env")"
# Check Portainer
eval "$(grep "^WARDEN_PORTAINER_ENABLE" "${WARDEN_HOME_DIR}/.env")"
# Check PMA
eval "$(grep "^WARDEN_PHPMYADMIN_ENABLE" "${WARDEN_HOME_DIR}/.env")"
fi

DOCKER_COMPOSE_ARGS+=("-f")
Expand All @@ -41,6 +43,12 @@ if [[ "${WARDEN_PORTAINER_ENABLE}" == 1 ]]; then
DOCKER_COMPOSE_ARGS+=("${WARDEN_DIR}/docker/docker-compose.portainer.yml")
fi

WARDEN_PHPMYADMIN_ENABLE="${WARDEN_PHPMYADMIN_ENABLE:-1}"
if [[ "${WARDEN_PHPMYADMIN_ENABLE}" == 1 ]]; then
DOCKER_COMPOSE_ARGS+=("-f")
DOCKER_COMPOSE_ARGS+=("${WARDEN_DIR}/docker/docker-compose.phpmyadmin.yml")
fi

## allow an additional docker-compose file to be loaded for global services
if [[ -f "${WARDEN_HOME_DIR}/docker-compose.yml" ]]; then
DOCKER_COMPOSE_ARGS+=("-f")
Expand All @@ -64,6 +72,11 @@ if [[ "${WARDEN_PARAMS[0]}" == "up" ]]; then
mkdir -p "${WARDEN_HOME_DIR}/etc/traefik"
cp "${WARDEN_DIR}/config/traefik/traefik.yml" "${WARDEN_HOME_DIR}/etc/traefik/traefik.yml"

if [[ "${WARDEN_PHPMYADMIN_ENABLE}" == 1 && \
! -f "${WARDEN_HOME_DIR}/etc/phpmyadmin/config.user.inc.php" ]]; then
mkdir -p "${WARDEN_HOME_DIR}/etc/phpmyadmin"
fi

## generate dynamic traefik ssl termination configuration
cat > "${WARDEN_HOME_DIR}/etc/traefik/dynamic.yml" <<-EOT
tls:
Expand Down Expand Up @@ -100,3 +113,5 @@ if [[ "${WARDEN_PARAMS[0]}" == "up" ]]; then
connectPeeredServices "${network}"
done
fi

regeneratePMAConfig
16 changes: 16 additions & 0 deletions docker/docker-compose.phpmyadmin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
services:
phpmyadmin:
container_name: phpmyadmin
image: phpmyadmin/phpmyadmin
environment:
- PMA_ARBITRARY=1
- PMA_ABSOLUTE_URI=https://phpmyadmin.${WARDEN_SERVICE_DOMAIN:-warden.test}
volumes:
- /sessions
- ${WARDEN_HOME_DIR}/etc/phpmyadmin/config.user.inc.php:/etc/phpmyadmin/config.user.inc.php
labels:
- traefik.enable=true
- traefik.http.routers.phpmyadmin.tls=true
- traefik.http.routers.phpmyadmin.rule=Host(`phpmyadmin.${WARDEN_SERVICE_DOMAIN:-warden.test}`)||Host(`phpmyadmin.warden.test`)
- traefik.http.services.phpmyadmin.loadbalancer.server.port=80
restart: ${WARDEN_RESTART_POLICY:-always}
33 changes: 31 additions & 2 deletions utils/core.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
[[ ! ${WARDEN_DIR} ]] && >&2 echo -e "\033[31mThis script is not intended to be run directly!\033[0m" && exit 1

## global service containers to be connected with the project docker network
DOCKER_PEERED_SERVICES=("traefik" "tunnel" "mailhog")
DOCKER_PEERED_SERVICES=("traefik" "tunnel" "mailhog" "phpmyadmin")

## messaging functions
function warning {
>&2 printf "\033[33mWARNING\033[0m: $@\n"
>&2 printf "\033[33mWARNING\033[0m: $@\n"
}

function error {
Expand Down Expand Up @@ -55,3 +55,32 @@ function disconnectPeeredServices {
(docker network disconnect "$1" ${svc} 2>&1| grep -v 'is not connected') || true
done
}
function regeneratePMAConfig() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please fix formatting for this function to be the same as in others?

if [[ -f "${WARDEN_HOME_DIR}/.env" ]]; then
# Recheck PMA since old versions of .env may not have WARDEN_PHPMYADMIN_ENABLE setting
eval "$(grep "^WARDEN_PHPMYADMIN_ENABLE" "${WARDEN_HOME_DIR}/.env")"
WARDEN_PHPMYADMIN_ENABLE="${WARDEN_PHPMYADMIN_ENABLE:-1}"
fi
if [[ "${WARDEN_PHPMYADMIN_ENABLE}" == 1 ]]; then
echo "Regenerating phpMyAdmin configuration..."
pma_config_file="${WARDEN_HOME_DIR}/etc/phpmyadmin/config.user.inc.php"
{
echo "<?php"
echo "\$i = 1;"
for container_id in $(docker ps -q --filter "name=mysql" --filter "name=mariadb" --filter "name=db"); do
container_name=$(docker inspect --format '{{.Name}}' "${container_id}" | sed 's#^/##')
container_ip=$(docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "${container_id}")
MYSQL_ROOT_PASSWORD=$(docker exec "${container_id}" printenv | grep MYSQL_ROOT_PASSWORD | awk -F '=' '{print $2}')
echo "\$cfg['Servers'][\$i]['host'] = '${container_ip}';"
echo "\$cfg['Servers'][\$i]['auth_type'] = 'config';"
echo "\$cfg['Servers'][\$i]['user'] = 'root';"
echo "\$cfg['Servers'][\$i]['password'] = '${MYSQL_ROOT_PASSWORD}';"
echo "\$cfg['Servers'][\$i]['AllowNoPassword'] = true;"
echo "\$cfg['Servers'][\$i]['hide_db'] = '(information_schema|performance_schema|mysql|sys)';"
echo "\$cfg['Servers'][\$i]['verbose'] = '${container_name}';"
echo "\$i++;"
done
} > "${pma_config_file}"
echo "phpMyAdmin configuration regenerated."
fi
}