Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e004904

Browse files
author
Doug Greiman
committed
Allow $GAE_APPLICATION_YAML_PATH as an alternative to --config
In some cases, gcloud sets an environment variable to indicate the location of the application configuration file, rather than using the --config flag.
1 parent 7584a14 commit e004904

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

scripts/gen_dockerfile.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
'3.6': '3.6',
5858
}
5959

60+
# Name of environment variable potentially set by gcloud
61+
GAE_APPLICATION_YAML_PATH = 'GAE_APPLICATION_YAML_PATH'
6062

6163
# Validated application configuration
6264
AppConfig = collections.namedtuple(
@@ -225,11 +227,19 @@ def parse_args(argv):
225227
validation_utils.validate_arg_regex, flag_regex=IMAGE_REGEX),
226228
default='gcr.io/google-appengine/python:latest',
227229
help='Name of Docker image to use as base')
230+
# In some cases, gcloud sets an environment variable to indicate
231+
# the location of the application configuration file, rather than
232+
# using the --config flag. The order of precedence from highest
233+
# to lowest is:
234+
#
235+
# 1) --config flag
236+
# 2) $GAE_APPLICATION_YAML_PATH environment variable
237+
# 3) a file named "app.yaml" in the current working directory
228238
parser.add_argument(
229239
'--config',
230240
type=functools.partial(
231241
validation_utils.validate_arg_regex, flag_regex=PRINTABLE_REGEX),
232-
default='app.yaml',
242+
default=(os.environ.get(GAE_APPLICATION_YAML_PATH) or 'app.yaml'),
233243
help='Path to application configuration file'
234244
)
235245
parser.add_argument(

scripts/gen_dockerfile_test.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,5 +225,26 @@ def mock_error(*args):
225225
gen_dockerfile.parse_args(argv)
226226

227227

228+
@pytest.mark.parametrize('argv, env, expected', [
229+
# Explicit flag wins
230+
(['argv0', '--config=flag/path'], 'env/path', 'flag/path'),
231+
(['argv0', '--config=flag/path'], '', 'flag/path'),
232+
(['argv0', '--config=flag/path'], None, 'flag/path'),
233+
# Otherwise env var wins
234+
(['argv0'], 'env/path', 'env/path'),
235+
# Otherwise use default name
236+
(['argv0'], '', 'app.yaml'),
237+
(['argv0'], None, 'app.yaml'),
238+
])
239+
def test_parse_args_config(argv, env, expected):
240+
if env is None:
241+
mock_environ = {}
242+
else:
243+
mock_environ = {gen_dockerfile.GAE_APPLICATION_YAML_PATH: env}
244+
with unittest.mock.patch.dict('os.environ', mock_environ, clear=True):
245+
args = gen_dockerfile.parse_args(argv)
246+
assert args.config == expected
247+
248+
228249
if __name__ == '__main__':
229250
pytest.main([__file__])

0 commit comments

Comments
 (0)