|
| 1 | +import inspect |
| 2 | +import subprocess |
| 3 | +from typing import Dict |
| 4 | + |
| 5 | +from piper.base.docker_compose.utils import ComposeServices |
| 6 | +from piper.configurations import get_configuration |
| 7 | +from piper.utils.logger_utils import logger |
| 8 | + |
| 9 | + |
| 10 | +def copy_piper(path: str): |
| 11 | + cfg = get_configuration() |
| 12 | + from distutils.dir_util import copy_tree |
| 13 | + copy_tree(cfg.piper_path, f"{path}/piper") |
| 14 | + |
| 15 | + |
| 16 | +def copy_scripts(path: str, scripts: Dict[str, str]): |
| 17 | + for script_name, script_path in scripts.items(): |
| 18 | + with open(f"{path}/{script_name}.py", "w") as output: |
| 19 | + with open(script_path, "r") as current_file: |
| 20 | + output.write(current_file.read()) |
| 21 | + |
| 22 | + |
| 23 | +def write_requirements(path, requirements): |
| 24 | + with open(f"{path}/requirements.txt", "w") as output: |
| 25 | + output.write("\n".join(requirements)) |
| 26 | + |
| 27 | + |
| 28 | +class ComposeExecutor: |
| 29 | + requirements = ["gunicorn", "fastapi", "uvicorn", "aiohttp", "Jinja2", "pydantic", "pymilvus", "numpy", "loguru"] |
| 30 | + |
| 31 | + def __init__(self): |
| 32 | + logger.info('ComposeExecutor init with is_compose_env()') |
| 33 | + |
| 34 | + cfg = get_configuration() |
| 35 | + self.project_output_path = cfg.path |
| 36 | + |
| 37 | + def scripts(self): |
| 38 | + return {"service": inspect.getfile(self.__class__)} |
| 39 | + |
| 40 | + def copy_struct_project(self): |
| 41 | + copy_piper(self.project_output_path) |
| 42 | + copy_scripts(self.project_output_path, self.scripts()) |
| 43 | + |
| 44 | + def create_files_for_compose(self, testing: bool = False): |
| 45 | + logger.info('ComposeExecutor create_fast_api_files_venv()') |
| 46 | + |
| 47 | + compose_service = ComposeServices( |
| 48 | + name_path=self.project_output_path, |
| 49 | + ) |
| 50 | + |
| 51 | + main_fastapi = compose_service.render_script_fastapi() |
| 52 | + with open(f"{self.project_output_path}/main.py", "w") as output: |
| 53 | + output.write(main_fastapi) |
| 54 | + |
| 55 | + docker_compose = compose_service.render_compose_services() |
| 56 | + with open(f"{self.project_output_path}/docker-compose.yaml", "w") as output: |
| 57 | + output.write(docker_compose) |
| 58 | + |
| 59 | + bash_start = compose_service.render_bash_start(testing=testing) |
| 60 | + with open(f"{self.project_output_path}/bash-start.sh", "w") as output: |
| 61 | + output.write(bash_start) |
| 62 | + |
| 63 | + bash_stop = compose_service.render_bash_stop() |
| 64 | + with open(f"{self.project_output_path}/bash-stop.sh", "w") as output: |
| 65 | + output.write(bash_stop) |
| 66 | + |
| 67 | + dockerfile = compose_service.render_dockerfile() |
| 68 | + with open(f"{self.project_output_path}/Dockerfile", "w") as output: |
| 69 | + output.write(dockerfile) |
| 70 | + |
| 71 | + write_requirements(self.project_output_path, self.requirements) |
| 72 | + |
| 73 | + def start_compose(self): |
| 74 | + process_chmod_start = subprocess.run(f'chmod +x {self.project_output_path}bash-start.sh', shell=True) |
| 75 | + process_run = subprocess.run(f'{self.project_output_path}bash-start.sh', shell=True) |
| 76 | + |
| 77 | + def stop_compose(self): |
| 78 | + process_chmod_stop = subprocess.run(f'chmod +x {self.project_output_path}bash-stop.sh', shell=True) |
| 79 | + process_run = subprocess.run(f'{self.project_output_path}bash-stop.sh', shell=True) |
0 commit comments