-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentrypoint.sh
More file actions
executable file
·161 lines (136 loc) · 3.98 KB
/
entrypoint.sh
File metadata and controls
executable file
·161 lines (136 loc) · 3.98 KB
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
#!/bin/bash
set -euo pipefail
INIT_DIR="/docker-entrypoint-init.d"
EMULATOR_TYPE=""
PORT=""
ADDITIONAL_ARGS=()
# Define emulator types that require beta
BETA_EMULATORS=("bigtable" "datastore" "pubsub")
# Define emulator types that have env-init
ENV_INIT_EMULATORS=("bigtable" "datastore" "pubsub")
# check if emulator type requires beta
function is_beta_emulator() {
local emulator="$1"
for beta_emulator in "${BETA_EMULATORS[@]}"; do
if [[ "$emulator" == "$beta_emulator" ]]; then
return 0
fi
done
return 1
}
# check if emulator type has env-init
function has_env_init() {
local emulator="$1"
for env_init_emulator in "${ENV_INIT_EMULATORS[@]}"; do
if [[ "$emulator" == "$env_init_emulator" ]]; then
return 0
fi
done
return 1
}
# show_usage displays the usage information
function show_usage() {
echo "Usage: $0 --emulator=<emulator_type> --port=<port> [additional args...]"
echo "Example: $0 --emulator=pubsub --port=8085 --project=my-project"
exit 1
}
# check arguments
if [ "$#" -eq 0 ]; then
show_usage
fi
# parse command-line arguments
while [[ $# -gt 0 ]]; do
case $1 in
--emulator=*)
EMULATOR_TYPE="${1#*=}"
shift
;;
--port=*)
PORT="${1#*=}"
shift
;;
-h|--help)
show_usage
;;
*)
# 残りの引数はすべて追加引数として扱う
ADDITIONAL_ARGS+=("$1")
shift
;;
esac
done
# check if required arguments are provided
if [ -z "$EMULATOR_TYPE" ] || [ -z "$PORT" ]; then
echo "Error: Both --emulator and --port are required"
show_usage
fi
echo "Emulator type: $EMULATOR_TYPE"
echo "Port: $PORT"
# Construct the gcloud command based on emulator type
if is_beta_emulator "$EMULATOR_TYPE"; then
COMMAND=("gcloud" "beta" "emulators" "$EMULATOR_TYPE" "start" "--host-port=0.0.0.0:$PORT")
else
COMMAND=("gcloud" "emulators" "$EMULATOR_TYPE" "start" "--host-port=0.0.0.0:$PORT")
fi
# Add any additional arguments
if [ "${#ADDITIONAL_ARGS[@]}" -gt 0 ]; then
COMMAND+=("${ADDITIONAL_ARGS[@]}")
fi
echo "Executing command: ${COMMAND[*]}"
# Start emulator in background and redirect output
echo "Starting emulator..."
"${COMMAND[@]}" > /proc/1/fd/1 2>&1 &
EMULATOR_PID=$!
# Wait a bit for the emulator to start properly
sleep 5
echo "Emulator started with PID: $EMULATOR_PID"
# Wait for emulator to be ready
echo "Waiting for emulator to be ready..."
until timeout 1 bash -c "</dev/tcp/localhost/${PORT}"; do
echo "Waiting for ${EMULATOR_TYPE} emulator to be ready..."
sleep 5
done
echo "Emulator is ready."
# Set environment variables for connecting to the emulator
echo "Setting up environment variables..."
if has_env_init "$EMULATOR_TYPE"; then
if is_beta_emulator "$EMULATOR_TYPE"; then
eval "$(gcloud beta emulators "${EMULATOR_TYPE}" env-init)"
else
eval "$(gcloud emulators "${EMULATOR_TYPE}" env-init)"
fi
echo "Environment variables set."
fi
# Firestore emulator does not support the env-init command, but requires manual environment variable configuration.
# Therefore, we need to set them manually.
if [ "$EMULATOR_TYPE" == "firestore" ]; then
export FIRESTORE_EMULATOR_HOST="localhost:$PORT"
export DATASTORE_EMULATOR_HOST="localhost:$PORT"
echo "Environment variables set."
fi
# Run ready.d scripts
READY_DIR="${INIT_DIR}/ready.d"
if [ -d "$READY_DIR" ] && [ -n "$(ls -A $READY_DIR 2>/dev/null)" ]; then
echo "Running ready scripts..."
for f in "$READY_DIR"/*.sh; do
if [ -f "$f" ] && [ -x "$f" ]; then
echo "Running hook: $f"
"$f"
elif [ -f "$f" ]; then
echo "Running hook: $f (with bash)"
bash "$f"
fi
done
for f in "$READY_DIR"/*.py; do
if [ -f "$f" ]; then
echo "Running hook: $f (with python)"
uv run "$f"
fi
done
echo "Ready scripts completed."
fi
# Create a file to indicate initialization is complete
touch /tmp/init-completed
echo "Initialization completed marker created at /tmp/init-completed"
# Keep the container running by waiting for the emulator process
wait $EMULATOR_PID