-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.sh
executable file
·156 lines (126 loc) · 3.09 KB
/
main.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
#!/usr/bin/env bash
#
# Prepare the local shell context with any globally needed env vars
#
environment() {
export SERVICE_NAME=prty
export ENV_REF=${1:-local}
export SERVICE_PORT=3200
export GIT_VER=$(git rev-parse --short HEAD)
export COMPOSE_NETWORK=${SERVICE_NAME}_${GIT_VER}
export DOCKER_TAG="inburst/${SERVICE_NAME}:${GIT_VER}"
}
#
# Docker Helpers
#
cleanup_compose_network() {
docker network rm ${COMPOSE_NETWORK} || true > /dev/null 2>&1 # ignore missing network
}
cleanup_compose_containers() {
docker-compose down -t 1 --remove-orphans > /dev/null 2>&1
docker volume prune -f
}
cleanup_compose() {
cleanup_compose_containers
cleanup_compose_network
}
make_docker_network() {
cleanup_compose_network
docker network create -d bridge ${COMPOSE_NETWORK}
}
# BUILD_TARGET can be local, testable, and deployable
cmd_build() {
environment
BUILD_TARGET="${1:-deployable}"
echo "Building container for ${BUILD_TARGET} ${SERVICE_NAME}"
docker build \
--build-arg SERVICE_NAME=${SERVICE_NAME} \
--target ${BUILD_TARGET} \
-t ${DOCKER_TAG} .
}
# local development
cmd_local() {
environment
if [[ $1 = "rebuild" ]]; then
REBUILD_FLAGS="--build --force-recreate"
echo "build with flags: ${REBUILD_FLAGS}"
fi
start() {
# clean up
cmd_build local
docker run -it ${DOCKER_TAG}
#docker-compose down -v > /dev/null 2>&1
#export BUILD_TARGET="local"
#make_docker_network
#docker-compose up ${REBUILD_FLAGS} --remove-orphans -d
# immediately bind to service logs
#cmd_logs ${SERVICE_NAME}
}
stop() {
echo "Stopping infrastructure within docker-compose"
cleanup_compose
}
if [[ $1 = "stop" ]]; then
stop
exit 0
fi
start
}
# runs tests
# add -w or watch to the command for watch mode
cmd_tests() {
environment
# run tests
export BUILD_TARGET="local"
#docker-compose exec ${SERVICE_NAME} bash -c "bash ./entrypoints/test.sh ${1}"
docker-compose exec ${SERVICE_NAME} bash -c "./entrypoints/test.sh ${1}"
}
# tail logs up to 3 given container names
cmd_logs() {
environment
docker-compose logs -f ${1} ${2} ${3}
}
# Get a shell into a specific container
cmd_sh() {
environment
docker-compose exec ${1} /bin/bash
}
################################################
#
# Print all available commands
#
help() {
echo "Usage:
$ ./main.sh COMMAND
Where COMMAND can be:
local # Start local development
local stop # Stop local development
test [-w] # Run tests. Add -w for watch mode
build # Build Docker container
logs CONTAINER_NAME # Get logs from CONTAINER_NAME
sh CONTAINER_NAME # Run shell in CONTAINER_NAME
" 1>&2
exit 1
}
#
# Command translation from arg to func call
# If you wish to accesas a new function you must add a hook here
#
case "$1" in
# local development
local)
cmd_local ${@:2};;
test)
cmd_tests ${@:2};;
# docker build
build)
cmd_build ${@:2};;
# helper tools
logs)
cmd_logs ${@:2};;
sh)
cmd_sh ${@:2};;
# fall back
*)
help; exit 1
esac