import psycopg2 from django.http import HttpResponse from django.views.decorators.http import require_POST from apps.kubernetes_connection import apps as kubernetes_connection import datetime from apps.cloudmanager import credentials import os def hello(request): kubernetes_connection.echoHello(); return HttpResponse('

Hello cmm !

') def DB_generation(host_in,port_in,user_in,password_in,db_in): db = psycopg2.connect(host=host_in, port=port_in, user=user_in, password=password_in,database=db_in) return db @require_POST def change_schedule(request): # HTMX sends these via standard POST data schedule_id = request.POST.get('schedule_id') server_name = request.POST.get('server_name') if not schedule_id or not server_name: return HttpResponse("Missing parameters.", status=400) try: # --- Your Core Logic Here --- # e.g., Server.objects.filter(name=server_name).update(schedule_id=schedule_id) print(f"Updating {server_name} to schedule profile {schedule_id}") # Return a success message that will instantly inject into the server's status div return HttpResponse("✓ Updated successfully!") except Exception as e: return HttpResponse(f"Error: {str(e)}", status=500) def Launch_sql_query(sql,db): # prepare a cursor object using cursor() method cursor = db.cursor() try: # Execute the SQL command cursor.execute(sql) # Fetch all the rows in a list of lists. results = cursor.fetchall() result_machine = [] #result_machine_type = [] for row in results: result_machine_type = [] namespace = row[0] name = row[1] replicas = row[2] result_machine_type.append(namespace) result_machine_type.append(name) result_machine_type.append(replicas) result_machine.append(result_machine_type) return result_machine except: print ("Error: unable to fetch data") #Return the day of the week as an integer, where Monday is 0 and Sunday is 6. def Give_current_weekday(): days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday','Saturday', 'Sunday'] weekday = days[datetime.datetime.today().weekday()] return weekday def Give_current_hour(): now = datetime.datetime.now() return now.hour def Check_server_supposed_XXX(generated_db_in,wanted_status,Wanted_hour,Wanted_day): sql_in="" if wanted_status == "up": sql_in = "select machine.namespace, machine.name, machine.replicas from scenario_hour,machine ,scenario_day_of_week where state_at_" + str(Wanted_hour) + "='1' and state_" + str(Wanted_day) + "='1' and machine.hour_behaviour_id=scenario_hour.id_scenario and machine.day_behaviour_id=scenario_day_of_week.id_scenario;" elif wanted_status == "down": sql_in = "select machine.namespace, machine.name, machine.replicas from scenario_hour,machine ,scenario_day_of_week where not(state_at_" + str(Wanted_hour) + "='1' and state_" + str(Wanted_day) + "='1') and machine.hour_behaviour_id=scenario_hour.id_scenario and machine.day_behaviour_id=scenario_day_of_week.id_scenario;" else: print("error value") print(sql_in) return_value = Launch_sql_query(sql_in, generated_db_in) return return_value def Is_the_server_already_wanted_state(id_machine, wanted_state,instance_type): #stopped/running bool_result = "" proxmox = function.proxmox_connection(credentials.proxmox.zeus.url_proxmox,credentials.proxmox.zeus.user,credentials.proxmox.zeus.password) if str(function.check_state_mix(id_machine,wanted_state,proxmox,instance_type)) == str(wanted_state): bool_result = True else: bool_result = False return bool_result def all_operations(request,wanted_state,list_machine): for machine_target in list_machine: print(machine_target[1]) info_to_display = "" if wanted_state == "up": kubernetes_connection.scale_up(request,namespace_name=machine_target[0],deploy_name=machine_target[1],replicas=machine_target[2]); elif wanted_state == "down": kubernetes_connection.scale_down(request,namespace_name=machine_target[0],deploy_name=machine_target[1]); else: print("Incorrect wanted state") # if not(Is_the_server_already_wanted_state(machine_target[0], wanted_state,machine_target[1])): # function.action(False,machine_target[0],operation_to_perform,machine_target[1]) # print(str(machine_target[0])+" is now "+str(wanted_state)) # elif Is_the_server_already_wanted_state(machine_target[0], wanted_state,machine_target[1]): # info_to_display = "operation already performed for "+machine_target[2] # print(info_to_display) #return info_to_display return "" def main(request): #get credentials from the env 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')) 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("------------------------------------------") print("-----------------UP-----------------------") print("------------------------------------------") print(list_servers_up) print(all_operations(request,"up", list_servers_up)) ## Server(s) supposed to be DOWN list_servers_down = Check_server_supposed_XXX(generated_db, "down", Give_current_hour(),Give_current_weekday()) print("------------------------------------------") print("----------------DOWN----------------------") print("------------------------------------------") print(list_servers_down) print(all_operations(request,"down",list_servers_down)) # disconnect from server generated_db.close() #print(Is_the_server_already_wanted_state(112, "stopped")) return HttpResponse('

CMM executed!

') if __name__ == "__main__": main() """ TO DO: EXTRACT TYPE OF MACHINE CREATE FUNCTION TO CHECK IF A MACHINE IS ALREADY DOWN OR NOT ADD FIELD IN SQL WITH PROJECT AND PUT DESCRIPTION -> ATTACH MACHINE TO PROJECT """