Skip to content

Commit

Permalink
Docker wip
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexTheGeek committed Jan 31, 2024
1 parent 5513053 commit 25ff8cc
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 7 deletions.
2 changes: 0 additions & 2 deletions Docker/API/DockerFile_API

This file was deleted.

43 changes: 43 additions & 0 deletions Docker/API/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
151 changes: 151 additions & 0 deletions Docker/API/back_openstack.py
Original file line number Diff line number Diff line change
@@ -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)



10 changes: 10 additions & 0 deletions Docker/API/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions Docker/API/static/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
User-agent: *
Disallow: /
Original file line number Diff line number Diff line change
@@ -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 .
Expand Down

0 comments on commit 25ff8cc

Please sign in to comment.