From 6a7f079fb1282a421582b8c144d12e70e3d3d435 Mon Sep 17 00:00:00 2001 From: Leo Neto Date: Wed, 17 Jan 2024 23:16:24 -0300 Subject: [PATCH] chore: cleanup --- README.md | 107 ++++++++++++++++++ geny/commands/generate/dockerfile.py | 9 +- geny/template_files/celery/celery.tpl | 24 ---- geny/template_files/celery/celery_init.tpl | 7 -- geny/template_files/celery/celery_tasks.tpl | 7 -- geny/template_files/docker/docker-compose.tpl | 45 ++------ .../docker/docker-entrypoint.tpl | 12 -- geny/template_files/dokku/app_json.tpl | 7 -- geny/template_files/dokku/dokku_checks.tpl | 2 - geny/template_files/dokku/dokku_scale.tpl | 1 - geny/template_files/dokku/procfile.tpl | 1 - geny/template_files/env.tpl | 19 +--- geny/template_files/fixture.tpl | 13 --- geny/template_files/form.tpl | 8 -- geny/template_files/github/ci.tpl | 0 geny/template_files/github/github_ci.tpl | 30 ----- .../github/pull_request_template.tpl | 0 geny/template_files/html/create.tpl | 15 --- geny/template_files/html/detail.tpl | 13 --- geny/template_files/html/list.tpl | 24 ---- geny/template_files/html/template.tpl | 7 -- geny/template_files/html/update.tpl | 15 --- geny/template_files/kubernetes/configmap.tpl | 6 - geny/template_files/kubernetes/deployment.tpl | 67 ----------- geny/template_files/kubernetes/ingress.tpl | 24 ---- geny/template_files/kubernetes/service.tpl | 11 -- geny/template_files/management.tpl | 22 ---- geny/template_files/serializer.tpl | 19 ---- 28 files changed, 126 insertions(+), 389 deletions(-) delete mode 100644 geny/template_files/celery/celery.tpl delete mode 100644 geny/template_files/celery/celery_init.tpl delete mode 100644 geny/template_files/celery/celery_tasks.tpl delete mode 100644 geny/template_files/docker/docker-entrypoint.tpl delete mode 100644 geny/template_files/dokku/app_json.tpl delete mode 100644 geny/template_files/dokku/dokku_checks.tpl delete mode 100644 geny/template_files/dokku/dokku_scale.tpl delete mode 100644 geny/template_files/dokku/procfile.tpl delete mode 100644 geny/template_files/fixture.tpl delete mode 100644 geny/template_files/form.tpl delete mode 100644 geny/template_files/github/ci.tpl delete mode 100644 geny/template_files/github/github_ci.tpl delete mode 100644 geny/template_files/github/pull_request_template.tpl delete mode 100644 geny/template_files/html/create.tpl delete mode 100644 geny/template_files/html/detail.tpl delete mode 100644 geny/template_files/html/list.tpl delete mode 100644 geny/template_files/html/template.tpl delete mode 100644 geny/template_files/html/update.tpl delete mode 100644 geny/template_files/kubernetes/configmap.tpl delete mode 100644 geny/template_files/kubernetes/deployment.tpl delete mode 100644 geny/template_files/kubernetes/ingress.tpl delete mode 100644 geny/template_files/kubernetes/service.tpl delete mode 100644 geny/template_files/management.tpl delete mode 100644 geny/template_files/serializer.tpl diff --git a/README.md b/README.md index a34dbde..0ce20e9 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,109 @@ # geny An extendable file generator + +![publish](https://github.com/oleoneto/geny/workflows/publish/badge.svg?branch=master) +![PyPI - Package](https://img.shields.io/pypi/v/geny) +![PyPI - Python](https://img.shields.io/pypi/pyversions/geny) +![PyPI - License](https://img.shields.io/pypi/l/geny) +![PyPI - Downloads](https://img.shields.io/pypi/dm/geny) + + +- [geny](#geny) + - [Installation](#installation) + - [Extending the CLI](#extending-the-cli) + - [Dependencies](#dependencies) + - [Interactive Docs](#interactive-docs) + - [To Do](#to-do) + - [Pull requests](#pull-requests) + - [LICENSE](#license) + +## Installation +Install via [pip](https://pypi.org/project/geny): +```bash +pip install geny +``` + +After installation, the CLI will expose the binary with the name: +``` +geny +``` + +## Extending the CLI + +Currently, there are two main ways of extending the functionality of the CLI: +1. Adding your own commands/plugins +2. Overriding the provided resource templates + +### Including your own commands + +If you would like to extend the functionality of this CLI, you can include your own `plugins/commands` by +setting an environment variable: `GENY_PLUGINS`. Simply set this variable to the path where your plugins are. + +Plugin commands are auto-discovered if they are placed under the plugins directory, +but please be sure to do the following for this to work: +1. **Name your package and click command the same**. That is, a package called `get`, for example, should define a `get` command. +2. **Place the command definition within the package's `main.py` module**. For example: +```python +# get/main.py +import click + + +@click.command() +@click.pass_context +def get(ctx): + pass +``` +3. **Sub-commands should be added to the top-most command group in the package's `main.py` module.** +```python +# get/main.py +import click + + +@click.group() # <- group +@click.pass_context +def get(ctx): + pass + + +@click.command() +@click.pass_context +def foo(ctx): + pass + + +get.add_command(foo) +``` +4. **Access your commands via your top-most command group.** +``` +django-clite get foo +``` + +**NOTE:** If you would like to skip a plugin/command from being auto-discovered, simply rename the package by either +prepending or appending any number of underscores (`_`). Any code contained within the package will be ignored. + +### Overriding the templates + +The flag `--templates-dir` can be used to configure an additional path wherein the CLI can look for resource templates. +Alternatively, you can use the environment variable `GENY_TEMPLATES_DIR` for the same purpose. + +## Development + +### Install from source: +``` +git clone https://github.com/oleoneto/geny.git +cd geny +pip install --editable . +``` + +### Dependencies +Check out [pyproject.toml](pyproject.toml) for all installation dependencies. + +## To Do +[Check out our open issues](https://github.com/oleoneto/geny/issues). + +## Pull requests +Found a bug? See a typo? Have an idea for new command? +Feel free to submit a pull request with your contributions. They are much welcome and appreciated. + +## LICENSE +**geny** is [BSD Licensed](LICENSE). diff --git a/geny/commands/generate/dockerfile.py b/geny/commands/generate/dockerfile.py index a39033c..ab752c8 100644 --- a/geny/commands/generate/dockerfile.py +++ b/geny/commands/generate/dockerfile.py @@ -20,14 +20,7 @@ def dockerfile(ctx, docker_compose): File( name="docker-compose.yaml", template="docker/docker-compose.tpl", - context={ - "services": [ - "celery", - "kafka", - "postgres", - "rabbitmq" "redis", - ], - }, + context={}, ) ) diff --git a/geny/template_files/celery/celery.tpl b/geny/template_files/celery/celery.tpl deleted file mode 100644 index f0f67e9..0000000 --- a/geny/template_files/celery/celery.tpl +++ /dev/null @@ -1,24 +0,0 @@ -from __future__ import absolute_import, unicode_literals - -import os - -from celery import Celery - -# set the default Django settings module for the 'celery' program. -os.environ.setdefault('DJANGO_SETTINGS_MODULE', '{{ project }}.settings') - -app = Celery('{{ project }}') - -# Using a string here means the worker doesn't have to serialize -# the configuration object to child processes. -# - namespace='CELERY' means all celery-related configuration keys -# should have a `CELERY_` prefix. -app.config_from_object('django.conf:settings', namespace='CELERY') - -# Load task modules from all registered Django app configs. -app.autodiscover_tasks() - - -@app.task(bind=True) -def debug_task(self): - print('Request: {0!r}'.format(self.request)) diff --git a/geny/template_files/celery/celery_init.tpl b/geny/template_files/celery/celery_init.tpl deleted file mode 100644 index 070e835..0000000 --- a/geny/template_files/celery/celery_init.tpl +++ /dev/null @@ -1,7 +0,0 @@ -from __future__ import absolute_import, unicode_literals - -# This will make sure the app is always imported when -# Django starts so that shared_task will use this app. -from .celery import app as celery_app - -__all__ = ('celery_app',) diff --git a/geny/template_files/celery/celery_tasks.tpl b/geny/template_files/celery/celery_tasks.tpl deleted file mode 100644 index a3bb734..0000000 --- a/geny/template_files/celery/celery_tasks.tpl +++ /dev/null @@ -1,7 +0,0 @@ -from celery import shared_task - - -@shared_task -def my_{{ project }}_task(data=None): - # This is a sample celery task - pass diff --git a/geny/template_files/docker/docker-compose.tpl b/geny/template_files/docker/docker-compose.tpl index 2e40e10..0caead4 100644 --- a/geny/template_files/docker/docker-compose.tpl +++ b/geny/template_files/docker/docker-compose.tpl @@ -1,79 +1,58 @@ +# These settings are provided for development purposes only. Not suitable for production. + version: '3' -{% if 'database' in services %} volumes: database: -{% endif %} + services: app: container_name: "{{ project }}_web" - labels: - com.{{ project }}.web.description: "{{ project }}: Web Application" build: context: . + # command: "gunicorn {{ project }}.wsgi:application --bind 0.0.0.0:{{ port }} --workers {{ workers }}" volumes: - .:/app env_file: - .env - environment: - DJANGO_ENV: docker - entrypoint: /docker-entrypoint.sh - command: "gunicorn {{ project }}.wsgi:application --bind 0.0.0.0:{{ port }} --workers {{ workers }}" ports: - 8007:8000 # host:docker - {%- if services %} depends_on: - {%- for service in services %} - - {{ service }} - {% endfor %} - {% endif %} + - database + - celery-worker - - {%- if 'database' in services %} database: container_name: "{{ project }}_database" - image: postgres:12-alpine - labels: - com.database.description: "{{ project }}: Database service" + image: postgres:15-alpine volumes: - ./database:/var/lib/postgresql/data/ ports: - 5437:5432 # host:docker + env_file: + - .env healthcheck: test: ["CMD-SHELL", "pg_ready -U postgres"] interval: 10s timeout: 83s retries: 40 - restart: always - {% endif %} + restart: unless-stopped - {%- if 'redis' in services %} redis: container_name: "{{ project }}_redis" image: redis:latest - labels: - com.redis.description: "{{ project }}: Redis cache service" ports: - 6377:6379 # host:docker - restart: always - {% endif %} + restart: unless-stopped - {%- if 'celery' in services %} celery_worker: container_name: "{{ project }}_celery_worker" - labels: - com.celery.description: "{{ project }}: Celery Worker" build: context: . - command: celery worker --app {{ project }} --concurrency=20 -linfo -E + # command: celery worker --app {{ project }} --concurrency=20 -linfo -E depends_on: - redis env_file: - .env restart: on-failure stop_grace_period: 5s - {% endif %} - - -# These settings are provided for development purposes only. Not suitable for production. \ No newline at end of file diff --git a/geny/template_files/docker/docker-entrypoint.tpl b/geny/template_files/docker/docker-entrypoint.tpl deleted file mode 100644 index 4a9b0de..0000000 --- a/geny/template_files/docker/docker-entrypoint.tpl +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash - -# Collect staticfiles -python3 manage.py collectstatic --noinput - -# Apply database migrations -python3 manage.py migrate - -# Seed database -# python3 manage.py loaddata /path/to/fixtures - -exec "$@" diff --git a/geny/template_files/dokku/app_json.tpl b/geny/template_files/dokku/app_json.tpl deleted file mode 100644 index 612492e..0000000 --- a/geny/template_files/dokku/app_json.tpl +++ /dev/null @@ -1,7 +0,0 @@ -{ - "scripts": { - "dokku": { - "predeploy": "/app/manage.py migrate --noinput" - } - } -} \ No newline at end of file diff --git a/geny/template_files/dokku/dokku_checks.tpl b/geny/template_files/dokku/dokku_checks.tpl deleted file mode 100644 index 69cd839..0000000 --- a/geny/template_files/dokku/dokku_checks.tpl +++ /dev/null @@ -1,2 +0,0 @@ -DOKKU_CHECKS_WAIT={{ wait }} -DOKKU_CHECKS_TIMEOUT={{ timeout }} \ No newline at end of file diff --git a/geny/template_files/dokku/dokku_scale.tpl b/geny/template_files/dokku/dokku_scale.tpl deleted file mode 100644 index 34579eb..0000000 --- a/geny/template_files/dokku/dokku_scale.tpl +++ /dev/null @@ -1 +0,0 @@ -web={{ web }} \ No newline at end of file diff --git a/geny/template_files/dokku/procfile.tpl b/geny/template_files/dokku/procfile.tpl deleted file mode 100644 index e5e1319..0000000 --- a/geny/template_files/dokku/procfile.tpl +++ /dev/null @@ -1 +0,0 @@ -web: gunicorn {{ project }}.wsgi \ No newline at end of file diff --git a/geny/template_files/env.tpl b/geny/template_files/env.tpl index cffea30..5bb0663 100644 --- a/geny/template_files/env.tpl +++ b/geny/template_files/env.tpl @@ -1,18 +1,11 @@ # Example variables that could be in an environment file. # Commit only the .env-example file, not the .env file. Use the example file to illustrate -# what variables your environment uses, but be sure to not commit this file with any sensitive information. +# what variables need to be in your application environment, but be sure to not commit this file +# as it may contain sensitive information. DEBUG=True +SERVER_EMAIL="no-reply@example.com" +REDIS_URL="redis://[:password]@127.0.0.1:6379" +DATABASE_URL="postgres://username:password@localhost:5432/database" -ADMINS=# i.e (('Admin', 'admin@example.com'),) - -SECRET_KEY= - -SERVER_EMAIL=# i.e no-reply@example.com -EMAIL_HOST= -EMAIL_HOST_USER= -EMAIL_HOST_PASSWORD= - -REDIS_URL=# i.e. redis://[:password]@127.0.0.1:6379 - -DATABASE_URL=postgres://u:p@service:5432/service +SECRET_KEY="" diff --git a/geny/template_files/fixture.tpl b/geny/template_files/fixture.tpl deleted file mode 100644 index b7efe88..0000000 --- a/geny/template_files/fixture.tpl +++ /dev/null @@ -1,13 +0,0 @@ -[ - {% for i in range(total) -%} - { - "pk": {{ loop.index }}, - "model": "{{ app }}.{{ classname }}", - "fields": { - {% for f in fields -%} - "{{ f.name }}": {{ f.example_value }}, - {% endfor %} - } - }{% if loop.index == loop.length %}{% else %},{% endif %} - {% endfor %} -] diff --git a/geny/template_files/form.tpl b/geny/template_files/form.tpl deleted file mode 100644 index 7f56bc1..0000000 --- a/geny/template_files/form.tpl +++ /dev/null @@ -1,8 +0,0 @@ -from django import forms -from {{ project }}.{{ app }}.models.{{ name }} import {{ classname }} - - -class {{ classname }}Form(forms.ModelForm): - class Meta: - model = {{ classname }} - fields = "__all__" diff --git a/geny/template_files/github/ci.tpl b/geny/template_files/github/ci.tpl deleted file mode 100644 index e69de29..0000000 diff --git a/geny/template_files/github/github_ci.tpl b/geny/template_files/github/github_ci.tpl deleted file mode 100644 index 7444cae..0000000 --- a/geny/template_files/github/github_ci.tpl +++ /dev/null @@ -1,30 +0,0 @@ -name: Django CI - -on: - pull_request: - branches: [main] - push: - branches: [main] - -jobs: - build: - - runs-on: ubuntu-latest - strategy: - max-parallel: 4 - matrix: - python-version: [3.7, 3.8, 3.9] - - steps: - - uses: actions/checkout@v2 - - name: Set up Python {% raw %}${{ matrix.python-version }}{% endraw %} - uses: actions/setup-python@v2 - with: - python-version: {% raw %}${{ matrix.python-version }}{% endraw %} - - name: Install Dependencies - run: | - python -m pip install --upgrade pip - pip install -r requirements.txt - - name: Run Tests - run: | - python manage.py test diff --git a/geny/template_files/github/pull_request_template.tpl b/geny/template_files/github/pull_request_template.tpl deleted file mode 100644 index e69de29..0000000 diff --git a/geny/template_files/html/create.tpl b/geny/template_files/html/create.tpl deleted file mode 100644 index 33ad983..0000000 --- a/geny/template_files/html/create.tpl +++ /dev/null @@ -1,15 +0,0 @@ -{%- raw -%} -{% comment %} - Describe the template here. -{% endcomment %} - -{% block content %} -
-

Create Form

- -
- {{ form }} -
-
-{% endblock %} -{%- endraw %} diff --git a/geny/template_files/html/detail.tpl b/geny/template_files/html/detail.tpl deleted file mode 100644 index 957dd3d..0000000 --- a/geny/template_files/html/detail.tpl +++ /dev/null @@ -1,13 +0,0 @@ -{%- raw -%} -{% comment %} - Describe the template here. -{% endcomment %} - -{% block content %} -
-

{{ object }}

- -
-
-{% endblock %} -{%- endraw %} diff --git a/geny/template_files/html/list.tpl b/geny/template_files/html/list.tpl deleted file mode 100644 index c9fdec7..0000000 --- a/geny/template_files/html/list.tpl +++ /dev/null @@ -1,24 +0,0 @@ -{%- raw -%} -{% comment %} - Describe the template here. -{% endcomment %} - -{% block content %} -
-

ListView

-
- {% for object in object_list %} -
-
-

- {{ object }} -

-
-
- -
- {% endfor %} -
-
-{% endblock %} -{%- endraw %} diff --git a/geny/template_files/html/template.tpl b/geny/template_files/html/template.tpl deleted file mode 100644 index 5e5998e..0000000 --- a/geny/template_files/html/template.tpl +++ /dev/null @@ -1,7 +0,0 @@ -{{ '{% load cache %}' }} - -{{ '{% comment %}' }} - Describe the template here. -{{ '{% endcomment %}' }} - -{{ '{% block content %}{% endblock content %}' }} diff --git a/geny/template_files/html/update.tpl b/geny/template_files/html/update.tpl deleted file mode 100644 index 138bc2a..0000000 --- a/geny/template_files/html/update.tpl +++ /dev/null @@ -1,15 +0,0 @@ -{%- raw -%} -{% comment %} - Describe the template here. -{% endcomment %} - -{% block content %} -
-

{{ object }}

- -
- {{ form }} -
-
-{% endblock %} -{%- endraw %} diff --git a/geny/template_files/kubernetes/configmap.tpl b/geny/template_files/kubernetes/configmap.tpl deleted file mode 100644 index 0ca3a6b..0000000 --- a/geny/template_files/kubernetes/configmap.tpl +++ /dev/null @@ -1,6 +0,0 @@ -apiVersion: v1 -kind: ConfigMap -metadata: - name: {{ project }} -data: - DEBUG: "false" diff --git a/geny/template_files/kubernetes/deployment.tpl b/geny/template_files/kubernetes/deployment.tpl deleted file mode 100644 index 1956e11..0000000 --- a/geny/template_files/kubernetes/deployment.tpl +++ /dev/null @@ -1,67 +0,0 @@ -apiVersion: apps/v1 -kind: Deployment -metadata: - name: &app {{ project }} -spec: - replicas: 1 - selector: - matchLabels: - app: *app - template: - metadata: - name: *app - labels: - app: *app - spec: - restartPolicy: Always - containers: - - name: *app - image: &image "docker.io/{{ project }}" # modify this value - imagePullPolicy: IfNotPresent - securityContext: - allowPrivilegeEscalation: false - resources: - limits: - memory: "128Mi" - ports: - - name: &port http - containerPort: 8080 - readinessProbe: - httpGet: - port: *port - path: /robots.txt - timeoutSeconds: 120 - livenessProbe: - exec: - command: - - cat - - /app/README.md - timeoutSeconds: 30 - envFrom: - - secretRef: - name: *app - - configMapRef: - name: *app - initContainers: - - name: migrations - image: *image - securityContext: - allowPrivilegeEscalation: false - command: ["manage.py"] - args: ["migrate"] - envFrom: - - secretRef: - name: *app - - configMapRef: - name: *app - - name: staticfiles - image: *image - securityContext: - allowPrivilegeEscalation: false - command: ["manage.py"] - args: ["collectstatic", "--noinput"] - envFrom: - - secretRef: - name: *app - - configMapRef: - name: *app diff --git a/geny/template_files/kubernetes/ingress.tpl b/geny/template_files/kubernetes/ingress.tpl deleted file mode 100644 index 88b82cc..0000000 --- a/geny/template_files/kubernetes/ingress.tpl +++ /dev/null @@ -1,24 +0,0 @@ -# Example ingress definition. Requires Cert Manager to have already been configured. -apiVersion: networking.k8s.io/v1 -kind: Ingress -metadata: - name: &app {{ project }} - annotations: - cert-manager.io/issuer: letsencrypt -spec: - # ingressClassName: nginx - tls: - - hosts: - - example.com # modify this value - secretName: "letsencrypt-{{ project }}" - rules: - - host: example.com # modify this value - http: - paths: - - pathType: Prefix - path: / - backend: - service: - name: *app - port: - name: http diff --git a/geny/template_files/kubernetes/service.tpl b/geny/template_files/kubernetes/service.tpl deleted file mode 100644 index 9d81ecd..0000000 --- a/geny/template_files/kubernetes/service.tpl +++ /dev/null @@ -1,11 +0,0 @@ -apiVersion: v1 -kind: Service -metadata: - name: &app {{ project }} -spec: - ports: - - name: http - port: 3000 - targetPort: http - selector: - app: *app diff --git a/geny/template_files/management.tpl b/geny/template_files/management.tpl deleted file mode 100644 index 308f15e..0000000 --- a/geny/template_files/management.tpl +++ /dev/null @@ -1,22 +0,0 @@ -from django.core.management.base import BaseCommand -from django.db.utils import IntegrityError -# from {{ project }}.{{ app }}.models import MyModel - - -class Command(BaseCommand): - help = "What this command does" - - def add_arguments(self, parser): - pass - - def handle(self, *args, **options): - """ - Example: - - try: - [MyModel.objects.create(**i) for i in [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]] - except IntegrityError: - print(f'Skipping creation of record {i} it seems to already exist.') - """ - - pass diff --git a/geny/template_files/serializer.tpl b/geny/template_files/serializer.tpl deleted file mode 100644 index 5239363..0000000 --- a/geny/template_files/serializer.tpl +++ /dev/null @@ -1,19 +0,0 @@ -from rest_framework import serializers -from {{ project }}.{{ app }}.models import {{ classname }} - - -class {{ classname }}Serializer(serializers.ModelSerializer): - # Add related fields below: - # Example relation fields are: - # -- HyperlinkedIdentityField - # -- HyperlinkedRelatedField - # -- PrimaryKeyRelatedField - # -- SlugRelatedField - # -- StringRelatedField - - # You can also add a custom serializer, like so: - # likes = LikeSerializer(many=True) - - class Meta: - model = {{ classname }} - fields = "__all__"