Skip to content

Commit

Permalink
Export documented environment variables into web container, fixes #2486
Browse files Browse the repository at this point in the history
… (#2488)
  • Loading branch information
rfay committed Sep 3, 2020
1 parent 6c04f39 commit ee09ab3
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 20 deletions.
1 change: 1 addition & 0 deletions docs/users/extend/custom-commands.md
Expand Up @@ -79,6 +79,7 @@ A number of environment variables are provided to the script. These are generall
* DDEV_HOST_WEBSERVER_PORT: Localhost port of the webserver
* DDEV_PHP_VERSION
* DDEV_PRIMARY_URL: Primary URL for the project
* DDEV_PROJECT: Project name, like "d8composer"
* DDEV_PROJECT_TYPE: drupal8, typo3, backdrop, wordpress, etc.
* DDEV_ROUTER_HTTP_PORT: Router port for http
* DDEV_ROUTER_HTTPS_PORT: Router port for https
Expand Down
1 change: 1 addition & 0 deletions pkg/ddevapp/ddevapp.go
Expand Up @@ -1313,6 +1313,7 @@ func (app *DdevApp) DockerEnv() {
"DDEV_TLD": app.ProjectTLD,
"DDEV_DBIMAGE": app.GetDBImage(),
"DDEV_DBAIMAGE": app.DBAImage,
"DDEV_PROJECT": app.Name,
"DDEV_WEBIMAGE": app.WebImage,
"DDEV_APPROOT": app.AppRoot,
"DDEV_HOST_DB_PORT": dbPortStr,
Expand Down
88 changes: 88 additions & 0 deletions pkg/ddevapp/ddevapp_test.go
Expand Up @@ -3221,6 +3221,94 @@ func TestDdevList(t *testing.T) {
ddevapp.List(true, false, 1)
}

// TestEnvironmentVariables tests to make sure that documented environment variables appear
// in the web container and on the host.
func TestEnvironmentVariables(t *testing.T) {
assert := asrt.New(t)
pwd, _ := os.Getwd()
customCmd := filepath.Join(pwd, "testdata", t.Name(), "showhostenvvar")
site := TestSites[0]
switchDir := site.Chdir()
defer switchDir()

app, err := ddevapp.NewApp(site.Dir, false, "")
assert.NoError(err)
customCmdDest := app.GetConfigPath("commands/host/" + "showhostenvvar")

err = os.MkdirAll(filepath.Dir(customCmdDest), 0755)
require.NoError(t, err)
err = fileutil.CopyFile(customCmd, customCmdDest)
require.NoError(t, err)

// This set of webContainerExpectations should be maintained to match the list in the docs
webContainerExpectations := map[string]string{
"DDEV_DOCROOT": app.GetDocroot(),
"DDEV_HOSTNAME": app.GetHostname(),
"DDEV_PHP_VERSION": app.PHPVersion,
"DDEV_PRIMARY_URL": app.GetPrimaryURL(),
"DDEV_PROJECT": app.Name,
"DDEV_PROJECT_TYPE": app.Type,
"DDEV_ROUTER_HTTP_PORT": app.RouterHTTPPort,
"DDEV_ROUTER_HTTPS_PORT": app.RouterHTTPSPort,
"DDEV_SITENAME": app.Name,
"DDEV_TLD": app.ProjectTLD,
"DDEV_WEBSERVER_TYPE": app.WebserverType,
}

err = app.Start()
require.NoError(t, err)
t.Cleanup(func() {
err = os.RemoveAll(customCmdDest)
assert.NoError(err)
err = app.Stop(true, false)
assert.NoError(err)
})

for k, v := range webContainerExpectations {
envVal, _, err := app.Exec(&ddevapp.ExecOpts{
Cmd: fmt.Sprintf("echo ${%s}", k),
})
assert.NoError(err)
envVal = strings.Trim(envVal, "\n")
assert.Equal(v, envVal)
}

dbPort, err := app.GetPublishedPort("db")
dbPortStr := strconv.Itoa(dbPort)
if dbPortStr == "-1" || err != nil {
dbPortStr = ""
}
if app.HostDBPort != "" {
dbPortStr = app.HostDBPort
}

// This set of hostExpections should bne maintained in parallel with documentation
hostExpectations := map[string]string{
"DDEV_APPROOT": app.AppRoot,
"DDEV_DOCROOT": app.GetDocroot(),
"DDEV_HOST_DB_PORT": dbPortStr,
"DDEV_HOST_HTTPS_PORT": app.HostHTTPSPort,
"DDEV_HOST_WEBSERVER_PORT": app.HostWebserverPort,
"DDEV_HOSTNAME": app.GetHostname(),
"DDEV_PHP_VERSION": app.PHPVersion,
"DDEV_PRIMARY_URL": app.GetPrimaryURL(),
"DDEV_PROJECT": app.Name,
"DDEV_PROJECT_TYPE": app.Type,
"DDEV_ROUTER_HTTP_PORT": app.RouterHTTPPort,
"DDEV_ROUTER_HTTPS_PORT": app.RouterHTTPSPort,
"DDEV_SITENAME": app.Name,
"DDEV_TLD": app.ProjectTLD,
"DDEV_WEBSERVER_TYPE": app.WebserverType,
}
for k, v := range hostExpectations {
envVal, err := exec.RunCommand(DdevBin, []string{"showhostenvvar", k})
assert.NoError(err, "could not run %s %s %s, result=%s", DdevBin, "showhostenvvar", k, envVal)
envVal = strings.Trim(envVal, "\n")
assert.Equal(v, envVal, "expected envvar $%s to equal '%s', but it was '%s'", k, v, envVal)
}

}

// constructContainerName builds a container name given the type (web/db/dba) and the app
func constructContainerName(containerType string, app *ddevapp.DdevApp) (string, error) {
container, err := app.FindContainerByType(containerType)
Expand Down
53 changes: 33 additions & 20 deletions pkg/ddevapp/templates.go
Expand Up @@ -39,10 +39,21 @@ services:
com.ddev.app-type: {{ .AppType }}
com.ddev.approot: $DDEV_APPROOT
environment:
- COLUMNS=$COLUMNS
- LINES=$LINES
- COLUMNS
- DDEV_HOSTNAME
- DDEV_PHP_VERSION
- DDEV_PRIMARY_URL
- DDEV_PROJECT
- DDEV_PROJECT_TYPE
- DDEV_ROUTER_HTTP_PORT
- DDEV_ROUTER_HTTPS_PORT
- DDEV_SITENAME
- DDEV_TLD
- DOCKER_IP={{ .DockerIP }}
- HOST_DOCKER_INTERNAL_IP={{ .HostDockerInternalIP }}
- IS_DDEV_PROJECT=true
- LINES
- TZ={{ .Timezone }}
- DDEV_PROJECT={{ .Name }}
command: "$DDEV_MARIADB_LOCAL_COMMAND"
healthcheck:
interval: 1s
Expand Down Expand Up @@ -95,33 +106,35 @@ services:
- "{{ .DockerIP }}:$DDEV_HOST_WEBSERVER_PORT:80"
- "{{ .DockerIP }}:$DDEV_HOST_HTTPS_PORT:443"
environment:
- DOCROOT=$DDEV_DOCROOT
- DDEV_PHP_VERSION=$DDEV_PHP_VERSION
- DDEV_WEBSERVER_TYPE=$DDEV_WEBSERVER_TYPE
- DDEV_PROJECT_TYPE=$DDEV_PROJECT_TYPE
- DDEV_ROUTER_HTTP_PORT=$DDEV_ROUTER_HTTP_PORT
- DDEV_ROUTER_HTTPS_PORT=$DDEV_ROUTER_HTTPS_PORT
- DDEV_XDEBUG_ENABLED=$DDEV_XDEBUG_ENABLED
- IS_DDEV_PROJECT=true
- COLUMNS
- DOCROOT=${DDEV_DOCROOT}
- DDEV_DOCROOT
- DDEV_HOSTNAME
- DDEV_PHP_VERSION
- DDEV_PRIMARY_URL
- DDEV_PROJECT
- DDEV_PROJECT_TYPE
- DDEV_ROUTER_HTTP_PORT
- DDEV_ROUTER_HTTPS_PORT
- DDEV_SITENAME
- DDEV_TLD
- DDEV_WEBSERVER_TYPE
- DDEV_XDEBUG_ENABLED
- DEPLOY_NAME=local
- DRUSH_OPTIONS_URI=$DDEV_PRIMARY_URL
- DOCKER_IP={{ .DockerIP }}
- HOST_DOCKER_INTERNAL_IP={{ .HostDockerInternalIP }}
- DEPLOY_NAME=local
- VIRTUAL_HOST=$DDEV_HOSTNAME
- COLUMNS=$COLUMNS
- LINES=$LINES
- TZ={{ .Timezone }}
# HTTP_EXPOSE allows for ports accepting HTTP traffic to be accessible from <site>.ddev.site:<port>
# To expose a container port to a different host port, define the port as hostPort:containerPort
- HTTP_EXPOSE=${DDEV_ROUTER_HTTP_PORT}:80,${DDEV_MAILHOG_PORT}:{{ .MailhogPort }}
# You can optionally expose an HTTPS port option for any ports defined in HTTP_EXPOSE.
# To expose an HTTPS port, define the port as securePort:containerPort.
- HTTPS_EXPOSE=${DDEV_ROUTER_HTTPS_PORT}:80,${DDEV_MAILHOG_HTTPS_PORT}:{{ .MailhogPort }}
- IS_DDEV_PROJECT=true
- LINES
- SSH_AUTH_SOCK=/home/.ssh-agent/socket
- DDEV_PROJECT={{ .Name }}
- DDEV_SITENAME
- DDEV_TLD
- DDEV_PRIMARY_URL
- TZ={{ .Timezone }}
- VIRTUAL_HOST=${DDEV_HOSTNAME}
labels:
com.ddev.site-name: ${DDEV_SITENAME}
com.ddev.platform: {{ .Plugin }}
Expand Down
8 changes: 8 additions & 0 deletions pkg/ddevapp/testdata/TestEnvironmentVariables/showhostenvvar
@@ -0,0 +1,8 @@
#!/bin/bash

## Description: showhostenvvar envname
## Usage: showhostenvvar [name]
## Example: "ddev showhostenvvar DDEV_PROJECT"

ENVNAME=$1
echo ${!ENVNAME}

0 comments on commit ee09ab3

Please sign in to comment.