-
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 dev into main | first push to main
- Loading branch information
Showing
14 changed files
with
595 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Fix line endings | ||
*.sh text eol=lf | ||
*.conf text eol=lf |
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 @@ | ||
# Project Files | ||
.vagrant/ | ||
**containerd/ | ||
|
||
# Visual Studio Code | ||
.vscode/ |
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,146 @@ | ||
# Protein DevOps Engineer Bootcamp | ||
|
||
## Project of the Third Week | ||
|
||
**_Built with;_** | ||
|
||
[![Linux][#linux]][@linux] [![Bash][#bash]][@bash] [![Docker][#docker]][@docker] [![Git][#git]][@git] [![VirtualBox][#virtualbox]][@virtualbox] [![Vagrant][#vagrant]][@vagrant] | ||
|
||
#### :hash: **Assignment** | ||
|
||
> Create a script that helps the developer to dockerize their applications[^1], deploy them locally or push to remote container registry. | ||
**Script has to be able to ;** | ||
|
||
> - Build and run docker images. | ||
> - Push the image to the Docker Hub or Gitlab Container Registry. | ||
> - Limit machine resources like CPU and memory if relevant arguments are given. _(Default is limitless)_ | ||
> - Deploy additional services like MySQL or Mongo Database with Docker Compose. | ||
### **Table of Contents** | ||
|
||
1. [Overview](#notebook_with_decorative_cover-overview) | ||
2. [Features](#sparkles-features) | ||
- [Bootstrapper](#bootstrapper) | ||
- [Sailboat](#sailboat) | ||
3. [Installation](#gear-installation) | ||
- [Automated](#automated) | ||
- [Manual](#manual) | ||
4. [Usage](#wrench-usage) | ||
- [Parameters](#parameters) | ||
5. [Technologies](#computer-technologies) | ||
6. [Directories](#open_file_folder-directories) | ||
|
||
### :notebook_with_decorative_cover: **Overview** | ||
|
||
This repository contains an assignment project developed under the [Patika.dev][@patika] & [Protein][@protein] DevOps Engineer Bootcamp. `Bootstrapper` is an initializer script that sets up the machine as instructed. `Sailboat` is the main script that is coded by the terms of the project. | ||
|
||
### :sparkles: **Features** | ||
|
||
#### _Bootstrapper_ | ||
|
||
> - Sets the timezone of the machine as UTC+3 (Europe/Istanbul) and enables Network Time Protocol. | ||
> - Downloads the latest package information. | ||
> - Downloads, installs, and configures Docker Engine and Docker Compose. | ||
> - Sets the environment variables for the stated technologies above. | ||
> - Sets the permissions of the script files. | ||
> - Defines a system-wide alias of the main script for easy use. | ||
| ||
|
||
<p align="center"><img src="./res/img/sailboat.png" alt="Sailboat"></a></p> | ||
|
||
#### _Sailboat_ | ||
|
||
> - Has three modes: Build, Deploy, and Template. | ||
> - Build and deploy modes may take additional arguments like image name and image tag to define image properties. | ||
> - Build mode may also take "--registry" argument which will cause re-tag the image with the defined username. | ||
> - The re-tagged image will be pushed to the specified container registry. | ||
> - With deploy mode, you may also name your container and set CPU and memory limits. | ||
> - Template mode will assist the user with predefined database services in addition to the main image. | ||
> - Docker-compose is used in template mode with pre-defined database services are MySQL and MongoDB. | ||
### :gear: **Installation** | ||
|
||
##### _Automated_ | ||
|
||
1. Install [VirtualBox][@virtualbox] and [Vagrant][@vagrant-download] to your machine _if you do not have them_. | ||
2. Clone the project to your machine. | ||
3. Open your CLI, change directory to project directory, and type `vagrant up`. | ||
4. Vagrant will configure the virtual machine first then you may test the project. | ||
|
||
##### _Manual_ | ||
|
||
1. Download all of the project files. | ||
2. Copy all scripts under the `/shared/scripts` and sample project from `/shared/projects/python` to your environment. | ||
3. Execute `bootstrap.sh` first to configure the machine. | ||
4. Make sure your environment has a Docker installation too. | ||
5. Run the scripts with the aliases configured or manually from the directories that are stated [**below**](#open_file_folder-directories). | ||
|
||
### :wrench: **Usage** | ||
|
||
- Built-in alias is `sailboat` You may use it if you executed `bootstrap.sh` first. | ||
- Optionally, you may call the scripts from any directory with an acceptable arguments that are stated below. | ||
- The target directory of _Sailboat_ script is `/opt/projects/python` directory. | ||
- You can change these settings by re-defining the `TARGET_DIR` variable within the configuration file of the script. | ||
|
||
##### _Parameters_ | ||
|
||
```bash | ||
OPTIONS: ARGUMENTS: DESCRIPTION: OBLIGATION: | ||
|
||
-m | --mode <build|deploy|template> Mode selection. Mandatory for ALL modes. | ||
-n | --image-name <image-name> Docker image name. Mandatory for BUILD and DEPLOY modes. | ||
-t | --image-tag <image-tag> Docker image tag. Mandatory for BUILD and DEPLOY modes. | ||
-r | --registry <dockerhub|gitlab> Docker Hub or GitLab Container Registry. Optional for BUILD Mode. | ||
-c | --container-name <container-name> Container name. Optional for DEPLOY mode. | ||
-p | --cpu <cpu-limit> Container CPU limit Optional for DEPLOY mode. | ||
-s | --memory <memory-limit> Container memory limit. Optional for DEPLOY mode. | ||
-a | --application-name <compose-name> Run MySQL or MongoDB server. Mandatory for TEMPLATE mode. | ||
-h | --help Shows this help message. | ||
``` | ||
|
||
### :computer: **Technologies** | ||
|
||
> - Linux | ||
> - Bash Scripting | ||
> - Git SCM | ||
> - Docker :heart: | ||
> - Oracle VM VirtualBox | ||
> - Vagrant by HashiCorp | ||
### :open_file_folder: **Directories** | ||
|
||
``` | ||
Scripts : /opt/scripts | ||
Configurations : /opt/scripts/configs | ||
Sample Project : /opt/projects/python | ||
``` | ||
|
||
<!-- View Counter --> | ||
<p align="right"><img src="https://komarev.com/ghpvc/?username=test85632&style=flat&label=Views&color=blue" alt="View Counter"></a></p> | ||
|
||
<!-- Footnotes --> | ||
|
||
[^1]: A simple Python application(flask) is provided as a sample project and the script is built upon this configuration. (Can be modified to any other project) | ||
|
||
<!-- Badge Index --> | ||
|
||
[#linux]: https://img.shields.io/badge/Linux-FCC624?style=flat&logo=linux&logoColor=black | ||
[#bash]: https://img.shields.io/badge/Bash-4EAA25?style=flat&logo=GNU%20Bash&logoColor=white | ||
[#git]: https://img.shields.io/badge/Git-E44C30?style=flat&logo=git&logoColor=white | ||
[#docker]: https://img.shields.io/badge/Docker-2CA5E0?style=flat&logo=docker&logoColor=white | ||
[#virtualbox]: https://img.shields.io/badge/VirtualBox-183A61?style=flat&logo=virtualbox&logoColor=white | ||
[#vagrant]: https://img.shields.io/badge/Vagrant-1868F2?style=flat&logo=vagrant&logoColor=white | ||
|
||
<!-- URL Index --> | ||
|
||
[@patika]: https://www.patika.dev/ | ||
[@protein]: https://protein.tech/ | ||
[@linux]: https://www.linux.org/ | ||
[@bash]: https://www.gnu.org/software/bash/ | ||
[@docker]: https://www.docker.com/ | ||
[@git]: https://git-scm.com/ | ||
[@virtualbox]: https://www.virtualbox.org/ | ||
[@vagrant]: https://www.vagrantup.com/ | ||
[@vagrant-download]: https://www.vagrantup.com/downloads/ |
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,37 @@ | ||
# -*- mode: ruby -*- | ||
# vi: set ft=ruby : | ||
|
||
Vagrant.configure("2") do |config| | ||
|
||
# Box Settings # | ||
|
||
# Define base image of the machine | ||
config.vm.box = "ubuntu/focal64" | ||
|
||
# Provider Settings # | ||
|
||
# Use virtualbox as a VM provider | ||
config.vm.provider "virtualbox" do |vb| | ||
|
||
# Fix SSH timeout & Stuck on boot | ||
vb.customize ["modifyvm", :id, "--uart1", "0x3F8", "4"] | ||
vb.customize ["modifyvm", :id, "--uartmode1", "file", File::NULL] | ||
|
||
# Virtual machine resources | ||
vb.memory = 4096 | ||
vb.cpus = 2 | ||
|
||
# Virtual machine name | ||
vb.name = "week-3-assignment-huyagci" | ||
end | ||
|
||
# Synced Folder Settings | ||
|
||
# Mount specified host paths to virtual machine | ||
config.vm.synced_folder "./shared", "/opt" | ||
|
||
# Provision Settings | ||
|
||
# Execute given script on boot | ||
config.vm.provision "shell", path: "./shared/scripts/bootstrap.sh" | ||
end |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,10 @@ | ||
FROM python:3.7-alpine | ||
WORKDIR /app | ||
|
||
COPY app/requirements.txt . | ||
RUN pip3 install -r requirements.txt --no-cache-dir | ||
COPY app . | ||
|
||
EXPOSE 8080 | ||
ENTRYPOINT [ "python3" ] | ||
CMD [ "app.py" ] |
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,12 @@ | ||
from flask import Flask, app, render_template | ||
|
||
app = Flask(__name__) | ||
|
||
|
||
@app.route('/') | ||
def index(): | ||
return render_template('index.html') | ||
|
||
|
||
if __name__ == '__main__': | ||
app.run(host="0.0.0.0",port="8080", debug=True) |
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,2 @@ | ||
Flask==2.1.1 | ||
gunicorn |
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,14 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Hello World</title> | ||
</head> | ||
<body> | ||
|
||
<div class="container"> | ||
<h1>It's working!</h1> | ||
<h2></h2> | ||
</div> | ||
</body> |
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,23 @@ | ||
version: "3.1" | ||
services: | ||
web: | ||
image: python:3.7-alpine | ||
container_name: flask-app | ||
build: . | ||
ports: | ||
- 8080:8080 | ||
db: | ||
image: mongo:3.5 | ||
container_name: mongo-db | ||
restart: always | ||
environment: | ||
- MONGO_INITDB_DATABASE=mongodb | ||
- MONGO_INITDB_ROOT_USERNAME=alice | ||
- MONGO_INITDB_ROOT_PASSWORD=123 | ||
ports: | ||
- 27017:27017 | ||
deploy: | ||
resources: | ||
limits: | ||
cpus: "1.0" | ||
memory: 1G |
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,23 @@ | ||
version: "3.1" | ||
services: | ||
web: | ||
image: python:3.7-alpine | ||
container_name: flask-app | ||
build: . | ||
ports: | ||
- 8080:8080 | ||
db: | ||
image: mysql:5.7 | ||
container_name: mysql-db | ||
restart: always | ||
environment: | ||
MYSQL_DATABASE: mysql-db | ||
MYSQL_USER: mysql-root | ||
MYSQL_ROOT_PASSWORD: mysql123 | ||
ports: | ||
- 3306:3306 | ||
deploy: | ||
resources: | ||
limits: | ||
cpus: "1.0" | ||
memory: 1G |
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,47 @@ | ||
# Set time zone to UTC+3 | ||
sudo timedatectl set-timezone Europe/Istanbul | ||
|
||
# Enable NTP and system clock synchronization. | ||
sudo timedatectl set-ntp true | ||
|
||
# Download latest package information. | ||
sudo apt update | ||
|
||
# Set up the repository | ||
sudo apt-get install \ | ||
ca-certificates \ | ||
curl \ | ||
gnupg \ | ||
lsb-release | ||
|
||
# Add Docker’s official GPG key | ||
sudo mkdir -p /etc/apt/keyrings | ||
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg | ||
|
||
# set up the repository | ||
echo \ | ||
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \ | ||
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null | ||
|
||
# Install Docker Engine | ||
sudo apt-get update | ||
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin -y | ||
|
||
# Install Docker Compose | ||
sudo apt install docker-compose -y | ||
|
||
# Fix Docker daemon socket error | ||
sudo chmod 666 /var/run/docker.sock | ||
|
||
# Give execution permission to scripts. | ||
sudo chmod 777 /opt/scripts/bootstrap.sh | ||
sudo chmod 777 /opt/scripts/sailboat.sh | ||
|
||
# Set system-wide aliases to easy use of the scripts | ||
sudo echo "# Protein DevOps Bootcamp - Week 3 Assignment Aliases" >> /etc/bash.bashrc | ||
|
||
# Define an alias to the system as "sailboat" for easy use. | ||
sudo echo "alias sailboat='/opt/scripts/sailboat.sh'" >> /etc/bash.bashrc | ||
|
||
# Activation | ||
source /etc/bash.bashrc |
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,43 @@ | ||
# Color Palette # | ||
|
||
#Standard Colors | ||
COFF='\033[0m' | ||
CRED='\033[1;31m' | ||
CGREEN='\033[0;32m' | ||
CYELLOW='\033[1;33m' | ||
CCYAN='\033[0;36m' | ||
CORANGE='\033[0;33m' | ||
CDGRAY='\033[1;30m' | ||
|
||
# Underlined Colors | ||
UORANGE='\033[4;33m' | ||
|
||
# Blinking Colors | ||
BBLUE='\033[5;36m' | ||
|
||
# Target project directory. | ||
TARGET_DIR=/opt/projects/python | ||
|
||
# Default state of the commands. | ||
BUILD_CMD="docker build" | ||
TAG_CMD="docker image tag" | ||
PUSH_CMD="docker push" | ||
DEPLOY_CMD="docker run -d -P" # Detached mode and publish exposed ports of the image. | ||
TEMPLATE_CMD="docker compose" | ||
|
||
# Usage message of the script. | ||
USAGE_MSG=" | ||
Usage: $(basename $0) [OPTION] <ARGUMENT> ... | ||
OPTIONS: ARGUMENTS: DESCRIPTION: OBLIGATION: | ||
-m | --mode <build|deploy|template> Mode selection. Mandatory for ALL modes. | ||
-n | --image-name <image-name> Docker image name. Mandatory for BUILD and DEPLOY modes. | ||
-t | --image-tag <image-tag> Docker image tag. Mandatory for BUILD and DEPLOY modes. | ||
-r | --registry <dockerhub|gitlab> Docker Hub or GitLab Container Registry. Optional for BUILD Mode. | ||
-c | --container-name <container-name> Container name. Optional for DEPLOY mode. | ||
-p | --cpu <cpu-limit> Container CPU limit Optional for DEPLOY mode. | ||
-s | --memory <memory-limit> Container memory limit. Optional for DEPLOY mode. | ||
-a | --application-name <compose-name> Run MySQL or MongoDB server. Mandatory for TEMPLATE mode. | ||
-h | --help Shows this help message. | ||
" |
Oops, something went wrong.