diff --git a/degtyarev_mikhail_lab_2/1.png b/degtyarev_mikhail_lab_2/1.png new file mode 100644 index 0000000..39f355f Binary files /dev/null and b/degtyarev_mikhail_lab_2/1.png differ diff --git a/degtyarev_mikhail_lab_2/2.png b/degtyarev_mikhail_lab_2/2.png new file mode 100644 index 0000000..014c75e Binary files /dev/null and b/degtyarev_mikhail_lab_2/2.png differ diff --git a/degtyarev_mikhail_lab_2/3.png b/degtyarev_mikhail_lab_2/3.png new file mode 100644 index 0000000..d2edcd0 Binary files /dev/null and b/degtyarev_mikhail_lab_2/3.png differ diff --git a/degtyarev_mikhail_lab_2/README.md b/degtyarev_mikhail_lab_2/README.md new file mode 100644 index 0000000..5a58a33 --- /dev/null +++ b/degtyarev_mikhail_lab_2/README.md @@ -0,0 +1,66 @@ +# Лабораторная 2 +## Вариант 9 + +## Задание +Разработать два приложения такие, что результат первого является исходными данными для второго. + +Описание задач двух программ: +- Берёт из каталога /var/data случайный файл и перекладывает его в /var/result/data.txt. +- Ищет наибольшее число из файла /var/data/data.txt и сохраняет его вторую степень в /var/result/result.txt. + +## Описание Программы + +### Как запустить? + +```docker compose up -d``` + +### Созданные файлы: + +**Dockerfile** + +Они идентичны + +``` +FROM python:3.11 +WORKDIR /app +COPY . . +CMD ["python", "worker_1.py"] +``` +**docker-compose** + +``` +version: "3" +services: + worker1: + build: + context: ./worker1 + dockerfile: Dockerfile + volumes: + - ./data:/var/data + - ./result:/var/result + + worker2: + depends_on: + - worker1 + build: + context: ./worker2 + dockerfile: Dockerfile + volumes: + - ./result:/var/data + - ./result:/var/result + +``` + +**Исходные файлы** + +Файлы содержат абсолютно рандомные числа: + +![](1.png) + + +### Результаты +![](2.png) + +![](3.png) + +Ссылка на видео: https://vk.com/video/@id151119348?z=video151119348_456239199%2Fln-K8jegCHykzxSWCSO4A%2Fpl_151119348_-2 \ No newline at end of file diff --git a/degtyarev_mikhail_lab_2/data/1.txt b/degtyarev_mikhail_lab_2/data/1.txt new file mode 100644 index 0000000..eeb53c7 --- /dev/null +++ b/degtyarev_mikhail_lab_2/data/1.txt @@ -0,0 +1,3 @@ +163 +446 +769 \ No newline at end of file diff --git a/degtyarev_mikhail_lab_2/data/2.txt b/degtyarev_mikhail_lab_2/data/2.txt new file mode 100644 index 0000000..96c9cac --- /dev/null +++ b/degtyarev_mikhail_lab_2/data/2.txt @@ -0,0 +1,4 @@ +234 +125 +664 +889 \ No newline at end of file diff --git a/degtyarev_mikhail_lab_2/data/3.txt b/degtyarev_mikhail_lab_2/data/3.txt new file mode 100644 index 0000000..9e18126 --- /dev/null +++ b/degtyarev_mikhail_lab_2/data/3.txt @@ -0,0 +1,4 @@ +1111 +522 +333 +531 \ No newline at end of file diff --git a/degtyarev_mikhail_lab_2/docker-compose.yml b/degtyarev_mikhail_lab_2/docker-compose.yml new file mode 100644 index 0000000..0f86bcf --- /dev/null +++ b/degtyarev_mikhail_lab_2/docker-compose.yml @@ -0,0 +1,19 @@ +version: "3" +services: + worker1: + build: + context: ./worker1 + dockerfile: Dockerfile + volumes: + - ./data:/var/data + - ./result:/var/result + + worker2: + depends_on: + - worker1 + build: + context: ./worker2 + dockerfile: Dockerfile + volumes: + - ./result:/var/data + - ./result:/var/result diff --git a/degtyarev_mikhail_lab_2/result/data.txt b/degtyarev_mikhail_lab_2/result/data.txt new file mode 100644 index 0000000..9e18126 --- /dev/null +++ b/degtyarev_mikhail_lab_2/result/data.txt @@ -0,0 +1,4 @@ +1111 +522 +333 +531 \ No newline at end of file diff --git a/degtyarev_mikhail_lab_2/result/result.txt b/degtyarev_mikhail_lab_2/result/result.txt new file mode 100644 index 0000000..59809fb --- /dev/null +++ b/degtyarev_mikhail_lab_2/result/result.txt @@ -0,0 +1 @@ +1234321 \ No newline at end of file diff --git a/degtyarev_mikhail_lab_2/worker1/Dockerfile b/degtyarev_mikhail_lab_2/worker1/Dockerfile new file mode 100644 index 0000000..a8e2a23 --- /dev/null +++ b/degtyarev_mikhail_lab_2/worker1/Dockerfile @@ -0,0 +1,4 @@ +FROM python:3.11 +WORKDIR /app +COPY . . +CMD ["python", "worker_1.py"] \ No newline at end of file diff --git a/degtyarev_mikhail_lab_2/worker1/worker_1.py b/degtyarev_mikhail_lab_2/worker1/worker_1.py new file mode 100644 index 0000000..2a88ee4 --- /dev/null +++ b/degtyarev_mikhail_lab_2/worker1/worker_1.py @@ -0,0 +1,33 @@ +import os +import shutil +import random +import logging + +def find_random_file(directory): + files = os.listdir(directory) + if not files: + return None + + random_file = random.choice(files) + return random_file + +def main(): + data_dir = '/var/data' + result_file = '/var/result/data.txt' + + try: + random_file = find_random_file(data_dir) + + if random_file: + src_path = os.path.join(data_dir, random_file) + shutil.copy2(src_path, result_file) + logging.info(f'Successfully copied {src_path} to {result_file}') + else: + logging.warning('No files to copy.') + + except Exception as e: + logging.error(f'An error occurred: {str(e)}') + +if __name__ == "__main__": + logging.basicConfig(level=logging.INFO) + main() diff --git a/degtyarev_mikhail_lab_2/worker2/Dockerfile b/degtyarev_mikhail_lab_2/worker2/Dockerfile new file mode 100644 index 0000000..1fb1053 --- /dev/null +++ b/degtyarev_mikhail_lab_2/worker2/Dockerfile @@ -0,0 +1,4 @@ +FROM python:3.11 +WORKDIR /app +COPY . . +CMD ["python", "worker_2.py"] \ No newline at end of file diff --git a/degtyarev_mikhail_lab_2/worker2/worker_2.py b/degtyarev_mikhail_lab_2/worker2/worker_2.py new file mode 100644 index 0000000..8788164 --- /dev/null +++ b/degtyarev_mikhail_lab_2/worker2/worker_2.py @@ -0,0 +1,30 @@ +def find_max_and_save_square(file_path): + try: + with open(file_path, 'r') as file: + numbers = [int(line.strip()) for line in file.readlines()] + + if numbers: + max_number = max(numbers) + square = max_number ** 2 + + with open("/var/result/result.txt", 'w') as output_file: + output_file.write(str(square)) + + print(f"Наибольшее число: {max_number}, его вторая степень: {max_number}^2 = {square}") + return square + else: + return None + + except Exception as e: + return None + +def main(): + input_file_path = "/var/data/data.txt" + + result = find_max_and_save_square(input_file_path) + + if result is None: + print("No result to save.") + +if __name__ == "__main__": + main()