diff --git a/django_code/apps/cloudmanager/admin.py b/django_code/apps/cloudmanager/admin.py index 4f0ff4b..8b08610 100644 --- a/django_code/apps/cloudmanager/admin.py +++ b/django_code/apps/cloudmanager/admin.py @@ -90,6 +90,7 @@ class DeploymentDataAdmin(ImportExportModelAdmin,ExtraButtonsMixin, MultiDBModel list_display = ('name', 'namespace', 'schedule_week', 'schedule_day', 'replicas') list_filter = (ActiveAtHourFilter,) actions = [Check_server_supposed_False,Check_server_supposed_true,scale_up_deploy,scale_down_deploy] + ordering = ('name',) @button() def retrieve_and_apply(self, request): # your business logic here diff --git a/django_code/apps/cloudmanager/migrations/0003_deployment_data_do_not_touch_until.py b/django_code/apps/cloudmanager/migrations/0003_deployment_data_do_not_touch_until.py new file mode 100644 index 0000000..7fcedba --- /dev/null +++ b/django_code/apps/cloudmanager/migrations/0003_deployment_data_do_not_touch_until.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.29 on 2026-09-13 15:38 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('cloudmanager', '0002_deployment_data_actual_replica'), + ] + + operations = [ + migrations.AddField( + model_name='deployment_data', + name='do_not_touch_until', + field=models.DateField(blank=True, null=True, verbose_name='do_not_touch_until'), + ), + ] diff --git a/django_code/apps/cloudmanager/migrations/0004_alter_deployment_data_do_not_touch_until.py b/django_code/apps/cloudmanager/migrations/0004_alter_deployment_data_do_not_touch_until.py new file mode 100644 index 0000000..4fd645a --- /dev/null +++ b/django_code/apps/cloudmanager/migrations/0004_alter_deployment_data_do_not_touch_until.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.30 on 2026-09-14 13:59 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('cloudmanager', '0003_deployment_data_do_not_touch_until'), + ] + + operations = [ + migrations.AlterField( + model_name='deployment_data', + name='do_not_touch_until', + field=models.DateTimeField(auto_now_add=True, null=True, verbose_name='do_not_touch_until'), + ), + ] diff --git a/django_code/apps/cloudmanager/migrations/0005_alter_deployment_data_do_not_touch_until.py b/django_code/apps/cloudmanager/migrations/0005_alter_deployment_data_do_not_touch_until.py new file mode 100644 index 0000000..691c765 --- /dev/null +++ b/django_code/apps/cloudmanager/migrations/0005_alter_deployment_data_do_not_touch_until.py @@ -0,0 +1,19 @@ +# Generated by Django 4.2.30 on 2026-09-14 14:14 + +import datetime +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('cloudmanager', '0004_alter_deployment_data_do_not_touch_until'), + ] + + operations = [ + migrations.AlterField( + model_name='deployment_data', + name='do_not_touch_until', + field=models.DateTimeField(default=datetime.datetime(2026, 9, 14, 14, 14, 29, 566084, tzinfo=datetime.timezone.utc), verbose_name='do_not_touch_until'), + ), + ] diff --git a/django_code/apps/cloudmanager/migrations/0006_alter_deployment_data_do_not_touch_until.py b/django_code/apps/cloudmanager/migrations/0006_alter_deployment_data_do_not_touch_until.py new file mode 100644 index 0000000..9a5ec42 --- /dev/null +++ b/django_code/apps/cloudmanager/migrations/0006_alter_deployment_data_do_not_touch_until.py @@ -0,0 +1,19 @@ +# Generated by Django 4.2.30 on 2026-09-14 14:14 + +import datetime +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('cloudmanager', '0005_alter_deployment_data_do_not_touch_until'), + ] + + operations = [ + migrations.AlterField( + model_name='deployment_data', + name='do_not_touch_until', + field=models.DateTimeField(default=datetime.datetime(2026, 9, 14, 14, 14, 33, 642627, tzinfo=datetime.timezone.utc), verbose_name='do_not_touch_until'), + ), + ] diff --git a/django_code/apps/cloudmanager/migrations/0007_alter_deployment_data_do_not_touch_until.py b/django_code/apps/cloudmanager/migrations/0007_alter_deployment_data_do_not_touch_until.py new file mode 100644 index 0000000..4beccb8 --- /dev/null +++ b/django_code/apps/cloudmanager/migrations/0007_alter_deployment_data_do_not_touch_until.py @@ -0,0 +1,19 @@ +# Generated by Django 4.2.30 on 2026-09-14 14:35 + +import datetime +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('cloudmanager', '0006_alter_deployment_data_do_not_touch_until'), + ] + + operations = [ + migrations.AlterField( + model_name='deployment_data', + name='do_not_touch_until', + field=models.DateTimeField(default=datetime.datetime(2026, 9, 14, 14, 35, 47, 473312, tzinfo=datetime.timezone.utc), verbose_name='do_not_touch_until'), + ), + ] diff --git a/django_code/apps/cloudmanager/migrations/0008_alter_deployment_data_do_not_touch_until.py b/django_code/apps/cloudmanager/migrations/0008_alter_deployment_data_do_not_touch_until.py new file mode 100644 index 0000000..35b1ec7 --- /dev/null +++ b/django_code/apps/cloudmanager/migrations/0008_alter_deployment_data_do_not_touch_until.py @@ -0,0 +1,19 @@ +# Generated by Django 4.2.30 on 2026-09-14 14:35 + +import datetime +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('cloudmanager', '0007_alter_deployment_data_do_not_touch_until'), + ] + + operations = [ + migrations.AlterField( + model_name='deployment_data', + name='do_not_touch_until', + field=models.DateTimeField(default=datetime.datetime(2026, 9, 14, 14, 35, 51, 657797, tzinfo=datetime.timezone.utc), verbose_name='do_not_touch_until'), + ), + ] diff --git a/django_code/apps/cloudmanager/migrations/0009_alter_deployment_data_do_not_touch_until.py b/django_code/apps/cloudmanager/migrations/0009_alter_deployment_data_do_not_touch_until.py new file mode 100644 index 0000000..c92d155 --- /dev/null +++ b/django_code/apps/cloudmanager/migrations/0009_alter_deployment_data_do_not_touch_until.py @@ -0,0 +1,19 @@ +# Generated by Django 4.2.30 on 2026-09-14 14:43 + +import datetime +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('cloudmanager', '0008_alter_deployment_data_do_not_touch_until'), + ] + + operations = [ + migrations.AlterField( + model_name='deployment_data', + name='do_not_touch_until', + field=models.DateTimeField(default=datetime.datetime(2026, 9, 14, 14, 43, 26, 985719, tzinfo=datetime.timezone.utc), verbose_name='do_not_touch_until'), + ), + ] diff --git a/django_code/apps/cloudmanager/migrations/0010_alter_deployment_data_do_not_touch_until.py b/django_code/apps/cloudmanager/migrations/0010_alter_deployment_data_do_not_touch_until.py new file mode 100644 index 0000000..d254818 --- /dev/null +++ b/django_code/apps/cloudmanager/migrations/0010_alter_deployment_data_do_not_touch_until.py @@ -0,0 +1,19 @@ +# Generated by Django 4.2.30 on 2026-09-14 14:43 + +import datetime +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('cloudmanager', '0009_alter_deployment_data_do_not_touch_until'), + ] + + operations = [ + migrations.AlterField( + model_name='deployment_data', + name='do_not_touch_until', + field=models.DateTimeField(default=datetime.datetime(2026, 9, 14, 14, 43, 31, 55115, tzinfo=datetime.timezone.utc), verbose_name='do_not_touch_until'), + ), + ] diff --git a/django_code/apps/cloudmanager/migrations/0011_alter_deployment_data_do_not_touch_until.py b/django_code/apps/cloudmanager/migrations/0011_alter_deployment_data_do_not_touch_until.py new file mode 100644 index 0000000..5306b27 --- /dev/null +++ b/django_code/apps/cloudmanager/migrations/0011_alter_deployment_data_do_not_touch_until.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.30 on 2026-09-14 14:46 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('cloudmanager', '0010_alter_deployment_data_do_not_touch_until'), + ] + + operations = [ + migrations.AlterField( + model_name='deployment_data', + name='do_not_touch_until', + field=models.DateTimeField(auto_now_add=True, null=True, verbose_name='do_not_touch_until'), + ), + ] diff --git a/django_code/apps/cloudmanager/migrations/0012_alter_deployment_data_do_not_touch_until.py b/django_code/apps/cloudmanager/migrations/0012_alter_deployment_data_do_not_touch_until.py new file mode 100644 index 0000000..2235739 --- /dev/null +++ b/django_code/apps/cloudmanager/migrations/0012_alter_deployment_data_do_not_touch_until.py @@ -0,0 +1,19 @@ +# Generated by Django 4.2.30 on 2026-09-14 15:13 + +from django.db import migrations, models +import django.utils.timezone + + +class Migration(migrations.Migration): + + dependencies = [ + ('cloudmanager', '0011_alter_deployment_data_do_not_touch_until'), + ] + + operations = [ + migrations.AlterField( + model_name='deployment_data', + name='do_not_touch_until', + field=models.DateTimeField(default=django.utils.timezone.now, verbose_name='do_not_touch_until'), + ), + ] diff --git a/django_code/apps/cloudmanager/models.py b/django_code/apps/cloudmanager/models.py index 3a05b3f..5eed0c7 100644 --- a/django_code/apps/cloudmanager/models.py +++ b/django_code/apps/cloudmanager/models.py @@ -1,7 +1,5 @@ from django.db import models - - - +from django.utils import timezone class Schedule_Week_Data(models.Model): name_schedule = models.CharField(max_length=100,unique=True) @@ -55,6 +53,7 @@ class Deployment_Data(models.Model): schedule_day = models.ForeignKey(Schedule_Day_Data,null=True, blank=True, on_delete=models.SET_NULL) replicas = models.IntegerField() actual_replica = models.IntegerField() + do_not_touch_until = models.DateTimeField('do_not_touch_until', default=timezone.now) class Meta: db_table = 'machine' diff --git a/django_code/apps/cloudmanager/urls.py b/django_code/apps/cloudmanager/urls.py index 2b05d99..c8869f0 100644 --- a/django_code/apps/cloudmanager/urls.py +++ b/django_code/apps/cloudmanager/urls.py @@ -16,15 +16,14 @@ Including another URLconf """ from django.urls import path -from .views import coucou,display,hello,import_namespace_view,retrieve_and_apply_view +from .views import hello,import_namespace_view,retrieve_and_apply_view,scale_up_gui,scale_down_gui urlpatterns = [ ################### cloudmanager ################### path('', hello, name='hello'), - path('coucou', coucou, name='coucou'), - path('display', display, name='display'), path('import_namespace', import_namespace_view, name='import_namespace'), path('retrieve_and_apply_view', retrieve_and_apply_view, name='retrieve_and_apply_view'), - + path('scale_up//', scale_up_gui), + path('scale_down//', scale_down_gui), ] diff --git a/django_code/apps/cloudmanager/utils.py b/django_code/apps/cloudmanager/utils.py index cebfe21..497b229 100644 --- a/django_code/apps/cloudmanager/utils.py +++ b/django_code/apps/cloudmanager/utils.py @@ -1,5 +1,5 @@ -import datetime - +from django.utils import timezone +import datetime from apps.kubernetes_connection.utils import get_namespace,get_deployments,scale_up,scale_down from apps.cloudmanager.models import Schedule_Day_Data,Schedule_Week_Data,Namespace_Data,Deployment_Data @@ -113,6 +113,8 @@ def retrieve_and_apply(verbose=False): #check if the expected replica (server[2]) match the current replica of the deployment (server[3].actual_replica) if server[3].actual_replica == server[2]: if verbose : print("name: "+str(server[1])+" namespace: "+str(server[0])+" already UP") + elif server[3].do_not_touch_until > timezone.now(): + if verbose : print("name: "+str(server[1])+" namespace: "+str(server[0])+" is in freeze period") else: echo = "Starting -> name: "+str(server[1])+" namespace: "+str(server[0])+" replicas: "+str(server[2]) output += str(echo) @@ -129,6 +131,8 @@ def retrieve_and_apply(verbose=False): #check if the current replica of the deployment (server[3].actual_replica) is already 0 meaning the server is already down if server[3].actual_replica == 0: if verbose : print("name: "+str(server[1])+" namespace: "+str(server[0])+" already DOWN") + elif server[3].do_not_touch_until > timezone.now(): + if verbose : print("name: "+str(server[1])+" namespace: "+str(server[0])+" is in freeze period") else: echo = "Shutting down -> name: "+str(server[1])+" namespace: "+str(server[0])+" replicas: "+str(server[2]) diff --git a/django_code/apps/cloudmanager/views.py b/django_code/apps/cloudmanager/views.py index 484e67b..c888453 100644 --- a/django_code/apps/cloudmanager/views.py +++ b/django_code/apps/cloudmanager/views.py @@ -1,30 +1,11 @@ from django.http import HttpResponse from django.shortcuts import render +from datetime import timedelta +from django.utils import timezone -# IMPORT AT THE TOP HERE: This is completely safe! -from .models import Deployment_Data,Schedule_Week_Data -from .db_interaction import DB_generation,Check_server_supposed_XXX,Give_current_weekday,Give_current_hour from .utils import import_namespace,retrieve_and_apply - -def coucou(request): - # 1. Prepare the data inside the function - data = CmmData( - name="test", - namespace="testspace", - schedule_hour="1", - schedule_day="deux", - replicas=3 - ) - - # 2. This line actually pushes the data into your cloudmanager_db! - data.save() - - return HttpResponse('

Hello cmm! Data saved successfully!

') - -def display(request): - data = CmmData.objects.all() - print(data[0].namespace) - return HttpResponse(data) +from apps.kubernetes_connection.utils import scale_up,scale_down +from .models import Deployment_Data def hello(request): list_schedule = ['schedule1','schedule2'] @@ -40,18 +21,14 @@ def retrieve_and_apply_view(request): output = retrieve_and_apply() return HttpResponse('

'+output+'

') -def redirect(request): - db_host = os.environ.get('DB_HOST') - db_port = os.environ.get('DB_PORT') - db_user = os.environ.get('DB_USERNAME') - db_password = os.environ.get('DB_PASSWORD') - db_name = os.environ.get('CUSTOM_DB_DATABASE',os.environ.get('DB_DATABASE')) +def scale_up_gui(request,namespace_name,deploy_name,replicas=1): + Deployment_Data.objects.filter(namespace=namespace_name,name=deploy_name).update(do_not_touch_until=timezone.now() + timedelta(hours=1)) + scale_up(namespace_name=namespace_name,deploy_name=deploy_name,replicas=replicas) + return render(request, 'dashboard/start.html', {'namespace':namespace_name,'deployment': deploy_name, 'url': 'https://namespace.com'}) - generated_db = DB_generation(db_host, db_port, db_user, db_password,db_name) - ##operation: - ## Server(s) supposed to be UP - - list_servers_up = Check_server_supposed_XXX(generated_db, "up", Give_current_hour(),Give_current_weekday()) - print(list_servers_up) - return render(request, 'cloudmanager/cmm_blank_page.html', {'list_servers_up':list_servers_up}) +def scale_down_gui(request,namespace_name,deploy_name): + Deployment_Data.objects.filter(namespace=namespace_name,name=deploy_name).update(do_not_touch_until=timezone.now() + timedelta(hours=1)) + scale_down(namespace_name=namespace_name,deploy_name=deploy_name) + return render(request, 'dashboard/shutdown.html', + {'namespace': namespace_name, 'deployment': deploy_name, 'url': 'https://namespace.com'})