138 lines
5.1 KiB
Python
138 lines
5.1 KiB
Python
import psycopg2
|
|
from django.http import HttpResponse
|
|
from kubernetes_connection import apps as kubernetes_connection
|
|
import datetime
|
|
from cloudmanager import credentials
|
|
|
|
|
|
def hello(request):
|
|
kubernetes_connection.echoHello();
|
|
return HttpResponse('<h1>Hello cmm !</h1>')
|
|
|
|
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
|
|
|
|
|
|
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():
|
|
weekday = 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):
|
|
sql_in=""
|
|
if wanted_status == "up" or wanted_status == "start":
|
|
sql_in = "select machine.namespace, machine.name, machine.replicas from scenario_hour,machine where state_at_" + str(Wanted_hour) + "='1' and machine.hour_behaviour_id=scenario_hour.id_scenario;"
|
|
elif wanted_status == "down" or wanted_status == "stop":
|
|
sql_in = "select machine.namespace, machine.name, machine.replicas from scenario_hour,machine where state_at_" + str(Wanted_hour) + "='0' and machine.hour_behaviour_id=scenario_hour.id_scenario;"
|
|
else:
|
|
print("error value")
|
|
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(wanted_state,list_machine):
|
|
info_to_display = ""
|
|
if wanted_state == "running":
|
|
operation_to_perform = "start"
|
|
elif wanted_state == "stopped":
|
|
operation_to_perform = "stop"
|
|
else:
|
|
print("Incorrect wanted state")
|
|
|
|
|
|
for machine_target in list_machine:
|
|
print(machine_target[1])
|
|
kubernetes_connection.scale_up(main.request,namespace_name=machine_target[0],name=machine_target[1],replicas=machine_target[2]);
|
|
# 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):
|
|
generated_db = DB_generation(credentials.sql.url, credentials.sql.port, credentials.sql.user, credentials.sql.password,credentials.sql.database)
|
|
##operation:
|
|
kubernetes_connection.hello(request);
|
|
## Server(s) supposed to be UP
|
|
#Check_server_supposed_XXX(generated_db, "up", Give_current_hour())
|
|
|
|
list_servers_up = Check_server_supposed_XXX(generated_db, "up", Give_current_hour())
|
|
print("------------------------------------------")
|
|
print("-----------------UP-----------------------")
|
|
print("------------------------------------------")
|
|
print(list_servers_up)
|
|
print(all_operations("running", list_servers_up))
|
|
#
|
|
#
|
|
# ## Server(s) supposed to be DOWN
|
|
# list_servers_down = Check_server_supposed_XXX(generated_db, "down", Give_current_hour())
|
|
#
|
|
# print("------------------------------------------")
|
|
# print("----------------DOWN----------------------")
|
|
# print("------------------------------------------")
|
|
# print(list_servers_down)
|
|
# print(all_operations("stopped",list_servers_down))
|
|
#
|
|
#
|
|
# # disconnect from server
|
|
generated_db.close()
|
|
|
|
#print(Is_the_server_already_wanted_state(112, "stopped"))
|
|
HttpResponse('<h1>CMM executed!</h1>')
|
|
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
|
|
|
|
"""
|