-
-
Notifications
You must be signed in to change notification settings - Fork 169
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
base: main
Are you sure you want to change the base?
Conversation
monteshot
commented
Sep 16, 2024
- Add docker-compose.phpmyadmin.yml for phpMyAdmin service
- Update svc.cmd to conditionally include phpMyAdmin configuration
- Ensure phpMyAdmin config file is generated if not present
- Add regeneratePMAConfig function to core.sh
- Call regeneratePMAConfig on env changes in env.cmd
- Include phpMyAdmin as a global service in core.sh
- Add docker-compose.phpmyadmin.yml for phpMyAdmin service - Update svc.cmd to conditionally include phpMyAdmin configuration - Ensure phpMyAdmin config file is generated if not present - Add regeneratePMAConfig function to core.sh - Call regeneratePMAConfig on env changes in env.cmd - Include phpMyAdmin as a global service in core.sh
@monteshot Thanks for the contribution. I would like to suggest that you add a feature flag to this, similar to that of Portainer and DNSMasq, so that those that use external tools like DBeaver or TablePlus can opt-out of running PMA. I should clarify. I see that you have a flag, but we're still doing unnecessary regeneration if the flag is disabled. |
This comment was marked as resolved.
This comment was marked as resolved.
- Add WARDEN_PHPMYADMIN_ENABLE variable to install.cmd - Modify svc.cmd to check WARDEN_PHPMYADMIN_ENABLE - Update core.sh to regenerate phpMyAdmin config when enabled
@bap14 Hello. Please review the updated PR. @poespas Hi. If you use PMA by extending the warden-env.yml file in the project directory, you will have two separate PMA apps. Regarding the file in the Warden home directory, this file only needs to give information about db containers(as mentioned in PMA documentation). |
…_ENABLE Add check to ensure WARDEN_HOME_DIR/.env exists and contains WARDEN_PHPMYADMIN_ENABLE. Default WARDEN_PHPMYADMIN_ENABLE to 1 if missing. Indent echo statement for consistency.
Looking over the code, I don't have any concerns. I think this is ready for testing and if all is working correctly - a merge! |
Include 'sys' in the list of databases to be hidden.
@@ -55,3 +55,45 @@ function disconnectPeeredServices { | |||
(docker network disconnect "$1" ${svc} 2>&1| grep -v 'is not connected') || true | |||
done | |||
} | |||
function regeneratePMAConfig() { |
There was a problem hiding this comment.
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?
utils/core.sh
Outdated
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 | ||
} |
There was a problem hiding this comment.
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:
- Format the function in the same way as others
- Is there any reason why we add"?>" to the end of file?
- Simlify logic with multi-line sections (EOT, EOL, cat) to something like this a
{
# ...
echo 'my line 1';
echo 'my line 2';
} > myfile
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 | |
} |
docker/docker-compose.phpmyadmin.yml
Outdated
- 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} |
There was a problem hiding this comment.
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
restart: ${WARDEN_RESTART_POLICY:-always} | |
restart: ${WARDEN_RESTART_POLICY:-always} | |
commands/svc.cmd
Outdated
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Could you explain why it can't be in the regeneratePMAConfig function?
- Can we simplify it to a single if like this?
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- The main reason to leave PMA file generation here is faulting to build by docker-compose
- ++
Combine the conditions for executing regeneratePMAConfig into a single if statement for improved readability.
Simplify the conditional logic for creating the phpMyAdmin configuration directory by merging nested if statements into a single condition.
Remove unnecessary function parentheses and streamline heredoc usage for phpMyAdmin configuration generation.
Streamline the generation of phpMyAdmin configuration by using a single block for echoing PHP script content, reducing redundancy and improving readability.
@ihor-sviziev Can you review PR again? |