Skip to content
Open

Lab 13 #2465

Show file tree
Hide file tree
Changes from all commits
Commits
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
91 changes: 91 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
name: Python CI

on:
push:
paths:
- 'app_python/**'
pull_request:
paths:
- 'app_python/**'

jobs:
test:
runs-on: ubuntu-latest
permissions:
contents: read
actions: read
security-events: write
defaults:
run:
shell: bash
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Cache Python dependencies
uses: actions/cache@v4
id: python-cache
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.12'

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

- name: Ensure visits.txt exists
run: |
mkdir -p /app_python
touch /app_python/visits.txt
chmod 666 /app_python/visits.txt

- name: Ensure visits file exists
run: echo "0" > app_python/visits.txt

- name: Run tests
run: pytest ./app_python/unit_test.py -v

- name: Run Snyk to check for vulnerabilities
uses: snyk/actions/python-3.10@master
with:
args: --skip-unresolved app_python/
env:
SNYK_TOKEN: ${{ secrets.API_SNYK }}

deploy:
needs: test
runs-on: ubuntu-latest
permissions:
contents: read
actions: read
defaults:
run:
shell: bash
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Login to DockerHub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_LOGIN }}
password: ${{ secrets.DOCKERHUB_PASS }}

- name: Build and push Docker image
uses: docker/build-push-action@v4
with:
context: ./app_python
file: ./app_python/Dockerfile
push: true
tags: ${{ secrets.DOCKERHUB_LOGIN }}/flask_app:latest
32 changes: 32 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
__pycache__/
*.pyc
*.pyo
*.pyd
.Python
env/
venv/
*.env
instance/
*.log
*.pot
*.pyc
.vscode/
.idea/
.DS_Store
Thumbs.db
.coverage
nosetests.xml
*.cover
*.hypothesis/
*.tox/
*.nox/
*.coverage
venv/
.env/
**/.terraform/
**/.terraform.lock.hcl
**/terraform.tfstate
**/terraform.tfstate.backup
yandex/key.json
**/terraform.tfstate.*.backup

66 changes: 4 additions & 62 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,62 +1,4 @@
# DevOps Engineering Labs

## Introduction

Welcome to the DevOps Engineering course labs! These hands-on labs are designed to guide you through various aspects of DevOps practices and principles. As you progress through the labs, you'll gain practical experience in application development, containerization, testing, infrastructure setup, CI/CD processes, and more.

## Architecture

This repository has a master branch containing an introduction. Each new lab assignment will be added as a markdown file with a lab number.

## Rules

To successfully complete the labs and pass the course, follow these rules:

1. **Lab Dependency:** Complete the labs in order; each lab builds upon the previous one.
2. **Submission and Grading:** Submit your solutions as pull requests (PRs) to the master branch of this repository. You need at least 6/10 points for each lab to pass.
3. **Fork Repository:** Fork this repository to your workspace to create your own version for solving the labs.
4. **Recommended Workflow:** Build your solutions incrementally. Complete lab N based on lab N-1.
5. **PR Creation:** Create a PR from your fork to the master branch of this repository and from your fork's branch to your fork's master branch.
6. **Wait for Grade:** Once your PR is created, wait for your lab to be reviewed and graded.

### Example for the first lab

1. Fork this repository.
2. Checkout to the lab1 branch.
3. Complete the lab1 tasks.
4. Push the code to your repository.
5. Create a PR to the master branch of this repository from your fork's lab1 branch.
6. Create a PR to the master branch of your repository from your lab1 branch.
7. Wait for your grade.

## Grading and Grades Distribution

Your final grade will be determined based on labs and a final exam:

- Labs: 70% of your final grade.
- Final Exam: 30% of your final grade.

Grade ranges:

- [90-100] - A
- [75-90) - B
- [60-75) - C
- [0-60) - D

### Labs Grading

Each lab is worth 10 points. Completing main tasks correctly earns you 10 points. Completing bonus tasks correctly adds 2.5 points. You can earn a maximum of 12.5 points per lab by completing all main and bonus tasks.

Finishing all bonus tasks lets you skip the exam and grants you 5 extra points. Incomplete bonus tasks require you to take the exam, which could save you from failing it.

>The labs account for 70% of your final grade. With 14 labs in total, each lab contributes 5% to your final grade. Completing all main tasks in a lab earns you the maximum 10 points, which corresponds to 5% of your final grade.
>If you successfully complete all bonus tasks, you'll earn an additional 2.5 points, totaling 12.5 points for that lab, or 6.25% of your final grade. Over the course of all 14 labs, the cumulative points from bonus tasks add up to 87.5% of your final grade.
>Additionally, a 5% bonus is granted for successfully finishing all bonus tasks, ensuring that if you successfully complete everything, your final grade will be 92.5%, which corresponds to an A grade.

## Deadlines and Labs Distribution

Each week, two new labs will be available. You'll have one week to submit your solutions. Refer to Moodle for presentation slides and deadlines.

## Submission Policy

Submitting your lab results on time is crucial for your grading. Late submissions receive a maximum score of 6 points for the corresponding lab. Remember, completing all labs is necessary to successfully pass the course.
## Unit Tests
Run tests:
```bash
pytest tests/
185 changes: 185 additions & 0 deletions ansible/ANSIBLE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
# Ansible Docs

## Check connection to server(s)
To check is connection to server for deploy successful, let's run following command
```sh
ansible -i ansible/inventory/default_aws_ec2.yml all -m ping
```
Output:
```
vm | SUCCESS => {
"changed": false,
"ping": "pong"
}
```

---

## Running the Playbook
To execute the playbook, run:
```sh
ansible-playbook -i ansible/inventory/default_aws_ec2.yml ansible/playbooks/dev/main.yaml -K
```

```
PLAY [Deploy Docker] **************************************************************************

TASK [Gathering Facts] ************************************************************************
ok: [vm]

TASK [docker : Include installation tasks] ****************************************************
included: /home/usr/hw/DevOps/S25-core-course-labs/ansible/roles/docker/tasks/install_docker.yml for vm

TASK [docker : Install required system packages] **********************************************
ok: [vm]

TASK [docker : Add Docker official GPG key] ***************************************************
ok: [vm]

TASK [docker : Set up the Docker stable repository] *******************************************
ok: [vm]

TASK [docker : Install Docker CE, CLI, and containerd] ****************************************
ok: [vm]

TASK [docker : Start and enable Docker service] ***********************************************
ok: [vm]

TASK [docker : Add user to Docker group] ******************************************************
ok: [vm]

TASK [docker : Include Docker Compose tasks] **************************************************
included: /home/usr/hw/DevOps/S25-core-course-labs/ansible/roles/docker/tasks/install_compose.yml for vm

TASK [docker : Download Docker Compose] *******************************************************
changed: [vm]

TASK [docker : Verify Docker Compose installation] ********************************************
ok: [vm]

TASK [docker : debug] *************************************************************************
ok: [vm] => {
"msg": "Docker Compose version: Docker Compose version v2.33.0"
}

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

```

## Output `nsible-inventory --list`
```
{
"_meta": {
"hostvars": {
"vm": {
"ansible_host": "89.169.154.202",
"ansible_python_interpreter": "/usr/bin/python3",
"ansible_ssh_private_key_file": "~/.ssh/id_ed25519",
"ansible_user": "ubuntu"
}
}
},
"all": {
"children": [
"ungrouped"
]
},
"ungrouped": {
"hosts": [
"vm"
]
}
}
```

## Output of `ansible-inventory --graph`
```
@all:
|--@ungrouped:
| |--vm
```

## Running the Playbook with web-app
```sh
ansible-playbook -i ansible/inventory/default_aws_ec2.yml ansible/playbooks/dev/main.yaml -K
```

```
PLAY [Deploy Docker] **************************************************************************

TASK [Gathering Facts] ************************************************************************
ok: [vm]

TASK [docker : Include installation tasks] ****************************************************
included: /home/usr/hw/DevOps/S25-core-course-labs/ansible/roles/docker/tasks/install_docker.yml for vm

TASK [docker : Install required system packages] **********************************************
ok: [vm]

TASK [docker : Add Docker official GPG key] ***************************************************
ok: [vm]

TASK [docker : Set up the Docker stable repository] *******************************************
ok: [vm]

TASK [docker : Install Docker CE, CLI, and containerd] ****************************************
ok: [vm]

TASK [docker : Start and enable Docker service] ***********************************************
ok: [vm]

TASK [docker : Add user to Docker group] ******************************************************
ok: [vm]

TASK [docker : Include Docker Compose tasks] **************************************************
included: /home/usr/hw/DevOps/S25-core-course-labs/ansible/roles/docker/tasks/install_compose.yml for vm

TASK [docker : Download Docker Compose] *******************************************************
ok: [vm]

TASK [docker : Verify Docker Compose installation] ********************************************
ok: [vm]

TASK [docker : debug] *************************************************************************
ok: [vm] => {
"msg": "Docker Compose version: Docker Compose version v2.33.0"
}

TASK [web_app : Create Docker network] ********************************************************
ok: [vm]

TASK [web_app : Deploy application using Docker Compose] **************************************
ok: [vm]

TASK [web_app : Start application with Docker Compose] ****************************************
changed: [vm]

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

## `ansible-playbook ansible/playbooks/dev/main.yaml --tags wipe`
```
PLAY [Deploy Web Application] *****************************************************************

TASK [Gathering Facts] ************************************************************************
ok: [vm]

PLAY [Wipe Web Application] *******************************************************************

TASK [Gathering Facts] ************************************************************************
ok: [vm]

TASK [Remove Docker container] ****************************************************************
skipping: [vm]

TASK [Remove Docker image] ********************************************************************
skipping: [vm]

TASK [Remove Docker volumes] ******************************************************************
skipping: [vm]

PLAY RECAP ************************************************************************************
vm : ok=2 changed=0 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
```
3 changes: 3 additions & 0 deletions ansible/ansible.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[defaults]
roles_path = ./roles
inventory = ./inventory/default_aws_ec2.yml
7 changes: 7 additions & 0 deletions ansible/inventory/default_aws_ec2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
all:
hosts:
vm:
ansible_host: 89.169.24.212
ansible_user: ubuntu
ansible_ssh_private_key_file: ~/.ssh/id_ed25519
ansible_python_interpreter: /usr/bin/python3
Loading