From 4cb26457d1254c74f40c2358ce53baf360cc45bf Mon Sep 17 00:00:00 2001 From: Iliyan Vutov Date: Wed, 16 Oct 2024 18:29:02 +0300 Subject: [PATCH 1/6] U24 --- .python-version | 1 + app/app.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+) create mode 100644 .python-version create mode 100644 app/app.py diff --git a/.python-version b/.python-version new file mode 100644 index 000000000..b6d8b7612 --- /dev/null +++ b/.python-version @@ -0,0 +1 @@ +3.11.8 diff --git a/app/app.py b/app/app.py new file mode 100644 index 000000000..67e0180c0 --- /dev/null +++ b/app/app.py @@ -0,0 +1,14 @@ +import os + +from flask import Flask + +app = Flask(__name__) + + +@app.route("/") +def hello_world(): + return "Hello, World!" + + +if __name__ == "__main__": + app.run(port=os.environ.get("PORT", 3000), host="0.0.0.0") From ae1daf88d06f2d900bc1b650f9966192246e661b Mon Sep 17 00:00:00 2001 From: Iliyan Vutov Date: Wed, 23 Oct 2024 19:23:42 +0300 Subject: [PATCH 2/6] Add requirements.txt --- requirements.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 requirements.txt diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 000000000..047e9501a --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +Flask==3.0.0 From 63b2492b829944f58a8ffd2e2a5d7d8295b72f8e Mon Sep 17 00:00:00 2001 From: Iliyan Vutov Date: Wed, 23 Oct 2024 19:24:30 +0300 Subject: [PATCH 3/6] Add requirements.txt --- requirements.txt | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 047e9501a..b5ba78cca 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,9 @@ -Flask==3.0.0 +blinker==1.6.3 ; python_version >= "3.10" and python_version < "4.0" +click==8.1.7 ; python_version >= "3.10" and python_version < "4.0" +colorama==0.4.6 ; python_version >= "3.10" and python_version < "4.0" and platform_system == "Windows" +flask==3.0.0 ; python_version >= "3.10" and python_version < "4.0" +itsdangerous==2.1.2 ; python_version >= "3.10" and python_version < "4.0" +jinja2==3.1.2 ; python_version >= "3.10" and python_version < "4.0" +markupsafe==2.1.3 ; python_version >= "3.10" and python_version < "4.0" +werkzeug==3.0.0 ; python_version >= "3.10" and python_version < "4.0" + From 69385c753969e1fcbbfeef3563ea72e889af19d0 Mon Sep 17 00:00:00 2001 From: mlutzkan Date: Sat, 26 Oct 2024 12:34:41 +0300 Subject: [PATCH 4/6] Martin Lutzkanov - Docker exercise 1 --- Dockerfile | 22 ++++++++++++++++++++++ app/app.py | 2 +- requirements.txt | 11 ++--------- 3 files changed, 25 insertions(+), 10 deletions(-) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..0f2836413 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,22 @@ +FROM ubuntu:22.04 + +RUN apt-get update && \ + apt-get install -y \ + python3 \ + python3-pip && \ + groupadd -g 1234 notroot && \ + useradd -m -u 1234 -g notroot notroot + +USER notroot + +WORKDIR /app + +COPY requirements.txt . + +RUN pip install -r requirements.txt + +COPY app . + +EXPOSE 5000 + +CMD ["python3", "app.py"] \ No newline at end of file diff --git a/app/app.py b/app/app.py index 67e0180c0..1c2c83d55 100644 --- a/app/app.py +++ b/app/app.py @@ -11,4 +11,4 @@ def hello_world(): if __name__ == "__main__": - app.run(port=os.environ.get("PORT", 3000), host="0.0.0.0") + app.run(port=os.environ.get("PORT", 5000), host="0.0.0.0") diff --git a/requirements.txt b/requirements.txt index b5ba78cca..34b9e906f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,9 +1,2 @@ -blinker==1.6.3 ; python_version >= "3.10" and python_version < "4.0" -click==8.1.7 ; python_version >= "3.10" and python_version < "4.0" -colorama==0.4.6 ; python_version >= "3.10" and python_version < "4.0" and platform_system == "Windows" -flask==3.0.0 ; python_version >= "3.10" and python_version < "4.0" -itsdangerous==2.1.2 ; python_version >= "3.10" and python_version < "4.0" -jinja2==3.1.2 ; python_version >= "3.10" and python_version < "4.0" -markupsafe==2.1.3 ; python_version >= "3.10" and python_version < "4.0" -werkzeug==3.0.0 ; python_version >= "3.10" and python_version < "4.0" - +Flask==3.0.3 + \ No newline at end of file From 40a742f0a1dc01a11c66f9e65cbbdae6d2f93d74 Mon Sep 17 00:00:00 2001 From: mlutzkan Date: Mon, 4 Nov 2024 18:34:58 +0200 Subject: [PATCH 5/6] ansible playbook updated --- ansible/playbook.yml | 49 ++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 14 +++++++++++-- 2 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 ansible/playbook.yml diff --git a/ansible/playbook.yml b/ansible/playbook.yml new file mode 100644 index 000000000..107cbf3b3 --- /dev/null +++ b/ansible/playbook.yml @@ -0,0 +1,49 @@ +- hosts: localhost + become: no + + vars: + image_name: "lutzkanov/test-python-app" + image_tag: "v0.2" + application_directory: "/tmp/app" + + tasks: + - name: Create {{ application_directory }} directory + file: + name: "{{ application_directory }}" + state: directory + mode: 0755 + + - name: Checkout repository + git: + repo: git@github.com:lutzkanov/devops-programme + dest: "{{ application_directory }}" + version: main + update: yes + + - name: Build Docker image + docker_image: + build: + path: "{{ application_directory }}" + name: "{{ image_name }}" + tag: "{{ image_tag }}" + push: true + source: build + docker_host: "unix://{{ ansible_env.HOME }}/.rd/docker.sock" + state: present + + - name: Run the built Docker image + docker_container: + name: "python-web" + image: "{{ image_name }}:{{ image_tag }}" + state: started + auto_remove: yes + ports: + - "5001:5001" + env: + PORT: "5001" + healthcheck: + test: ["CMD", "curl", "--fail", "http://localhost:5001"] + interval: 10s + retries: 3 + start_period: 5s + docker_host: "unix://{{ ansible_env.HOME }}/.rd/docker.sock" \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 34b9e906f..a1a99e359 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,12 @@ -Flask==3.0.3 - \ No newline at end of file +ansible==10.3.0 +ansible-compat==24.9.1 +ansible-core==2.17.5 +ansible-lint==24.9.2 +blinker==1.6.3 ; python_version >= "3.10" and python_version < "4.0" +click==8.1.7 ; python_version >= "3.10" and python_version < "4.0" +colorama==0.4.6 ; python_version >= "3.10" and python_version < "4.0" and platform_system == "Windows" +flask==3.0.0 ; python_version >= "3.10" and python_version < "4.0" +itsdangerous==2.1.2 ; python_version >= "3.10" and python_version < "4.0" +jinja2==3.1.2 ; python_version >= "3.10" and python_version < "4.0" +markupsafe==2.1.3 ; python_version >= "3.10" and python_version < "4.0" +werkzeug==3.0.0 ; python_version >= "3.10" and python_version < "4.0" \ No newline at end of file From 7a20c502791b85bce89b83cf777d32e4070037b9 Mon Sep 17 00:00:00 2001 From: mlutzkan Date: Mon, 4 Nov 2024 19:59:22 +0200 Subject: [PATCH 6/6] Working Ansible playbook --- Dockerfile | 34 +++++++++++++++++----------------- ansible/playbook.yml | 8 ++++---- app/app.py | 2 +- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/Dockerfile b/Dockerfile index 0f2836413..ef4df40ca 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,22 +1,22 @@ FROM ubuntu:22.04 - -RUN apt-get update && \ - apt-get install -y \ + +RUN apt-get update \ + && apt-get install -y --no-install-recommends \ python3 \ - python3-pip && \ - groupadd -g 1234 notroot && \ - useradd -m -u 1234 -g notroot notroot - -USER notroot + python3-pip \ + curl \ + && mkdir /app \ + && useradd -d /app app-user \ + && chown --recursive app-user /app + +COPY app/requirements.txt /app/requirements.txt +RUN pip3 install -r app/requirements.txt +COPY app/app.py /app WORKDIR /app - -COPY requirements.txt . - -RUN pip install -r requirements.txt - -COPY app . - + EXPOSE 5000 - -CMD ["python3", "app.py"] \ No newline at end of file + +USER app-user + +CMD [ "python3", "app.py" ] \ No newline at end of file diff --git a/ansible/playbook.yml b/ansible/playbook.yml index 107cbf3b3..d8eb18734 100644 --- a/ansible/playbook.yml +++ b/ansible/playbook.yml @@ -2,7 +2,7 @@ become: no vars: - image_name: "lutzkanov/test-python-app" + image_name: "mlutzkan/test-python-app" image_tag: "v0.2" application_directory: "/tmp/app" @@ -17,7 +17,7 @@ git: repo: git@github.com:lutzkanov/devops-programme dest: "{{ application_directory }}" - version: main + version: lutzkanov_docker_1 update: yes - name: Build Docker image @@ -28,7 +28,7 @@ tag: "{{ image_tag }}" push: true source: build - docker_host: "unix://{{ ansible_env.HOME }}/.rd/docker.sock" + docker_host: "unix:///var/run/docker.sock" state: present - name: Run the built Docker image @@ -46,4 +46,4 @@ interval: 10s retries: 3 start_period: 5s - docker_host: "unix://{{ ansible_env.HOME }}/.rd/docker.sock" \ No newline at end of file + docker_host: "unix:///var/run/docker.sock" \ No newline at end of file diff --git a/app/app.py b/app/app.py index 1c2c83d55..2fe1f5152 100644 --- a/app/app.py +++ b/app/app.py @@ -11,4 +11,4 @@ def hello_world(): if __name__ == "__main__": - app.run(port=os.environ.get("PORT", 5000), host="0.0.0.0") + app.run(port=os.environ.get("PORT", 5000), host="0.0.0.0") \ No newline at end of file