From 24445b9d9ae366d2cafc4b507a4e4f833ed8dd50 Mon Sep 17 00:00:00 2001 From: wass Date: Sun, 14 Apr 2024 10:15:16 +0200 Subject: [PATCH] created initial docker automated workflow --- Dockerfile | 16 ---------------- containers/.gitignore | 1 + containers/docker-compose.yaml | 21 +++++++++++++++++++++ containers/run.py | 21 +++++++++++++++++++++ containers/seeds/Dockerfile | 17 +++++++++++++++++ containers/seeds/fetch.py | 29 +++++++++++++++++++++++++++++ containers/seeds/seeds.yaml | 1 + docker-compose.yaml | 12 ------------ run.cmd | 12 +----------- 9 files changed, 91 insertions(+), 39 deletions(-) delete mode 100644 Dockerfile create mode 100644 containers/.gitignore create mode 100644 containers/docker-compose.yaml create mode 100644 containers/run.py create mode 100644 containers/seeds/Dockerfile create mode 100644 containers/seeds/fetch.py create mode 100644 containers/seeds/seeds.yaml delete mode 100644 docker-compose.yaml diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 8c51924..0000000 --- a/Dockerfile +++ /dev/null @@ -1,16 +0,0 @@ -FROM node:20 -WORKDIR /workspace - -RUN npm install -g pnpm -RUN apt-get update && apt-get install -y git -RUN git clone https://github.com/MicroWebStacks/astro-big-doc.git /astro-big-doc -WORKDIR /astro-big-doc - -RUN pnpm install - -EXPOSE 3001 - -RUN pnpm astro telemetry disable - -ENTRYPOINT ["pnpm", "run"] -CMD ["build"] diff --git a/containers/.gitignore b/containers/.gitignore new file mode 100644 index 0000000..8852f99 --- /dev/null +++ b/containers/.gitignore @@ -0,0 +1 @@ +dockerfiles diff --git a/containers/docker-compose.yaml b/containers/docker-compose.yaml new file mode 100644 index 0000000..3b1b8ef --- /dev/null +++ b/containers/docker-compose.yaml @@ -0,0 +1,21 @@ +version: "3.8" + +services: + #fetches dockerfiles e.g. ./seeds.yaml => ./dockerfiles/astro-big-doc.Dockerfile,... + seeds: + build: + context: ./seeds + volumes: + - ./dockerfiles:/dockerfiles + astro-big-doc: + build: + context: ./dockerfiles + dockerfile: astro-big-doc.Dockerfile + volumes: + - ../content:/astro-big-doc/content + - ../public:/astro-big-doc/public + - ../menu.yaml:/astro-big-doc/menu.yaml + - ../dist:/astro-big-doc/dist + ports: + - "3001:3001" + - "4321:4321" diff --git a/containers/run.py b/containers/run.py new file mode 100644 index 0000000..9094025 --- /dev/null +++ b/containers/run.py @@ -0,0 +1,21 @@ +import sys +import subprocess +import os + +# Path to the directory containing docker-compose.yml +docker_compose_dir = os.path.join(os.path.dirname(__file__)) + +# Default command if no argument is provided +command = "build" + +# Check if an argument is provided and use it as the command +if len(sys.argv) > 1: + command = sys.argv[1] + +# Change the working directory +os.chdir(docker_compose_dir) + +if(command == "build"): + subprocess.run(["docker-compose", "run", "--rm", "astro-big-doc", "build"]) +elif(command == "init"): + subprocess.run(["docker-compose", "run", "--rm", "seeds"]) diff --git a/containers/seeds/Dockerfile b/containers/seeds/Dockerfile new file mode 100644 index 0000000..6e588d0 --- /dev/null +++ b/containers/seeds/Dockerfile @@ -0,0 +1,17 @@ +# Use an official Python runtime as a parent image +FROM python:3.9-slim + +# Set the working directory in the container +WORKDIR /app + +# Install Python dependencies +RUN pip install pyyaml requests + +# Copy the Python script into the container at /app +COPY fetch.py /app/ + +# Copy the YAML file containing the mappings into the container at /app +COPY seeds.yaml /app/ + +# Command to run the fetch script +CMD ["python", "fetch.py", "seeds.yaml"] diff --git a/containers/seeds/fetch.py b/containers/seeds/fetch.py new file mode 100644 index 0000000..514b36a --- /dev/null +++ b/containers/seeds/fetch.py @@ -0,0 +1,29 @@ +import yaml +import requests +import sys + +def fetch_and_save_dockerfiles(yaml_input): + # Load the YAML data from string + dockerfile_mappings = yaml.safe_load(yaml_input) + + for item in dockerfile_mappings: + for key, url in item.items(): + # Fetch the Dockerfile from the URL + response = requests.get(url) + if response.status_code == 200: + # Save the Dockerfile content to a file named .Dockerfile + filename = f"/dockerfiles/{key}.Dockerfile" + with open(filename, 'w') as file: + file.write(response.text) + print(f"Saved {filename}") + else: + print(f"Failed to fetch {url} with status code {response.status_code}") + +if __name__ == "__main__": + # Assume the YAML data is passed as a command line argument + if len(sys.argv) > 1: + with open(sys.argv[1], 'r') as file: + yaml_input = file.read() + fetch_and_save_dockerfiles(yaml_input) + else: + print("Please provide a file path to the YAML input as an argument.") diff --git a/containers/seeds/seeds.yaml b/containers/seeds/seeds.yaml new file mode 100644 index 0000000..ebed3b9 --- /dev/null +++ b/containers/seeds/seeds.yaml @@ -0,0 +1 @@ +- astro-big-doc: https://raw.githubusercontent.com/MicroWebStacks/astro-big-doc/main/Dockerfile diff --git a/docker-compose.yaml b/docker-compose.yaml deleted file mode 100644 index 5d0d4cc..0000000 --- a/docker-compose.yaml +++ /dev/null @@ -1,12 +0,0 @@ -version: "3.8" - -services: - astro-doc: - build: . - volumes: - - ./content:/astro-big-doc/content - - ./public:/astro-big-doc/public - - ./menu.yaml:/astro-big-doc/menu.yaml - - ./dist:/astro-big-doc/dist - container_name: astro-doc-builder - network_mode: host diff --git a/run.cmd b/run.cmd index b8df9e2..838e3da 100644 --- a/run.cmd +++ b/run.cmd @@ -1,12 +1,2 @@ @echo off -setlocal - -:: Default command if no argument is provided -set COMMAND=build - -:: Check if an argument is provided and use it as the command -if not "%~1"=="" set COMMAND=%~1 - -:: Run the Docker Compose command -docker-compose run --rm astro-doc %COMMAND% -endlocal +python containers\run.py %*