-
Notifications
You must be signed in to change notification settings - Fork 1
/
appup.plugin.zsh
178 lines (155 loc) · 5.41 KB
/
appup.plugin.zsh
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# Docker
_appup_docker () {
if hash docker-compose >/dev/null 2>&1; then
# Check if docker has been started
if [ "${APPUP_CHECK_STARTED:-true}" = true ]; then
if hash docker-machine >/dev/null 2>&1 && [ "${APPUP_DOCKER_MACHINE:-true}" = true ]; then
if docker-machine status | grep -qi "Stopped"; then
read -r -k 1 "REPLY?Docker Machine is not running, would you like to start it? [y/N] "
echo ""
if [[ "$REPLY" == "y" ]]; then
docker-machine start default && eval $(docker-machine env default)
echo ""
else
return 0
fi
fi
elif docker ps 2>&1 | grep -qi "Is the docker daemon running?"; then
read -r -k 1 "REPLY?Docker for Mac is not running, would you like to start it? [y/N] "
echo ""
if [[ "$REPLY" == "y" ]]; then
open -a Docker
echo ""
echo "Waiting for docker to start.."
echo ""
# Wait for it to start
while true; do
if docker ps 2>&1 | grep -qi "Is the docker daemon running?" || docker ps 2>&1 | grep -qi "connection refused"; then
sleep 5
else
break
fi
done
else
return0
fi
fi
fi
# Find docker-compose file
compose_file=''
if [ -e "docker-compose.yml" ]; then
compose_file='docker-compose.yml'
elif [ -e "docker-compose.yaml" ]; then
compose_file='docker-compose.yaml'
fi
# Env files
env_files=()
if [ "${APPUP_LOAD_ENVS:-true}" = true ]; then
if [ -e ".env" ]; then
env_files+=( .env )
fi
if [ -e ".env.local" ]; then
env_files+=( .env.local )
fi
if [ -e ".env.docker" ]; then
env_files+=( .env.docker )
fi
if [ -e ".env.docker.local" ]; then
env_files+=( .env.docker.local )
fi
fi
# <cmd> <project name> will look for docker-compose.<project name>.yml
if [ -n "$2" ]; then
# Find project-specific docker-compose file
compose_project_file=''
if [ -e "docker-compose.$2.yml" ]; then
compose_project_file="docker-compose.$2.yml"
elif [ -e "docker-compose.$2.yaml" ]; then
compose_project_file="docker-compose.$2.yaml"
fi
if [ -n "$compose_project_file" ]; then
# Project specific env file
if [ "${APPUP_LOAD_ENVS:-true}" = true ]; then
if [ -e ".env.$2" ]; then
env_files+=( ".env.$2" )
fi
if [ -e ".env.$2.local" ]; then
env_files+=( ".env.$2.local" )
fi
fi
# Run docker compose.
docker-compose -f "$compose_file" -f "$compose_project_file" --env-file=$^env_files $1 "${@:3}"
return
fi
fi
# Run docker compose.
docker-compose --env-file=$^env_files $1 "${@:2}"
else
echo >&2 "Docker compose file found but docker-compose is not installed."
fi
}
# Vagrant
_appup_vagrant () {
if hash vagrant >/dev/null 2>&1; then
vagrant $1 "${@:2}"
else
echo >&2 "Vagrant file found but vagrant is not installed."
fi
}
# Commands
up () {
if [ -e "docker-compose.yml" ] || [ -e "docker-compose.yaml" ]; then
_appup_docker up "$@"
elif [ -e "Vagrantfile" ]; then
_appup_vagrant up "$@"
elif hash up >/dev/null 2>&1; then
env up "$@"
fi
}
down () {
if [ -e "docker-compose.yml" ] || [ -e "docker-compose.yaml" ]; then
_appup_docker down "$@"
elif [ -e "Vagrantfile" ]; then
_appup_vagrant destroy "$@"
elif hash down >/dev/null 2>&1; then
env down "$@"
fi
}
start () {
if [ -e "docker-compose.yml" ] || [ -e "docker-compose.yaml" ]; then
_appup_docker start "$@"
elif [ -e "Vagrantfile" ]; then
_appup_vagrant up "$@"
elif hash start >/dev/null 2>&1; then
env start "$@"
fi
}
restart () {
if [ -e "docker-compose.yml" ] || [ -e "docker-compose.yaml" ]; then
_appup_docker restart "$@"
elif [ -e "Vagrantfile" ]; then
_appup_vagrant reload "$@"
elif hash start >/dev/null 2>&1; then
env start "$@"
fi
}
stop () {
if [ -e "docker-compose.yml" ] || [ -e "docker-compose.yaml" ]; then
_appup_docker stop "$@"
elif [ -e "Vagrantfile" ]; then
_appup_vagrant halt "$@"
elif hash stop >/dev/null 2>&1; then
env stop "$@"
fi
}
enter () {
if [ -e "docker-compose.yml" ] || [ -e "docker-compose.yaml" ]; then
CMD=( "${@:2}" )
if [ $# -eq 1 ]; then
CMD=( /bin/bash -l )
fi
_appup_docker exec "$1" $CMD
elif hash enter >/dev/null 2>&1; then
env enter "$@"
fi
}