-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocal_run.sh
More file actions
180 lines (151 loc) Β· 5.72 KB
/
local_run.sh
File metadata and controls
180 lines (151 loc) Β· 5.72 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/bin/sh
set -euo pipefail
# ----------------------------------------------------------------------
# Load environment variables
# ----------------------------------------------------------------------
. ./.env.example # or 'source .env' in bash
cp .env.example .env # create local .env file if it doesn't exist
INPUT_ENV_FILE=".env"
OUTPUT_ENV_FILE=".env_docker"
# ----------------------------------------------------------------------
# Generate .env_docker for Docker containers
# ----------------------------------------------------------------------
SEARCH="127.0.0.1"
REPLACE="172.17.0.1"
# Create or overwrite output file
> "$OUTPUT_ENV_FILE"
# Read .env line by line
while IFS= read -r line || [[ -n "$line" ]]; do
# Preserve empty lines and comments
if [[ -z "$line" || "$line" =~ ^# ]]; then
echo "$line" >> "$OUTPUT_ENV_FILE"
continue
fi
# Split key and value
key="${line%%=*}"
value="${line#*=}"
# Replace specified substring in the value
new_value="${value//$SEARCH/$REPLACE}"
# Write updated line to output file
echo "$key=$new_value" >> "$OUTPUT_ENV_FILE"
done < "$INPUT_ENV_FILE"
. ./$OUTPUT_ENV_FILE # or 'source .env_docker' in bash
# ----------------------------------------------------------------------
# Docker configuration
# ----------------------------------------------------------------------
DOCKER_PREFIX="local"
RECREATE=false
RUN_API=false
RUN_GRPC=false
# Parse script arguments
while [ "$#" -gt 0 ]; do
case $1 in
--recreate) RECREATE=true ;;
--run_api) RUN_API=true ;;
--run_grpc) RUN_GRPC=true ;;
esac
shift
done
BASE_IMAGE="base_img"
IMAGE_CELERY="celery_img"
IMAGE_CONSUME="consume_img"
IMAGE_API="api_img"
IMAGE_GRPC="grpc_img"
# ----------------------------------------------------------------------
# REBUILD images, remove old containers and images if requested
# ----------------------------------------------------------------------
if [ "$RECREATE" = true ]; then
# Remove old containers
docker ps -a --filter "name=${DOCKER_PREFIX}*" --format "{{.ID}}" | xargs -r docker rm -f
# Remove old images
docker rmi $BASE_IMAGE || true
docker rmi $IMAGE_CELERY || true
docker rmi $IMAGE_CONSUME || true
docker rmi $IMAGE_API || true
docker rmi $IMAGE_GRPC || true
echo " ποΈ Removed old containers and images"
# Build new images
docker build -t $BASE_IMAGE --no-cache -f .launch/Dockerfile_base .
echo " ποΈ Built ${BASE_IMAGE} image"
docker build --build-arg BASE_IMAGE=$BASE_IMAGE -t $IMAGE_CELERY --no-cache -f .launch/celery/Dockerfile .
echo " ποΈ Built celery_img image"
docker build --build-arg BASE_IMAGE=$BASE_IMAGE -t $IMAGE_CONSUME --no-cache -f .launch/consume/Dockerfile .
echo " ποΈ Built ${IMAGE_CONSUME} image"
if [ "$RUN_API" = true ]; then
docker build --build-arg BASE_IMAGE=$BASE_IMAGE -t $IMAGE_API --no-cache -f .launch/api/Dockerfile .
echo " ποΈ Built ${IMAGE_API} image"
fi
if [ "$RUN_GRPC" = true ]; then
docker build --build-arg BASE_IMAGE=$BASE_IMAGE -t $IMAGE_GRPC --no-cache -f .launch/grpc/Dockerfile .
echo " ποΈ Built ${IMAGE_GRPC} image"
fi
fi
# ----------------------------------------------------------------------
# Run Docker containers
# ----------------------------------------------------------------------
# Celery container
if [ ! "$(docker ps -aq -f name=${DOCKER_PREFIX}_celery)" ]; then
docker run -d \
--name "${DOCKER_PREFIX}_celery" \
--env-file ./.env_docker \
--shm-size="512m" \
--cpus=2 \
-e CELERY_ARGS="worker -l INFO -E -B -Q default_queue --concurrency=2 -n default@%h" \
$IMAGE_CELERY || true
docker run -d --name "${DOCKER_PREFIX}_flower" \
-e broker_url=$CELERY_BROKER_URL \
-e CELERY_BROKER_URL=$CELERY_BROKER_URL \
-e CELERY_BROKER_API=$CELERY_RESULT_BACKEND \
-p 5555:5555 mher/flower
fi
echo " β
${DOCKER_PREFIX}_celery UP"
echo " β
${DOCKER_PREFIX}_flower UP"
# Consume container
if [ ! "$(docker ps -aq -f name=${DOCKER_PREFIX}_consume)" ]; then
docker run -d \
--name "${DOCKER_PREFIX}_consume" \
--env-file ./.env_docker \
--shm-size="512m" \
--cpus=1 \
$IMAGE_CONSUME || true
fi
echo " β
${DOCKER_PREFIX}_consume UP"
# API container (optional)
if [ -z "$(docker ps -aq -f name=${DOCKER_PREFIX}_api)" ] && [ "$RUN_API" = true ]; then
docker run -d \
--name "${DOCKER_PREFIX}_api" \
--env-file ./.env_docker \
--shm-size="1g" \
--cpus=1 \
-p $API_PORT:$API_PORT \
$IMAGE_API || true
fi
if [ "$RUN_API" = true ]; then
echo " β
${DOCKER_PREFIX}_api UP"
fi
# gRpc container (optional)
if [ -z "$(docker ps -aq -f name=${DOCKER_PREFIX}_grpc)" ] && [ "$RUN_GRPC" = true ]; then
docker run -d \
--name "${DOCKER_PREFIX}_grpc" \
--env-file ./.env_docker \
--shm-size="1g" \
--cpus=1 \
-p $GRPC_PORT:$GRPC_PORT \
$IMAGE_GRPC || true
fi
if [ "$RUN_GRPC" = true ]; then
echo " β
${DOCKER_PREFIX}_grpc UP"
fi
# ----------------------------------------------------------------------
# Print results
# ----------------------------------------------------------------------
echo ""
echo "-------------------------------------------------------------------------------"
if [ "$RUN_API" = true ]; then
echo " π http://0.0.0.0:${API_PORT}/docs **** API"
fi
if [ "$RUN_GRPC" = true ]; then
echo " π http://0.0.0.0:${GRPC_PORT}/ ******* gRpc"
fi
echo " βοΈ http://0.0.0.0:5555 ********* Flower[celery monitoring]"
echo "-------------------------------------------------------------------------------"