Skip to content

FPM controller #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jun 15, 2022
2 changes: 2 additions & 0 deletions .autoenv.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export PROJDIR=$(pwd)
source "$PROJDIR"/etc/zsh/auto.sh
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
repos:
- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
64 changes: 64 additions & 0 deletions etc/zsh/auto.sh
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Django==4.0.4
black==22.3.0
psycopg2==2.9.3
psycopg2==2.9.3
ipython==8.4.0
pre-commit==2.19.0
Empty file added src/dj/fpm/__init__.py
Empty file.
8 changes: 8 additions & 0 deletions src/dj/fpm/admin.py
Original file line number Diff line number Diff line change
@@ -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")
6 changes: 6 additions & 0 deletions src/dj/fpm/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class FpmConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "fpm"
109 changes: 109 additions & 0 deletions src/dj/fpm/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -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"
),
),
],
),
]
Empty file.
47 changes: 47 additions & 0 deletions src/dj/fpm/models.py
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 3 additions & 0 deletions src/dj/fpm/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
8 changes: 8 additions & 0 deletions src/dj/fpm/urls.py
Original file line number Diff line number Diff line change
@@ -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),
]
60 changes: 60 additions & 0 deletions src/dj/fpm/views.py
Original file line number Diff line number Diff line change
@@ -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})
4 changes: 2 additions & 2 deletions src/dj/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -18,5 +18,5 @@ def main():
execute_from_command_line(sys.argv)


if __name__ == '__main__':
if __name__ == "__main__":
main()
2 changes: 1 addition & 1 deletion src/dj/proj/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Loading