Skip to content

Commit d34f0e2

Browse files
authored
Merge pull request #7 from TatraDev/venv_logic
Docker Compose (fastapi, milvus) logic
2 parents 79008c1 + c7d842e commit d34f0e2

23 files changed

+717
-74
lines changed

.github/workflows/test.yaml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Python package
1+
name: test
22

33
on: [push]
44

@@ -27,6 +27,5 @@ jobs:
2727
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
2828
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
2929
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
30-
- name: Test with pytest
31-
run: |
32-
pytest --verbose tests/running_piper_test.py::TestVenv
30+
- name: Test with makefile
31+
run: make test

MANIFEST.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
include piper/base/backend/templates/*.j2
2+
include piper/base/docker/images/*.j2
3+
include piper/base/docker_compose/templates/*.j2
4+
include piper/base/virtualenv/templates/*.j2

Makefile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
SHELL:=/usr/bin/env bash
2+
3+
.PHONY: lint
4+
lint:
5+
flake8 piper
6+
7+
.PHONY: unit
8+
unit:
9+
pytest -vs tests/running_piper_test.py::TestCompose
10+
11+
.PHONY: package
12+
package:
13+
pip check
14+
15+
.PHONY: test
16+
test: package unit

Readme.rst

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
• `Chat (Community & Support) <https://t.me/pipertool>`_
66
• `Tutorials <http://pipertool.org>`_
77

8+
|Contribs| |License| |Release| |Python Version| |Docs| |Build status| |Contact| |Blog|
89

910
**Piper** is an **open-source** platform for data science and machine
1011
learning prototyping. Concentrate only on your goals. Key features:
@@ -31,6 +32,48 @@ How Piper works
3132

3233
Quick start
3334
===========
35+
Quick start pipertool package compose env
36+
===========
37+
38+
In root directory project run command in terminal
39+
40+
- sudo -u root /bin/bash
41+
42+
- create and activate venv
43+
44+
- pip install -r requirements.txt
45+
46+
- in configuration.py rename for correctly path for new directory
47+
48+
- python setup.py install
49+
50+
- piper --env-type compose start
51+
52+
- 0.0.0.0:7585 - FastApi
53+
54+
- 0.0.0.0:9001 - Milvus Console (minioadmin/minioadmin)
55+
56+
- piper --env-type compose stop
57+
58+
- pip uninstall piper
59+
60+
Quick start pipertool package compose env
61+
===========
62+
63+
In root directory project run command in terminal
64+
65+
- sudo -u root /bin/bash
66+
67+
- create and activate venv
68+
69+
- pip install -r requirements.txt
70+
71+
- in configuration.py rename for correctly path for new directory
72+
73+
- python main.py
74+
75+
- await click CTRL+C from compose env
76+
3477

3578

3679
Installation
@@ -88,6 +131,28 @@ By submitting a pull request to this project, you agree to license your contribu
88131
:alt: Piper logo
89132

90133

91-
.. |Contribs| image:: https://tatradev.com
92-
:target: https://github.com/TatraDev/piper/graphs/contributors
134+
.. |Contribs| image:: https://img.shields.io/badge/contributors-3-brightgreen
135+
:target: https://github.com/TatraDev/pipertool/graphs/contributors
93136
:alt: Contributors
137+
138+
.. |Python Version| image:: https://img.shields.io/badge/python-3.8%20%7C%203.9%20%7C%203.10-blue
139+
:target: https://pypi.org/project/pipertool
140+
:alt: Python Version
141+
142+
.. |Release| image:: https://img.shields.io/badge/release-v0.0.2-blue
143+
:target: https://github.com/TatraDev/pipertool/releases
144+
145+
.. |Build status| image:: https://github.com/TatraDev/pipertool/workflows/test/badge.svg?branch=venv_logic&event=push
146+
:target: https://github.com/TatraDev/pipertool/actions?query=workflow%3Atest
147+
148+
.. |Contact| image:: https://img.shields.io/badge/telegram-write%20me-blue.svg
149+
:target: https://t.me/pipertool
150+
151+
.. |Blog| image:: https://img.shields.io/badge/site-my%20blog-yellow.svg
152+
:target: https://tatradev.com
153+
154+
.. |License| image:: https://img.shields.io/badge/License-Apache_2.0-yellow.svg
155+
:target: https://www.apache.org/licenses/LICENSE-2.0
156+
157+
.. |Docs| image:: https://img.shields.io/badge/docs-passing-brightgreen
158+
:target: http://pipertool.org

main.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
from loguru import logger
66

77
from piper.configurations import get_configuration
8-
from piper.envs import CurrentEnv, DockerEnv, VirtualEnv
8+
from piper.envs import ComposeEnv, CurrentEnv, DockerEnv, VirtualEnv
9+
910
# from piper.services import (SpacyNER, StringValue, TesseractRecognizer,
1011
# TestMessageAdder)
1112
# from piper.utils import tesrct_utils as tu
@@ -21,7 +22,7 @@
2122
# result = loop.run_until_complete(adder(x))
2223
# print(result)
2324

24-
# x = StringValue(value="hello, world")
25+
# x = StringValue(value="hello, world")
2526
# adder = TestMessageAdder(appender="!", port=cfg.docker_app_port)
2627
# result = loop.run_until_complete(adder(x))
2728
# print(result)
@@ -37,11 +38,21 @@
3738
# result = loop.run_until_complete(recognizer())
3839
# logger.info(f'result of recognition is {result}')
3940

40-
with VirtualEnv() as env:
41-
env.copy_struct_project()
42-
env.create_files_for_venv()
43-
env.create_files_for_tests()
44-
41+
# with VirtualEnv() as env:
42+
# env.copy_struct_project()
43+
# env.create_files_for_venv()
44+
# env.create_files_for_tests()
45+
46+
with ComposeEnv() as env:
47+
try:
48+
env.copy_struct_project()
49+
env.create_files_for_compose(testing=True)
50+
env.start_compose()
51+
except KeyboardInterrupt:
52+
logger.info('Ctrl+C pressed. Except KeyboardInterrupt.')
53+
env.stop_compose()
54+
sys.exit(1)
55+
4556
# sys.exit()
4657

4758
# sn = SpacyNER()
@@ -55,4 +66,4 @@
5566
# result1_str = "\n".join(str(x) for x in result1)
5667
# logger.info(f'result of NER for model {avalable_model} is {result1_str}')
5768
# else:
58-
# logger.info(f'module didn`t get NER data')
69+
# logger.info(f'module didn`t get NER data')

piper/__init__.py

Whitespace-only changes.

piper/__main__.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import sys
2+
3+
import click
4+
5+
from piper.envs import ComposeEnv
6+
7+
8+
@click.command()
9+
@click.argument('type_command')
10+
@click.option(
11+
'--env-type', '-e',
12+
help='your current interpretation',
13+
)
14+
def main(type_command: str, env_type: str):
15+
if env_type == 'compose':
16+
if type_command == 'start':
17+
print("type_command == 'start'")
18+
with ComposeEnv() as env:
19+
env.copy_struct_project()
20+
env.create_files_for_compose()
21+
env.start_compose()
22+
elif type_command == 'stop':
23+
print("type_command == 'stop'")
24+
with ComposeEnv() as env:
25+
env.stop_compose()
26+
else:
27+
raise NotImplementedError(f'{env_type} not released in this version pipertool')
28+
29+
30+
if __name__ == '__main__':
31+
args = sys.argv
32+
if "--help" in args or len(args) == 1:
33+
print("CVE")
34+
main()

piper/base/docker_compose/__init__.py

Whitespace-only changes.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
3+
sudo docker-compose -f {{ name_path }}docker-compose.yaml up --build {{ compose_flag }}

0 commit comments

Comments
 (0)