-
Notifications
You must be signed in to change notification settings - Fork 0
/
gdrcd-stack
executable file
·124 lines (101 loc) · 3.21 KB
/
gdrcd-stack
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env bash
# The structure behind this script is based on the following snippet:
# @source: https://github.com/nickjj/docker-flask-example/blob/main/run
set -eo pipefail
# ---------------------------------------------------------------------
# Variables
# ---------------------------------------------------------------------
_PATH_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd)"
_PATH_DOCKER="$_PATH_DIR/.docker"
# If we're running in CI we need to disable TTY allocation for docker-compose
# commands that enable it by default, such as exec and run.
TTY=""
if [[ ! -t 1 ]]; then
TTY="-T"
fi
# ---------------------------------------------------------------------
# Environment
# ---------------------------------------------------------------------
# import env file
if [[ -f "${_PATH_DIR}/.env" ]]; then
# shellcheck source=.env
source "${_PATH_DIR}/.env"
else
echo "Warning! No .env file found, please create one."
exit 1;
fi
# -----------------------------------------------------------------------------
# Helper functions start with _ and aren't listed in this script's help menu.
# -----------------------------------------------------------------------------
# Custom docker-compose command
_dc() {
docker compose -p "$PROJECT" -f "$_PATH_DOCKER/compose.yml" --env-file "$_PATH_DIR/.env" ${TTY} "$@"
}
# Check non-existent arguments
_isCommand() {
if ! type "$1" >/dev/null 2>&1; then
echo "Warning! Command not found: $1"
exit 1;
fi;
}
# Check if docker is installed
_checkDocker() {
if ! command -v docker &> /dev/null; then
echo "Error: Docker not found"
exit 1;
fi;
}
# Check if docker compose is installed
_checkDockerCompose() {
# check if docker compose is installed
if ! command -v docker compose >/dev/null 2>&1; then
# check if docker-compose is installed
if ! command -v docker-compose >/dev/null 2>&1; then
echo "Error: Docker Compose not found"
exit 1;
fi;
# if docker-compose is installed, suggest to install docker compose v2
echo "Error: this stack requires Docker Compose v2, please install it and try again"
exit 1;
fi;
}
# -----------------------------------------------------------------------------
# Actions
# -----------------------------------------------------------------------------
# Build the project
build() {
_dc build
}
# Remove all containers, images, and volumes associated with the project
clean() {
_dc down --rmi all -v --remove-orphans
}
# Get an help message
help() {
printf "%s <task> [args]\n\nTasks:\n" "${0}"
compgen -A function | grep -v "^_" | cat -n
printf "\nExtended help:\n Each task has comments for general usage\n"
}
# Run the project
start() {
_dc up -d
}
# Stop the project
stop() {
_dc stop
}
# Restart the project
restart() {
_dc restart
}
# ---------------------------------------------------------------------
# Run the Project
# ---------------------------------------------------------------------
# !IMPORTANT
# Execute the action passed as the first argument
_checkDocker; \
_checkDockerCompose; \
_isCommand "${1:-help}"; \
# This idea is heavily inspired by: https://github.com/adriancooney/Taskfile
TIMEFORMAT=$'\nProcess completed in %3lR'; \
time "${@:-help}";