This repository has been archived by the owner on Jan 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsandbox
executable file
·99 lines (86 loc) · 2.63 KB
/
sandbox
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
#!/bin/bash
set -e
# Set up sandbox environment.
export SANDBOX=true
export BASE_DIR=$(cd `dirname "${BASH_SOURCE%/*}"` && pwd)
export SANDBOX_DIR="${BASE_DIR}"
export SANDBOX_PROJECT_NAME="${PWD##*/}"
# Bits and pieces required to run sandbox containers.
export SANDBOX_USER_ID=$(id -u)
export SANDBOX_GROUP_ID=$(id -g)
export SANDBOX_USER_NAME=$(id -un)
export SANDBOX_GROUP_NAME=$(id -gn)
function _stack() {
#
# Stack helper function.
#
# Wraps docker-compose command for simple invocation.
#
docker-compose \
--project-name ${SANDBOX_PROJECT_NAME} \
--file docker-compose.sandbox.yml \
"${@:1}"
}
function _help() {
#
# Helper for printing usage info.
#
echo
echo "Manage sandbox."
echo
echo "Usage:"
echo " sandbox COMMAND [ARGUMENTS]"
echo
echo "Available commands:"
echo " build [ARGS] Build or rebuild the stack. Accepts same arguments as 'docker-compose build'."
echo " up [ARGS] Start the stack. Accepts same argument as 'docker-compose up'."
echo " down Stop the stack."
echo " clean Run the cleanup."
echo " exec SERVICE CMD Run a one-off command against service."
echo " logs [SERVICE] Attach to logs output."
echo
}
case ${1:-} in
"build")
# Build sandbox.
_stack build "${@:2}"
# Clean dangling images.
docker image prune -f
;;
"clean")
# Tear down stack.
_stack down --remove-orphans
# Clean dangling images.
docker image prune -f
;;
"exec")
_stack exec "${@:2}"
;;
"help")
_help
;;
"logs")
_stack logs -f "${@:2}"
;;
"up")
echo -e "Ensuring directories..."
# Ensure packages directory exists before launching the stack.
# This allows the mounted volume to have current user's permissions
# within container.
mkdir -p ${SANDBOX_DIR}/.data/packages
echo -e "\033[1;37mLaunching stack...\033[0m"
_stack up -d ${@:2}
# Attach terminals.
echo -e "\033[1;37mAttaching terminals...\033[0m"
exec gnome-terminal \
--tab --title "web-app" --command "docker attach --sig-proxy=false `_stack ps -q web-app`" \
--tab --title "web-api" --command "docker attach --sig-proxy=false `_stack ps -q web-api`" \
--tab --title "database" --command "docker exec -it `_stack ps -q database` /bin/bash" \
> /dev/null 2>&1
;;
"down")
_stack stop "${@:2}"
;;
*)
_help
esac