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 function
|
||||
import credentials
|
||||
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
|
||||
@@ -22,15 +27,13 @@ def Launch_sql_query(sql,db):
|
||||
#result_machine_type = []
|
||||
for row in results:
|
||||
result_machine_type = []
|
||||
id_scenario = row[0]
|
||||
id_hour = bool(row[1])
|
||||
name = row[2]
|
||||
proxmox = row[3]
|
||||
machine_type = row[4]
|
||||
namespace = row[0]
|
||||
name = row[1]
|
||||
replicas = row[2]
|
||||
|
||||
result_machine_type.append(proxmox)
|
||||
result_machine_type.append(machine_type)
|
||||
result_machine_type.append(namespace)
|
||||
result_machine_type.append(name)
|
||||
result_machine_type.append(replicas)
|
||||
result_machine.append(result_machine_type)
|
||||
return result_machine
|
||||
|
||||
@@ -51,9 +54,9 @@ def Give_current_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 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":
|
||||
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:
|
||||
print("error value")
|
||||
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:
|
||||
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)
|
||||
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():
|
||||
def main(request):
|
||||
generated_db = DB_generation(credentials.sql.url, credentials.sql.port, credentials.sql.user, credentials.sql.password,credentials.sql.database)
|
||||
print(generated_db)
|
||||
##operation:
|
||||
|
||||
kubernetes_connection.hello(request);
|
||||
## 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())
|
||||
# print("------------------------------------------")
|
||||
# print("-----------------UP-----------------------")
|
||||
# print("------------------------------------------")
|
||||
# print(list_servers_up)
|
||||
# print(all_operations("running", list_servers_up))
|
||||
print("------------------------------------------")
|
||||
print("-----------------UP-----------------------")
|
||||
print("------------------------------------------")
|
||||
print(list_servers_up)
|
||||
print(all_operations("running", list_servers_up))
|
||||
#
|
||||
#
|
||||
# ## Server(s) supposed to be DOWN
|
||||
@@ -119,7 +123,7 @@ def main():
|
||||
generated_db.close()
|
||||
|
||||
#print(Is_the_server_already_wanted_state(112, "stopped"))
|
||||
|
||||
HttpResponse('<h1>CMM executed!</h1>')
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
@@ -16,7 +16,7 @@ Including another URLconf
|
||||
"""
|
||||
from django.contrib import admin
|
||||
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 paperless_django import apps as paperless_django
|
||||
from onenote import apps as onenote
|
||||
@@ -88,5 +88,8 @@ urlpatterns = [
|
||||
# path('simpsons/', random_simpsons.hello),
|
||||
# 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):
|
||||
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')
|
||||
api = client.AppsV1Api()
|
||||
deployment = api.read_namespaced_deployment(name=deploy_name, namespace=namespace_name)
|
||||
deployment.spec.replicas = 1
|
||||
deployment.spec.replicas = replicas
|
||||
resp = api.patch_namespaced_deployment(
|
||||
name=deploy_name, namespace=namespace_name, body=deployment);
|
||||
#return HttpResponse('<h1>Scaled up !</h1>')
|
||||
|
||||
Reference in New Issue
Block a user