Skip to content

Commit

Permalink
Merge pull request #29 from minos-framework/0.1.1
Browse files Browse the repository at this point in the history
0.1.1
  • Loading branch information
albamig authored Feb 10, 2022
2 parents 807b189 + ed54ce5 commit 4659f9c
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM ghcr.io/clariteia/minos-microservice:0.1.8 as development

COPY ./pyproject.toml ./poetry.lock ./
COPY ./pyproject.toml ./
RUN poetry install --no-root
COPY . .
CMD ["poetry", "run", "microservice", "start"]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ build_docker_compose(destination.parent.parent / "docker-compose.yml", name) }}
36 changes: 36 additions & 0 deletions microservice/language/python/init/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,41 @@ def build_deploy_playbook(path: Path) -> str:
data = list()

data.append({"name": "Create Database", "import_playbook": "create-database.yaml"})
data.append({"name": "Create Query Database", "import_playbook": "create-database.yaml"})

return yaml.dump(data, sort_keys=False)


def build_docker_compose(path: Path, microservice_name: str) -> str:
"""Build Docker Compose file content."""

if not path.exists():
raise ValueError("A base Compose file must exist.")

with path.open() as file:
data = yaml.safe_load(file)

if "x-microservice-environment" not in data:
data["x-microservice-environment"] = {
"MINOS_BROKER_QUEUE_HOST": "postgres",
"MINOS_BROKER_HOST": "kafka",
"MINOS_REPOSITORY_HOST": "postgres",
"MINOS_SNAPSHOT_HOST": "postgres",
"MINOS_DISCOVERY_HOST": "discovery",
}
if "x-microservice-depends-on" not in data:
data["x-microservice-depends-on"] = ["postgres", "kafka", "discovery"]

microservice_container = {
"restart": "always",
"build": {"context": f"microservices/{microservice_name}", "target": "production"},
"environment": data["x-microservice-environment"],
"depends_on": data["x-microservice-depends-on"],
}

data["services"][f"microservice-{microservice_name}"] = microservice_container

with path.open("w") as file:
yaml.dump(data, file, sort_keys=False)

return ""
14 changes: 10 additions & 4 deletions microservice/language/python/init/config.yml.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,28 @@ broker:
port: 9092
queue:
database: {{ name }}_db
user: postgres
password: ""
user: minos
password: min0s
host: localhost
port: 5432
records: 1000
retry: 2
repository:
database: {{ name }}_db
user: minos
password: min0s
host: localhost
port: 5432
query_repository:
database: {{ name }}_query_db
user: postgres
password: ""
host: localhost
port: 5432
snapshot:
database: {{ name }}_db
user: postgres
password: ""
user: minos
password: min0s
host: localhost
port: 5432
saga:
Expand Down
3 changes: 2 additions & 1 deletion microservice/language/python/init/copier.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ deploy:
choices:
- docker-compose
- kubernetes
default: kubernetes
default: docker-compose

_envops:
block_start_string: "{%"
Expand All @@ -45,3 +45,4 @@ _exclude:

_functions:
- "_utils.build_deploy_playbook"
- "_utils.build_docker_compose"
1 change: 1 addition & 0 deletions microservice/language/python/init/src/__init__.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ from .commands import (
)
from .queries import (
{{ aggregate }}QueryService,
{{ aggregate }}QueryServiceRepository,
)
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from .services import (
{{ aggregate }}QueryService,
)

from .repository import (
{{ aggregate }}QueryServiceRepository,
)
3 changes: 3 additions & 0 deletions microservice/language/python/init/src/queries/models.py.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from sqlalchemy.orm import declarative_base

Base = declarative_base()
22 changes: 22 additions & 0 deletions microservice/language/python/init/src/queries/repository.py.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from minos.common import MinosSetup, MinosConfig
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from src.queries.models import Base


class {{ aggregate }}QueryServiceRepository(MinosSetup):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.engine = create_engine("postgresql+psycopg2://postgres:@localhost:5432/{{ name }}_query_db".format(**kwargs))
self.session = sessionmaker(bind=self.engine)

async def _setup(self) -> None:
Base.metadata.create_all(self.engine)

@classmethod
def _from_config(cls, *args, config: MinosConfig, **kwargs) -> {{ aggregate }}QueryRepository:
return cls(*args, **(config.query_repository._asdict()) | kwargs)

@property
def session(self):
return self.session
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ minos-microservice-cqrs = "^0.5.0"
minos-broker-kafka = "^0.5.0"
minos-discovery-minos = "^0.5.0"
typer = "^0.3.2"
SQLAlchemy = "^1.4.32"

[tool.poetry.dev-dependencies]
black = "^19.10b"
Expand Down
2 changes: 1 addition & 1 deletion project/apigateway/minos/deploy/docker-compose/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def build_docker_compose(path: Path) -> str:
container = {
"restart": "always",
"build": "external/apigateway",
"ports": ["5566"],
"ports": ["5566:5566"],
"depends_on": ["discovery"],
"environment": {
"PYTHONPATH": "/api_gateway",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ spec:
fieldPath: status.podIP
volumeMounts:
- name: kafka
mountPath: /kafka
mountPath: /kafka/kafka-logs
volumes:
- name: kafka
persistentVolumeClaim:
Expand Down
2 changes: 0 additions & 2 deletions project/deploy/docker-compose/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ def build_docker_compose(path: Path) -> str:

data = dict()
data["version"] = "3.9"
data["x-microservice-environment"] = []
data["x-microservice-depends-on"] = []
data["volumes"] = {}
data["services"] = {}

Expand Down
10 changes: 10 additions & 0 deletions project/init/copier.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ project_version:
type: str
help: What is the version of the project?
default: 0.1.0
project_deploy:
type: str
link:
docker-compose: "{{ template_registry }}/project-deploy-docker-compose.tar.gz"
kubernetes: "{{ template_registry }}/project-deploy-kubernetes.tar.gz"
help: What deploy system do you want to use?
choices:
- docker-compose
- kubernetes
default: docker-compose

_envops:
block_start_string: "{%"
Expand Down

0 comments on commit 4659f9c

Please sign in to comment.