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:
+31
-27
@@ -1,9 +1,14 @@
|
|||||||
import psycopg2
|
import psycopg2
|
||||||
#import function
|
from django.http import HttpResponse
|
||||||
import credentials
|
from kubernetes_connection import apps as kubernetes_connection
|
||||||
import datetime
|
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):
|
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)
|
db = psycopg2.connect(host=host_in, port=port_in, user=user_in, password=password_in,database=db_in)
|
||||||
return db
|
return db
|
||||||
@@ -22,15 +27,13 @@ def Launch_sql_query(sql,db):
|
|||||||
#result_machine_type = []
|
#result_machine_type = []
|
||||||
for row in results:
|
for row in results:
|
||||||
result_machine_type = []
|
result_machine_type = []
|
||||||
id_scenario = row[0]
|
namespace = row[0]
|
||||||
id_hour = bool(row[1])
|
name = row[1]
|
||||||
name = row[2]
|
replicas = row[2]
|
||||||
proxmox = row[3]
|
|
||||||
machine_type = row[4]
|
|
||||||
|
|
||||||
result_machine_type.append(proxmox)
|
result_machine_type.append(namespace)
|
||||||
result_machine_type.append(machine_type)
|
|
||||||
result_machine_type.append(name)
|
result_machine_type.append(name)
|
||||||
|
result_machine_type.append(replicas)
|
||||||
result_machine.append(result_machine_type)
|
result_machine.append(result_machine_type)
|
||||||
return result_machine
|
return result_machine
|
||||||
|
|
||||||
@@ -51,9 +54,9 @@ def Give_current_hour():
|
|||||||
def Check_server_supposed_XXX(generated_db_in,wanted_status,Wanted_hour):
|
def Check_server_supposed_XXX(generated_db_in,wanted_status,Wanted_hour):
|
||||||
sql_in=""
|
sql_in=""
|
||||||
if wanted_status == "up" or wanted_status == "start":
|
if wanted_status == "up" or wanted_status == "start":
|
||||||
sql_in = "select scenario_hour.id_scenario , scenario_hour.state_at_" + str(Wanted_hour) + " , machine.name, machine.id_proxmox, machine.type from scenario_hour,machine where state_at_" + str(Wanted_hour) + "=1 and machine.hour_behaviour_id=scenario_hour.id_scenario;"
|
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":
|
elif wanted_status == "down" or wanted_status == "stop":
|
||||||
sql_in = "select scenario_hour.id_scenario , scenario_hour.state_at_" + str(Wanted_hour) + " , machine.name, machine.id_proxmox, machine.type from scenario_hour,machine where state_at_" + str(Wanted_hour) + "=0 and machine.hour_behaviour_id=scenario_hour.id_scenario;"
|
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:
|
else:
|
||||||
print("error value")
|
print("error value")
|
||||||
return_value = Launch_sql_query(sql_in, generated_db_in)
|
return_value = Launch_sql_query(sql_in, generated_db_in)
|
||||||
@@ -80,29 +83,30 @@ def all_operations(wanted_state,list_machine):
|
|||||||
|
|
||||||
|
|
||||||
for machine_target in list_machine:
|
for machine_target in list_machine:
|
||||||
if not(Is_the_server_already_wanted_state(machine_target[0], wanted_state,machine_target[1])):
|
print(machine_target[1])
|
||||||
function.action(False,machine_target[0],operation_to_perform,machine_target[1])
|
kubernetes_connection.scale_up(main.request,namespace_name=machine_target[0],name=machine_target[1],replicas=machine_target[2]);
|
||||||
print(str(machine_target[0])+" is now "+str(wanted_state))
|
# if not(Is_the_server_already_wanted_state(machine_target[0], wanted_state,machine_target[1])):
|
||||||
elif 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])
|
||||||
info_to_display = "operation already performed for "+machine_target[2]
|
# print(str(machine_target[0])+" is now "+str(wanted_state))
|
||||||
print(info_to_display)
|
# 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 info_to_display
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
def main():
|
def main(request):
|
||||||
generated_db = DB_generation(credentials.sql.url, credentials.sql.port, credentials.sql.user, credentials.sql.password,credentials.sql.database)
|
generated_db = DB_generation(credentials.sql.url, credentials.sql.port, credentials.sql.user, credentials.sql.password,credentials.sql.database)
|
||||||
print(generated_db)
|
|
||||||
##operation:
|
##operation:
|
||||||
|
kubernetes_connection.hello(request);
|
||||||
## Server(s) supposed to be UP
|
## Server(s) supposed to be UP
|
||||||
Check_server_supposed_XXX(generated_db, "up", Give_current_hour())
|
#Check_server_supposed_XXX(generated_db, "up", Give_current_hour())
|
||||||
|
|
||||||
list_servers_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("------------------------------------------")
|
||||||
# print("-----------------UP-----------------------")
|
print("-----------------UP-----------------------")
|
||||||
# print("------------------------------------------")
|
print("------------------------------------------")
|
||||||
# print(list_servers_up)
|
print(list_servers_up)
|
||||||
# print(all_operations("running", list_servers_up))
|
print(all_operations("running", list_servers_up))
|
||||||
#
|
#
|
||||||
#
|
#
|
||||||
# ## Server(s) supposed to be DOWN
|
# ## Server(s) supposed to be DOWN
|
||||||
@@ -119,7 +123,7 @@ def main():
|
|||||||
generated_db.close()
|
generated_db.close()
|
||||||
|
|
||||||
#print(Is_the_server_already_wanted_state(112, "stopped"))
|
#print(Is_the_server_already_wanted_state(112, "stopped"))
|
||||||
|
HttpResponse('<h1>CMM executed!</h1>')
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
||||||
@@ -16,7 +16,7 @@ Including another URLconf
|
|||||||
"""
|
"""
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from django.urls import path
|
from django.urls import path
|
||||||
#from docker_connection import views, apps
|
from cloudmanager import credentials,db_interaction
|
||||||
from random_simpsons import apps as random_simpsons
|
from random_simpsons import apps as random_simpsons
|
||||||
from paperless_django import apps as paperless_django
|
from paperless_django import apps as paperless_django
|
||||||
from onenote import apps as onenote
|
from onenote import apps as onenote
|
||||||
@@ -88,5 +88,8 @@ urlpatterns = [
|
|||||||
# path('simpsons/', random_simpsons.hello),
|
# path('simpsons/', random_simpsons.hello),
|
||||||
# path('docker/<str:action>/<str:id_docker>', docker_connection.apps.docker_call),
|
# path('docker/<str:action>/<str:id_docker>', docker_connection.apps.docker_call),
|
||||||
|
|
||||||
|
################### cloudmanager ###################
|
||||||
|
path('cmm/', db_interaction.hello),
|
||||||
|
path('cmm/main/', db_interaction.main),
|
||||||
|
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -17,11 +17,15 @@ import urllib.request
|
|||||||
def hello(request):
|
def hello(request):
|
||||||
return HttpResponse('<h1>Hello k8s!</h1>')
|
return HttpResponse('<h1>Hello k8s!</h1>')
|
||||||
|
|
||||||
def scale_up(request,namespace_name,deploy_name):
|
def echoHello():
|
||||||
|
print("hello from k8s")
|
||||||
|
return "returned from k8s"
|
||||||
|
|
||||||
|
def scale_up(request,namespace_name,deploy_name,replicas=1):
|
||||||
config.load_kube_config(config_file='/kubeconfig/config')
|
config.load_kube_config(config_file='/kubeconfig/config')
|
||||||
api = client.AppsV1Api()
|
api = client.AppsV1Api()
|
||||||
deployment = api.read_namespaced_deployment(name=deploy_name, namespace=namespace_name)
|
deployment = api.read_namespaced_deployment(name=deploy_name, namespace=namespace_name)
|
||||||
deployment.spec.replicas = 1
|
deployment.spec.replicas = replicas
|
||||||
resp = api.patch_namespaced_deployment(
|
resp = api.patch_namespaced_deployment(
|
||||||
name=deploy_name, namespace=namespace_name, body=deployment);
|
name=deploy_name, namespace=namespace_name, body=deployment);
|
||||||
#return HttpResponse('<h1>Scaled up !</h1>')
|
#return HttpResponse('<h1>Scaled up !</h1>')
|
||||||
|
|||||||
Reference in New Issue
Block a user