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
16 changes: 16 additions & 0 deletions commands/env.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,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 @@ -163,6 +166,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 @@ -202,6 +208,16 @@ ${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" ]]; then
regeneratePMAConfig
fi


if [[ "${WARDEN_PARAMS[0]}" == "up" ]] || [[ "${WARDEN_PARAMS[0]}" == "start" ]]; then
regeneratePMAConfig
fi
monteshot marked this conversation as resolved.
Show resolved Hide resolved

## 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
16 changes: 16 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,12 @@ 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 ]]; then
if [[ ! -f "${WARDEN_HOME_DIR}/etc/phpmyadmin/config.user.inc.php" ]]; then
mkdir -p "${WARDEN_HOME_DIR}/etc/phpmyadmin"
fi
fi
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. Could you explain why it can't be in the regeneratePMAConfig function?
  2. Can we simplify it to a single if like this?
Suggested change
if [[ "${WARDEN_PHPMYADMIN_ENABLE}" == 1 ]]; then
if [[ ! -f "${WARDEN_HOME_DIR}/etc/phpmyadmin/config.user.inc.php" ]]; then
mkdir -p "${WARDEN_HOME_DIR}/etc/phpmyadmin"
fi
fi
if [[ "${WARDEN_PHPMYADMIN_ENABLE}" == 1 && \
! -f "${WARDEN_HOME_DIR}/etc/phpmyadmin/config.user.inc.php" ]]; then
mkdir -p "${WARDEN_HOME_DIR}/etc/phpmyadmin"
fi

Copy link
Author

Choose a reason for hiding this comment

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

  1. The main reason to leave PMA file generation here is faulting to build by docker-compose
  2. ++


## generate dynamic traefik ssl termination configuration
cat > "${WARDEN_HOME_DIR}/etc/traefik/dynamic.yml" <<-EOT
tls:
Expand Down Expand Up @@ -100,3 +114,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}
Copy link
Contributor

Choose a reason for hiding this comment

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

Having a new line at the end of all files is a best-practice

Suggested change
restart: ${WARDEN_RESTART_POLICY:-always}
restart: ${WARDEN_RESTART_POLICY:-always}

44 changes: 43 additions & 1 deletion utils/core.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
[[ ! ${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 {
Expand Down Expand Up @@ -55,3 +55,45 @@ 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..."
## generate phpmyadmin connection configuration
pma_config_file="${WARDEN_HOME_DIR}/etc/phpmyadmin/config.user.inc.php"

cat > "${pma_config_file}" <<-EOL
<?php
\$i = 1;
EOL

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}')
MYSQL_PASSWORD=$(docker exec "${container_id}" printenv | grep MYSQL_PASSWORD | awk -F '=' '{print $2}')
cat >> "${pma_config_file}" <<-EOT
\$cfg['Servers'][\$i]['host'] = '${container_ip}';
\$cfg['Servers'][\$i]['auth_type'] = 'config';
\$cfg['Servers'][\$i]['user'] = 'root';
\$cfg['Servers'][\$i]['password'] = '${MYSQL_ROOT_PASSWORD}';
\$cfg['Servers'][\$i]['AllowNoPassword'] = true;
\$cfg['Servers'][\$i]['hide_db'] = '(information_schema|performance_schema|mysql)';
\$cfg['Servers'][\$i]['verbose'] = '${container_name}';
\$i++;
EOT
done

cat >> "${pma_config_file}" <<-EOT
?>
EOT

echo "phpMyAdmin configuration regenerated."
fi
}
Copy link
Contributor

Choose a reason for hiding this comment

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

TBH this function look quite complicated to read.
I suggest doing following improvements:

  1. Format the function in the same way as others
  2. Is there any reason why we add"?>" to the end of file?
  3. Simlify logic with multi-line sections (EOT, EOL, cat) to something like this a
{ 
   # ...
   echo 'my line 1';
   echo 'my line 2';
} > myfile
Suggested change
function regeneratePMAConfig() {
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..."
## generate phpmyadmin connection configuration
pma_config_file="${WARDEN_HOME_DIR}/etc/phpmyadmin/config.user.inc.php"
cat > "${pma_config_file}" <<-EOL
<?php
\$i = 1;
EOL
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}')
MYSQL_PASSWORD=$(docker exec "${container_id}" printenv | grep MYSQL_PASSWORD | awk -F '=' '{print $2}')
cat >> "${pma_config_file}" <<-EOT
\$cfg['Servers'][\$i]['host'] = '${container_ip}';
\$cfg['Servers'][\$i]['auth_type'] = 'config';
\$cfg['Servers'][\$i]['user'] = 'root';
\$cfg['Servers'][\$i]['password'] = '${MYSQL_ROOT_PASSWORD}';
\$cfg['Servers'][\$i]['AllowNoPassword'] = true;
\$cfg['Servers'][\$i]['hide_db'] = '(information_schema|performance_schema|mysql)';
\$cfg['Servers'][\$i]['verbose'] = '${container_name}';
\$i++;
EOT
done
cat >> "${pma_config_file}" <<-EOT
?>
EOT
echo "phpMyAdmin configuration regenerated."
fi
}
function regeneratePMAConfig() {
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)';"
echo "\$cfg['Servers'][\$i]['verbose'] = '${container_name}';"
echo "\$i++;"
done
echo "?>"
} > "${pma_config_file}"
echo "phpMyAdmin configuration regenerated."
fi
}