Skip to content

Commit 881196a

Browse files
author
Doug Greiman
committed
Move file templates into separate files.
The templates were copied from the gae-xrt-python template files in the gcloud SDK, with the same names and contents, except for Dockerfile.preamble which no longer hardcodes the base image, and Dockerfile.entrypoint.template which is new.
1 parent 336e42a commit 881196a

9 files changed

+61
-63
lines changed

build.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ done
128128
for file in \
129129
gen_dockerfile.py \
130130
validation_utils.py \
131+
'data/*' \
131132
; do
132133
cp -a "scripts/${file}" "builder/gen-dockerfile/${file}"
133134
done
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CMD {entrypoint}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ADD . /app/
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
FROM {base_image}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ADD requirements.txt /app/
2+
RUN pip install -r requirements.txt
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
LABEL python_version=python{python_version}
2+
RUN virtualenv --no-download /env -p python{python_version}
3+
4+
# Set virtualenv environment variables. This is equivalent to running
5+
# source /env/bin/activate
6+
ENV VIRTUAL_ENV /env
7+
ENV PATH /env/bin:$PATH

scripts/data/dockerignore

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright 2015 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
.dockerignore
16+
Dockerfile
17+
.git
18+
.hg
19+
.svn

scripts/gen_dockerfile.py

Lines changed: 27 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -56,50 +56,6 @@
5656
'3.6': '3.6',
5757
}
5858

59-
# File templates.
60-
# Designed to exactly match the current output of 'gcloud app gen-config'
61-
DOCKERFILE_TEMPLATE = """\
62-
FROM {base_image}
63-
LABEL python_version=python{dockerfile_python_version}
64-
RUN virtualenv --no-download /env -p python{dockerfile_python_version}
65-
66-
# Set virtualenv environment variables. This is equivalent to running
67-
# source /env/bin/activate
68-
ENV VIRTUAL_ENV /env
69-
ENV PATH /env/bin:$PATH
70-
{optional_requirements_txt}ADD . /app/
71-
{optional_entrypoint}"""
72-
73-
DOCKERFILE_REQUIREMENTS_TXT = """\
74-
ADD requirements.txt /app/
75-
RUN pip install -r requirements.txt
76-
"""
77-
78-
DOCKERFILE_ENTRYPOINT_TEMPLATE = """\
79-
CMD {entrypoint}
80-
"""
81-
82-
DOCKERIGNORE = """\
83-
# Copyright 2015 Google Inc. All Rights Reserved.
84-
#
85-
# Licensed under the Apache License, Version 2.0 (the "License");
86-
# you may not use this file except in compliance with the License.
87-
# You may obtain a copy of the License at
88-
#
89-
# http://www.apache.org/licenses/LICENSE-2.0
90-
#
91-
# Unless required by applicable law or agreed to in writing, software
92-
# distributed under the License is distributed on an "AS IS" BASIS,
93-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
94-
# See the License for the specific language governing permissions and
95-
# limitations under the License.
96-
97-
.dockerignore
98-
Dockerfile
99-
.git
100-
.hg
101-
.svn
102-
"""
10359

10460
# Validated application configuration
10561
AppConfig = collections.namedtuple(
@@ -130,6 +86,14 @@ def get_app_config(raw_config, base_image, config_file, source_dir):
13086
if not PRINTABLE_REGEX.match(entrypoint):
13187
raise ValueError('Invalid character in "entrypoint" field of app.yaml')
13288

89+
# Mangle entrypoint in the same way as the Cloud SDK
90+
# (googlecloudsdk/third_party/appengine/api/validation.py)
91+
#
92+
# We could handle both string ("shell form") and list ("exec
93+
# form") but it appears that gcloud only handles string form.
94+
if entrypoint and not entrypoint.startswith('exec '):
95+
entrypoint = 'exec ' + entrypoint
96+
13397
raw_runtime_config = validation_utils.get_field_value(raw_config, 'runtime_config', dict)
13498
python_version = validation_utils.get_field_value(raw_runtime_config, 'python_version', str)
13599

@@ -152,6 +116,13 @@ def get_app_config(raw_config, base_image, config_file, source_dir):
152116
has_requirements_txt=has_requirements_txt)
153117

154118

119+
def get_data(name):
120+
"""Return the contents of the named data resource"""
121+
filename = os.path.join(os.path.dirname(__file__), 'data', name)
122+
with io.open(filename, 'r', encoding='utf8') as template_file:
123+
return template_file.read()
124+
125+
155126
def generate_files(app_config):
156127
"""Generate a Dockerfile and helper files for an application.
157128
@@ -162,33 +133,28 @@ def generate_files(app_config):
162133
dict: Map of filename to desired file contents
163134
"""
164135
if app_config.has_requirements_txt:
165-
optional_requirements_txt = DOCKERFILE_REQUIREMENTS_TXT
136+
optional_requirements_txt = get_data('Dockerfile.requirements_txt')
166137
else:
167138
optional_requirements_txt = ''
168139

169140
if app_config.entrypoint:
170-
# Mangle entrypoint in the same way as the Cloud SDK
171-
# (googlecloudsdk/third_party/appengine/api/validation.py)
172-
#
173-
# We could handle both string ("shell form") and list ("exec
174-
# form") but it appears that gcloud only handles string form.
175-
entrypoint = app_config.entrypoint
176-
if entrypoint and not entrypoint.startswith('exec '):
177-
entrypoint = 'exec ' + entrypoint
178-
optional_entrypoint = DOCKERFILE_ENTRYPOINT_TEMPLATE.format(
179-
entrypoint=entrypoint)
141+
optional_entrypoint = get_data('Dockerfile.entrypoint.template').format(
142+
entrypoint=app_config.entrypoint)
180143
else:
181144
optional_entrypoint = ''
182145

183-
dockerfile = DOCKERFILE_TEMPLATE.format(
184-
base_image=app_config.base_image,
185-
dockerfile_python_version=app_config.dockerfile_python_version,
186-
optional_requirements_txt=optional_requirements_txt,
187-
optional_entrypoint=optional_entrypoint)
146+
dockerfile = (
147+
get_data('Dockerfile.preamble.template').format(
148+
base_image=app_config.base_image) +
149+
get_data('Dockerfile.virtualenv.template').format(
150+
python_version=app_config.dockerfile_python_version) +
151+
optional_requirements_txt +
152+
get_data('Dockerfile.install_app') +
153+
optional_entrypoint)
188154

189155
return {
190156
'Dockerfile': dockerfile,
191-
'.dockerignore': DOCKERIGNORE,
157+
'.dockerignore': get_data('dockerignore'),
192158
}
193159

194160

scripts/gen_dockerfile_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def test_get_app_config(self):
8282
}),
8383
# entrypoint present
8484
('entrypoint: my entrypoint', False, {
85-
'entrypoint': 'my entrypoint',
85+
'entrypoint': 'exec my entrypoint',
8686
}),
8787
)
8888
for valid_case in valid_cases:
@@ -129,7 +129,7 @@ def test_generate_files(self):
129129
# Entrypoint
130130
(base, False, 'CMD'),
131131
(base._replace(entrypoint='my entrypoint'), True,
132-
'CMD exec my entrypoint'),
132+
'CMD my entrypoint'),
133133
(base._replace(entrypoint='exec my entrypoint'), True,
134134
'CMD exec my entrypoint'),
135135
# Base runtime image

0 commit comments

Comments
 (0)