diff --git a/.env.backend.sample b/.env.backend.sample new file mode 100644 index 0000000..6739215 --- /dev/null +++ b/.env.backend.sample @@ -0,0 +1,13 @@ +SECRET_KEY=[SECRET_KEY] + +DATABASE_ENGINE=django.db.backends.postgresql_psycopg2 +DATABASE_NAME=[DATABASE NAME] +DATABASE_USER=[DATABASE USER] +DATABASE_PASSWORD=[DATABASE PASSWORD] +DATABASE_HOST=[DATABASE HOST] +DATABASE_PORT=[DATABASE PORT] + +EMAIL_HOST_USER=[EMAIL HOST USER] +EMAIL_HOST_PASSWORD=[EMAIL HOST PASSWORD] + +REDIRECT_URL=[SOME REDIRECT URL] diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5a96805 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +### Environment files +.env.backend diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..b3ded19 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,6 @@ +[submodule "backend"] + path = backend + url = https://github.com/TuiChain/backend +[submodule "frontend"] + path = frontend + url = https://github.com/TuiChain/frontend diff --git a/README.md b/README.md new file mode 100644 index 0000000..23520ae --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ + + +# TuiChain: Deployment + +Repository for the deployment of the TuiChain application. + + + +To deploy the TuiChain platform locally, you need to run: + +- `cd bin && ./deploy.sh && cd ..` + +If you want to delete the containers and the volumes, you need to run: + +- `docker-compose down -v` diff --git a/backend b/backend new file mode 160000 index 0000000..e52d49d --- /dev/null +++ b/backend @@ -0,0 +1 @@ +Subproject commit e52d49df72f40f5efcc8d591228f1b52e79cc84a diff --git a/bin/backend-docker/Dockerfile b/bin/backend-docker/Dockerfile new file mode 100644 index 0000000..257ddf3 --- /dev/null +++ b/bin/backend-docker/Dockerfile @@ -0,0 +1,29 @@ +# pull official base image + +FROM python:3.8.3-alpine + +# Set work directory +WORKDIR /usr/src/app + +# Set environment variables +## Prevents Python from writing pyc files to disk +ENV PYTHONDONTWRITEBYTECODE 1 +## Prevents Python from buffering stdout and stderr +ENV PYTHONUNBUFFERED 1 + +# Install dependencies +RUN pip install --upgrade pip +## Install psycopg2 dependencies +RUN apk update +RUN apk add postgresql-dev gcc python3-dev musl-dev +COPY ./requirements.txt . +RUN pip install -r requirements.txt + +# Copy entrypoint.sh +COPY ./entrypoint.sh . + +# Copy project +COPY . . + +# Run entrypoint.sh +ENTRYPOINT ["/usr/src/app/entrypoint.sh"] diff --git a/bin/backend-docker/entrypoint.sh b/bin/backend-docker/entrypoint.sh new file mode 100755 index 0000000..4848e3d --- /dev/null +++ b/bin/backend-docker/entrypoint.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +while ! nc -z $DATABASE_HOST $DATABASE_PORT; do + sleep 0.1 +done + +python manage.py migrate + +exec "$@" diff --git a/bin/deploy.sh b/bin/deploy.sh new file mode 100755 index 0000000..0a9bd70 --- /dev/null +++ b/bin/deploy.sh @@ -0,0 +1,65 @@ +#!/bin/bash + +set -e +. "functions.sh" + +# ---------------------------------------------------------------------------- # + +pp_info "required-tools" "Checking if you have all the required tools" + +## docker +if not_installed "docker"; then + pp_error "required-tools" "We are using docker for deploying locally the TuiChain Platform. Pls install it and run this script again." + + exit 1 +else + pp_success "required-tools" "docker is installed" + + if ! docker info > /dev/null 2>&1; then + pp_warn "required-tools" "docker does not seem to be running, run it first and retry" + exit 1 + else + pp_success "required-tools" "docker is up-and-running" + fi + +fi + +## docker-compose +if not_installed "docker-compose"; then + + pp_error "required-tools" "We are using docker-compose for deploying locally the TuiChain Platform. Pls install it and run this script again." + + exit 1 + +else + + pp_success "required-tools" "docker-compose is installed" + +fi + +pp_success "required-tools" "All tools are installed" + +# ---------------------------------------------------------------------------- # + +pp_info "setup" "Setting up the environment" + +## backend +cp ./backend-docker/Dockerfile ../backend/Dockerfile +cp ./backend-docker/entrypoint.sh ../backend/entrypoint.sh +pp_success "setup" "Backend needed files copied to backend folder" + +## frontend +# cp ./frontend-docker/Dockerfile ../frontend/Dockerfile +# pp_success "setup" "Frontend Dockerfile copied to frontend folder" + + +# ---------------------------------------------------------------------------- # + +pp_info "deployment" "Deploying components in docker" + +cd .. && docker-compose up -d --build + +pp_success "deployment" "All components are deployed\n\n\tdatabase -> localhost:10000\n\tbackend -> localhost:10001\n" + + + diff --git a/bin/functions.sh b/bin/functions.sh new file mode 100755 index 0000000..31fdced --- /dev/null +++ b/bin/functions.sh @@ -0,0 +1,47 @@ +#!/bin/bash + +BLUE='\e[34m' +GREEN='\e[32m' +YELLOW='\e[33m' +RED='\e[31m' +RESET='\e[39m' + +pp() { + printf "$1[$2]: $3${RESET}\n" +} + +pp_info() { + pp $BLUE "$1" "$2" +} + +pp_success() { + pp $GREEN "$1" "$2" +} + +pp_error() { + pp $RED "$1" "$2" +} + +pp_warn() { + pp $YELLOW "$1" "$2" +} + +not_installed() { + [ ! -x "$(command -v "$@")" ] +} + +ensure_confirmation() { + read -r "confirmation?please confirm you want to continue [y/n] (default: y) " + confirmation=${confirmation:-"y"} + + if [ "$confirmation" != "y" ]; then + exit 1 + fi +} + +ask_confirmation() { + read -r "confirmation?please confirm you want to continue [y/n] (default: y) " + confirmation=${confirmation:-"y"} + + [[ "$confirmation" == "y" ]]; +} diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..67d8ba8 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,40 @@ +version: '3.7' + +services: + database: + image: postgres:latest + container_name: database + environment: + - POSTGRES_USER=postgres + - POSTGRES_PASSWORD=postgres + - POSTGRES_DB=tuichainDB + - PGDATA=/var/lib/postgresql/data/pgdata + ports: + - 10000:5432 + volumes: + - pgdata:/var/lib/postgresql/data + restart: always + + backend: + build: ./backend + image: tuichain-backend + container_name: tuichain-backend + environment: + DATABASE_NAME: tuichainDB + DATABASE_USER: postgres + DATABASE_PASSWORD: postgres + DATABASE_HOST: database + DATABASE_PORT: 5432 + command: python manage.py runserver 0.0.0.0:8000 + volumes: + - ./backend/:/usr/src/app/ + ports: + - 10001:8000 + env_file: + - ./.env.backend + restart: always + depends_on: + - database + +volumes: + pgdata: diff --git a/frontend b/frontend new file mode 160000 index 0000000..1f0e96e --- /dev/null +++ b/frontend @@ -0,0 +1 @@ +Subproject commit 1f0e96e127de6e6c2f89be45115b4d8e48f426a3