diff --git a/.autoenv.zsh b/.autoenv.zsh new file mode 100644 index 0000000..f23a7d1 --- /dev/null +++ b/.autoenv.zsh @@ -0,0 +1,2 @@ +export PROJDIR=$(pwd) +source "$PROJDIR"/etc/zsh/auto.sh \ No newline at end of file diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..1e87e62 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,5 @@ +repos: +- repo: https://github.com/psf/black + rev: 22.3.0 + hooks: + - id: black \ No newline at end of file diff --git a/etc/zsh/auto.sh b/etc/zsh/auto.sh new file mode 100644 index 0000000..84896f9 --- /dev/null +++ b/etc/zsh/auto.sh @@ -0,0 +1,64 @@ +export PYTHONPATH=$PYTHONPATH:$PROJDIR/src:$PROJDIR/src/dj + +function push() { + CWD=$(pwd) + cd "${1:-$PROJDIR}" >> /dev/null || return; +} + +function pop() { + cd "${CWD:-$PROJDIR}" >> /dev/null || return; + unset CWD +} + + +function manage() { + push "$PROJDIR"/src/dj/ + python manage.py "$*" + response=$? + pop + return $response +} + +function run() { + push "$PROJDIR"/src/dj/ + manage runserver + pop +} + +function fmt() { + push "$PROJDIR"/src/dj/ + black . + pop +} + +function 0() { + cd "$PROJDIR" || return; +} + +function migrate() { + manage migrate $* +} + +function recreatedb() { + psql -h 127.0.0.1 -d postgres -c "CREATE USER root;" + psql -h 127.0.0.1 -d postgres -c "ALTER USER root WITH SUPERUSER;" + psql -h 127.0.0.1 -c "DROP DATABASE IF EXISTS fpm_controller;" template1 + psql -h 127.0.0.1 -c "CREATE DATABASE fpm_controller;" template1 + migrate $* +} + +function makemigrations() { + manage makemigrations $* +} + +function djshell() { + manage shell +} + +function dbshell() { + manage dbshell +} + +function createsuperuser() { + manage createsuperuser +} diff --git a/requirements.txt b/requirements.txt index 7e8375c..cc63f0f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,5 @@ Django==4.0.4 black==22.3.0 -psycopg2==2.9.3 \ No newline at end of file +psycopg2==2.9.3 +ipython==8.4.0 +pre-commit==2.19.0 \ No newline at end of file diff --git a/src/dj/fpm/__init__.py b/src/dj/fpm/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/dj/fpm/admin.py b/src/dj/fpm/admin.py new file mode 100644 index 0000000..dec8c8f --- /dev/null +++ b/src/dj/fpm/admin.py @@ -0,0 +1,8 @@ +from django.contrib import admin +from fpm.models import Package + + +@admin.register(Package) +class PackageAdmin(admin.ModelAdmin): + list_display = ("name", "plan", "hours", "status") + exclude = ("created_at", "updated_at") diff --git a/src/dj/fpm/apps.py b/src/dj/fpm/apps.py new file mode 100644 index 0000000..b06ed96 --- /dev/null +++ b/src/dj/fpm/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class FpmConfig(AppConfig): + default_auto_field = "django.db.models.BigAutoField" + name = "fpm" diff --git a/src/dj/fpm/migrations/0001_initial.py b/src/dj/fpm/migrations/0001_initial.py new file mode 100644 index 0000000..2508317 --- /dev/null +++ b/src/dj/fpm/migrations/0001_initial.py @@ -0,0 +1,109 @@ +# Generated by Django 4.0.4 on 2022-06-15 10:41 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [] + + operations = [ + migrations.CreateModel( + name="Package", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ( + "name", + models.CharField( + help_text="name of the FPM package", max_length=255, unique=True + ), + ), + ( + "git", + models.CharField( + help_text="git url of FPM package", max_length=1023 + ), + ), + ( + "hash", + models.CharField( + blank=True, + help_text="latest deployed hash of git", + max_length=512, + null=True, + ), + ), + ( + "plan", + models.CharField( + help_text="We have different plans like free, paid for serving FPM package", + max_length=255, + ), + ), + ("hours", models.IntegerField(default=0)), + ( + "status", + models.CharField( + choices=[ + ("deployed", "Package Deployed"), + ("in_dev", "In development state"), + ], + help_text="status of the package", + max_length=255, + ), + ), + ("created_at", models.DateTimeField(auto_now_add=True)), + ("updated_at", models.DateTimeField(auto_now=True)), + ], + ), + migrations.CreateModel( + name="DedicatedInstance", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("ec2_reservation", models.CharField(db_index=True, max_length=255)), + ( + "ec2_instance_id", + models.CharField(blank=True, max_length=255, null=True), + ), + ( + "status", + models.CharField( + choices=[ + ("initializing", "Instance Initializing"), + ("ready", "Instance ready to serve"), + ("failed", "Failed to initialize"), + ], + help_text="status of EC2 instance, ready, initializing, ect...", + max_length=127, + ), + ), + ("created_at", models.DateTimeField(auto_now_add=True)), + ("updated_at", models.DateTimeField(auto_now=True)), + ( + "package", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, to="fpm.package" + ), + ), + ], + ), + ] diff --git a/src/dj/fpm/migrations/__init__.py b/src/dj/fpm/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/dj/fpm/models.py b/src/dj/fpm/models.py new file mode 100644 index 0000000..b1a181d --- /dev/null +++ b/src/dj/fpm/models.py @@ -0,0 +1,47 @@ +from django.db import models + +# Create your models here. + +package_status = [("deployed", "Package Deployed"), ("in_dev", "In development state")] + +instance_status = [ + ("initializing", "Instance Initializing"), + ("ready", "Instance ready to serve"), + ("failed", "Failed to initialize"), +] + + +class Package(models.Model): + name = models.CharField( + unique=True, help_text="name of the FPM package", max_length=255 + ) + git = models.CharField(help_text="git url of FPM package", max_length=1023) + hash = models.CharField( + null=True, blank=True, help_text="latest deployed hash of git", max_length=512 + ) + plan = models.CharField( + help_text="We have different plans like free, paid for serving FPM package", + max_length=255, + ) + hours = models.IntegerField(default=0) + status = models.CharField( + help_text="status of the package", max_length=255, choices=package_status + ) + created_at = models.DateTimeField(auto_now_add=True) + updated_at = models.DateTimeField(auto_now=True) + + def __str__(self): + return self.name + + +class DedicatedInstance(models.Model): + package = models.ForeignKey(Package, on_delete=models.CASCADE) + ec2_reservation = models.CharField(db_index=True, max_length=255) + ec2_instance_id = models.CharField(null=True, blank=True, max_length=255) + status = models.CharField( + help_text="status of EC2 instance, ready, initializing, ect...", + max_length=127, + choices=instance_status, + ) + created_at = models.DateTimeField(auto_now_add=True) + updated_at = models.DateTimeField(auto_now=True) diff --git a/src/dj/fpm/tests.py b/src/dj/fpm/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/src/dj/fpm/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/src/dj/fpm/urls.py b/src/dj/fpm/urls.py new file mode 100644 index 0000000..a625966 --- /dev/null +++ b/src/dj/fpm/urls.py @@ -0,0 +1,8 @@ +from django.urls import path + +from . import views + +urlpatterns = [ + path(r"fpm-ready", views.fpm_ready), + path(r"get-package", views.get_package), +] diff --git a/src/dj/fpm/views.py b/src/dj/fpm/views.py new file mode 100644 index 0000000..e27889c --- /dev/null +++ b/src/dj/fpm/views.py @@ -0,0 +1,60 @@ +import django.http +from django.http import JsonResponse +from fpm.models import DedicatedInstance + +# Create your views here. + + +def success(data, status=200): + return JsonResponse({"result": data, "success": True}, status=status) + + +def error(message, status=500): + return JsonResponse({"message": message, "success": False}, status=status) + + +def fpm_ready(req: django.http.HttpRequest): + + if req.method != "GET": + return error("request method is not supported", status=402) + + ec2_reservation = req.GET.get("ec2_reservation", None) + git_hash = req.GET.get("hash", None) + + if not ec2_reservation: + return error("ec2_reservation is mandatory parameter", status=402) + + if not git_hash: + return error("hash is mandatory parameter", status=402) + + try: + instance: DedicatedInstance = DedicatedInstance.objects.get( + ec2_reservation=ec2_reservation + ) + except: + return error("instance with ec2_reservation id not found", status=404) + + instance.package.hash = git_hash + instance.status = "ready" + instance.save() + + return success({}) + + +def get_package(req: django.http.HttpRequest): + if req.method != "GET": + return error("request method is not supported", status=402) + + ec2_reservation = req.GET.get("ec2_reservation", None) + + if not ec2_reservation: + return error("ec2_reservation is mandatory parameter", status=402) + + try: + instance: DedicatedInstance = DedicatedInstance.objects.get( + ec2_reservation=ec2_reservation + ) + except: + return error("instance with ec2_reservation id not found", status=404) + + return success({"package": instance.package.name, "git": instance.package.git}) diff --git a/src/dj/manage.py b/src/dj/manage.py index 96864bb..ce21b31 100755 --- a/src/dj/manage.py +++ b/src/dj/manage.py @@ -6,7 +6,7 @@ def main(): """Run administrative tasks.""" - os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings') + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "proj.settings") try: from django.core.management import execute_from_command_line except ImportError as exc: @@ -18,5 +18,5 @@ def main(): execute_from_command_line(sys.argv) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/src/dj/proj/asgi.py b/src/dj/proj/asgi.py index b0f11c2..bb6c1ea 100644 --- a/src/dj/proj/asgi.py +++ b/src/dj/proj/asgi.py @@ -11,6 +11,6 @@ from django.core.asgi import get_asgi_application -os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings') +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "proj.settings") application = get_asgi_application() diff --git a/src/dj/proj/settings.py b/src/dj/proj/settings.py index 9816ccc..010c3fc 100644 --- a/src/dj/proj/settings.py +++ b/src/dj/proj/settings.py @@ -20,7 +20,7 @@ # See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = 'django-insecure-edztux3%v77%_-4fa*w&14+%$&vf#90vr=l)k_1*69+u#@ojga' +SECRET_KEY = "django-insecure-edztux3%v77%_-4fa*w&14+%$&vf#90vr=l)k_1*69+u#@ojga" # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True @@ -31,52 +31,57 @@ # Application definition INSTALLED_APPS = [ - 'django.contrib.admin', - 'django.contrib.auth', - 'django.contrib.contenttypes', - 'django.contrib.sessions', - 'django.contrib.messages', - 'django.contrib.staticfiles', + "django.contrib.admin", + "django.contrib.auth", + "django.contrib.contenttypes", + "django.contrib.sessions", + "django.contrib.messages", + "django.contrib.staticfiles", + "fpm", ] MIDDLEWARE = [ - 'django.middleware.security.SecurityMiddleware', - 'django.contrib.sessions.middleware.SessionMiddleware', - 'django.middleware.common.CommonMiddleware', - 'django.middleware.csrf.CsrfViewMiddleware', - 'django.contrib.auth.middleware.AuthenticationMiddleware', - 'django.contrib.messages.middleware.MessageMiddleware', - 'django.middleware.clickjacking.XFrameOptionsMiddleware', + "django.middleware.security.SecurityMiddleware", + "django.contrib.sessions.middleware.SessionMiddleware", + "django.middleware.common.CommonMiddleware", + "django.middleware.csrf.CsrfViewMiddleware", + "django.contrib.auth.middleware.AuthenticationMiddleware", + "django.contrib.messages.middleware.MessageMiddleware", + "django.middleware.clickjacking.XFrameOptionsMiddleware", ] -ROOT_URLCONF = 'proj.urls' +ROOT_URLCONF = "proj.urls" TEMPLATES = [ { - 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [], - 'APP_DIRS': True, - 'OPTIONS': { - 'context_processors': [ - 'django.template.context_processors.debug', - 'django.template.context_processors.request', - 'django.contrib.auth.context_processors.auth', - 'django.contrib.messages.context_processors.messages', + "BACKEND": "django.template.backends.django.DjangoTemplates", + "DIRS": [], + "APP_DIRS": True, + "OPTIONS": { + "context_processors": [ + "django.template.context_processors.debug", + "django.template.context_processors.request", + "django.contrib.auth.context_processors.auth", + "django.contrib.messages.context_processors.messages", ], }, }, ] -WSGI_APPLICATION = 'proj.wsgi.application' +WSGI_APPLICATION = "proj.wsgi.application" # Database # https://docs.djangoproject.com/en/4.0/ref/settings/#databases DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': BASE_DIR / 'db.sqlite3', + "default": { + "ENGINE": "django.db.backends.postgresql", + "NAME": "fpm_controller", + "USER": "root", + "PASSWORD": "", + "HOST": "127.0.0.1", + "PORT": 5432, } } @@ -86,16 +91,16 @@ AUTH_PASSWORD_VALIDATORS = [ { - 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator", }, { - 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator", }, { - 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator", }, { - 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator", }, ] @@ -103,9 +108,9 @@ # Internationalization # https://docs.djangoproject.com/en/4.0/topics/i18n/ -LANGUAGE_CODE = 'en-us' +LANGUAGE_CODE = "en-us" -TIME_ZONE = 'UTC' +TIME_ZONE = "UTC" USE_I18N = True @@ -115,9 +120,9 @@ # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/4.0/howto/static-files/ -STATIC_URL = 'static/' +STATIC_URL = "static/" # Default primary key field type # https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field -DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' +DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" diff --git a/src/dj/proj/urls.py b/src/dj/proj/urls.py index 215db06..e7a684b 100644 --- a/src/dj/proj/urls.py +++ b/src/dj/proj/urls.py @@ -14,8 +14,10 @@ 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin -from django.urls import path +from django.urls import path, include + urlpatterns = [ - path('admin/', admin.site.urls), + path("admin/", admin.site.urls), + path(r"v1/fpm/", include("fpm.urls")), ] diff --git a/src/dj/proj/wsgi.py b/src/dj/proj/wsgi.py index 3fefe7a..57bbb97 100644 --- a/src/dj/proj/wsgi.py +++ b/src/dj/proj/wsgi.py @@ -11,6 +11,6 @@ from django.core.wsgi import get_wsgi_application -os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings') +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "proj.settings") application = get_wsgi_application()