diff --git a/sergeev_evgenii_lab_2/README.md b/sergeev_evgenii_lab_2/README.md index 0a5d9cc..98e84b5 100644 --- a/sergeev_evgenii_lab_2/README.md +++ b/sergeev_evgenii_lab_2/README.md @@ -3,7 +3,7 @@ Вариант № 4 для первой программы: * Сервис формирует файл /var/result/data.txt так, что каждая строка файла - количество символов в именах файлов из каталога /var/data. -Вариант № 2 для второй программы: +Вариант № 2 для второй программы: * Сервис ищет наименьшее число из файла /var/data/data.txt и сохраняет его третью степень в /var/result/result.txt. ## Выполнение Были написаны два сервиса на языке python с использованием технологии flask. diff --git a/sergeev_evgenii_lab_2/docker-compose.yml b/sergeev_evgenii_lab_2/docker-compose.yml index e69de29..cc3b6c7 100644 --- a/sergeev_evgenii_lab_2/docker-compose.yml +++ b/sergeev_evgenii_lab_2/docker-compose.yml @@ -0,0 +1,20 @@ +version: '3' + +services: + service1: + build: + context: ./service1 + ports: + - "8081:8081" + volumes: + - .\var\data:/var/data + - .\var\result:/var/result + + service2: + build: + context: ./service2 + ports: + - "8082:8082" + volumes: + - ./var/data:/var/data + - ./var/result:/var/result \ No newline at end of file diff --git a/sergeev_evgenii_lab_2/service1/Dockerfile b/sergeev_evgenii_lab_2/service1/Dockerfile index c33b0f9..6379a70 100644 --- a/sergeev_evgenii_lab_2/service1/Dockerfile +++ b/sergeev_evgenii_lab_2/service1/Dockerfile @@ -1,4 +1,15 @@ -FROM ubuntu:latest -LABEL authors="User" +FROM python:3.11 -ENTRYPOINT ["top", "-b"] \ No newline at end of file +ENV LISTEN_PORT=8081 +EXPOSE 8081 + +RUN ["mkdir", "/work"] +RUN ["mkdir", "/var/data"] +RUN ["mkdir", "/var/result"] +RUN pip install Flask + +WORKDIR /work + +COPY index.html service1.py ./ + +CMD ["python", "service1.py"] \ No newline at end of file diff --git a/sergeev_evgenii_lab_2/service1/service1.py b/sergeev_evgenii_lab_2/service1/service1.py index 20b0002..8a9e63f 100644 --- a/sergeev_evgenii_lab_2/service1/service1.py +++ b/sergeev_evgenii_lab_2/service1/service1.py @@ -1,7 +1,7 @@ from flask import Flask, redirect, render_template import os -app = Flask(__name__) +app = Flask(__name__, template_folder='') @app.route('/') @@ -11,8 +11,11 @@ def home(): @app.route('/ex') def do(): - data_dir = '/var/data' - path_result_file = '/var/result/data.txt' + current_directory = os.getcwd() + directory_elements = current_directory.split(os.path.sep) # Разделяем по разделителю каталогов + cur_d = os.path.sep.join(directory_elements[:-1]) + data_dir = cur_d + '/var/data' + path_result_file = cur_d + '/var/result/data.txt' try: # Получаем список файлов в указанном каталоге diff --git a/sergeev_evgenii_lab_2/service2/Dockerfile b/sergeev_evgenii_lab_2/service2/Dockerfile index c33b0f9..339421e 100644 --- a/sergeev_evgenii_lab_2/service2/Dockerfile +++ b/sergeev_evgenii_lab_2/service2/Dockerfile @@ -1,4 +1,14 @@ -FROM ubuntu:latest -LABEL authors="User" +FROM python:3.11 -ENTRYPOINT ["top", "-b"] \ No newline at end of file +ENV LISTEN_PORT=8082 +EXPOSE 8082 + +RUN ["mkdir", "/work"] +RUN ["mkdir", "/var/result"] +RUN pip install Flask + +WORKDIR /work + +COPY index.html result.html service2.py ./ + +CMD ["python", "service2.py"] \ No newline at end of file diff --git a/sergeev_evgenii_lab_2/service2/service2.py b/sergeev_evgenii_lab_2/service2/service2.py index e69de29..f6fde43 100644 --- a/sergeev_evgenii_lab_2/service2/service2.py +++ b/sergeev_evgenii_lab_2/service2/service2.py @@ -0,0 +1,43 @@ +import os +from flask import Flask, render_template + +app = Flask(__name__, template_folder='') + + +@app.route('/') +def home(): + return render_template("index.html") + + +@app.route('/ex') +def do(): + current_directory = os.getcwd() + directory_elements = current_directory.split(os.path.sep) # Разделяем по разделителю каталогов + cur_d = os.path.sep.join(directory_elements[:-1]) + path_data_file = cur_d + '/var/result/data.txt' + path_result_file = cur_d + '/var/result/result.txt' + + min_number = 0 + try: + # Чтение чисел из файла + with open(path_data_file, 'r') as file: + numbers = [float(line.strip()) for line in file] + + # Поиск минимального числа + min_number = min(numbers) + + # Возведение минимального числа в третью степень + result = min_number ** 3 + + # Запись результата в файл + with open(path_result_file, 'w') as result_file: + result_file.write(str(result)) + + print(f'Наименьшее число из файла {path_data_file} в третьей степени сохранено в {path_result_file}.') + except Exception as e: + print(f'Произошла ошибка: {e}') + + return render_template("result.html", num=min_number) + + +app.run(host='0.0.0.0', port=8082) diff --git a/sergeev_evgenii_lab_2/var/data/1 b/sergeev_evgenii_lab_2/var/data/1 new file mode 100644 index 0000000..e69de29 diff --git a/sergeev_evgenii_lab_2/var/data/123 b/sergeev_evgenii_lab_2/var/data/123 new file mode 100644 index 0000000..e69de29 diff --git a/sergeev_evgenii_lab_2/var/data/tutmnogobukv b/sergeev_evgenii_lab_2/var/data/tutmnogobukv new file mode 100644 index 0000000..e69de29 diff --git a/sergeev_evgenii_lab_2/var/data/zdestozhe.txt b/sergeev_evgenii_lab_2/var/data/zdestozhe.txt new file mode 100644 index 0000000..e69de29 diff --git a/sergeev_evgenii_lab_2/var/result/data.txt b/sergeev_evgenii_lab_2/var/result/data.txt new file mode 100644 index 0000000..467b4da --- /dev/null +++ b/sergeev_evgenii_lab_2/var/result/data.txt @@ -0,0 +1,4 @@ +1 +3 +12 +13 diff --git a/sergeev_evgenii_lab_2/var/result/result.txt b/sergeev_evgenii_lab_2/var/result/result.txt new file mode 100644 index 0000000..9f8e9b6 --- /dev/null +++ b/sergeev_evgenii_lab_2/var/result/result.txt @@ -0,0 +1 @@ +1.0 \ No newline at end of file