From 96d56033e21089ad5be552ed8e70389c9182965a Mon Sep 17 00:00:00 2001 From: Caio Trevisan Date: Wed, 16 Nov 2022 23:02:40 -0300 Subject: [PATCH 1/5] add exercises bootstrapper --- Makefile | 9 ++- docker-compose.yml | 6 ++ exercises/Dockerfile | 9 +++ exercises/run.py | 56 +++++++++++++++++++ students/caiocezart/exercises/git04/README.md | 26 --------- .../caiocezart/exercises/git04/config.json | 3 - .../caiocezart/exercises/git04/my_env.txt | 3 - 7 files changed, 79 insertions(+), 33 deletions(-) create mode 100644 exercises/Dockerfile create mode 100644 exercises/run.py delete mode 100644 students/caiocezart/exercises/git04/README.md delete mode 100644 students/caiocezart/exercises/git04/config.json delete mode 100644 students/caiocezart/exercises/git04/my_env.txt diff --git a/Makefile b/Makefile index 4facd8028..4aba3f805 100644 --- a/Makefile +++ b/Makefile @@ -54,4 +54,11 @@ shell: @ if [ "${${*}}" = "" ]; then \ echo "Environment variable $* not set"; \ exit 1; \ - fi \ No newline at end of file + fi + +exercises: + $(RUNNER) exercises +.PHONY: exercises + +exercises-sh: + $(RUNNER) --entrypoint "" exercises bash diff --git a/docker-compose.yml b/docker-compose.yml index 67ad77be5..3d925c51b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -38,3 +38,9 @@ services: - AWS_DEFAULT_REGION entrypoint: - bash + exercises: + image: devopsacademyau/reviewer:latest + volumes: + - ${PWD}/students:/students + environment: + - GH_USER diff --git a/exercises/Dockerfile b/exercises/Dockerfile new file mode 100644 index 000000000..928188f12 --- /dev/null +++ b/exercises/Dockerfile @@ -0,0 +1,9 @@ +FROM python:3.10-slim-bullseye +WORKDIR /app + +# ADD requirements.txt /app +COPY . /app + +# RUN pip3 install -r requirements.txt + +CMD [ "python", "./run.py"] \ No newline at end of file diff --git a/exercises/run.py b/exercises/run.py new file mode 100644 index 000000000..a2e5055a6 --- /dev/null +++ b/exercises/run.py @@ -0,0 +1,56 @@ +import argparse +import os +import shutil +import sys +from pathlib import Path + +EXERCISES_FOLDER=Path("/app/") +STUDENT_EXERCISES_FOLDER=Path("/students/{}".format(os.environ['GH_USER'])) + +def exercise(exercise): + if not exercise: + exercise = input("What exercise would you like to generate files for: ") + + exercise_folder = Path(EXERCISES_FOLDER, "{}".format(exercise)) + student_exercise_folder = Path(STUDENT_EXERCISES_FOLDER, "exercises/{}".format(exercise)) + + print(exercise_folder) + print(student_exercise_folder) + if not exercise_folder.exists(): + sys.exit("Exercise not found.") + + if student_exercise_folder.exists(): + print("Exercise folder already exists.") + refresh = input("Do you want to refresh all the exercise files anyway? (y/Y): ") + if refresh.lower() != 'y': + print("Aborted.") + sys.exit() + + if not student_exercise_folder.exists(): + print("Creating folder {}".format(student_exercise_folder)) + Path.mkdir(student_exercise_folder) + + print("Copying all files from exercise {}".format(exercise)) + shutil.copytree(exercise_folder, student_exercise_folder, dirs_exist_ok=True) + +def setup(): + # override folder path when running python script outside docker + if os.getenv('LOCAL_RUN', False): + global EXERCISES_FOLDER + global STUDENT_EXERCISES_FOLDER + EXERCISES_FOLDER=Path(Path().resolve(), "exercises") + STUDENT_EXERCISES_FOLDER=Path(Path().resolve(), "students/{}".format(os.environ['GH_USER'])) + +def main(): + setup() + parser = argparse.ArgumentParser() + # parser.add_argument(dest='option', help="What option to run") + parser.add_argument('-e', dest='exercise', help="What exercise name to run", required=False) + args = parser.parse_args() + + # if args.option == "exercise": + exercise(args.exercise) + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/students/caiocezart/exercises/git04/README.md b/students/caiocezart/exercises/git04/README.md deleted file mode 100644 index 3c72d0a77..000000000 --- a/students/caiocezart/exercises/git04/README.md +++ /dev/null @@ -1,26 +0,0 @@ -## Secrets (c01-git04) - -Perform the following commands: -1. In the same repository of the previous exercise: -2. Add a file called `my_env.txt` that contains - ``` - SERVICE_NAME=account-management - ENVIRONMENT=prod - PASSWORD=pass1234 - ``` -1. Commit it to your local repository -2. Check the log with `git log` - -**Questions** - -1. Let's suppose you remotely pushed the `my_env.txt` file above. A colleague asks you to remove this information from Git. What's your colleague worried about? -2. If you modify the file in your workspace, then commit and push it, will it be enough to erase this password information from the repository? (It's not). Why? -3. If you delete the file and push it, then create a new one with the rest of the information, is it enough? (It's not). Why? -4. How to fix this? How do you remove something from Git history when it is in the remote repository? -5. Which commands would you use? Explain what the command does. - -## Submit a PR with the following files - -> Remember to follow the instructions on [how to submit a PR here](/README.md#exercises) - -- **README.md**: copy from file [ANSWER.md](ANSWER.md), answering the questions above. Include details and commands used. \ No newline at end of file diff --git a/students/caiocezart/exercises/git04/config.json b/students/caiocezart/exercises/git04/config.json deleted file mode 100644 index 874436614..000000000 --- a/students/caiocezart/exercises/git04/config.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "version": "v1.0.0" -} \ No newline at end of file diff --git a/students/caiocezart/exercises/git04/my_env.txt b/students/caiocezart/exercises/git04/my_env.txt deleted file mode 100644 index 185888270..000000000 --- a/students/caiocezart/exercises/git04/my_env.txt +++ /dev/null @@ -1,3 +0,0 @@ -SERVICE_NAME=account-management -ENVIRONMENT=prod -PASSWORD=pass1234 \ No newline at end of file From 521ef5b9800f6cf699b6268f7762e12614c7ca18 Mon Sep 17 00:00:00 2001 From: Caio Trevisan Date: Wed, 16 Nov 2022 23:20:35 -0300 Subject: [PATCH 2/5] git04 poc --- exercises/run.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/exercises/run.py b/exercises/run.py index a2e5055a6..5a34a2284 100644 --- a/exercises/run.py +++ b/exercises/run.py @@ -14,8 +14,8 @@ def exercise(exercise): exercise_folder = Path(EXERCISES_FOLDER, "{}".format(exercise)) student_exercise_folder = Path(STUDENT_EXERCISES_FOLDER, "exercises/{}".format(exercise)) - print(exercise_folder) - print(student_exercise_folder) + # print(exercise_folder) + # print(student_exercise_folder) if not exercise_folder.exists(): sys.exit("Exercise not found.") @@ -28,11 +28,21 @@ def exercise(exercise): if not student_exercise_folder.exists(): print("Creating folder {}".format(student_exercise_folder)) - Path.mkdir(student_exercise_folder) + Path.mkdir(student_exercise_folder, parents=True) print("Copying all files from exercise {}".format(exercise)) shutil.copytree(exercise_folder, student_exercise_folder, dirs_exist_ok=True) + # run exercise after script setup + custom_config(exercise) + +def custom_config(exercise): + if exercise == "git04": + with open(Path(STUDENT_EXERCISES_FOLDER, "exercises/{}/my_env.txt".format(exercise)), 'w') as f: + f.write("SERVICE_NAME=account-management\nENVIRONMENT=prod\nPASSWORD=pass1234") + os.system("git add {}".format(Path(STUDENT_EXERCISES_FOLDER, "exercises/{}/my_env.txt".format(exercise)))) + os.system("git commit -am 'commit my secret'") + def setup(): # override folder path when running python script outside docker if os.getenv('LOCAL_RUN', False): From 0ab0764184a741f4948aa43321359fe46bb8701f Mon Sep 17 00:00:00 2001 From: Caio Trevisan Date: Wed, 16 Nov 2022 23:46:35 -0300 Subject: [PATCH 3/5] fix git04 exercise --- exercises/Dockerfile | 3 +++ students/caiocezart/exercises/git04/README.md | 26 +++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 students/caiocezart/exercises/git04/README.md diff --git a/exercises/Dockerfile b/exercises/Dockerfile index 928188f12..6425cf04c 100644 --- a/exercises/Dockerfile +++ b/exercises/Dockerfile @@ -1,6 +1,9 @@ FROM python:3.10-slim-bullseye WORKDIR /app +RUN apt-get update -y && apt-get install -y git +RUN git config --global user.email "dummy@devopsacademy.com" && \ + git config --global user.name "Dummy DA" # ADD requirements.txt /app COPY . /app diff --git a/students/caiocezart/exercises/git04/README.md b/students/caiocezart/exercises/git04/README.md new file mode 100644 index 000000000..3c72d0a77 --- /dev/null +++ b/students/caiocezart/exercises/git04/README.md @@ -0,0 +1,26 @@ +## Secrets (c01-git04) + +Perform the following commands: +1. In the same repository of the previous exercise: +2. Add a file called `my_env.txt` that contains + ``` + SERVICE_NAME=account-management + ENVIRONMENT=prod + PASSWORD=pass1234 + ``` +1. Commit it to your local repository +2. Check the log with `git log` + +**Questions** + +1. Let's suppose you remotely pushed the `my_env.txt` file above. A colleague asks you to remove this information from Git. What's your colleague worried about? +2. If you modify the file in your workspace, then commit and push it, will it be enough to erase this password information from the repository? (It's not). Why? +3. If you delete the file and push it, then create a new one with the rest of the information, is it enough? (It's not). Why? +4. How to fix this? How do you remove something from Git history when it is in the remote repository? +5. Which commands would you use? Explain what the command does. + +## Submit a PR with the following files + +> Remember to follow the instructions on [how to submit a PR here](/README.md#exercises) + +- **README.md**: copy from file [ANSWER.md](ANSWER.md), answering the questions above. Include details and commands used. \ No newline at end of file From ce67d76b56470b9956c3035c9f09d84d63516654 Mon Sep 17 00:00:00 2001 From: Caio Trevisan Date: Thu, 17 Nov 2022 01:10:08 -0300 Subject: [PATCH 4/5] fix docker paths --- exercises/run.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/exercises/run.py b/exercises/run.py index 5a34a2284..2ffabc5e7 100644 --- a/exercises/run.py +++ b/exercises/run.py @@ -5,7 +5,8 @@ from pathlib import Path EXERCISES_FOLDER=Path("/app/") -STUDENT_EXERCISES_FOLDER=Path("/students/{}".format(os.environ['GH_USER'])) +DA_ROOT_PATH=Path("/da") +STUDENT_EXERCISES_FOLDER=Path("{}/students/{}".format(DA_ROOT_PATH, os.environ['GH_USER'])) def exercise(exercise): if not exercise: @@ -38,18 +39,25 @@ def exercise(exercise): def custom_config(exercise): if exercise == "git04": - with open(Path(STUDENT_EXERCISES_FOLDER, "exercises/{}/my_env.txt".format(exercise)), 'w') as f: + secret_file = "my_env.txt" + secret_file_path = Path(STUDENT_EXERCISES_FOLDER, "exercises/{}/{}".format(exercise, secret_file)) + if secret_file_path.exists(): + Path.unlink(secret_file_path) + print("Creating secret file {}".format(secret_file_path)) + with open(secret_file_path, 'w') as f: f.write("SERVICE_NAME=account-management\nENVIRONMENT=prod\nPASSWORD=pass1234") - os.system("git add {}".format(Path(STUDENT_EXERCISES_FOLDER, "exercises/{}/my_env.txt".format(exercise)))) - os.system("git commit -am 'commit my secret'") + + os.system("cd {} && git add {} && git commit -am 'commit my secret'".format(DA_ROOT_PATH, secret_file_path)) def setup(): # override folder path when running python script outside docker if os.getenv('LOCAL_RUN', False): global EXERCISES_FOLDER global STUDENT_EXERCISES_FOLDER - EXERCISES_FOLDER=Path(Path().resolve(), "exercises") - STUDENT_EXERCISES_FOLDER=Path(Path().resolve(), "students/{}".format(os.environ['GH_USER'])) + global DA_ROOT_PATH + EXERCISES_FOLDER = Path(Path().resolve(), "exercises") + STUDENT_EXERCISES_FOLDER = Path(Path().resolve(), "students/{}".format(os.environ['GH_USER'])) + DA_ROOT_PATH = Path(Path().resolve()) def main(): setup() From 1c82e0b502b0a69960aa37d5ab516ef175bb7fa9 Mon Sep 17 00:00:00 2001 From: Caio Trevisan Date: Thu, 17 Nov 2022 01:10:18 -0300 Subject: [PATCH 5/5] fix docker paths --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index 3d925c51b..67416a03e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -41,6 +41,6 @@ services: exercises: image: devopsacademyau/reviewer:latest volumes: - - ${PWD}/students:/students + - ${PWD}:/da environment: - GH_USER