diff --git a/.gitignore b/.gitignore index 5b8e206..7d8ac36 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ */__pycache__/* */.idea venv/* +.env/* *__pycache__* Webcal-python/* .idea/* diff --git a/django_code/cloud-manager/__init__.py b/django_code/cloud-manager/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/django_code/cloud-manager/credentials.py b/django_code/cloud-manager/credentials.py new file mode 100644 index 0000000..a0c953a --- /dev/null +++ b/django_code/cloud-manager/credentials.py @@ -0,0 +1,7 @@ + +class sql: + url = 'localhost' + port = 5432 + user = 'postgres' + password = 'example' + database = 'cloudmanager' diff --git a/django_code/cloud-manager/db_interaction.py b/django_code/cloud-manager/db_interaction.py new file mode 100644 index 0000000..503b57c --- /dev/null +++ b/django_code/cloud-manager/db_interaction.py @@ -0,0 +1,132 @@ +import psycopg2 +#import function +import credentials +import datetime + + +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 = [] + id_scenario = row[0] + id_hour = bool(row[1]) + name = row[2] + proxmox = row[3] + machine_type = row[4] + + result_machine_type.append(proxmox) + result_machine_type.append(machine_type) + result_machine_type.append(name) + 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 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;" + 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;" + 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: + 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(): + + generated_db = DB_generation(credentials.sql.url, credentials.sql.port, credentials.sql.user, credentials.sql.password,credentials.sql.database) + + ###operation: + + ## Server(s) supposed to be UP + 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")) + +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 + +""" diff --git a/docker-compose.yml b/docker-compose.yml index b0b2389..6a252cf 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,12 +1,12 @@ services: db: - image: django:local + image: docker.io/library/postgres:latest restart: always volumes: - - type: bind - target: /code - source: ./django_code - command: python manage.py runserver 0.0.0.0:7000 + - db:/var/lib/postgresql/ ports: - - 7000:7000 - + - 5432:5432 + environment: + POSTGRES_PASSWORD: example +volumes: + db: