forked from inno-devops-labs/S24-core-course-labs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from kolayne-IU-assignments/lab6
Lab6
- Loading branch information
Showing
17 changed files
with
273 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
- name: Deploy go app | ||
hosts: all | ||
roles: | ||
- name: "Deploy kolay0ne/app_go:lab6" | ||
role: web_app | ||
image_name: "kolay0ne/app_go" | ||
image_tag: "lab6" | ||
publish_ports: | ||
- "5500:5000" | ||
wipe: false | ||
tags: [] # Run by default | ||
|
||
- name: "Wipe kolay0ne/app_go:lab6" | ||
role: web_app | ||
image_name: "kolay0ne/app_go" | ||
image_tag: "lab6" | ||
wipe: true | ||
tags: [never, wipe] # Only run on wipe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
- name: Deploy python app | ||
hosts: all | ||
roles: | ||
- name: "Deploy kolay0ne/app_py:lab6" | ||
role: web_app | ||
image_name: "kolay0ne/app_py" | ||
image_tag: "lab6" | ||
publish_ports: | ||
- "5000:5000" | ||
wipe: false | ||
tags: [] # Run by default | ||
|
||
- name: "Wipe kolay0ne/app_py:lab6" | ||
role: web_app | ||
image_name: "kolay0ne/app_py" | ||
image_tag: "lab6" | ||
wipe: true | ||
tags: [never, wipe] # Only run on wipe |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Dockerized web app deploy role | ||
|
||
An ansible role for deploying a dockerized web app. | ||
|
||
## Requirements | ||
|
||
This role depends on the `docker` role from the same repository. No additional | ||
requirements are imposed. | ||
|
||
## Usage | ||
|
||
All parameters of the role are optional. The most basic usage: | ||
``` | ||
roles: | ||
- name: "Deploy hello-world" | ||
role: web_app | ||
``` | ||
The above will pull the `hello-world` image tagged `latest`, create a container | ||
`hello-world-0` corresponding to it, and start it. | ||
|
||
The most advanced usage: | ||
``` | ||
roles: | ||
- name: "Deploy asciiquarium" | ||
role: web_app | ||
image_name: "danielkraic/asciiquarium" | ||
image_tag: "master" | ||
container_name: "aq" | ||
publish_ports: | ||
- 5000:6000 | ||
- 7000 | ||
wipe: false | ||
``` | ||
The above will pull the `danielkraic/asciiquarium` image tagged `master`, create | ||
a container `aq` corresponding to it, and start it. The container's port `6000` will | ||
be mapped to host `5000` and container's port `7000` will be mapped to an arbitrarily | ||
selected host port. | ||
|
||
Changing the `wipe` parameter to `true` (or another value that represents a boolean true | ||
according to ansible) causes the container to be destroyed and the pulled image to be | ||
removed from the managed machine. When `wipe` is `true`, the value of `publish_ports` is | ||
ignored. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
image_name: "hello-world" | ||
image_tag: "latest" | ||
container_name: "{{ image_name | basename }}-0" | ||
publish_ports: [] | ||
wipe: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--- | ||
dependencies: | ||
- docker |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
--- | ||
- name: Create directory for compose | ||
ansible.builtin.file: | ||
path: "~/{{image_name|basename}}" | ||
state: "directory" | ||
register: compose_dir | ||
|
||
- name: Stop old containers | ||
community.docker.docker_compose_v2: | ||
project_src: "{{ compose_dir.path }}" | ||
state: stopped | ||
# Compose file may not exist yet, thus ignoring errors | ||
ignore_errors: true | ||
|
||
- name: Render new docker-compose.yml | ||
ansible.builtin.template: | ||
src: docker-compose.yml.j2 | ||
dest: "{{ compose_dir.path }}/docker-compose.yml" | ||
vars: | ||
image_full: "{{image_name}}:{{image_tag}}" | ||
expose_ports: "{{ publish_ports }}" | ||
when: not wipe | ||
|
||
- name: Manage containers | ||
# If wiping, remove containers, otherwise start them | ||
community.docker.docker_compose_v2: | ||
project_src: "{{ compose_dir.path }}" | ||
state: "{{ wipe | ternary('absent', 'present') }}" | ||
pull: always | ||
|
||
- name: Wipe image | ||
community.docker.docker_image: | ||
name: "{{ image_name }}" | ||
tag: "{{ image_tag }}" | ||
state: "absent" | ||
when: wipe | ||
|
||
- name: Remove compose file from the filesystem | ||
ansible.builtin.file: | ||
path: "{{ compose_dir.path }}" | ||
state: "absent" | ||
when: wipe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
services: | ||
web_app: | ||
image: {{ image_full }} | ||
{% if expose_ports is iterable and expose_ports|length > 0 %} | ||
ports: | ||
{% for port_expr in expose_ports %} | ||
- {{ port_expr }} | ||
{% endfor %} | ||
{% endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import datetime | ||
|
||
from flask import Flask | ||
import requests | ||
|
||
from .cache import cache_for | ||
|
||
|
||
app = Flask(__name__) | ||
|
||
|
||
# In case of high load, to avoid frequent requests, cache results for | ||
# one second | ||
@cache_for(1000) | ||
def get_time(): | ||
""" | ||
Get current Moscow time from worldtimeapi.org. | ||
The returned value is of type `datetime.time` and it may be up to | ||
1000ms out of date, as results of calls to the function are cached | ||
for up to 1 second. | ||
""" | ||
r = requests.get('http://worldtimeapi.org/api/timezone/Europe/Moscow') | ||
dt = datetime.datetime.fromisoformat(r.json()['datetime']) | ||
return dt.time() | ||
|
||
|
||
@app.route('/') | ||
def index(): | ||
time = get_time() | ||
return f"In MSK it's {time.hour}:{time.minute}:{time.second}. " \ | ||
"Have you brushed your teeth today yet?" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from . import app | ||
|
||
|
||
app.run(host='0.0.0.0', port=5000, debug=False) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters