From f5ec3f1767a83571d06d1536032bc44bcf75c188 Mon Sep 17 00:00:00 2001 From: VictoriaPresnyakova Date: Thu, 10 Oct 2024 18:52:01 +0400 Subject: [PATCH] lab2 --- presnyakova_victoria_lab_2/.gitignore | 5 +++ presnyakova_victoria_lab_2/data/test1.txt | 5 +++ presnyakova_victoria_lab_2/data/test2.txt | 4 ++ presnyakova_victoria_lab_2/data/test3.txt | 4 ++ presnyakova_victoria_lab_2/docker-compose.yml | 18 +++++++++ presnyakova_victoria_lab_2/readme.md | 19 ++++++++++ .../worker-1/Dockerfile | 11 ++++++ .../worker-1/worker-1.py | 38 +++++++++++++++++++ .../worker-2/Dockerfile | 11 ++++++ .../worker-2/worker-2.py | 13 +++++++ 10 files changed, 128 insertions(+) create mode 100644 presnyakova_victoria_lab_2/.gitignore create mode 100644 presnyakova_victoria_lab_2/data/test1.txt create mode 100644 presnyakova_victoria_lab_2/data/test2.txt create mode 100644 presnyakova_victoria_lab_2/data/test3.txt create mode 100644 presnyakova_victoria_lab_2/docker-compose.yml create mode 100644 presnyakova_victoria_lab_2/readme.md create mode 100644 presnyakova_victoria_lab_2/worker-1/Dockerfile create mode 100644 presnyakova_victoria_lab_2/worker-1/worker-1.py create mode 100644 presnyakova_victoria_lab_2/worker-2/Dockerfile create mode 100644 presnyakova_victoria_lab_2/worker-2/worker-2.py diff --git a/presnyakova_victoria_lab_2/.gitignore b/presnyakova_victoria_lab_2/.gitignore new file mode 100644 index 0000000..d26e67a --- /dev/null +++ b/presnyakova_victoria_lab_2/.gitignore @@ -0,0 +1,5 @@ +__pycache__/ +*.pyc +*.pyo +*.log +.DS_Store diff --git a/presnyakova_victoria_lab_2/data/test1.txt b/presnyakova_victoria_lab_2/data/test1.txt new file mode 100644 index 0000000..a985331 --- /dev/null +++ b/presnyakova_victoria_lab_2/data/test1.txt @@ -0,0 +1,5 @@ +73 +5 +99 +27 +1234 \ No newline at end of file diff --git a/presnyakova_victoria_lab_2/data/test2.txt b/presnyakova_victoria_lab_2/data/test2.txt new file mode 100644 index 0000000..9495f72 --- /dev/null +++ b/presnyakova_victoria_lab_2/data/test2.txt @@ -0,0 +1,4 @@ +8 +102 +65 +23 diff --git a/presnyakova_victoria_lab_2/data/test3.txt b/presnyakova_victoria_lab_2/data/test3.txt new file mode 100644 index 0000000..981fb77 --- /dev/null +++ b/presnyakova_victoria_lab_2/data/test3.txt @@ -0,0 +1,4 @@ +34 +57 +92 +14 diff --git a/presnyakova_victoria_lab_2/docker-compose.yml b/presnyakova_victoria_lab_2/docker-compose.yml new file mode 100644 index 0000000..9ed8bd1 --- /dev/null +++ b/presnyakova_victoria_lab_2/docker-compose.yml @@ -0,0 +1,18 @@ +version: '3.8' + +services: + worker1: + build: + context: ./worker-1 + volumes: + - ./data:/var/data + - ./result:/var/result + + + worker2: + build: + context: ./worker-2 + volumes: + - ./result:/var/result + depends_on: + - worker1 diff --git a/presnyakova_victoria_lab_2/readme.md b/presnyakova_victoria_lab_2/readme.md new file mode 100644 index 0000000..1e7acc5 --- /dev/null +++ b/presnyakova_victoria_lab_2/readme.md @@ -0,0 +1,19 @@ +# Вариант 1: + +Сервис 1 ищет в каталоге /var/data файл с наибольшим количеством строк и перекладывает его в /var/result/data.txt. + +Сервис 2 ищет набольшее число из файла /var/data/data.txt и сохраняет его вторую степень в /var/result/result.txt. + +## Выполнение + +Созданы Docker-файлы для обоих приложений: + +Также создан файл `docker-compose.yml`, в котором выполнены следующие действия: + +- Определены два сервиса +- Для каждого сервиса указана рабочая директория. +- Настроено монтирование папок из хостовой системы. +- Описаны зависимости одного сервиса от другого. + + +## Видео https://drive.google.com/file/d/1pbz-3jQbwZqLBBkdSeJ4FWIvMafsdIqj/view?usp=sharing \ No newline at end of file diff --git a/presnyakova_victoria_lab_2/worker-1/Dockerfile b/presnyakova_victoria_lab_2/worker-1/Dockerfile new file mode 100644 index 0000000..e520de3 --- /dev/null +++ b/presnyakova_victoria_lab_2/worker-1/Dockerfile @@ -0,0 +1,11 @@ +# Используем базовый образ с Python +FROM python:3.9-slim + +# Устанавливаем рабочую директорию +WORKDIR /app + +# Копируем файл с кодом программы в контейнер +COPY worker-1.py . + +# Запускаем приложение +CMD ["python", "worker-1.py"] diff --git a/presnyakova_victoria_lab_2/worker-1/worker-1.py b/presnyakova_victoria_lab_2/worker-1/worker-1.py new file mode 100644 index 0000000..25ceb93 --- /dev/null +++ b/presnyakova_victoria_lab_2/worker-1/worker-1.py @@ -0,0 +1,38 @@ +import os +import shutil +#Ищет в каталоге /var/data файл с наибольшим количеством строк и перекладывает его в /var/result/data.txt. + +def find_file_with_most_lines(data_dir): + max_lines = 0 + file_with_most_lines = None + + # Перебираем файлы в каталоге + for filename in os.listdir(data_dir): + file_path = os.path.join(data_dir, filename) + + # Проверяем, что это файл + if os.path.isfile(file_path): + with open(file_path, 'r') as file: + line_count = sum(1 for _ in file) + if line_count > max_lines: + max_lines = line_count + file_with_most_lines = file_path + + return file_with_most_lines + + +def copy_file_to_result(file_path, result_path): + os.makedirs(os.path.dirname(result_path), exist_ok=True) + shutil.copyfile(file_path, result_path) + + +if __name__ == "__main__": + data_dir = "/var/data" + result_file = "/var/result/data.txt" + + file_with_most_lines = find_file_with_most_lines(data_dir) + if file_with_most_lines: + copy_file_to_result(file_with_most_lines, result_file) + print(f"Copied {file_with_most_lines} to {result_file}") + else: + print("No files found.") diff --git a/presnyakova_victoria_lab_2/worker-2/Dockerfile b/presnyakova_victoria_lab_2/worker-2/Dockerfile new file mode 100644 index 0000000..177b94e --- /dev/null +++ b/presnyakova_victoria_lab_2/worker-2/Dockerfile @@ -0,0 +1,11 @@ +# Используем базовый образ с Python +FROM python:3.9-slim + +# Устанавливаем рабочую директорию +WORKDIR /app + +# Копируем файл с кодом программы в контейнер +COPY worker-2.py . + +# Запускаем приложение +CMD ["python", "worker-2.py"] diff --git a/presnyakova_victoria_lab_2/worker-2/worker-2.py b/presnyakova_victoria_lab_2/worker-2/worker-2.py new file mode 100644 index 0000000..43a0aaf --- /dev/null +++ b/presnyakova_victoria_lab_2/worker-2/worker-2.py @@ -0,0 +1,13 @@ +input_file = "/var/result/data.txt" +output_file = "/var/result/result.txt" + +#Ищет набольшее число из файла /var/data/data.txt и сохраняет его вторую степень в /var/result/result.txt. + +with open(input_file, "r") as infile: + numbers = [int(line.strip()) for line in infile if line.strip().isdigit()] + if numbers: + max_number = max(numbers) + result = max_number ** 2 + + with open(output_file, "w") as outfile: + outfile.write(str(result))