Skip to content

Commit

Permalink
Merge pull request #5 from rfay/20220605_auto_login
Browse files Browse the repository at this point in the history
Add auto-login and `ddev launcha`, fixes #4, fixes #1, fixes #3
  • Loading branch information
rfay authored Jun 22, 2022
2 parents 37fd251 + a8ee1dc commit 2e0810d
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 37 deletions.
10 changes: 6 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: tests
on:
pull_request:
push:
branches: [ master, main ]
branches: [ main ]

schedule:
- cron: '01 07 * * *'
Expand All @@ -29,12 +29,11 @@ jobs:

strategy:
matrix:
# ddev_version: [latest, edge, HEAD]
ddev_version: [edge, HEAD]
ddev_version: [latest, HEAD]
# ddev_version: [PR]
fail-fast: false

runs-on: ubuntu-20.04
runs-on: ubuntu-22.04

steps:
- uses: actions/checkout@v2
Expand Down Expand Up @@ -76,3 +75,6 @@ jobs:
- name: tests
run: bats tests

# keepalive-workflow adds a dummy commit if there's no other action here, keeps
# GitHub from turning off tests after 60 days
- uses: gautamkrishnar/keepalive-workflow@v1
22 changes: 5 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,20 @@ with no _external_ plugins. It contains all official plugins and themes and allo
to easily chose one by editing the `docker-compose.adminer.yaml` file after
installation.

The official container supports:
This currently supports:

* MySQL / MariaDB
* PostgreSQL
* SQLite
* SimpleDB
* Elasticsearch

## Why?

Because you might need something other than MySQL / MariaDB in your DDEV project. We got you covered.

## Installation

* `ddev get bserem/ddev-adminer && ddev restart`

## Caveats

You need to pass database credentials manually for the time being. If you have
any ideas on how to overcome this issue please let me know.
Then you can just `ddev launch -a` or use `ddev describe` to get the URL (`https://<project>.ddev.site:9101`).

Default ddev database credentials are:
## What does this add-on do?

* user `db`
* password `db`
* database `db`
* Adds the adminer container as a service
* Overrides the `ddev launch` command at the project level with a `ddev launch -a` (or `ddev launch --adminer`) option.

---
**Contributed and maintained by [@bserem](https://github.com/bserem).**
3 changes: 3 additions & 0 deletions adminer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#ddev-generated
FROM adminer:standalone
ADD ddev-login.php /var/www/html/plugins-enabled/
28 changes: 28 additions & 0 deletions adminer/ddev-login.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
// #ddev-generated
class DDEVLogin {
function loginForm() {
?>
<script type="text/javascript" <?php echo nonce(); ?>>
addEventListener('load', function () {
// Prevent infinite reload loop when auto login failed.
if (document.querySelector('.error')) {
return
}
let dbimage = '<?php echo $_ENV["DDEV_DBIMAGE"] ?>'
// Default driver is mysql ("server"). We don't want to set it if that's the case.dbimage
// The only other currently supported driver is pgsql
if (!dbimage.includes('mariadb') && !dbimage.includes('mysql')) {
document.querySelector('[name="auth[driver]"]').value = "pgsql"
}
document.querySelector('[name="auth[db]"]').value = '<?php echo $_ENV['DDEV_DB_NAME']; ?>'
document.querySelector('[name="auth[username]"]').value = '<?php echo $_ENV['DDEV_DB_USER']; ?>'
document.querySelector('[name="auth[password]"]').value = '<?php echo $_ENV['DDEV_DB_PASS']; ?>'
document.querySelector('[name="auth[permanent]"]:not(:checked)')?.click()
document.querySelector('[type=submit][value=Login]').click()
});
</script>
<?php
}
}
return new DDEVLogin();
76 changes: 76 additions & 0 deletions commands/host/launcha
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/bin/bash

#ddev-generated
## Description: Launch a browser with the current site
## Usage: launcha [path] [-a|--adminer] [-p|--phpmyadmin] [-m|--mailhog]
## Example: "ddev launcha" or "ddev launcha /admin/reports/status/php" or "ddev launcha phpinfo.php", for Adminer "ddev launcha -a", MailHog "ddev launch -m"

FULLURL=${DDEV_PRIMARY_URL}
HTTPS=""
if [ ${DDEV_PRIMARY_URL%://*} = "https" ]; then HTTPS=true; fi

while :; do
case ${1:-} in
-h|-\?|--help)
show_help
exit
;;
-a|--adminer)
if [ "${HTTPS}" = "" ]; then
FULLURL="${FULLURL%:[0-9]*}:9100"
else
FULLURL="${FULLURL%:[0-9]*}:9101"
fi
;;
-p|--phpmyadmin)
if [ "${HTTPS}" = "" ]; then
FULLURL="${FULLURL%:[0-9]*}:${DDEV_PHPMYADMIN_PORT}"
else
FULLURL="${FULLURL%:[0-9]*}:${DDEV_PHPMYADMIN_HTTPS_PORT}"
fi
;;
-m|--mailhog)
if [ "${HTTPS}" = "" ]; then
FULLURL="${FULLURL%:[0-9]*}:${DDEV_MAILHOG_PORT}"
else
FULLURL="${FULLURL%:[0-9]*}:${DDEV_MAILHOG_HTTPS_PORT}"
fi
;;

--) # End of all options.
shift
break
;;
-?*)
printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2
;;
*) # Default case: No more options, so break out of the loop.
break
esac

shift
done

if [ -n "${1:-}" ] ; then
if [[ ${1::1} != "/" ]] ; then
FULLURL="${FULLURL}/";
fi

FULLURL="${FULLURL}${1}";
fi

if [ ! -z ${DDEV_DEBUG:-} ]; then
printf "FULLURL $FULLURL\n" && exit 0
fi

case $OSTYPE in
linux-gnu)
xdg-open ${FULLURL}
;;
"darwin"*)
open ${FULLURL}
;;
"win*"* | "msys"*)
start ${FULLURL}
;;
esac
19 changes: 12 additions & 7 deletions docker-compose.adminer.yaml
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
#ddev-generated
version: '3.6'
services:
adminer:
container_name: ddev-${DDEV_SITENAME}-adminer
image: adminer:standalone
build:
context: './adminer'
environment:
# Use the line below to change the adminer theme.
# - ADMINER_DESIGN=nette
- ADMINER_DEFAULT_SERVER=ddev-${DDEV_SITENAME}-db
- ADMINER_DEFAULT_SERVER=db
- ADMINER_PLUGINS=tables-filter
- VIRTUAL_HOST=$DDEV_HOSTNAME
- HTTP_EXPOSE=8080:8080
- HTTP_EXPOSE=9100:8080
- HTTPS_EXPOSE=9101:8080
- DDEV_DBIMAGE
- DDEV_DB_NAME=db
- DDEV_DB_USER=db
- DDEV_DB_PASS=db
labels:
com.ddev.site-name: ${DDEV_SITENAME}
com.ddev.approot: $DDEV_APPROOT
depends_on:
- db

# We do not ship any plugin configuration at the moment,
# but keep this here for future reference.
#volumes:
# - "./adminer:/var/www/html/plugins-enabled"
web:
links:
- adminer:adminer
6 changes: 5 additions & 1 deletion install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ name: adminer

# list of files and directories listed that are copied into project .ddev directory
project_files:
- docker-compose.adminer.yaml
- docker-compose.adminer.yaml
- adminer

global_files:
- commands/host/launcha
17 changes: 9 additions & 8 deletions tests/test.bats
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
setup() {
mkcert -install >/dev/null
export DIR="$( cd "$( dirname "$BATS_TEST_FILENAME" )" >/dev/null 2>&1 && pwd )/.."
export TESTDIR=$(mktemp -d -t testadminer-XXXXXXXXXX)
export PROJNAME=testadminer
export DDEV_NON_INTERACTIVE=true
ddev delete -Oy ${PROJNAME} || true
ddev delete -Oy ${PROJNAME} >/dev/null || true
cd "${TESTDIR}"
ddev config --project-name=${PROJNAME}
echo ${PROJNAME}
ddev start
ddev start -y >/dev/null
}

teardown() {
Expand All @@ -17,12 +18,12 @@ teardown() {
}

@test "basic installation" {
cd ${TESTDIR} || ( printf "unable to cd to ${TESTDIR}\n" && exit 1 )
set -o pipefail
cd ${TESTDIR} || ( printf "# unable to cd to ${TESTDIR}\n" >&3 && false )
echo "# ddev get ${DIR} with project ${PROJNAME} in ${TESTDIR} ($(pwd))" >&3
ddev get ${DIR}
ddev restart
# This would be a clearer test, but doesn't work
# status=$(ddev describe | grep adminer | head -1 | awk '{print $4}')
# [[ "${status}" = "OK" ]]
ddev describe | grep adminer | head -1 | awk '{print $4}'
(ddev restart >/dev/null || (echo "# ddev restart returned exit code=%?" >&3 && false))
ddev help launcha | grep adminer >/dev/null
# echo "# Trying curl -s -L -k https://${PROJNAME}.ddev.site:9101/" >&3
curl --fail -s -L -k https://${PROJNAME}.ddev.site:9101/ | grep 'document.querySelector.*auth.*db' >/dev/null
}

0 comments on commit 2e0810d

Please sign in to comment.