From 25ff8ccc85828cf1b740b255f429fb9afde40385 Mon Sep 17 00:00:00 2001 From: AlexTheGeek <28763288+AlexTheGeek@users.noreply.github.com> Date: Wed, 31 Jan 2024 19:26:40 +0100 Subject: [PATCH] Docker wip --- Docker/API/DockerFile_API | 2 - Docker/API/Dockerfile | 43 +++++ Docker/API/back_openstack.py | 151 ++++++++++++++++++ Docker/API/entrypoint.sh | 10 ++ Docker/API/static/robots.txt | 2 + .../{DockerFile_Scheduler => Dockerfile} | 12 +- 6 files changed, 213 insertions(+), 7 deletions(-) delete mode 100644 Docker/API/DockerFile_API create mode 100644 Docker/API/Dockerfile create mode 100644 Docker/API/back_openstack.py create mode 100644 Docker/API/entrypoint.sh create mode 100644 Docker/API/static/robots.txt rename Docker/Scheduler/{DockerFile_Scheduler => Dockerfile} (51%) diff --git a/Docker/API/DockerFile_API b/Docker/API/DockerFile_API deleted file mode 100644 index 47e28e4..0000000 --- a/Docker/API/DockerFile_API +++ /dev/null @@ -1,2 +0,0 @@ -FROM python:3.11.2 - diff --git a/Docker/API/Dockerfile b/Docker/API/Dockerfile new file mode 100644 index 0000000..fd863dc --- /dev/null +++ b/Docker/API/Dockerfile @@ -0,0 +1,43 @@ +FROM python:3.11.2-bullseye + +RUN apt update + +RUN pip install python-openstackclient==6.0.0 + +RUN mkdir /api +WORKDIR /api + +COPY entrypoint.sh . +COPY main.py . +COPY back_openstack.py . + +RUN mkdir -p /api/static +COPY robots.txt /api/static/robots.txt + +# Partie variable d'environnement URLs +ENV URL_VNC="https://vnc.insa-cvl.com/" +ENV URL_VDI="https://vdi.insa-cvl.com/" +ENV URL_API="https://api.insa-cvl.com/" + +# Partie variable d'environnement API + + +# Partie variable d'environnement DB +ENV DB_HOST="127.0.0.1" +ENV DB_NAME="vdi4" +ENV DB_USER="root" +ENV DB_PASSWORD="azerty" + +# Partie variable d'environnement OpenStack +ENV OPENSTACK_PRIVATE_NETWORK_ID="0d49c37b-7077-4152-985c-f5a00ad20677" +ENV OPENSTACK_USERNAME="admin" +ENV OPENSTACK_PASSWORD="3pMrmW899b9y^2kiJa!6#Z#kE%@a2r" +ENV OPENSTACK_AUTH_URL="http://172.10.3.60:5000/v3" +ENV OPENSTACK_PROJECT_NAME="admin" +ENV OPENSTACK_USER_DOMAIN_ID="default" +ENV OPENSTACK_PROJECT_DOMAIN_ID="default" + + +EXPOSE 5001 + +CMD ["/bin/bash", "entrypoint.sh"] \ No newline at end of file diff --git a/Docker/API/back_openstack.py b/Docker/API/back_openstack.py new file mode 100644 index 0000000..35663b3 --- /dev/null +++ b/Docker/API/back_openstack.py @@ -0,0 +1,151 @@ +import base64 +from openstack import connection as connexion + +#Constants +USERNAME = "admin" +PASSWORD = "3pMrmW899b9y^2kiJa!6#Z#kE%@a2r" +#Network Constants +PRIVATE_NETWORK_ID = "0d49c37b-7077-4152-985c-f5a00ad20677" +EXTERNAL_NETWORK_ID = "e64da4e4-57c4-473a-9b4d-548c800b654a" +AUTH_URL = "http://172.10.3.60:5000/v3" +#Openstack Constants +PROJECT_NAME = "admin" +USER_DOMAIN_ID = "default" +PROJECT_DOMAIN_ID = "default" + +conn = connexion.Connection(auth_url=AUTH_URL, project_name=PROJECT_NAME, username=USERNAME, user_domain_id=USER_DOMAIN_ID, project_domain_id=PROJECT_DOMAIN_ID, password=PASSWORD) + +""" +Unused functions +def get_infos(conn): + for server in conn.compute.servers(): + print(f"Server name : {server.name}\n") + + for image in conn.compute.images(): + print("Image name : " + image.name) + +def get_flavor(conn): + for flavor in conn.compute.flavors(): + print("Flavor name : " + flavor.name) + +def get_network(conn): + print("--------------------") + for network in conn.network.networks(): + print("Network name : " + network.name) + print("Network id : " + network.id) + print("Network status : " + network.status) + print("--------------------") + +### Admin only +def get_endpoint(conn): + for endpoint in conn.identity.endpoints(): + print(f"Endpoint name : {endpoint.name}") + +def get_projects(conn): + for project in conn.identity.projects(): + print(f"Project name : {project.name}") + +def get_users(conn): + for user in conn.identity.users(): + print(f"User name : {user.name}") + +def get_floating_IPs(conn): + for ip in conn.network.ips(): + print(f"IP name : {ip.name}") + + #create a new floating ip (fonctionne) + # ip = conn.network.create_ip(floating_network_id=EXTERNAL_NETWORK_ID) + +def create_floating_IP(conn): + ip = conn.network.create_ip(floating_network_id=EXTERNAL_NETWORK_ID) + return ip.floating_ip_address + +def remove_floating_IP(conn, ip:str): + conn.network.delete_ip(ip) +""" + +#Used functions + +def get_image(conn): + images = [] + for image in conn.compute.images(): + images.append(image.name) + return images + +def get_infos_project(conn, print_infos=True): + project = conn.identity.find_project("admin") + nova_client = conn.compute + quota_info = nova_client.get(f"/os-quota-sets/{project.id}?usage=True").json()['quota_set'] + usage_info = nova_client.get(f"/os-simple-tenant-usage/{project.id}").json()['tenant_usage'] + + quota_ram = quota_info['ram'] + quota_servers = quota_info['instances'] + quota_vcpu = quota_info['cores'] + + quotas = [quota_servers, quota_ram, quota_vcpu] + servers = nova_client.servers(details=True, project_id=project.id) + + total_ram_used = 0 + total_vcpu_used = 0 + # total_disk_used = 0 + nb_servers = 0 + + for server in servers: + total_ram_used += server.to_dict()['flavor']['ram'] + total_vcpu_used += server.to_dict()['flavor']['vcpus'] + # total_disk_used += server.to_dict()['flavor']['disk'] + nb_servers += 1 + + used = [nb_servers, total_ram_used, total_vcpu_used] + if print_infos: + print("Project Name:", project.name) + print("NB Servers:", nb_servers) + print("Total RAM Used (MB):", total_ram_used) + print("Total vCPU Used:", total_vcpu_used) + # print("Total Disk Used:", total_disk_used) + return [quotas, used] + +def get_console_url(conn, vm_name:str): + server = conn.compute.find_server(vm_name) + object_url = server.get_console_url(conn.compute, "novnc") + return object_url['url'] + +def get_status_server(conn, vm_name:str): + server = conn.compute.find_server(vm_name) + instance = conn.compute.get_server(server['id']) + status = instance.to_dict()['status'] + vm_state = instance.to_dict()['power_state'] + return vm_state, status + +def create_instance(conn, vm_name:str, vm_image:str): + image = conn.compute.find_image(f"{vm_image}") + flavor = conn.compute.find_flavor("vdi") + template_ram = flavor.ram + template_vcpu = flavor.vcpus + template_disk = flavor.disk + # Get Quotas and Used + quota, used = get_infos_project(conn, print_infos=False) + if (used[0] < quota[0]) and (template_ram <= quota[1]-used[1]) and (template_vcpu <= quota[2]-used[2]): + userdata = """#!/bin/bash\necho 'INSTALL VDI'\n""" + server = conn.compute.create_server( + name=vm_name, + image_id=image.id, + flavor_id=flavor.id, + networks=[{"uuid": PRIVATE_NETWORK_ID}], + user_data=base64.b64encode(userdata.encode("utf-8")).decode("utf-8"), + ) + conn.compute.wait_for_server(server) + return 0 + else: + return 1 + +def remove_instance(conn, name:str): + server = conn.compute.find_server(name) + conn.compute.delete_server(server) + +def remove_all_instances(conn): + for server in conn.compute.servers(): + conn.compute.delete_server(server) + + + diff --git a/Docker/API/entrypoint.sh b/Docker/API/entrypoint.sh new file mode 100644 index 0000000..3293536 --- /dev/null +++ b/Docker/API/entrypoint.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +if [[ ! -d "/var/log/VDI/API" ]]; then + mkdir -p /var/log/VDI/API + touch /var/log/VDI/API/access.log + touch /var/log/VDI/API/error.log + touch /var/log/VDI/API/api-flask.log +fi + +gunicorn --access-logfile /var/log/VDI/API/access.log --error-logfile /var/log/VDI/API/error.log --workers 3 --bind 0.0.0.0:5001 main:app \ No newline at end of file diff --git a/Docker/API/static/robots.txt b/Docker/API/static/robots.txt new file mode 100644 index 0000000..70c2374 --- /dev/null +++ b/Docker/API/static/robots.txt @@ -0,0 +1,2 @@ +User-agent: * +Disallow: / diff --git a/Docker/Scheduler/DockerFile_Scheduler b/Docker/Scheduler/Dockerfile similarity index 51% rename from Docker/Scheduler/DockerFile_Scheduler rename to Docker/Scheduler/Dockerfile index af0431e..b96ad20 100644 --- a/Docker/Scheduler/DockerFile_Scheduler +++ b/Docker/Scheduler/Dockerfile @@ -1,22 +1,24 @@ FROM python:3.11.2 -RUN pip install schedule mysql-connector-python python-openstackclient +RUN pip install schedule==1.2.1 mysql-connector-python==8.2.0 python-openstackclient==6.0.0 RUN mkdir /scheduler WORKDIR /scheduler +# Partie variable d'environnement DB ENV DB_HOST="127.0.0.1" ENV DB_NAME="vdi4" ENV DB_USER="root" ENV DB_PASSWORD="azerty" +# Partie variable d'environnement OpenStack ENV OPENSTACK_PRIVATE_NETWORK_ID="0d49c37b-7077-4152-985c-f5a00ad20677" ENV OPENSTACK_USERNAME="admin" ENV OPENSTACK_PASSWORD="3pMrmW899b9y^2kiJa!6#Z#kE%@a2r" -ENV OPENSTACK_AUTH_URL = "http://172.10.3.60:5000/v3" -ENV OPENSTACK_PROJECT_NAME = "admin" -ENV OPENSTACK_USER_DOMAIN_ID = "default" -ENV OPENSTACK_PROJECT_DOMAIN_ID = "default" +ENV OPENSTACK_AUTH_URL="http://172.10.3.60:5000/v3" +ENV OPENSTACK_PROJECT_NAME="admin" +ENV OPENSTACK_USER_DOMAIN_ID="default" +ENV OPENSTACK_PROJECT_DOMAIN_ID="default" COPY scheduler.py . COPY back_openstack.py .