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/Dockerfile b/Dockerfile new file mode 100644 index 000000000..ef4df40ca --- /dev/null +++ b/Dockerfile @@ -0,0 +1,22 @@ +FROM ubuntu:22.04 + +RUN apt-get update \ + && apt-get install -y --no-install-recommends \ + python3 \ + 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 + +EXPOSE 5000 + +USER app-user + +CMD [ "python3", "app.py" ] \ No newline at end of file diff --git a/ansible/playbook.yml b/ansible/playbook.yml new file mode 100644 index 000000000..d8eb18734 --- /dev/null +++ b/ansible/playbook.yml @@ -0,0 +1,49 @@ +- hosts: localhost + become: no + + vars: + image_name: "mlutzkan/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: lutzkanov_docker_1 + 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:///var/run/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:///var/run/docker.sock" \ No newline at end of file diff --git a/app/app.py b/app/app.py new file mode 100644 index 000000000..2fe1f5152 --- /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", 5000), host="0.0.0.0") \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 000000000..a1a99e359 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,12 @@ +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