def GenerateConfigs(self): """Generate all config files for the module.""" # Write "Saving file" messages to the user or to log depending on whether # we're in "deploy." if self.params.deploy: notify = log.info else: notify = log.status.Print # Generate app.yaml. cleaner = fingerprinting.Cleaner() if not self.params.appinfo: app_yaml = os.path.join(self.root, 'app.yaml') if not os.path.exists(app_yaml): notify('Saving [app.yaml] to [%s].' % self.root) runtime = 'custom' if self.params.custom else 'python' with open(app_yaml, 'w') as f: f.write( PYTHON_APP_YAML.format(entrypoint=self.entrypoint, runtime=runtime)) if self.params.custom or self.params.deploy: dockerfile = os.path.join(self.root, config.DOCKERFILE) if not os.path.exists(dockerfile): notify('Saving [%s] to [%s].' % (config.DOCKERFILE, self.root)) # Customize the dockerfile. with open(dockerfile, 'w') as out: out.write(DOCKERFILE_PREAMBLE) out.write( DOCKERFILE_VIRTUALENV_TEMPLATE.format( python_version='3.4' if self.use_python_3 else '')) if self.got_requirements_txt: out.write(DOCKERFILE_REQUIREMENTS_TXT) out.write(DOCKERFILE_INSTALL_APP) # Generate the appropriate start command. if self.entrypoint: out.write('CMD %s\n' % self.entrypoint) cleaner.Add(dockerfile) # Generate .dockerignore TODO(user): eventually this file will just be # copied verbatim. dockerignore = os.path.join(self.root, '.dockerignore') if not os.path.exists(dockerignore): notify('Saving [.dockerignore] to [%s].' % self.root) with open(dockerignore, 'w') as f: f.write(DOCKERIGNORE) cleaner.Add(dockerignore) if not cleaner.HasFiles(): notify('All config files already exist, not generating anything.') return cleaner
def GenerateConfigs(self): """Generate all config files for the module.""" # Write "Saving file" messages to the user or to log depending on whether # we're in "deploy." if self.params.deploy: notify = log.info else: notify = log.status.Print # Generate app.yaml. cleaner = fingerprinting.Cleaner() if not self.params.appinfo: app_yaml = os.path.join(self.root, 'app.yaml') if not os.path.exists(app_yaml): notify('Saving [app.yaml] to [%s].' % self.root) runtime = 'custom' if self.params.custom else 'python27' with open(app_yaml, 'w') as f: f.write(PYTHON_APP_YAML.format(runtime=runtime)) cleaner.Add(app_yaml) if self.params.custom or self.params.deploy: dockerfile = os.path.join(self.root, config.DOCKERFILE) if not os.path.exists(dockerfile): notify('Saving [%s] to [%s].' % (config.DOCKERFILE, self.root)) # Customize the dockerfile. with open(dockerfile, 'w') as out: out.write(DOCKERFILE_PREAMBLE) out.write(DOCKERFILE_INSTALL_APP) cleaner.Add(dockerfile) dockerignore = os.path.join(self.root, '.dockerignore') if not os.path.exists(dockerignore): notify('Saving [.dockerignore] to [%s].' % self.root) with open(dockerignore, 'w') as f: f.write(DOCKERIGNORE) cleaner.Add(dockerignore) if not cleaner.HasFiles(): notify('All config files already exist, not generating anything.') return cleaner
def GenerateConfigs(self): """Generates all config files for the module. Returns: (fingerprinting.Cleaner) A cleaner populated with the generated files """ self.notify('') cleaner = fingerprinting.Cleaner() if not self.params.appinfo: self._GenerateAppYaml(cleaner) if self.params.custom or self.params.deploy: self._GenerateDockerfile(cleaner) self._GenerateDockerignore(cleaner) if not cleaner.HasFiles(): self.notify('All config files already exist. No files generated.') return cleaner
def GenerateConfigs(self): """Generate all config files for the module. Returns: (callable()) fingerprinting.Cleaner instance. """ # Write the message to the user or to the log depending on whether # we're in "deploy" or not. if self.params.deploy: notify = log.info else: notify = log.status.Print cleaner = fingerprinting.Cleaner() generated_app_yaml = False # Generate app.yaml. if not self.params.appinfo: app_yaml = os.path.join(self.application.path, 'app.yaml') if not os.path.exists(app_yaml): notify('Saving [app.yaml] to [%s].' % self.application.path) runtime = 'custom' if self.params.custom else NAME with open(app_yaml, 'w') as f: f.write(ASPNET_APP_YAML.format(runtime=runtime)) generated_app_yaml = True # Generate the necessary files to create the Docker image that will # ultimately run the app. If we're in "deploy" mode these files will be be # created and then deleted after the deployment is done. If created with the # --custom flag these files will be created and then left for the user to # customize the app's image. if self.params.custom or self.params.deploy: # Generate dockefile. dockerfile = os.path.join(self.application.path, config.DOCKERFILE) if not os.path.exists(dockerfile): notify('Saving [%s] to [%s].' % (config.DOCKERFILE, self.application.path)) with open(dockerfile, 'w') as f: f.write(DOCKERFILE) cleaner.Add(dockerfile) # Generate .dockerignore. dockerignore = os.path.join(self.application.path, '.dockerignore') if not os.path.exists(dockerignore): notify('Saving [.dockerignore] to [%s].' % self.application.path) with open(dockerignore, 'w') as f: f.write(DOCKERIGNORE) cleaner.Add(dockerignore) # Generate the entry point if not already present in the target. app_entry_point_path = os.path.join(self.application.path, APP_ENTRY_POINT_NAME) if not os.path.exists(app_entry_point_path): notify('Creating [%s] to [%s].' % (APP_ENTRY_POINT_NAME, self.application.path)) with open(app_entry_point_path, 'w') as f: f.write(self.application.GenerateEntryPoint()) cleaner.Add(app_entry_point_path) if not generated_app_yaml and not cleaner.HasFiles(): notify('All config files already exist, not generating anything.') return cleaner
def GenerateConfigs(self, configurator): """Do config generation on the runtime. This should generally be called from the configurator's GenerateConfigs() method. Args: configurator: (ExternalRuntimeConfigurator) The configurator retuned by Detect(). Returns: (fingerprinting.Cleaner) The cleaner for the generated files. Raises: InvalidRuntimeDefinition: For a variety of problems with the runtime definition. """ # Log or print status messages depending on whether we're in gen-config or # deploy. notify = log.info if configurator.params.deploy else log.status.Print generate_configs = self.config.get('generateConfigs') if generate_configs: cleaner = fingerprinting.Cleaner() files_to_copy = generate_configs.get('filesToCopy') if files_to_copy: # Make sure there's nothing else. if len(generate_configs) != 1: raise InvalidRuntimeDefinition( 'If "files_to_copy" is specified, ' 'it must be the only field in ' 'generate_configs.') for filename in files_to_copy: full_name = _NormalizePath(self.root, filename) if not os.path.isfile(full_name): raise InvalidRuntimeDefinition( 'File [%s] specified in ' 'files_to_copy, but is not in ' 'the runtime definition.' % filename) dest_path = _NormalizePath(configurator.path, filename) if not os.path.exists(dest_path): notify( WRITING_FILE_MESSAGE.format( filename, configurator.path)) cleaner.Add(dest_path) shutil.copy(full_name, dest_path) else: notify(FILE_EXISTS_MESSAGE.format(filename)) else: result = self.RunPlugin('generate_configs', generate_configs, configurator.params, runtime_data=configurator.data) for file_info in result.files: dest_filename = file_info.WriteTo(configurator.path, notify) # Cleanup everything except app.yaml files - these are never # temporary. if dest_filename and file_info.filename != 'app.yaml': cleaner.Add(dest_filename) if not cleaner.HasFiles(): notify( 'All config files already exist, not generating anything.') return cleaner else: raise InvalidRuntimeDefinition('Runtime definition contains no ' 'generate_configs section.')