Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM egeselcuk/quiz_cli_bashfile:latest

# Install required packages
RUN pip install toml termcolor

# Create app directory
WORKDIR /app

# Copy all directories to /app
COPY ./ /app/

# Execute the delete_directories.sh script
RUN /app/delete_directories.sh

# Delete delete_directories.sh file
RUN rm /app/delete_directories.sh

# Set entry point to start the container in interactive mode with main.py
#CMD [ "python", "-i", "main.py" ]
Binary file added __pycache__/color.cpython-39.pyc
Binary file not shown.
Binary file modified __pycache__/main.cpython-39.pyc
Binary file not shown.
Binary file modified __pycache__/test_executor.cpython-39.pyc
Binary file not shown.
Binary file modified __pycache__/test_folder_utils.cpython-39.pyc
Binary file not shown.
Binary file modified __pycache__/test_utils.cpython-39.pyc
Binary file not shown.
6 changes: 6 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: '3'
services:
myapp:
build:
context: .
dockerfile: Dockerfile
45 changes: 45 additions & 0 deletions docker_operations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import subprocess # Importing the `subprocess` module for running shell commands
from pathlib import Path # Importing the `Path` class from the `pathlib` module for working with file paths

def start_container_from_compose(compose_file):
try:
# Running the `docker-compose` command to start containers in detached mode (-d flag)
subprocess.run(["docker-compose", "-f", compose_file, "up", "-d"], check=True)
print(f"Containers started successfully using Docker Compose: {compose_file}")
except subprocess.CalledProcessError as e:
# Handling any errors that occur during the process
print(f"An error occurred while starting the containers using Docker Compose: {e}")

def stop_and_remove_container_from_compose(compose_file):
try:
# Listing the images used by the created containers using `docker-compose images` command
images = subprocess.check_output(["docker-compose", "-f", compose_file, "images", "-q"]).decode().splitlines()

# Stopping the containers using `docker-compose down` command
subprocess.run(["docker-compose", "-f", compose_file, "down"], check=True)
print(f"Containers stopped and removed successfully using Docker Compose: {compose_file}")

# Removing the Docker Compose services using `docker-compose rm` command
subprocess.run(["docker-compose", "-f", compose_file, "rm", "-fsv"], check=True)
print(f"Docker Compose services removed successfully: {compose_file}")

# Removing the images using `docker image rm` command
for image in images:
subprocess.run(["docker", "image", "rm", image], check=True)

print("Docker images deleted successfully")
except subprocess.CalledProcessError as e:
print(f"An error occurred while stopping or removing the containers using Docker Compose: {e}")

# Example usage
compose_file = Path("docker-compose.yaml") # Creating a `Path` object for the Docker Compose file path
start = 0 # Set to 1 to start the containers from Docker Compose

if start == 1:
start_container_from_compose(compose_file) # Calling the function to start the containers
else:
stop_and_remove_container_from_compose(compose_file) # Calling the function to stop and remove the containers


# Copies the binary_options directory from the host machine to the /app/binary_options directory inside the Docker image.
# COPY binary_options/ /app/binary_options/
10 changes: 10 additions & 0 deletions to_be_deleted/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM python:3.11-slim-buster

WORKDIR /app

# Copy the delete_directories.sh script to /app
COPY delete_directories.sh /app/

# Set execute permissions for the script
RUN chmod +x /app/delete_directories.sh

17 changes: 17 additions & 0 deletions to_be_deleted/delete_directories.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

# Function to check if a directory contains a .toml file
directory_contains_toml() {
for file in "$1"/*.toml; do
[ -e "$file" ] && return 0
done
return 1
}

# Loop through directories and delete those without .toml files or named "docs"
for dir in */; do
if [[ "$dir" == "docs/" ]] || ! directory_contains_toml "$dir"; then
echo "Deleting directory: $dir"
rm -rf "$dir"
fi
done
35 changes: 35 additions & 0 deletions to_be_deleted/directory_structure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import os

def create_directory_structure():
# Create the top-level directory
os.makedirs("folder1")
os.makedirs("folder2")
os.makedirs("folder3")
os.makedirs("docs")

# Create files within folder1
with open("folder1/file1.txt", "w") as file:
file.write("This is file1.txt")

with open("folder1/file2.doc", "w") as file:
file.write("This is file2.doc")

# Create files within folder2
with open("folder2/file3.toml", "w") as file:
file.write("This is file3.toml")

with open("folder2/file4.toml", "w") as file:
file.write("This is file4.toml")

# Create files within folder3
with open("folder3/file5.xml", "w") as file:
file.write("This is file5.xml")

with open("folder3/file6.toml", "w") as file:
file.write("This is file6.toml")

with open("docs/file7.toml", "w") as file:
file.write("This is file7.toml")

# Call the function to create the directory structure
create_directory_structure()
6 changes: 6 additions & 0 deletions to_be_deleted/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: '3'
services:
myapp:
build:
context: .
dockerfile: Dockerfile
45 changes: 45 additions & 0 deletions to_be_deleted/docker_operations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import subprocess # Importing the `subprocess` module for running shell commands
from pathlib import Path # Importing the `Path` class from the `pathlib` module for working with file paths

def start_container_from_compose(compose_file):
try:
# Running the `docker-compose` command to start containers in detached mode (-d flag)
subprocess.run(["docker-compose", "-f", compose_file, "up", "-d"], check=True)
print(f"Containers started successfully using Docker Compose: {compose_file}")
except subprocess.CalledProcessError as e:
# Handling any errors that occur during the process
print(f"An error occurred while starting the containers using Docker Compose: {e}")

def stop_and_remove_container_from_compose(compose_file):
try:
# Listing the images used by the created containers using `docker-compose images` command
images = subprocess.check_output(["docker-compose", "-f", compose_file, "images", "-q"]).decode().splitlines()

# Stopping the containers using `docker-compose down` command
subprocess.run(["docker-compose", "-f", compose_file, "down"], check=True)
print(f"Containers stopped and removed successfully using Docker Compose: {compose_file}")

# Removing the Docker Compose services using `docker-compose rm` command
subprocess.run(["docker-compose", "-f", compose_file, "rm", "-fsv"], check=True)
print(f"Docker Compose services removed successfully: {compose_file}")

# Removing the images using `docker image rm` command
for image in images:
subprocess.run(["docker", "image", "rm", image], check=True)

print("Docker images deleted successfully")
except subprocess.CalledProcessError as e:
print(f"An error occurred while stopping or removing the containers using Docker Compose: {e}")

# Example usage
compose_file = Path("docker-compose.yaml") # Creating a `Path` object for the Docker Compose file path
start = 0 # Set to 1 to start the containers from Docker Compose

if start == 1:
start_container_from_compose(compose_file) # Calling the function to start the containers
else:
stop_and_remove_container_from_compose(compose_file) # Calling the function to stop and remove the containers


# Copies the binary_options directory from the host machine to the /app/binary_options directory inside the Docker image.
# COPY binary_options/ /app/binary_options/
1 change: 1 addition & 0 deletions to_be_deleted/docs/file7.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is file7.toml
1 change: 1 addition & 0 deletions to_be_deleted/folder1/file1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is file1.txt
1 change: 1 addition & 0 deletions to_be_deleted/folder1/file2.doc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is file2.doc
1 change: 1 addition & 0 deletions to_be_deleted/folder2/file3.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is file3.toml
1 change: 1 addition & 0 deletions to_be_deleted/folder2/file4.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is file4.toml
1 change: 1 addition & 0 deletions to_be_deleted/folder3/file5.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is file5.xml
1 change: 1 addition & 0 deletions to_be_deleted/folder3/file6.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is file6.toml