Skip to content
This repository has been archived by the owner on Oct 19, 2023. It is now read-only.

Commit

Permalink
Added runtime_config to env var mapping (#308)
Browse files Browse the repository at this point in the history
* Added runtime_config to env var mapping

* Added tests for runtime_config values

* Throw an exception when keys conflict between env_variables and
runtime_config.
  • Loading branch information
Takashi Matsuo authored Jun 8, 2017
1 parent 4990b99 commit 8309924
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 475 deletions.
423 changes: 0 additions & 423 deletions builder/gen-dockerfile/composer.lock

This file was deleted.

53 changes: 38 additions & 15 deletions builder/gen-dockerfile/src/Builder/GenFilesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,22 +138,45 @@ protected function envsFromRuntimeConfig()
if (!is_array($this->appYaml) || !array_key_exists('runtime_config', $this->appYaml)) {
return $ret;
}
$runtime_config = $this->appYaml['runtime_config'];

// TODO: Generalize this process
if (array_key_exists('whitelist_functions', $runtime_config)) {
$ret['WHITELIST_FUNCTIONS'] =
$runtime_config['whitelist_functions'];
}
if (array_key_exists('front_controller_file', $runtime_config)
&& !empty($runtime_config['front_controller_file'])) {
$ret['FRONT_CONTROLLER_FILE'] =
$runtime_config['front_controller_file'];
$runtimeConfig = $this->appYaml['runtime_config'];
$envVariables = array_key_exists('env_variables', $this->appYaml)
? $this->appYaml['env_variables']
: [];
$maps = [
'document_root' => 'DOCUMENT_ROOT',
'whitelist_functions' => 'WHITELIST_FUNCTIONS',
'front_controller_file' => 'FRONT_CONTROLLER_FILE',
'nginx_conf_http_include' => 'NGINX_CONF_HTTP_INCLUDE',
'nginx_conf_include' => 'NGINX_CONF_INCLUDE',
'nginx_conf_override' => 'NGINX_CONF_OVERRIDE',
'php_fpm_conf_override' => 'PHP_FPM_CONF_OVERRIDE',
'php_ini_override' => 'PHP_INI_OVERRIDE',
'supervisord_conf_addition' => 'SUPERVISORD_CONF_ADDITION',
'supervisord_conf_override' => 'SUPERVISORD_CONF_OVERRIDE'
];
$errorKeys = [];
foreach ($maps as $k => $v) {
if (array_key_exists($k, $runtimeConfig)
&& !empty($runtimeConfig[$k])) {
// Fail if we find the corresponding values in env_variables.
if (array_key_exists($v, $envVariables)) {
$errorKeys[] = $v;
}
if ($v === 'DOCUMENT_ROOT') {
// Add ${APP_DIR} for making it a full path.
$ret[$v] = self::APP_DIR . '/' . $runtimeConfig[$k];
} else {
$ret[$v] = $runtimeConfig[$k];
}
}
}
if (array_key_exists('document_root', $runtime_config)
&& !empty($runtime_config['document_root'])) {
$ret['DOCUMENT_ROOT'] =
self::APP_DIR . '/' . $runtime_config['document_root'];
if (count($errorKeys) > 0) {
throw new \RuntimeException(
"There are values defined on both 'env_variables' and "
. "'runtime_config'. Remove the following keys in "
. "'env_variables': "
. implode(" ", $errorKeys)
);
}
return $ret;
}
Expand Down
55 changes: 36 additions & 19 deletions builder/gen-dockerfile/tests/GenFilesCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public function testGenFilesCommand(
$expectedDocRoot,
$expectedDockerIgnore,
$expectedFrom,
$otherExpectations = []
$otherExpectations = [],
$expectedException = null
) {
if ($baseImages === null) {
$baseImages =
Expand All @@ -79,6 +80,9 @@ public function testGenFilesCommand(
'--php71-image' => 'gcr.io/google-appengine/php71:latest',
];
}
if ($expectedException !== null) {
$this->setExpectedExceptionRegExp($expectedException);
}
// Copy all the files to the test dir
foreach (self::$files as $file) {
if (file_exists($dir . '/' . $file)) {
Expand Down Expand Up @@ -162,34 +166,45 @@ public function dataProvider()
]
],
[
// whitelist_functions
__DIR__ . '/test_data/whitelist_functions',
null,
'',
'/app',
'added by the php runtime builder',
'gcr.io/google-appengine/php71:latest',
["WHITELIST_FUNCTIONS=exec \\\n"]
],
[
// whitelist_functions on env_variables
__DIR__ . '/test_data/whitelist_functions_on_env',
// values on env_variables
__DIR__ . '/test_data/values_only_on_env',
null,
'',
'/app',
'added by the php runtime builder',
'gcr.io/google-appengine/php71:latest',
["WHITELIST_FUNCTIONS=exec \\\n"]
[
"WHITELIST_FUNCTIONS=exec \\\n",
"FRONT_CONTROLLER_FILE=app.php",
"NGINX_CONF_HTTP_INCLUDE=files/nginx-http.conf",
"NGINX_CONF_INCLUDE=files/nginx-app.conf",
"NGINX_CONF_OVERRIDE=files/nginx.conf",
"PHP_FPM_CONF_OVERRIDE=files/php-fpm.conf",
"PHP_INI_OVERRIDE=files/php.ini",
"SUPERVISORD_CONF_ADDITION=files/additional-supervisord.conf",
"SUPERVISORD_CONF_OVERRIDE=files/supervisord.conf"
]
],
[
// whitelist_functions runtime_config wins
__DIR__ . '/test_data/whitelist_functions_on_both',
// Values in both places will throw an exception.
__DIR__ . '/test_data/values_on_both',
null,
'',
'/app',
'added by the php runtime builder',
'gcr.io/google-appengine/php71:latest',
["WHITELIST_FUNCTIONS=exec \\\n"]
[
"WHITELIST_FUNCTIONS=exec \\\n",
"FRONT_CONTROLLER_FILE=app.php",
"NGINX_CONF_HTTP_INCLUDE=files/nginx-http.conf",
"NGINX_CONF_INCLUDE=files/nginx-app.conf",
"NGINX_CONF_OVERRIDE=files/nginx.conf",
"PHP_FPM_CONF_OVERRIDE=files/php-fpm.conf",
"PHP_INI_OVERRIDE=files/php.ini",
"SUPERVISORD_CONF_ADDITION=files/additional-supervisord.conf",
"SUPERVISORD_CONF_OVERRIDE=files/supervisord.conf"
],
'\\RuntimeException'
],
[
// front_controller_file
Expand Down Expand Up @@ -242,13 +257,15 @@ public function dataProvider()
'gcr.io/google-appengine/php71:latest'
],
[
// document_root in runtime_config wins
// document_root in both will throw exception
__DIR__ . '/test_data/docroot_on_both',
null,
'',
'/app/web',
'added by the php runtime builder',
'gcr.io/google-appengine/php71:latest'
'gcr.io/google-appengine/php71:latest',
[],
'\\RuntimeException'
],
[
// Has files already
Expand Down
24 changes: 24 additions & 0 deletions builder/gen-dockerfile/tests/test_data/values_on_both/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
env: flex
runtime: php

runtime_config:
whitelist_functions: exec
front_controller_file: app.php
nginx_conf_http_include: files/nginx-http.conf
nginx_conf_include: files/nginx-app.conf
nginx_conf_override: files/nginx.conf
php_fpm_conf_override: files/php-fpm.conf
php_ini_override: files/php.ini
supervisord_conf_addition: files/additional-supervisord.conf
supervisord_conf_override: files/supervisord.conf

env_variables:
WHITELIST_FUNCTIONS: none
FRONT_CONTROLLER_FILE: bogus
NGINX_CONF_HTTP_INCLUDE: bogus
NGINX_CONF_INCLUDE: bogus
NGINX_CONF_OVERRIDE: bogus
PHP_FPM_CONF_OVERRIDE: bogus
PHP_INI_OVERRIDE: bogus
SUPERVISORD_CONF_ADDITION: bogus
SUPERVISORD_CONF_OVERRIDE: bogus
13 changes: 13 additions & 0 deletions builder/gen-dockerfile/tests/test_data/values_only_on_env/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
env: flex
runtime: php

env_variables:
WHITELIST_FUNCTIONS: exec
FRONT_CONTROLLER_FILE: app.php
NGINX_CONF_HTTP_INCLUDE: files/nginx-http.conf
NGINX_CONF_INCLUDE: files/nginx-app.conf
NGINX_CONF_OVERRIDE: files/nginx.conf
PHP_FPM_CONF_OVERRIDE: files/php-fpm.conf
PHP_INI_OVERRIDE: files/php.ini
SUPERVISORD_CONF_ADDITION: files/additional-supervisord.conf
SUPERVISORD_CONF_OVERRIDE: files/supervisord.conf

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 8309924

Please sign in to comment.