-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgoliath.sh
executable file
·154 lines (134 loc) · 3.96 KB
/
goliath.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
#!/usr/bin/env bash
# A script to build and package Goliath.
set -euo pipefail
CRDB_SRC_DIR="cockroach-data"
CRDB_VOL_NAME="crdb_volume"
CRDB_VOL_MOUNT="/cockroach-data"
CRDB_DEST_DIR="."
BUILD_TIMESTAMP=$(date +%s)
BUILD_HASH=$(git rev-parse HEAD)
FORCE_VOL=false
ACTION=""
ATTACHED=false
DOCKER_COMPOSE_ARGS=()
ENV=prod
# Export variables to make them visible during the build process
export BUILD_TIMESTAMP
export BUILD_HASH
function usage() {
echo "Usage: $0 [ACTION] [OPTION...] [-- [EXTRA_ARGS...]]"
echo ""
echo " ACTION must be \"up\" or \"down\"."
echo ""
echo "Options:"
echo " -h, --help Display this help message."
echo " --env ENV Environment to start (\"prod\", \"dev\", or \"debug\"). Default to \"prod\"."
echo " --attached Starts containers in attached mode. Only applies to \"up\" action."
echo " --crdb_dir DIR CockroachDB data directory to populate volume. Ignored if volume exists unless --force_volume is set."
echo " --force_volume Force creation of volume even if already exists (destroys existing volume)."
echo ""
echo " EXTRA_ARGS are passed to \"docker compose\"."
}
function handle_options() {
ACTION="$1"
if [[ ! "${ACTION}" =~ ^up|down$ ]]; then
echo "ACTION must be set to one of {up, down}, got: ${ACTION}. Exiting."
usage
exit 1
fi
shift
while [ $# -gt 0 ]; do
case $1 in
-h | --help)
usage
exit 0
;;
--crdb_dir)
CRDB_SRC_DIR="$2"
if [[ -z "${CRDB_SRC_DIR}" ]]; then
echo "Value of crdb dir flag must be non-empty. Exiting."
exit 1
fi
if [[ ! -d "${CRDB_SRC_DIR}" ]]; then
echo "Directory specified by crdb dir flag does not exist: ${CRDB_SRC_DIR}. Exiting."
usage
exit 1
fi
shift
;;
--env)
ENV="$2"
if [[ ! "${ENV}" =~ ^debug|dev|prod$ ]]; then
echo "--env must be set to one of {prod, dev, debug}, got: ${ENV}. Exiting."
usage
exit 1
fi
shift
;;
--attached)
ATTACHED=true
;;
--force_volume)
FORCE_VOL=true
;;
--)
shift
DOCKER_COMPOSE_ARGS+=("$@")
break
;;
*)
echo "Invalid option: $1" >&2
usage
exit 1
;;
esac
shift
done
}
# Inspired by https://stackoverflow.com/a/68511611.
function copy_to_volume() {
SRC_PATH=$1
VOLUME_MOUNT=$2
DEST_PATH=$3
# Create minimal, empty container.
echo -e 'FROM scratch\nLABEL empty=""' | docker build -t empty -
CONTAINER_ID=$(docker container create -v "${VOLUME_MOUNT}" empty cmd)
docker cp "${SRC_PATH}/" "${CONTAINER_ID}:${DEST_PATH}"
docker rm "${CONTAINER_ID}" --volumes
}
function setup_volume() {
if [[ $FORCE_VOL = true ]]; then
echo "Destroying existing CockroachDB data volume: ${CRDB_VOL_NAME} ..."
# Make sure all volumes referenced in the Compose file are downed.
docker compose down --volumes
# Then make sure any stopped containers in the Compose file are also removed.
docker compose rm --volumes
docker volume rm -f "${CRDB_VOL_NAME}" > /dev/null
fi
if [[ -z $(docker volume ls -f name="${CRDB_VOL_NAME}" -q) ]]; then
docker volume ls -f name="${CRDB_VOL_NAME}" -q
echo "Creating CockroachDB data volume..."
exit 1
docker volume create --name "${CRDB_VOL_NAME}" > /dev/null
copy_to_volume \
"${CRDB_SRC_DIR}" \
"${CRDB_VOL_NAME}:${CRDB_VOL_MOUNT}" \
"${CRDB_DEST_DIR}"
fi
}
# Flag parsing.
handle_options "$@"
setup_volume
echo "Executing:"
echo " Action: ${ACTION}"
echo " Environment: ${ENV}"
echo " Extra args: ${DOCKER_COMPOSE_ARGS[*]}"
if [[ "${ACTION}" = "up" ]]; then
if [[ $ATTACHED = true ]]; then
# If running in attached mode, down services on Ctrl-C.
trap 'docker compose --profile ${ENV} down' INT
else
DOCKER_COMPOSE_ARGS+=("-d")
fi
fi
docker compose --profile "${ENV}" "${ACTION}" "${DOCKER_COMPOSE_ARGS[@]}"