108108)
109109
110110
111- def get_app_config (raw_config , args ):
111+ def get_app_config (raw_config , base_image , config_file , source_dir ):
112112 """Read and validate the application runtime configuration.
113113
114114 Args:
115115 raw_config (dict): deserialized app.yaml
116- args (argparse.Namespace): command line flags
116+ base_image (str): Docker image name to build on top of
117+ config_file (str): Path to user's app.yaml (might be <service-name>.yaml)
118+ source_dir (str): Directory container user's source code
117119
118120 Returns:
119121 AppConfig: valid configuration
@@ -122,7 +124,7 @@ def get_app_config(raw_config, args):
122124 if not isinstance (raw_config , dict ):
123125 raise ValueError (
124126 'Expected {} contents to be of type "dict", but found type "{}"' .
125- format (args . config , type (raw_config )))
127+ format (config_file , type (raw_config )))
126128
127129 entrypoint = validation_utils .get_field_value (raw_config , 'entrypoint' , str )
128130 if not PRINTABLE_REGEX .match (entrypoint ):
@@ -139,10 +141,7 @@ def get_app_config(raw_config, args):
139141
140142 # Examine user's files
141143 has_requirements_txt = os .path .isfile (
142- os .path .join (args .source_dir , 'requirements.txt' ))
143-
144- # Compute base image name and Dockerfile contents
145- base_image = args .base_image
144+ os .path .join (source_dir , 'requirements.txt' ))
146145
147146 return AppConfig (
148147 base_image = base_image ,
@@ -191,46 +190,49 @@ def generate_files(app_config):
191190 }
192191
193192
194- def gen_dockerfile (args ):
193+ def gen_dockerfile (base_image , config_file , source_dir ):
195194 """Write a Dockerfile and helper files for an application.
196195
197196 Args:
198- args (argparse.Namespace): command line flags
197+ base_image (str): Docker image name to build on top of
198+ config_file (str): Path to user's app.yaml (might be <service-name>.yaml)
199+ source_dir (str): Directory container user's source code
199200 """
200201 # Read yaml file. Does not currently support multiple services
201202 # with configuration filenames besides app.yaml
202- with io .open (args . config , 'r' , encoding = 'utf8' ) as yaml_config_file :
203+ with io .open (config_file , 'r' , encoding = 'utf8' ) as yaml_config_file :
203204 raw_config = yaml .load (yaml_config_file )
204205
205- # Determine configuration
206- app_config = get_app_config (raw_config , args )
206+ # Determine complete configuration
207+ app_config = get_app_config (raw_config , base_image , config_file ,
208+ source_dir )
207209
208210 # Generate list of filenames and their textual contents
209211 files = generate_files (app_config )
210212
211213 # Write files
212214 for filename , contents in files .items ():
213- full_filename = os .path .join (args . source_dir , filename )
215+ full_filename = os .path .join (source_dir , filename )
214216 with io .open (full_filename , 'w' , encoding = 'utf8' ) as outfile :
215217 outfile .write (contents )
216218
217219
218220def parse_args (argv ):
219221 """Parse and validate command line flags"""
220222 parser = argparse .ArgumentParser ()
223+ parser .add_argument (
224+ '--base-image' ,
225+ type = functools .partial (
226+ validation_utils .validate_arg_regex , flag_regex = IMAGE_REGEX ),
227+ default = 'gcr.io/google-appengine/python:latest' ,
228+ help = 'Name of Docker image to use as base' )
221229 parser .add_argument (
222230 '--config' ,
223231 type = functools .partial (
224232 validation_utils .validate_arg_regex , flag_regex = PRINTABLE_REGEX ),
225233 default = 'app.yaml' ,
226234 help = 'Path to application configuration file'
227235 )
228- parser .add_argument (
229- '--base-image' ,
230- type = functools .partial (
231- validation_utils .validate_arg_regex , flag_regex = IMAGE_REGEX ),
232- default = 'gcr.io/google-appengine/python:latest' ,
233- help = 'Name of Docker image to use as base' )
234236 parser .add_argument (
235237 '--source-dir' ,
236238 type = functools .partial (
@@ -243,7 +245,7 @@ def parse_args(argv):
243245
244246def main ():
245247 args = parse_args (sys .argv )
246- gen_dockerfile (args )
248+ gen_dockerfile (args . base_image , args . config , args . source_dir )
247249
248250
249251if __name__ == '__main__' :
0 commit comments