class TemporaryServerlessConfig(object): def __init__( self, archive_path, deployment_name, region, stage, functions, provider_name, _cleanup=True, ): self.archive_path = archive_path self.temp_directory = TempDirectory() self.deployment_name = deployment_name self.region = region self.stage = stage self.functions = functions self.provider_name = provider_name self._cleanup = _cleanup self.path = None def __enter__(self): self.generate() return self.path def __exit__(self, exc_type, exc_val, exc_tb): if self._cleanup: self.cleanup() def generate(self): install_serverless_package() serverless_config = { "service": self.deployment_name, "provider": { "region": self.region, "stage": self.stage, "name": self.provider_name, }, "functions": self.functions, } # if self.platform == "google-python": # serverless_config["provider"]["name"] = "google" # for api in apis: # serverless_config["functions"][api.name] = { # "handler": api.name, # "events": [{"http": "path"}], # } yaml = YAML() self.temp_directory.create() tempdir = self.temp_directory.path saved_path = os.path.join(tempdir, "serverless.yml") yaml.dump(serverless_config, Path(saved_path)) self.path = tempdir def cleanup(self): self.temp_directory.cleanup() self.path = None
class TemporaryServerlessContent(object): def __init__(self, archive_path, deployment_name, bento_name, template_type, _cleanup=True): self.archive_path = archive_path self.deployment_name = deployment_name self.bento_name = bento_name self.temp_directory = TempDirectory() self.template_type = template_type self._cleanup = _cleanup self.path = None def __enter__(self): self.generate() return self.path def __exit__(self, exc_type, exc_val, exc_tb): if self._cleanup: self.cleanup() def generate(self): self.temp_directory.create() tempdir = self.temp_directory.path call_serverless_command( [ "serverless", "create", "--template", self.template_type, "--name", self.deployment_name, ], tempdir, ) shutil.copy(os.path.join(self.archive_path, "requirements.txt"), tempdir) model_serivce_archive_path = os.path.join(tempdir, self.bento_name) model_path = os.path.join(self.archive_path, self.bento_name) shutil.copytree(model_path, model_serivce_archive_path) self.path = tempdir def cleanup(self): self.temp_directory.cleanup() self.path = None
class TemporarySageMakerContent(object): def __init__(self, archive_path, bento_name, bento_version, _cleanup=True): self.archive_path = archive_path self.bento_name = bento_name self.bento_version = bento_version self.temp_directory = TempDirectory() self._cleanup = _cleanup self.path = None def __enter__(self): self.generate() return self.path def generate(self): self.temp_directory.create() tempdir = self.temp_directory.path saved_path = os.path.join(tempdir, self.bento_name, self.bento_version) shutil.copytree(self.archive_path, saved_path) with open(os.path.join(saved_path, "nginx.conf"), "w") as f: f.write(DEFAULT_NGINX_CONFIG) with open(os.path.join(saved_path, "wsgi.py"), "w") as f: f.write(DEFAULT_WSGI_PY) with open(os.path.join(saved_path, "serve"), "w") as f: f.write(DEFAULT_SERVE_SCRIPT) # permission 755 is required for entry script 'serve' permission = "755" octal_permission = int(permission, 8) os.chmod(os.path.join(saved_path, "serve"), octal_permission) self.path = saved_path def cleanup(self): self.temp_directory.cleanup() self.path = None def __exit__(self, exc_type, exc_val, exc_tb): if self._cleanup: self.cleanup()
class TemporaryServerlessContent(object): def __init__(self, archive_path, deployment_name, bento_name, template_type, _cleanup=True): self.archive_path = archive_path self.deployment_name = deployment_name self.bento_name = bento_name self.temp_directory = TempDirectory() self.template_type = template_type self._cleanup = _cleanup self.path = None def __enter__(self): self.generate() return self.path def __exit__(self, exc_type, exc_val, exc_tb): if self._cleanup: self.cleanup() def generate(self): install_serverless_package() self.temp_directory.create() tempdir = self.temp_directory.path call_serverless_command( [ "create", "--template", self.template_type, "--name", self.deployment_name, ], tempdir, ) requirement_txt_path = os.path.join(self.archive_path, 'requirements.txt') shutil.copy(requirement_txt_path, tempdir) bento_archive_path = os.path.join(tempdir, self.bento_name) model_path = os.path.join(self.archive_path, self.bento_name) shutil.copytree(model_path, bento_archive_path) bundled_dependencies_path = os.path.join(self.archive_path, 'bundled_pip_dependencies') # If bundled_pip_dependencies directory exists, we copy over and update # requirements.txt. We need to remove the bentoml entry in the file, because # when pip install, it will NOT override the pypi released version. if os.path.isdir(bundled_dependencies_path): dest_bundle_path = os.path.join(tempdir, 'bundled_pip_dependencies') shutil.copytree(bundled_dependencies_path, dest_bundle_path) bundled_files = os.listdir(dest_bundle_path) has_bentoml_bundle = False for index, bundled_file_name in enumerate(bundled_files): bundled_files[ index] = '\n./bundled_pip_dependencies/{}'.format( bundled_file_name) # If file name start with `BentoML-`, assuming it is a # bentoml targz bundle if bundled_file_name.startswith('BentoML-'): has_bentoml_bundle = True with open(os.path.join(tempdir, 'requirements.txt'), 'r+') as requirement_file: required_modules = requirement_file.readlines() if has_bentoml_bundle: # Assuming bentoml is always the first one in # requirements.txt. We are removing it required_modules = required_modules[1:] required_modules = required_modules + bundled_files # Write from beginning of the file, instead of appending to # the end. requirement_file.seek(0) requirement_file.writelines(required_modules) self.path = tempdir def cleanup(self): self.temp_directory.cleanup() self.path = None