-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmycontainer.sh
130 lines (118 loc) · 3.45 KB
/
mycontainer.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
#!/usr/bin/env zsh
myContainer() {
docker-compose --project-directory "${MYENV_DIR}" --file "${MYENV_DIR}/container.docker-compose.yml" --project-name "container" "${@}"
}
myContainerConfigServices() {
myContainer config --services
}
function usage() {
echo "$(basename "${0}") [action]"
echo "----------- actions -----------"
echo "up [service]: Up service on local"
echo "exec [service] <shell name>: Get in the service container"
echo "down: Cleanup all services"
echo "down [service]: Cleanup the service"
echo "ps|ls: Show running services"
echo "----------- services ----------"
myContainer config --services | while IFS="\n" read -r service
do
echo "-- ${service}"
done
echo
}
myContainerShowWrongServices() {
local inputService
inputService="${1}"
local services
services=$(myContainerConfigServices)
showUsage(){
echo "Please try with: [service]"
local count=0
echo "${services}" | while IFS="\n" read -r service
do
((count+=1))
echo "${count}) ${service}"
done
}
if [ -z "${inputService}" ]; then
showUsage
return 1
fi
local isServiceExistent="false"
echo "${services}" | while IFS="\n" read -r service
do
if [ "${service}" = "${inputService}" ]; then
isServiceExistent="true"
break
fi
done
if [ "${isServiceExistent}" = "false" ]; then
showUsage
return 1
fi
}
myContainerUp() {
local inputService
inputService="${1}"
if ! myContainerShowWrongServices "${inputService}"; then
return 1
fi
# Check running
myContainer ps --services | while IFS="\n" read -r runningService
do
if [ "${runningService}" = "${inputService}" ]; then
return 0
fi
done
myContainer up -d "${inputService}"
}
myContainerExec() {
local inputService
inputService="${1}"
if ! myContainerShowWrongServices "${inputService}"; then
return 1
fi
myContainerUp "${inputService}"
local inputShell="${2}"
if [ -z "${inputShell}" ]; then
echo "Default bash shell will be used"
inputShell="bash"
fi
echo "Exec into ${inputService} with ${inputShell} shell"
myContainer exec -u 0 "${inputService}" "${inputShell}"
}
myContainerDown() {
local service="${1}"
if [ -z "${service}" ]; then
myContainer down
return
fi
if ! myContainerShowWrongServices "${service}"; then
return 1
fi
# -f, --force Don't ask to confirm removal
# -s, --stop Stop the containers, if required, before removing
# -v, --volumes Remove any anonymous volumes attached to containers
myContainer rm -s -v -f "${service}"
}
myContainerMain-479ba3472071fe003c48f3806f6d58fb4cf5ce9101af532f114604038b7495c0() {
local action="${1}"
local service="${2}"
local shell="${3}"
case "${action}" in
up)
myContainerUp "${service}";;
down)
myContainerDown "${service}";;
exec)
myContainerExec "${service}" "${shell}";;
ls|ps)
myContainer ps;;
*)
usage
;;
esac
}
if myenv_lib_983459816_has_command "docker-compose"; then
myenv_lib_983459816_set_command_aliases 'mycontainer,myc' 'myContainerMain-479ba3472071fe003c48f3806f6d58fb4cf5ce9101af532f114604038b7495c0' 'Create OS environment in container'
fi