Skip to content

Commit

Permalink
Merge dev into main | first push to main
Browse files Browse the repository at this point in the history
  • Loading branch information
huyagci committed Jun 12, 2022
1 parent 2beade3 commit 2f64633
Show file tree
Hide file tree
Showing 14 changed files with 595 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitattributes
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
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Project Files
.vagrant/
**containerd/

# Visual Studio Code
.vscode/
146 changes: 146 additions & 0 deletions README.md
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/
37 changes: 37 additions & 0 deletions Vagrantfile
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
Binary file added res/img/sailboat.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions shared/projects/python/Dockerfile
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" ]
12 changes: 12 additions & 0 deletions shared/projects/python/app/app.py
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)
2 changes: 2 additions & 0 deletions shared/projects/python/app/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Flask==2.1.1
gunicorn
14 changes: 14 additions & 0 deletions shared/projects/python/app/templates/index.html
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>
23 changes: 23 additions & 0 deletions shared/projects/python/mongo.yaml
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
23 changes: 23 additions & 0 deletions shared/projects/python/mysql.yaml
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
47 changes: 47 additions & 0 deletions shared/scripts/bootstrap.sh
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
43 changes: 43 additions & 0 deletions shared/scripts/configs/sailboat.cfg
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.
"
Loading

0 comments on commit 2f64633

Please sign in to comment.