-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdc
executable file
·134 lines (107 loc) · 3.24 KB
/
dc
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
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env bash
UNAMEOUT="$(uname -s)"
WHITE="\033[1;37m"
NC="\033[0m"
ENV_FILE="./.docker/.env"
ENV_USER="devilbox"
COMPOSE="docker-compose --file=./.docker/docker-compose.yml --env-file=${ENV_FILE}"
PWD=$(pwd)
# Verify operating system is supported...
case "${UNAMEOUT}" in
Linux*) MACHINE=linux;;
Darwin*) MACHINE=mac;;
*) MACHINE="UNKNOWN"
esac
if [ "$MACHINE" == "UNKNOWN" ]; then
echo "Unsupported operating system [$(uname -s)]." >&2
exit 1
fi
# Define environment variables...
export APP_PORT=${APP_PORT:-80}
export APP_SERVICE=${APP_SERVICE:-"app"}
export DB_PORT=${DB_PORT:-3306}
export WWWUSER=${WWWUSER:-$UID}
export WWWGROUP=${WWWGROUP:-$(id -g)}
if [ "$MACHINE" == "linux" ]; then
export SEDCMD="sed -i"
elif [ "$MACHINE" == "mac" ]; then
export SEDCMD="sed -i .bak"
fi
# Ensure that Docker is running...
if ! docker info > /dev/null 2>&1; then
echo -e "${WHITE}Docker is not running.${NC}" >&2
exit 1
fi
# Determine if docker compose is currently up...
PSRESULT="$($COMPOSE ps -q)"
if $COMPOSE ps | grep 'Exit' &> /dev/null; then
$COMPOSE down > /dev/null 2>&1
EXEC="no"
elif [ -n "$PSRESULT" ]; then
EXEC="yes"
else
EXEC="no"
fi
# Function that outputs docker compose is not running...
function app_is_not_running {
echo -e "${WHITE}App is not running.${NC}" >&2
echo "" >&2
echo -e "${WHITE}You may App using the following commands:${NC} 'docker-compose up' or 'docker-compose up -d'" >&2
exit 1
}
if [ $# -gt 0 ]; then
# Source the ".env" file so Laravel environment variables are available...
if [ -f "${ENV_FILE}" ]; then
source "${ENV_FILE}"
fi
# Proxy PHP commands to the "php" binary on the application container...
if [ "$1" == "php" ]; then
shift 1
if [ "$EXEC" == "yes" ]; then
$COMPOSE exec --user=$ENV_USER \
"$APP_SERVICE" \
php "$@"
else
app_is_not_running
fi
# Initiate a MariaDb CLI terminal session within the "mariadb" container...
elif [ "$1" == "mysql" ]; then
shift 1
if [ "$EXEC" == "yes" ]; then
$COMPOSE exec \
mariadb \
bash -c "MYSQL_PWD=${MYSQL_PASSWORD} mysql -u ${MYSQL_USER} ${MYSQL_DATABASE}"
else
app_is_not_running
fi
# Initiate a Bash shell within the application container...
elif [ "$1" == "shell" ] || [ "$1" == "bash" ]; then
shift 1
if [ "$EXEC" == "yes" ]; then
$COMPOSE exec --user=$ENV_USER $APP_SERVICE bash
else
app_is_not_running
fi
# Initiate a Bash shell within the MariaDB container...
elif [ "$1" == "mariadb" ]; then
shift 1
if [ "$EXEC" == "yes" ]; then
$COMPOSE exec mariadb bash
else
app_is_not_running
fi
# Initiate a Bash shell within the Nginx container...
elif [ "$1" == "nginx" ]; then
shift 1
if [ "$EXEC" == "yes" ]; then
$COMPOSE exec nginx sh "$@"
else
app_is_not_running
fi
# Pass unknown commands to the "docker-compose" binary...
else
$COMPOSE "$@"
fi
else
${COMPOSE} ps
fi