adding all the files , still work in progress but getting there , needs to move scale_up function to somrthing not managed by REQUEST
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
|
||||
class sql:
|
||||
url = 'localhost'
|
||||
port = 5432
|
||||
user = 'postgres'
|
||||
password = 'example'
|
||||
database = 'cloudmanager'
|
||||
@@ -0,0 +1,137 @@
|
||||
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
|
||||
|
||||
"""
|
||||
@@ -0,0 +1,13 @@
|
||||
drop table IF EXISTS machine;
|
||||
CREATE TABLE IF NOT EXISTS machine (
|
||||
id integer NOT NULL PRIMARY KEY,
|
||||
namespace varchar(100) NOT null,
|
||||
name varchar(100) NOT null,
|
||||
hour_behaviour_id integer NOT null,
|
||||
day_behaviour_id integer NOT null,
|
||||
replicas integer NOT null
|
||||
);
|
||||
|
||||
INSERT into machine (id, namespace, name, hour_behaviour_id, day_behaviour_id,replicas) VALUES (1, 'echo', 'echo-server', 0, 1,1);
|
||||
INSERT into machine (id, namespace, name, hour_behaviour_id, day_behaviour_id,replicas) VALUES (2, 'wiki', 'wikijs', 1, 1,2);
|
||||
INSERT into machine (id, namespace, name, hour_behaviour_id, day_behaviour_id,replicas) VALUES (3, 'book', 'bookstack-db', 2, 1,1);
|
||||
@@ -0,0 +1,16 @@
|
||||
create table scenario_day_of_week
|
||||
(
|
||||
id_scenario integer NOT NULL PRIMARY KEY,
|
||||
name_scenario varchar(255) null,
|
||||
state_Monday BOOLEAN null,
|
||||
state_Tuesday BOOLEAN null,
|
||||
state_Wednesday BOOLEAN null,
|
||||
state_Thursday BOOLEAN null,
|
||||
state_Friday BOOLEAN null,
|
||||
state_Saturday BOOLEAN null,
|
||||
state_Sunday BOOLEAN null
|
||||
);
|
||||
|
||||
|
||||
INSERT INTO scenario_day_of_week (id_scenario, name_scenario, state_Monday, state_Tuesday, state_Wednesday, state_Thursday, state_Friday, state_Saturday, state_Sunday) VALUES (0, 'always off', FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE);
|
||||
INSERT INTO scenario_day_of_week (id_scenario, name_scenario, state_Monday, state_Tuesday, state_Wednesday, state_Thursday, state_Friday, state_Saturday, state_Sunday) VALUES (1, 'always on', TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE);
|
||||
@@ -0,0 +1,33 @@
|
||||
create table scenario_hour
|
||||
(
|
||||
id_scenario integer NOT NULL PRIMARY KEY,
|
||||
name_scenario varchar(255) null,
|
||||
state_at_0 BOOLEAN null,
|
||||
state_at_1 BOOLEAN null,
|
||||
state_at_2 BOOLEAN null,
|
||||
state_at_3 BOOLEAN null,
|
||||
state_at_4 BOOLEAN null,
|
||||
state_at_5 BOOLEAN null,
|
||||
state_at_6 BOOLEAN null,
|
||||
state_at_7 BOOLEAN null,
|
||||
state_at_8 BOOLEAN null,
|
||||
state_at_9 BOOLEAN null,
|
||||
state_at_10 BOOLEAN null,
|
||||
state_at_11 BOOLEAN null,
|
||||
state_at_12 BOOLEAN null,
|
||||
state_at_13 BOOLEAN null,
|
||||
state_at_14 BOOLEAN null,
|
||||
state_at_15 BOOLEAN null,
|
||||
state_at_16 BOOLEAN null,
|
||||
state_at_17 BOOLEAN null,
|
||||
state_at_18 BOOLEAN null,
|
||||
state_at_19 BOOLEAN null,
|
||||
state_at_20 BOOLEAN null,
|
||||
state_at_21 BOOLEAN null,
|
||||
state_at_22 BOOLEAN null,
|
||||
state_at_23 BOOLEAN null
|
||||
);
|
||||
|
||||
INSERT INTO scenario_hour (id_scenario, name_scenario, state_at_0, state_at_1, state_at_2, state_at_3, state_at_4, state_at_5, state_at_6, state_at_7, state_at_8, state_at_9, state_at_10, state_at_11, state_at_12, state_at_13, state_at_14, state_at_15, state_at_16, state_at_17, state_at_18, state_at_19, state_at_20, state_at_21, state_at_22, state_at_23) VALUES (0, 'always off uk', FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE);
|
||||
INSERT INTO scenario_hour (id_scenario, name_scenario, state_at_0, state_at_1, state_at_2, state_at_3, state_at_4, state_at_5, state_at_6, state_at_7, state_at_8, state_at_9, state_at_10, state_at_11, state_at_12, state_at_13, state_at_14, state_at_15, state_at_16, state_at_17, state_at_18, state_at_19, state_at_20, state_at_21, state_at_22, state_at_23) VALUES (1, 'always on uk', TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE);
|
||||
INSERT INTO scenario_hour (id_scenario, name_scenario, state_at_0, state_at_1, state_at_2, state_at_3, state_at_4, state_at_5, state_at_6, state_at_7, state_at_8, state_at_9, state_at_10, state_at_11, state_at_12, state_at_13, state_at_14, state_at_15, state_at_16, state_at_17, state_at_18, state_at_19, state_at_20, state_at_21, state_at_22, state_at_23) VALUES (2, 'awake hours uk', FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE);
|
||||
Reference in New Issue
Block a user