Skip to content

Commit 3fbbce3

Browse files
Merge pull request #1 from FifthTry/feature/fpm-controller
FPM controller
2 parents 6227ddf + a7819a0 commit 3fbbce3

18 files changed

+364
-43
lines changed

.autoenv.zsh

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export PROJDIR=$(pwd)
2+
source "$PROJDIR"/etc/zsh/auto.sh

.pre-commit-config.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
repos:
2+
- repo: https://github.com/psf/black
3+
rev: 22.3.0
4+
hooks:
5+
- id: black

etc/zsh/auto.sh

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
export PYTHONPATH=$PYTHONPATH:$PROJDIR/src:$PROJDIR/src/dj
2+
3+
function push() {
4+
CWD=$(pwd)
5+
cd "${1:-$PROJDIR}" >> /dev/null || return;
6+
}
7+
8+
function pop() {
9+
cd "${CWD:-$PROJDIR}" >> /dev/null || return;
10+
unset CWD
11+
}
12+
13+
14+
function manage() {
15+
push "$PROJDIR"/src/dj/
16+
python manage.py "$*"
17+
response=$?
18+
pop
19+
return $response
20+
}
21+
22+
function run() {
23+
push "$PROJDIR"/src/dj/
24+
manage runserver
25+
pop
26+
}
27+
28+
function fmt() {
29+
push "$PROJDIR"/src/dj/
30+
black .
31+
pop
32+
}
33+
34+
function 0() {
35+
cd "$PROJDIR" || return;
36+
}
37+
38+
function migrate() {
39+
manage migrate $*
40+
}
41+
42+
function recreatedb() {
43+
psql -h 127.0.0.1 -d postgres -c "CREATE USER root;"
44+
psql -h 127.0.0.1 -d postgres -c "ALTER USER root WITH SUPERUSER;"
45+
psql -h 127.0.0.1 -c "DROP DATABASE IF EXISTS fpm_controller;" template1
46+
psql -h 127.0.0.1 -c "CREATE DATABASE fpm_controller;" template1
47+
migrate $*
48+
}
49+
50+
function makemigrations() {
51+
manage makemigrations $*
52+
}
53+
54+
function djshell() {
55+
manage shell
56+
}
57+
58+
function dbshell() {
59+
manage dbshell
60+
}
61+
62+
function createsuperuser() {
63+
manage createsuperuser
64+
}

requirements.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
Django==4.0.4
22
black==22.3.0
3-
psycopg2==2.9.3
3+
psycopg2==2.9.3
4+
ipython==8.4.0
5+
pre-commit==2.19.0

src/dj/fpm/__init__.py

Whitespace-only changes.

src/dj/fpm/admin.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from django.contrib import admin
2+
from fpm.models import Package
3+
4+
5+
@admin.register(Package)
6+
class PackageAdmin(admin.ModelAdmin):
7+
list_display = ("name", "plan", "hours", "status")
8+
exclude = ("created_at", "updated_at")

src/dj/fpm/apps.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.apps import AppConfig
2+
3+
4+
class FpmConfig(AppConfig):
5+
default_auto_field = "django.db.models.BigAutoField"
6+
name = "fpm"

src/dj/fpm/migrations/0001_initial.py

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# Generated by Django 4.0.4 on 2022-06-15 10:41
2+
3+
from django.db import migrations, models
4+
import django.db.models.deletion
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
initial = True
10+
11+
dependencies = []
12+
13+
operations = [
14+
migrations.CreateModel(
15+
name="Package",
16+
fields=[
17+
(
18+
"id",
19+
models.BigAutoField(
20+
auto_created=True,
21+
primary_key=True,
22+
serialize=False,
23+
verbose_name="ID",
24+
),
25+
),
26+
(
27+
"name",
28+
models.CharField(
29+
help_text="name of the FPM package", max_length=255, unique=True
30+
),
31+
),
32+
(
33+
"git",
34+
models.CharField(
35+
help_text="git url of FPM package", max_length=1023
36+
),
37+
),
38+
(
39+
"hash",
40+
models.CharField(
41+
blank=True,
42+
help_text="latest deployed hash of git",
43+
max_length=512,
44+
null=True,
45+
),
46+
),
47+
(
48+
"plan",
49+
models.CharField(
50+
help_text="We have different plans like free, paid for serving FPM package",
51+
max_length=255,
52+
),
53+
),
54+
("hours", models.IntegerField(default=0)),
55+
(
56+
"status",
57+
models.CharField(
58+
choices=[
59+
("deployed", "Package Deployed"),
60+
("in_dev", "In development state"),
61+
],
62+
help_text="status of the package",
63+
max_length=255,
64+
),
65+
),
66+
("created_at", models.DateTimeField(auto_now_add=True)),
67+
("updated_at", models.DateTimeField(auto_now=True)),
68+
],
69+
),
70+
migrations.CreateModel(
71+
name="DedicatedInstance",
72+
fields=[
73+
(
74+
"id",
75+
models.BigAutoField(
76+
auto_created=True,
77+
primary_key=True,
78+
serialize=False,
79+
verbose_name="ID",
80+
),
81+
),
82+
("ec2_reservation", models.CharField(db_index=True, max_length=255)),
83+
(
84+
"ec2_instance_id",
85+
models.CharField(blank=True, max_length=255, null=True),
86+
),
87+
(
88+
"status",
89+
models.CharField(
90+
choices=[
91+
("initializing", "Instance Initializing"),
92+
("ready", "Instance ready to serve"),
93+
("failed", "Failed to initialize"),
94+
],
95+
help_text="status of EC2 instance, ready, initializing, ect...",
96+
max_length=127,
97+
),
98+
),
99+
("created_at", models.DateTimeField(auto_now_add=True)),
100+
("updated_at", models.DateTimeField(auto_now=True)),
101+
(
102+
"package",
103+
models.ForeignKey(
104+
on_delete=django.db.models.deletion.CASCADE, to="fpm.package"
105+
),
106+
),
107+
],
108+
),
109+
]

src/dj/fpm/migrations/__init__.py

Whitespace-only changes.

src/dj/fpm/models.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from django.db import models
2+
3+
# Create your models here.
4+
5+
package_status = [("deployed", "Package Deployed"), ("in_dev", "In development state")]
6+
7+
instance_status = [
8+
("initializing", "Instance Initializing"),
9+
("ready", "Instance ready to serve"),
10+
("failed", "Failed to initialize"),
11+
]
12+
13+
14+
class Package(models.Model):
15+
name = models.CharField(
16+
unique=True, help_text="name of the FPM package", max_length=255
17+
)
18+
git = models.CharField(help_text="git url of FPM package", max_length=1023)
19+
hash = models.CharField(
20+
null=True, blank=True, help_text="latest deployed hash of git", max_length=512
21+
)
22+
plan = models.CharField(
23+
help_text="We have different plans like free, paid for serving FPM package",
24+
max_length=255,
25+
)
26+
hours = models.IntegerField(default=0)
27+
status = models.CharField(
28+
help_text="status of the package", max_length=255, choices=package_status
29+
)
30+
created_at = models.DateTimeField(auto_now_add=True)
31+
updated_at = models.DateTimeField(auto_now=True)
32+
33+
def __str__(self):
34+
return self.name
35+
36+
37+
class DedicatedInstance(models.Model):
38+
package = models.ForeignKey(Package, on_delete=models.CASCADE)
39+
ec2_reservation = models.CharField(db_index=True, max_length=255)
40+
ec2_instance_id = models.CharField(null=True, blank=True, max_length=255)
41+
status = models.CharField(
42+
help_text="status of EC2 instance, ready, initializing, ect...",
43+
max_length=127,
44+
choices=instance_status,
45+
)
46+
created_at = models.DateTimeField(auto_now_add=True)
47+
updated_at = models.DateTimeField(auto_now=True)

src/dj/fpm/tests.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.test import TestCase
2+
3+
# Create your tests here.

src/dj/fpm/urls.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from django.urls import path
2+
3+
from . import views
4+
5+
urlpatterns = [
6+
path(r"fpm-ready", views.fpm_ready),
7+
path(r"get-package", views.get_package),
8+
]

src/dj/fpm/views.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import django.http
2+
from django.http import JsonResponse
3+
from fpm.models import DedicatedInstance
4+
5+
# Create your views here.
6+
7+
8+
def success(data, status=200):
9+
return JsonResponse({"result": data, "success": True}, status=status)
10+
11+
12+
def error(message, status=500):
13+
return JsonResponse({"message": message, "success": False}, status=status)
14+
15+
16+
def fpm_ready(req: django.http.HttpRequest):
17+
18+
if req.method != "GET":
19+
return error("request method is not supported", status=402)
20+
21+
ec2_reservation = req.GET.get("ec2_reservation", None)
22+
git_hash = req.GET.get("hash", None)
23+
24+
if not ec2_reservation:
25+
return error("ec2_reservation is mandatory parameter", status=402)
26+
27+
if not git_hash:
28+
return error("hash is mandatory parameter", status=402)
29+
30+
try:
31+
instance: DedicatedInstance = DedicatedInstance.objects.get(
32+
ec2_reservation=ec2_reservation
33+
)
34+
except:
35+
return error("instance with ec2_reservation id not found", status=404)
36+
37+
instance.package.hash = git_hash
38+
instance.status = "ready"
39+
instance.save()
40+
41+
return success({})
42+
43+
44+
def get_package(req: django.http.HttpRequest):
45+
if req.method != "GET":
46+
return error("request method is not supported", status=402)
47+
48+
ec2_reservation = req.GET.get("ec2_reservation", None)
49+
50+
if not ec2_reservation:
51+
return error("ec2_reservation is mandatory parameter", status=402)
52+
53+
try:
54+
instance: DedicatedInstance = DedicatedInstance.objects.get(
55+
ec2_reservation=ec2_reservation
56+
)
57+
except:
58+
return error("instance with ec2_reservation id not found", status=404)
59+
60+
return success({"package": instance.package.name, "git": instance.package.git})

src/dj/manage.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
def main():
88
"""Run administrative tasks."""
9-
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')
9+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "proj.settings")
1010
try:
1111
from django.core.management import execute_from_command_line
1212
except ImportError as exc:
@@ -18,5 +18,5 @@ def main():
1818
execute_from_command_line(sys.argv)
1919

2020

21-
if __name__ == '__main__':
21+
if __name__ == "__main__":
2222
main()

src/dj/proj/asgi.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111

1212
from django.core.asgi import get_asgi_application
1313

14-
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')
14+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "proj.settings")
1515

1616
application = get_asgi_application()

0 commit comments

Comments
 (0)