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

Lab9 #1503

Open
wants to merge 34 commits into
base: main
Choose a base branch
from
Open

Lab9 #1503

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
5fce4f9
implement lab1
qexik1 Feb 4, 2024
e82abac
lab2
qexik1 Feb 14, 2024
5bd4928
fixes
qexik1 Feb 14, 2024
018b9c9
Merge branch 'inno-2023-masters:main' into lab2
qexik0 Feb 14, 2024
6e6bdd4
Merge pull request #2 from inno-devops-labs/main
qexik0 Nov 10, 2024
2b5c42b
Merge pull request #1 from qexik0/lab1
qexik0 Nov 10, 2024
01d7ed4
Merge branch 'inno-devops-labs:main' into lab2
qexik0 Nov 10, 2024
f699b5b
fix docker description
qexik0 Nov 10, 2024
e963a3e
Merge branch 'main' into lab2
qexik0 Nov 10, 2024
c3883f8
Merge pull request #3 from qexik0/lab2
qexik0 Nov 10, 2024
547002f
Add tests and workflow
qexik0 Nov 10, 2024
864fdd8
modify .gitignore
qexik0 Nov 10, 2024
135dc5e
add missing dependency
qexik0 Nov 10, 2024
249c7be
add docker push
qexik0 Nov 10, 2024
2368410
Relax workflow docker requirements
qexik0 Nov 10, 2024
aaee556
fixes
qexik0 Nov 10, 2024
8ee4016
add cache
qexik0 Nov 10, 2024
208fd4f
workflow fixes
qexik0 Nov 10, 2024
3bfff85
Add CI.md
qexik0 Nov 10, 2024
efcff7d
Merge pull request #4 from qexik0/lab3
qexik0 Nov 10, 2024
549a346
add terraform staff
qexik0 Nov 10, 2024
2bb6540
Merge pull request #5 from qexik0/lab4
qexik0 Nov 10, 2024
faead58
add ansible role for docker
qexik0 Nov 10, 2024
46743f6
Merge pull request #6 from qexik0/lab5
qexik0 Nov 10, 2024
28ba7f3
Add lab6 implementation
qexik0 Nov 10, 2024
855fe40
Merge pull request #7 from qexik0/lab6
qexik0 Nov 10, 2024
1f2ca42
lab7 files
qexik0 Nov 10, 2024
f3136d0
Update LOGGING.md
qexik0 Nov 10, 2024
98c9778
Merge pull request #8 from qexik0/lab7
qexik0 Nov 10, 2024
fa9c3b7
add prometheus configs
qexik0 Nov 11, 2024
1fcafcd
Update METRICS.md
qexik0 Nov 11, 2024
64d43bb
Merge pull request #9 from qexik0/lab8
qexik0 Nov 11, 2024
4480546
add kubernetes lab
qexik0 Nov 11, 2024
d03935e
Update README.md
qexik0 Nov 11, 2024
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
98 changes: 98 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@

name: CI

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build-and-test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x'
cache: 'pip'

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

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest requests

- name: Run linters
run: |
flake8 app_python/ --count --select=E9,F63,F7,F82 --show-source --statistics
flake8 app_python/ --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-

- name: Build docker container
uses: docker/build-push-action@v4
with:
context: app_python/
load: true
tags: flask-time-server:latest
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max

- name: Move cache
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache

- name: Start docker container
run: |
docker run -d -p 5000:5000 flask-time-server:latest
sleep 10

- name: Run tests
run: |
pytest app_python/tests/*.py

- name: Login to Docker Hub
if: github.event_name != 'pull_request'
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Push Docker image
if: github.event_name != 'pull_request'
run: |
IMAGE_ID=${{ secrets.DOCKERHUB_USERNAME }}/flask-time-server

VERSION=$(echo "${{ github.sha }}" | cut -c1-7)

docker tag flask-time-server $IMAGE_ID:$VERSION
docker tag flask-time-server $IMAGE_ID:latest
docker push $IMAGE_ID:$VERSION
docker push $IMAGE_ID:latest

- name: Clean up
run: |
docker ps -q | xargs -r docker stop
docker ps -aq | xargs -r docker rm

7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
venv
__pycache__
*tfstate*
key.json
*tfvars*
.terraform/
*.hcl
142 changes: 142 additions & 0 deletions ansible/ANSIBLE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
# Ansible lab

## Command output for docker role

### ansible-playbook playbooks/dev/main.yml --diff

```
PLAY [Install Docker] *************************************************************

TASK [Gathering Facts] ************************************************************
[WARNING]: Platform linux on host default_host is using the discovered Python
interpreter at /usr/bin/python3.12, but future installation of another Python
interpreter could change the meaning of that path. See
https://docs.ansible.com/ansible-
core/2.17/reference_appendices/interpreter_discovery.html for more information.
ok: [default_host]

TASK [docker : include_tasks] *****************************************************
included: /home/qexik/S24-core-course-labs/ansible/roles/docker/tasks/install_docker.yml for default_host

TASK [docker : Install apt dependencies] ******************************************
ok: [default_host]

TASK [docker : Add Docker’s GPG key] **********************************************
ok: [default_host]

TASK [docker : Add Docker apt repository] *****************************************
ok: [default_host]

TASK [docker : Install Docker] ****************************************************
ok: [default_host]

TASK [docker : include_tasks] *****************************************************
included: /home/qexik/S24-core-course-labs/ansible/roles/docker/tasks/install_compose.yml for default_host

TASK [docker : Install compose via apt] *******************************************
The following additional packages will be installed:
python3-compose python3-docker python3-dockerpty python3-docopt
python3-dotenv python3-packaging python3-texttable python3-websocket
Recommended packages:
docker.io
The following NEW packages will be installed:
docker-compose python3-compose python3-docker python3-dockerpty
python3-docopt python3-dotenv python3-packaging python3-texttable
python3-websocket
0 upgraded, 9 newly installed, 0 to remove and 17 not upgraded.
changed: [default_host]

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

### ansible-inventory -i inventory/default_yandex_compute.yml --list

```
{
"_meta": {
"hostvars": {
"default_host": {
"ansible_host": "89.169.138.171",
"ansible_user": "ubuntu"
}
}
},
"all": {
"children": [
"ungrouped",
"myhosts"
]
},
"myhosts": {
"hosts": [
"default_host"
]
}
}
```

## Command output for web_app role

### ansible-playbook playbooks/dev/main.yml --diff

```
PLAY [Install flask time server] **************************************************

TASK [Gathering Facts] ************************************************************
[WARNING]: Platform linux on host default_host is using the discovered Python
interpreter at /usr/bin/python3.12, but future installation of another Python
interpreter could change the meaning of that path. See
https://docs.ansible.com/ansible-
core/2.17/reference_appendices/interpreter_discovery.html for more information.
ok: [default_host]

TASK [docker : include_tasks] *****************************************************
included: /home/qexik/S24-core-course-labs/ansible/roles/docker/tasks/install_docker.yml for default_host

TASK [docker : Install apt dependencies] ******************************************
ok: [default_host]

TASK [docker : Add Docker’s GPG key] **********************************************
ok: [default_host]

TASK [docker : Add Docker apt repository] *****************************************
ok: [default_host]

TASK [docker : Install Docker] ****************************************************
ok: [default_host]

TASK [docker : include_tasks] *****************************************************
included: /home/qexik/S24-core-course-labs/ansible/roles/docker/tasks/install_compose.yml for default_host

TASK [docker : Install compose via apt] *******************************************
ok: [default_host]

TASK [web_app : Create app directory] *********************************************
ok: [default_host]

TASK [web_app : Docker compose] ***************************************************
--- before
+++ after: /home/qexik/.ansible/tmp/ansible-local-52812vb7z8_f/tmpl1bhyw13/docker-compose.yml.j2
@@ -0,0 +1,6 @@
+services:
+ web_app:
+ image: qexik1/flask-time-server:latest
+ ports:
+ - 5000:5000
+

changed: [default_host]

TASK [web_app : Start the app] ****************************************************
changed: [default_host]

TASK [web_app : Remove build] *****************************************************
skipping: [default_host]

TASK [web_app : Remove source] ****************************************************
skipping: [default_host]

PLAY RECAP ************************************************************************
default_host : ok=11 changed=2 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0
```
13 changes: 13 additions & 0 deletions ansible/ansible.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[defaults]
inventory = inventory
remote_user = ubuntu
host_key_checking = false
playbook_dir = playbooks
roles_path = roles

[privelege_escalation]
become = true
become_method = sudo
become_user = ubuntu
become_ask_pass = false

6 changes: 6 additions & 0 deletions ansible/inventory/default_yandex_compute.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
myhosts:
hosts:
default_host:
ansible_host: 89.169.138.171
ansible_user: ubuntu

7 changes: 7 additions & 0 deletions ansible/playbooks/dev/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- name: Install flask time server
hosts: all
become: true

roles:
- web_app

24 changes: 24 additions & 0 deletions ansible/roles/docker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Ansible docker role

The role installs docker on the target machine.

# Requirements

## Control node requirements

Host requirements are described in the [docs](https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html#control-node-requirements).

Basically, any UNIX machine with Python installed will suffice.

## Managed node requirements

Basic managed node requirements are described in the [docs](https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html#managed-node-requirements).

Current implementation also assumes that apt is present. Therefore, docker could only be installed on debian-based distros using this role.

# Usage

```bash
ansible-playbook playbooks/dev/main.yml
```

Empty file.
6 changes: 6 additions & 0 deletions ansible/roles/docker/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
- name: Restart docker
ansible.builtin.service:
name: docker
state: restarted

8 changes: 8 additions & 0 deletions ansible/roles/docker/tasks/install_compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
- name: Install compose via apt
ansible.builtin.apt:
name:
- docker-compose
state: present
update_cache: yes

28 changes: 28 additions & 0 deletions ansible/roles/docker/tasks/install_docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
- name: Install apt dependencies
ansible.builtin.apt:
name:
- apt-transport-https
- ca-certificates
- curl
- software-properties-common
state: present
update_cache: yes

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

- name: Add Docker apt repository
ansible.builtin.apt_repository:
repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable
state: present

- name: Install Docker
ansible.builtin.apt:
name: docker-ce
state: present
update_cache: yes
notify: Restart docker

4 changes: 4 additions & 0 deletions ansible/roles/docker/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
- include_tasks: install_docker.yml
- include_tasks: install_compose.yml

17 changes: 17 additions & 0 deletions ansible/roles/web_app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Ansible web_app role

Install qexik1/flask-time-server docker image and run it on port 5000.

# Requirements

## Control node requirements

Host requirements are described in the [docs](https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html#control-node-requirements).

Basically, any UNIX machine with Python installed will suffice.

## Managed node requirements

Basic managed node requirements are described in the [docs](https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html#managed-node-requirements).

Current implementation also assumes that apt is present. Therefore, docker could only be installed on debian-based distros using this role.
7 changes: 7 additions & 0 deletions ansible/roles/web_app/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
docker_image: "qexik1/flask-time-server"
image_tag: "latest"
container_name: "time-server"
app_directory: "app_python"
port_forwarding: "5000:5000"
wipe: false

Empty file.
3 changes: 3 additions & 0 deletions ansible/roles/web_app/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dependencies:
- role: docker

13 changes: 13 additions & 0 deletions ansible/roles/web_app/tasks/0-wipe.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
- name: Remove build
community.docker.docker_compose_v2:
project_src: "{{ app_directory }}"
remove_images: all
remove_volumes: true
state: absent

- name: Remove source
ansible.builtin.file:
path: "{{ app_directory }}/"
state: absent

Loading