Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nikolaeva Elizaveta SD-01 Lab12 #1354

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
9aa5396
Lab 1: solution
nikolaevaElizaveta Jan 28, 2025
ecddc9d
Lab2 solution
nikolaevaElizaveta Jan 30, 2025
8389eec
Merge remote-tracking branch 'upstream/master' into lab2
nikolaevaElizaveta Feb 5, 2025
a3aec93
Added CI pipeline with tests and Docker integration
nikolaevaElizaveta Feb 5, 2025
d635c12
Updated ci.yml
nikolaevaElizaveta Feb 5, 2025
7fdc8d4
Updated ci.yml
nikolaevaElizaveta Feb 5, 2025
f1b2b9d
Fix flake8 linting errors
nikolaevaElizaveta Feb 5, 2025
7a45ee6
Fix flake8 linting errors
nikolaevaElizaveta Feb 5, 2025
49a02a0
Fix werkzeug version for Flask compatibility
nikolaevaElizaveta Feb 5, 2025
78d9246
Snyk added
nikolaevaElizaveta Feb 5, 2025
dc60054
Snyk added
nikolaevaElizaveta Feb 5, 2025
b26e493
Final version of Lab3
nikolaevaElizaveta Feb 5, 2025
15db5f8
md fixes
nikolaevaElizaveta Feb 5, 2025
532eeec
Terraform
nikolaevaElizaveta Feb 6, 2025
205d593
Yandex Cloud
nikolaevaElizaveta Feb 6, 2025
76ab3b2
Lab 4 Solution
nikolaevaElizaveta Feb 6, 2025
a664311
Lab5 done&VM from Lab4
nikolaevaElizaveta Feb 16, 2025
784191b
Lab6 done
nikolaevaElizaveta Feb 16, 2025
7263a56
Lab 7 Solution
nikolaevaElizaveta Feb 19, 2025
3f2a732
Lab 7 Solution fixed screenshots
nikolaevaElizaveta Feb 19, 2025
f21d899
Lab 8 Solution
nikolaevaElizaveta Feb 19, 2025
56352fd
Lab 8 Solution linter fixes
nikolaevaElizaveta Feb 19, 2025
5ce2b4b
Lab 8 Solution linter fixes2
nikolaevaElizaveta Feb 19, 2025
4a014a6
Lab 8 Solution linter fixes3
nikolaevaElizaveta Feb 19, 2025
0eaa808
Lab 9 task 1
nikolaevaElizaveta Feb 28, 2025
876f714
Lab 9 task 2
nikolaevaElizaveta Feb 28, 2025
a056f5b
Lab 10 done
nikolaevaElizaveta Feb 28, 2025
9fc666e
Lab 11 done
nikolaevaElizaveta Mar 4, 2025
ae6b0b1
Lab 12 done
nikolaevaElizaveta Mar 4, 2025
e79d56f
Lab 12 done fixed linter
nikolaevaElizaveta Mar 4, 2025
b7963ee
Lab 12 done fixed linter2
nikolaevaElizaveta Mar 4, 2025
b472af9
Lab 12 done fixed linter3
nikolaevaElizaveta Mar 4, 2025
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
47 changes: 47 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: CI Pipeline

on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r app_python/requirements.txt

- name: Run linter (flake8)
run: |
pip install flake8
flake8 app_python --max-line-length=120

- name: Run tests
run: |
python -m unittest discover -s tests

docker:
needs: build
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Log in to Docker Hub
run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin

- name: Build Docker image
run: docker build -t nikolaevaelizaveta/my-flask-app:latest app_python/

- name: Push Docker image
run: docker push nikolaevaelizaveta/my-flask-app:latest
88 changes: 88 additions & 0 deletions ansible/ANSIBLE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Ansible related work

During this stage of homework I fully followed task requirements about project's structure, files' names and the idea in general.

Also to work with Ansible I had to install WSL, thus there were some errors related to the fact that all the previous steps were done on my Windows machine, including authorization in services and all te settings.

Here are some commands I used.

## Executing playbook to deploy the Docker role

```bash
liza@LAPTOP-T7RS5DP1:/mnt/c/Users/Elizoveta/Desktop/S25-core-course-labs/ansible$ ansible-playbook -i inventory/default_aws_ec2.yml playbooks/dev/main.yml

[WARNING]: Ansible is being run in a world writable directory (/mnt/c/Users/Elizoveta/Desktop/S25-core-course-
labs/ansible), ignoring it as an ansible.cfg source. For more information see
https://docs.ansible.com/ansible/devel/reference_appendices/config.html#cfg-in-world-writable-dir

PLAY [Docker VM] *******************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************
fatal: [host_01]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: no such identity: /home/liza/.ssh/id_ed25519_neww.pub: No such file or directory\r\[email protected]: Permission denied (publickey,password).", "unreachable": true}

PLAY RECAP *************************************************************************************************************
host_01 : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0
```

I could not manage to resolve this error because I followed lecture slides and lectures' ideas, so my guess is that there is some problem with transfering from Windows to WSL. When I tried to connect to VM from WSL I was asked for the password but I did not set any passwords. I tried to reset the password but it did not help: the command was performing for so long with no result.

## Inventory Details

```bash
liza@LAPTOP-T7RS5DP1:/mnt/c/Users/Elizoveta/Desktop/S25-core-course-labs/ansible$ ansible-inventory -i inventory/default_aws_ec2.yml --list
[WARNING]: Ansible is being run in a world writable directory (/mnt/c/Users/Elizoveta/Desktop/S25-core-course-
labs/ansible), ignoring it as an ansible.cfg source. For more information see
https://docs.ansible.com/ansible/devel/reference_appendices/config.html#cfg-in-world-writable-dir
{
"_meta": {
"hostvars": {
"host_01": {
"ansible_host": "158.160.57.28",
"ansible_ssh_private_key_file": "~/.ssh/id_ed25519_neww.pub",
"ansible_user": "ubuntu"
}
}
},
"all": {
"children": [
"ungrouped",
"myhosts"
]
},
"myhosts": {
"hosts": [
"host_01"
]
}
}

liza@LAPTOP-T7RS5DP1:/mnt/c/Users/Elizoveta/Desktop/S25-core-course-labs/ansible$ ansible-inventory -i inventory/default_aws_ec2.yml --graph
[WARNING]: Ansible is being run in a world writable directory (/mnt/c/Users/Elizoveta/Desktop/S25-core-course-
labs/ansible), ignoring it as an ansible.cfg source. For more information see
https://docs.ansible.com/ansible/devel/reference_appendices/config.html#cfg-in-world-writable-dir
@all:
|--@ungrouped:
|--@myhosts:
| |--host_01
```

# Lab 6 Logs of work on Web-App

```bash
PLAY [Docker VM] ******************************************************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************************************************
ok: [host_01]
PLAY [Deploy web application] **********************************************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************************************************
ok: [host_01]
TASK [web_app : Duplicate Docker configuration] ********************************************************************************************************************
changed: [host_01]
TASK [web_app : Docker compose installation] ************************************************************************************************************************
ok: [host_01]
TASK [web_app : Launch Docker compose] *************************************************************************************************************************************
changed: [host_01]
TASK [web_app : include_tasks] *********************************************************************************************************************************************
skipping: [host_01]
PLAY RECAP *****************************************************************************************************************************************************************
host_01 : ok=5 changed=2 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
```
4 changes: 4 additions & 0 deletions ansible/ansible.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[defaults]
inventory = inventory
playbook_dir = playbooks
roles_path = roles
14 changes: 14 additions & 0 deletions ansible/deployment_output.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[WARNING]: Ansible is being run in a world writable directory
(/mnt/c/Users/Elizoveta/Desktop/S25-core-course-labs/ansible), ignoring it as
an ansible.cfg source. For more information see
https://docs.ansible.com/ansible/devel/reference_appendices/config.html#cfg-in-
world-writable-dir

PLAY [Docker VM] ***************************************************************

TASK [Gathering Facts] *********************************************************
fatal: [host_01]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: no such identity: /home/liza/.ssh/id_ed25519_neww.pub: No such file or directory\r\[email protected]: Permission denied (publickey,password).", "unreachable": true}

PLAY RECAP *********************************************************************
host_01 : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0

6 changes: 6 additions & 0 deletions ansible/inventory/default_aws_ec2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
all:
hosts:
host_01:
ansible_host: 158.160.57.28
ansible_user: ubuntu
ansible_ssh_private_key_file: ~/.ssh/id_ed25519_neww
12 changes: 12 additions & 0 deletions ansible/playbooks/dev/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
- name: Docker VM
hosts: all
become: true

roles:
- docker

- name: Deploy web application
hosts: all
become: true
roles:
- web_app
21 changes: 21 additions & 0 deletions ansible/roles/docker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Docker Role

This role installs and configures Docker and Docker Compose.

## Requirements

- Ansible 2.9+
- Ubuntu 22.04

## Role Variables

- `docker_version`: The version of Docker to install (default: `latest`).
- `docker_compose_version`: The version of Docker Compose to install (default: `1.29.2`).

## Example Playbook

```yaml
- hosts: all
roles:
- role: ansible/roles/docker

2 changes: 2 additions & 0 deletions ansible/roles/docker/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
docker_version: latest
docker_compose_version: "1.29.2"
Empty file.
14 changes: 14 additions & 0 deletions ansible/roles/docker/tasks/install_compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
- name: Docker Compose
ansible.builtin.get_url:
url: "https://github.com/docker/compose/releases/download/{{ docker_compose_version }}/docker-compose-{{ ansible_system | lower }}-{{ ansible_architecture }}"
dest: /usr/local/bin/docker-compose
mode: 'a+x'

- name: Check Docker Compose
ansible.builtin.command: docker-compose --version
register: compose_version
changed_when: false

- name: Version od Docker Compose
ansible.builtin.debug:
msg: "Docker Compose version: {{ compose_version.stdout }}"
26 changes: 26 additions & 0 deletions ansible/roles/docker/tasks/install_docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
- name: Install packets
ansible.builtin.apt:
name:
- apt-transport-https
- ca-certificates
- curl
- gnupg-agent
- software-properties-common
state: present
update_cache: yes

- name: Add key GPG Docker
ansible.builtin.apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present

- name: Add repository of Docker
ansible.builtin.apt_repository:
repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
state: present

- name: Install Docker CE
ansible.builtin.apt:
name: docker-ce
state: latest
update_cache: yes
66 changes: 66 additions & 0 deletions ansible/roles/docker/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
- import_tasks: install_docker.yml
- import_tasks: install_compose.yml

- name: Apt cache
apt:
update_cache: yes

- name: Install packages
apt:
name:
- python3-pip
- apt-transport-https
- ca-certificates
- curl
- software-properties-common
state: present

- name: Docker GPG keys
file:
path: /etc/apt/sources.list.d/docker.list
state: absent
ignore_errors: yes

- name: Clear cache
apt:
autoclean: yes

- name: Docker repository configuration
apt_repository:
repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable
state: present
filename: docker
register: docker_repo

- name: Install Docker
apt:
name: docker-ce
state: present
when: docker_repo.changed

- name: Install Docker Compose
pip:
name: docker-compose
state: present

- name: Add Docker GPG key
apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present

- name: Add Docker repository
apt_repository:
repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable
state: present

- name: Auto-start Docker
ansible.builtin.systemd:
name: docker
enabled: yes
state: started

- name: Add user to Docker
ansible.builtin.user:
name: "{{ ansible_user }}"
groups: docker
append: yes
30 changes: 30 additions & 0 deletions ansible/roles/web_app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Web app

## Overview

This role is designed to deploy a web application using a Docker image and Docker Compose.

## Requirements

- Ansible 2.9+
- Ubuntu 22.04 or compatible Linux distributions
- Docker and Docker Compose

## Role Variables

- `docker_image`: The name of the Docker image to deploy (e.g., `my-flask-app:latest`).
- `app_port`: The port on which the application will be exposed (e.g., `8080`).
- `web_app_full_wipe`: A boolean flag to enable a full wipe of the container and associated files. Defaults to `false`.

## Usage

The role in playbook:

```yaml
- hosts: all
become: true
roles:
- role: ansible/roles/docker
- role: ansible/roles/web_app
```

5 changes: 5 additions & 0 deletions ansible/roles/web_app/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
docker_image: "my-flask-app:latest"

app_port: 8080

web_app_full_wipe: false
Empty file.
2 changes: 2 additions & 0 deletions ansible/roles/web_app/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dependencies:
- role: docker
15 changes: 15 additions & 0 deletions ansible/roles/web_app/tasks/0-wipe.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
- name: Stop and remove Docker container if any
docker_container:
name: web_app_container
state: absent
when: web_app_full_wipe
tags:
- wipe

- name: Remove Docker Compose file
file:
path: /home/{{ ansible_user }}/docker-compose.yml
state: absent
when: web_app_full_wipe
tags:
- wipe
Loading