-
Notifications
You must be signed in to change notification settings - Fork 4
/
install.sh
executable file
·255 lines (226 loc) · 7.82 KB
/
install.sh
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/bin/bash
# Author Munis Isazade Django developer
VERSION="0.3"
ERROR_STATUS=0
#if [[ "$OSTYPE" == "darwin"* ]]; then
# Detect Operation system is Macbook pro OSX
LOCAL_COMMAND_DIRECTORY=/usr/local/bin/
ROOT_COMMAND_DIRECTORY=/usr/local/bin/
#else
# LOCAL_COMMAND_DIRECTORY=~/.local/bin/
# ROOT_COMMAND_DIRECTORY=/bin/
#fi
WORKING_DIRECTRY=$(pwd)
ISSUE_URL="https://github.com/munisisazade/docker-helper-commands/issues"
#usage: ChangeColor $COLOR text/background
function ChangeColor()
{
TYPE=""
case "$2" in
"text") TYPE="setaf"
;;
"back") TYPE="setab"
;;
*) TYPE="setaf"
esac
case "$1" in
"red") tput "$TYPE" 1
;;
"orange") tput "$TYPE" 3
;;
"blue") tput "$TYPE" 4
;;
"green") tput "$TYPE" 2
;;
"black") tput "$TYPE" 0
;;
"white") tput "$TYPE" 7
;;
"magenta") tput "$TYPE" 5
;;
"cyan") tput "$TYPE" 7
;;
*) tput "$TYPE" 0
esac
}
function usage {
echo -e "Thans for using this tool:"
if [ "$EUID" -ne 0 ]; then
echo -e "You install $(whoami) user all shels here $LOCAL_COMMAND_DIRECTORY"
else
echo -e "You install $(whoami) user all shels here $ROOT_COMMAND_DIRECTORY"
fi
echo -e "Usage:"
echo -e "$(ChangeColor blue text) build-docker $(ChangeColor green text)Update and create docker compose up $(ChangeColor white text)"
echo -e "\n"
echo -e "$(ChangeColor blue text) down-docker $(ChangeColor green text) Down all docker compose containers $(ChangeColor white text)"
echo -e "\n"
echo -e "$(ChangeColor blue text) connect-docker <sh|bash> <container_name> $(ChangeColor green text) Connect available docker container with sh or bash $(ChangeColor white text)"
echo -e "\n"
echo -e "$(ChangeColor blue text) dangling-remove-docker $(ChangeColor green text) Remove dangling images and containers $(ChangeColor white text)"
echo -e "\n"
echo -e "Run $(ChangeColor blue text)create-django-app --help$(ChangeColor white text) to see all options."
echo -e "\n"
echo -e " If you have any problems, do not hesitate to file an issue:"
echo -e " $(ChangeColor blue text)$ISSUE_URL$(ChangeColor white text)"
}
function build_docker_shell() {
touch build-docker
echo "#!/bin/bash" >> build-docker
echo "" >> build-docker
echo "echo -e \"Command Created by Munis\"" >> build-docker
echo "echo -e \"Building docker-compose containers ...\"" >> build-docker
echo "docker-compose up --build -d" >> build-docker
echo "echo -e \"Successfuly build .... [OK]\"" >> build-docker
}
function down_docker_shell() {
touch down-docker
echo "#!/bin/bash" >> down-docker
echo "" >> down-docker
echo "echo -e \"Command Created by Munis\"" >> down-docker
echo "echo -e \"Down all containers .. width docker-composefile\"" >> down-docker
echo "" >> down-docker
echo "docker-compose down" >> down-docker
}
function connect_docker_shell() {
touch connect-docker
echo "#!/bin/bash" >> connect-docker
echo "" >> connect-docker
echo "echo -e \"Command Created by Munis\"" >> connect-docker
echo "echo -e \"Connecting with \$1 inside \$2 container with docker command \"" >> connect-docker
echo "docker exec -it \$2 \$1" >> connect-docker
}
function dangling_remove_docker_shell() {
touch dangling-remove-docker
echo "#!/bin/bash" >> dangling-remove-docker
echo "" >> dangling-remove-docker
echo "echo -e \"Remove all exited images\"" >> dangling-remove-docker
echo "docker rmi \$(docker images -f dangling=true -q)" >> dangling-remove-docker
echo "" >> dangling-remove-docker
echo "echo -e \"Remove all exited containers\"" >> dangling-remove-docker
echo "docker rm \$(docker ps -a -f status=exited -q)" >> dangling-remove-docker
}
function restart_docker_shell() {
touch restart-docker
echo "#!/bin/bash" >> restart-docker
echo "" >> restart-docker
echo "echo -e \"Command Created by Munis\"" >> restart-docker
echo "echo -e \"Restart all containers\"" >> restart-docker
echo "docker-compose restart" >> restart-docker
echo "" >> restart-docker
}
function migrate_docker_shell() {
touch migrate-docker
echo "#!/bin/bash" >> migrate-docker
echo "" >> migrate-docker
echo "echo -e \"Command Created by Munis\"" >> migrate-docker
echo "echo -e \"Migrate django project inside \$1 container ...\"" >> migrate-docker
echo "" >> migrate-docker
echo "docker exec -it \$1 sh -c \"/venv/bin/python manage.py migrate\"" >> migrate-docker
echo "" >> migrate-docker
}
function logs_docker_shell() {
touch logs-docker
echo "#!/bin/bash" >> logs-docker
echo "" >> logs-docker
echo "echo -e \"Command Created by Munis\"" >> logs-docker
echo "echo -e \"Tails all logs on docker containers\"" >> logs-docker
echo "" >> logs-docker
echo "docker-compose logs -f" >> logs-docker
echo "" >> logs-docker
}
function backup_database_shell() {
touch backup-database-docker
echo "#!/bin/bash" >> backup-database-docker
echo "" >> backup-database-docker
echo "echo -e \"Command Created by Munis\"" >> backup-database-docker
echo "echo -e \"Backup Posgresql Database\"" >> backup-database-docker
echo "" >> backup-database-docker
echo "if [ \"\$1\" ]; then" >> backup-database-docker
echo " docker exec -t \$1 pg_dumpall -c -U postgres > dump_\`date +%d-%m-%Y\"_\"%H_%M_%S\`.sql" >> backup-database-docker
echo " echo -e \"Successfuly created Dump file name: \$(tput setaf 2 && tput bold)dump_\`date +%d-%m-%Y\"_\"%H_%M_%S\`.sql \"" >> backup-database-docker
echo "else" >> backup-database-docker
echo " echo -e \"Please Select Database Container name for example \"" >> backup-database-docker
echo " echo -e \"backup-database-docker postgres-db \"" >> backup-database-docker
echo "fi" >> backup-database-docker
echo "" >> backup-database-docker
}
function restore_database_shell() {
touch restore-database-docker
echo "#!/bin/bash" >> restore-database-docker
echo "" >> restore-database-docker
echo "echo -e \"Command Created by Munis\"" >> restore-database-docker
echo "echo -e \"Restore Posgresql Database\"" >> restore-database-docker
echo "" >> restore-database-docker
echo "if [[ \"\$1\" && \"\$2\" ]]; then" >> restore-database-docker
echo " cat \$1 | docker exec -i \$2 psql -U postgres" >> restore-database-docker
echo " echo -e \"Successfuly Restored from dump file name: \$(tput setaf 2 && tput bold)\$1 \"" >> restore-database-docker
echo "else" >> restore-database-docker
echo " echo -e \"Please Select Dump Sql file and Container name for example \"" >> restore-database-docker
echo " echo -e \"restore-database-docker dump_07-04-2018_22_29_33.sql postgres-db \"" >> restore-database-docker
echo "fi" >> restore-database-docker
echo "" >> restore-database-docker
}
if [ "$EUID" -ne 0 ]; then
# If not root user
if [ -d $LOCAL_COMMAND_DIRECTORY ]; then
cd $LOCAL_COMMAND_DIRECTORY
rm -rf ./*-docker
build_docker_shell
down_docker_shell
connect_docker_shell
dangling_remove_docker_shell
restart_docker_shell
migrate_docker_shell
logs_docker_shell
backup_database_shell
restore_database_shell
chmod +x *
else
cd ~
mkdir .local
cd .local/
mkdir bin/
cd $LOCAL_COMMAND_DIRECTORY
build_docker_shell
down_docker_shell
connect_docker_shell
dangling_remove_docker_shell
restart_docker_shell
migrate_docker_shell
logs_docker_shell
backup_database_shell
restore_database_shell
chmod +x *
fi
else
# if user is root
cd $ROOT_COMMAND_DIRECTORY
rm -rf ./*-docker
build_docker_shell
down_docker_shell
connect_docker_shell
dangling_remove_docker_shell
restart_docker_shell
migrate_docker_shell
logs_docker_shell
backup_database_shell
restore_database_shell
chmod +x build-docker
chmod +x down-docker
chmod +x connect-docker
chmod +x dangling-remove-docker
chmod +x restart-docker
chmod +x migrate-docker
chmod +x logs-docker
chmod +x backup-database-docker
chmod +x restore-database-docker
fi
cd $WORKING_DIRECTRY
rm -rf install.sh
if [[ "$OSTYPE" == "darwin"* ]]; then
# Detect Operation system is Macbook pro OSX
echo -e "Successfuly installed"
else
$SHELL
fi