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
10561AppConfig = 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+
155126def 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
0 commit comments